Files
apes-sidebar/src/client/cm-themes.ts
T
Menghuan1918 d4ca9eb6fb feat(transfer): 流式传输与原生打包,JSONL 预览/编辑与大文件分页预览
宿主半:
- /sidebar/file 与 /sidebar/html 改为 createReadStream + pipeline(新增
  file-stream.ts),删除 mediaLimit;流开始后失败由 writeJson 的
  headersSent 守卫降级为 destroy,不再抛 ERR_HTTP_HEADERS_SENT
- 上传本就是流式的,只删掉客户端 MAX_UPLOAD_BYTES 预检与宿主 limit 逐块检查
- 删除 readLimit / mediaLimit / uploadLimit 三个配置键,新增共享常量模块
  editor-limits.ts(MAX_EDITABLE_BYTES = 5 MiB、PAGE_BYTES、写入体护栏)
- fs.read 重塑为 text | binary | too-large 三态,不再有「截断文本」这个
  既不可信又不可保存的中间态;新增 fs.readPage 字节窗口分页(按行对齐、
  按 path+size+mtime 缓存首次探测到的编码)
- 删除 JSON 版 fs.write,改为 /sidebar/write raw 路由(文档即请求体),
  拆掉 MAX_BODY_BYTES=1MiB 这道保存墙;编码与行尾还原逻辑原样搬到
  fs-operations.writeWorkspaceText
- 打包改原生命令:新增 archive-tools.ts(zip → tar 探测、公共祖先定位、
  argv 规划、stdio 重定向),archive-route 改为后台写临时文件 + 流式下发,
  删除手写 ZIP writer src/zip.ts;新增 unavailable 错误码

客户端半:
- 新增 text-pages.ts(字节偏移分页游标,显式翻页)、jsonl.ts(逐行记录
  解析与摘要)、lang-jsonl.ts(每行独立的 JSON tokenizer,替代会为每条记录
  产生错误节点的 json() 语法)、LargeFileView.tsx(记录卡片 / 日志行两种
  只读分页视图)
- 新增 jsonl viewer 描述符(预览 = 记录列表,编辑 = 可保存的 CodeMirror)
- FileViewerProps 用 size/largeFile 取代 truncated;超限文件改为只读分页
  预览并可下载,不再提供编辑器
- tags.invalid 由纯白改为红色波浪线(浅色主题下原本白底白字不可见)
- zip 下载改回普通 <a download> 直链,不再把整包放进浏览器内存
2026-10-11 22:25:25 +08:00

138 lines
5.7 KiB
TypeScript

/**
* CodeMirror 6 theme pieces for the sidebar editor. The editor surface
* (background, caret, gutter) rides the DSH theme tokens so it blends with
* the panel in both schemes; only the syntax token colors need concrete
* values, and those come from the same designed syntax families the app's
* code surfaces use — one-dark for dark, one-light for light
* (one-dark-palette.ts, shared with the terminal's ANSI palette). The
* scheme flip reconfigures these via a compartment (see TextEditor), so
* the document, undo history and scroll survive re-theming.
*/
import { Compartment } from '@codemirror/state'
import { HighlightStyle, syntaxHighlighting } from '@codemirror/language'
import { tags, type Tag } from '@lezer/highlight'
import { EditorView } from '@codemirror/view'
import { ONE_DARK, ONE_LIGHT } from './one-dark-palette.ts'
/** Token-driven surface shared by both schemes (pure CSS values). */
export const cmSurfaceTheme = EditorView.theme({
'&': {
height: '100%',
fontSize: '13px',
backgroundColor: 'transparent',
color: 'var(--dsw-alias-label-primary)',
},
'.cm-scroller': {
overflow: 'auto',
fontFamily: 'var(--ds-font-family-code)',
},
'.cm-content': {
caretColor: 'var(--dsw-alias-label-primary)',
},
'.cm-gutters': {
backgroundColor: 'transparent',
color: 'var(--dsw-alias-label-tertiary)',
border: 'none',
},
})
/** Scheme-specific surface tints (selection, active line). */
function cmSurfaceTint(dark: boolean): ReturnType<typeof EditorView.theme> {
return EditorView.theme({
'.cm-selectionBackground, .cm-focused .cm-selectionBackground, ::selection': {
backgroundColor: dark ? 'rgba(255,255,255,0.22)' : 'rgba(0,0,0,0.12)',
},
'.cm-activeLine, .cm-activeLineGutter': {
backgroundColor: dark ? 'rgba(255,255,255,0.04)' : 'rgba(0,0,0,0.03)',
},
})
}
/** One syntax rule: a tag (or tag set) mapped to a concrete color/style. */
interface HighlightRule {
tag: Tag | readonly Tag[]
color?: string
fontStyle?: string
textDecoration?: string
}
/** one-dark syntax rules (mirrors @codemirror/theme-one-dark; hues from one-dark-palette.ts). */
const HIGHLIGHTS_DARK: HighlightRule[] = [
{ tag: tags.comment, color: ONE_DARK.faintGray, fontStyle: 'italic' },
{ tag: tags.keyword, color: ONE_DARK.magenta },
{ tag: tags.string, color: ONE_DARK.green },
{ tag: tags.number, color: ONE_DARK.orange },
{ tag: tags.bool, color: ONE_DARK.orange },
{ tag: tags.atom, color: ONE_DARK.orange },
{ tag: tags.typeName, color: ONE_DARK.yellow },
{ tag: tags.className, color: ONE_DARK.yellow },
{ tag: tags.propertyName, color: ONE_DARK.red },
{ tag: tags.function(tags.variableName), color: ONE_DARK.blue },
{ tag: tags.variableName, color: ONE_DARK.red },
{ tag: tags.operator, color: ONE_DARK.cyan },
{ tag: tags.tagName, color: ONE_DARK.red },
{ tag: tags.attributeName, color: ONE_DARK.orange },
{ tag: tags.heading, color: ONE_DARK.red, fontStyle: 'bold' },
{ tag: tags.emphasis, fontStyle: 'italic' },
{ tag: tags.strong, fontStyle: 'bold' },
{ tag: tags.link, color: ONE_DARK.blue, fontStyle: 'underline' },
{ tag: tags.meta, color: ONE_DARK.yellow },
// A visible mark, not the one-dark convention's white: `invalid` is the
// token a JSONL/JSON syntax error lands on, and white-on-a-light-panel is
// invisible. The reader must be able to SEE which text did not parse.
{ tag: tags.invalid, color: ONE_DARK.red, textDecoration: 'underline wavy' },
]
/** one-light syntax rules (the light counterpart; hues from one-dark-palette.ts). */
const HIGHLIGHTS_LIGHT: HighlightRule[] = [
{ tag: tags.comment, color: ONE_LIGHT.gray, fontStyle: 'italic' },
{ tag: tags.keyword, color: ONE_LIGHT.magenta },
{ tag: tags.string, color: ONE_LIGHT.green },
{ tag: tags.number, color: ONE_LIGHT.orange },
{ tag: tags.bool, color: ONE_LIGHT.blue },
{ tag: tags.atom, color: ONE_LIGHT.blue },
{ tag: tags.typeName, color: ONE_LIGHT.yellow },
{ tag: tags.className, color: ONE_LIGHT.yellow },
{ tag: tags.propertyName, color: ONE_LIGHT.red },
{ tag: tags.function(tags.variableName), color: ONE_LIGHT.yellow },
{ tag: tags.variableName, color: ONE_LIGHT.red },
{ tag: tags.operator, color: ONE_LIGHT.black },
{ tag: tags.tagName, color: ONE_LIGHT.red },
{ tag: tags.attributeName, color: ONE_LIGHT.orange },
{ tag: tags.heading, color: ONE_LIGHT.red, fontStyle: 'bold' },
{ tag: tags.emphasis, fontStyle: 'italic' },
{ tag: tags.strong, fontStyle: 'bold' },
{ tag: tags.link, color: ONE_LIGHT.link, fontStyle: 'underline' },
{ tag: tags.meta, color: ONE_LIGHT.yellow },
// Same reasoning as the dark family: this used to be pure white, which on a
// light panel is exactly the background — an error the reader cannot see.
{ tag: tags.invalid, color: ONE_LIGHT.red, textDecoration: 'underline wavy' },
]
/** The scheme-dependent extension pair (surface tint + syntax highlight). */
function cmThemeExtensions(dark: boolean): Array<ReturnType<typeof EditorView.theme>> {
return [
cmSurfaceTint(dark),
syntaxHighlighting(HighlightStyle.define(dark ? HIGHLIGHTS_DARK : HIGHLIGHTS_LIGHT)),
]
}
/**
* A Compartment holding the two scheme-dependent extensions. Created once
* per editor view; a scheme flip dispatches `reconfigure(dark)` on it, so
* the document, undo history, scroll and keymaps survive re-theming.
*/
export class CmThemeCompartment {
private readonly compartment = new Compartment()
/** `of(...)` payload for EditorState.create. */
of(dark: boolean): ReturnType<Compartment['of']> {
return this.compartment.of(cmThemeExtensions(dark))
}
/** Reconfigure for a new scheme. */
reconfigure(dark: boolean): ReturnType<Compartment['reconfigure']> {
return this.compartment.reconfigure(cmThemeExtensions(dark))
}
}