- 新 src/client/EditorHost.tsx:'editor' tab 组件。matchFileViewer → 按 fetchStrategy 分发(纯逻辑在 editor-load.ts)→ 渲染 viewer.component; binary 无匹配时渲染共享 BinaryDownload。标题栏保留在 host - 新 src/client/TextEditor.tsx:code/markdown 的 viewer 组件(CodeMirror + 保存/脏点/截断横幅 + md 预览/编辑切换),内容由 host 经 fsRead 传入, 自身不再 fetch;按 viewerId === 'markdown' 分支(不再解析扩展名) - 新 src/client/editor-load.ts:planFirstMatch / planFsReadOutcome / decodeHead 纯函数,策略分发可单测 - 新 src/client/binary-download.tsx:共享下载兜底组件(viewer 注册与 host fallback 同一 UI) - host src/index.ts:fs.read 二进制响应增加 head 字段(base64,前 4KB, 唯一 host 触碰,设计文档 detect 机制所需);客户端 FsBinaryResult 同步 - builtins:注册 8 个 viewer(新增 markdown exts md/markdown、code catch-all -100);binary-download 增加 NUL detect + 复用 BinaryDownload - 删除 src/client/EditorView.tsx(职责一分为二);陈旧注释同步 - 新增 tests/editor-load.spec.ts(12 用例)
102 lines
4.2 KiB
TypeScript
102 lines
4.2 KiB
TypeScript
/**
|
|
* The editor tab host: resolves a file's previewer through the sidebar
|
|
* registry (`matchFileViewer`), fetches bytes per the matched viewer's
|
|
* fetch strategy, and renders its component — or the shared download pane
|
|
* when nothing can render the file. The header shows the file title; the
|
|
* editable code/markdown viewers render their own toolbar below it.
|
|
*
|
|
* The strategy dispatch is pure (planFirstMatch / planFsReadOutcome in
|
|
* editor-load.ts); this component only wires it to the host APIs.
|
|
*/
|
|
import { useEffect, useState } from 'react'
|
|
import { createElement } from 'react'
|
|
import type { Context } from '../context-types.ts'
|
|
import { api, mediaUrl, type SessionScope } from './api.ts'
|
|
import { BinaryDownload } from './binary-download.tsx'
|
|
import { planFirstMatch, planFsReadOutcome, type EditorLoadAction } from './editor-load.ts'
|
|
import { t } from './locales.ts'
|
|
import type { FileViewerDescriptor } from './service.ts'
|
|
import type { SidebarStore } from './state.ts'
|
|
import css from './sidebar.module.css'
|
|
|
|
type EditorLoad =
|
|
| { status: 'loading' }
|
|
| { status: 'error'; message: string }
|
|
| { status: 'ready'; viewer: FileViewerDescriptor; content?: string; truncated?: boolean; mediaUrl?: string; customData?: unknown }
|
|
| { status: 'binary' }
|
|
|
|
export function EditorHost(props: { ctx: Context; store: SidebarStore; scope: SessionScope; path: string; title: string }) {
|
|
const { ctx, store, scope, path, title } = props
|
|
const [load, setLoad] = useState<EditorLoad>({ status: 'loading' })
|
|
|
|
useEffect(() => {
|
|
let cancelled = false
|
|
setLoad({ status: 'loading' })
|
|
const mediaUrlOf = (): string => mediaUrl(scope, path)
|
|
const apply = (action: EditorLoadAction): void => {
|
|
if (cancelled) return
|
|
switch (action.kind) {
|
|
case 'binary':
|
|
setLoad({ status: 'binary' })
|
|
return
|
|
case 'render':
|
|
setLoad({
|
|
status: 'ready',
|
|
viewer: action.viewer,
|
|
content: action.content,
|
|
truncated: action.truncated,
|
|
mediaUrl: action.mediaUrl,
|
|
customData: action.customData,
|
|
})
|
|
return
|
|
case 'customLoad':
|
|
void action.viewer.load?.(path, scope).then((data) => {
|
|
if (cancelled) return
|
|
setLoad({ status: 'ready', viewer: action.viewer, customData: data })
|
|
}).catch((error: unknown) => {
|
|
if (cancelled) return
|
|
setLoad({ status: 'error', message: error instanceof Error ? error.message : String(error) })
|
|
})
|
|
return
|
|
case 'fetchFsRead':
|
|
api.fsRead(scope, path).then((result) => {
|
|
if (cancelled) return
|
|
// Binary reads carry the head bytes for the detect re-match.
|
|
const outcome = planFsReadOutcome(action.viewer, {
|
|
binary: result.kind === 'binary',
|
|
content: result.kind === 'text' ? result.content : '',
|
|
truncated: result.truncated,
|
|
head: result.kind === 'binary' ? result.head : undefined,
|
|
}, (head) => ctx.betterSidebar?.matchFileViewer(path, head), mediaUrlOf)
|
|
apply(outcome)
|
|
}).catch((error: unknown) => {
|
|
if (cancelled) return
|
|
setLoad({ status: 'error', message: error instanceof Error ? error.message : String(error) })
|
|
})
|
|
return
|
|
}
|
|
}
|
|
apply(planFirstMatch(ctx.betterSidebar?.matchFileViewer(path), mediaUrlOf))
|
|
return () => { cancelled = true }
|
|
}, [scope.sessionId, scope.cwd, path, ctx])
|
|
|
|
return (
|
|
<div className={css.editor}>
|
|
<div className={css.editorHeader}>
|
|
<span className={css.editorTitle} title={path}>{title}</span>
|
|
</div>
|
|
{load.status === 'loading' && <div className={css.editorPlaceholder}>{t('loading')}</div>}
|
|
{load.status === 'error' && <div className={css.editorError}>{load.message}</div>}
|
|
{load.status === 'binary' && <BinaryDownload scope={scope} path={path} />}
|
|
{load.status === 'ready' && createElement(load.viewer.component, {
|
|
ctx, store, scope, path, title,
|
|
viewerId: load.viewer.id,
|
|
content: load.content,
|
|
truncated: load.truncated,
|
|
mediaUrl: load.mediaUrl,
|
|
customData: load.customData,
|
|
})}
|
|
</div>
|
|
)
|
|
}
|