From 62a9ceeed5ed32c0211481cd77dba3ce3c89537b Mon Sep 17 00:00:00 2001 From: Menghuan1918 Date: Wed, 18 Nov 2026 00:07:11 +0800 Subject: [PATCH] fix(editor): keep a single very long line out of the edit buffer (closes #1165) --- CHANGELOG.md | 3 ++ CHANGELOG_EN.md | 3 ++ docs/external-plugin-guide.md | 4 +-- src/client/EditorHost.tsx | 3 +- src/client/LargeFileView.tsx | 21 +++++++---- src/client/TextEditor.tsx | 1 + src/client/api.ts | 11 +++--- src/client/editor-load.ts | 19 +++++++--- src/client/locales-ar.ts | 1 + src/client/locales-de.ts | 1 + src/client/locales-fr.ts | 1 + src/client/locales-hi.ts | 1 + src/client/locales-id.ts | 1 + src/client/locales-it.ts | 1 + src/client/locales-ja.ts | 1 + src/client/locales-ko.ts | 1 + src/client/locales-nl.ts | 1 + src/client/locales-pl.ts | 1 + src/client/locales-pt.ts | 1 + src/client/locales-ru.ts | 1 + src/client/locales-sv.ts | 1 + src/client/locales-th.ts | 1 + src/client/locales-tr.ts | 1 + src/client/locales-vi.ts | 1 + src/client/locales-zh-HK.ts | 1 + src/client/locales-zh-MO.ts | 1 + src/client/locales-zh-TW.ts | 1 + src/client/locales.ts | 2 ++ src/client/service.ts | 11 +++--- src/editor-limits.ts | 62 ++++++++++++++++++++++++++++++++ src/index.ts | 36 +++++++++++++++---- tests/editor-host.spec.tsx | 4 +-- tests/editor-large-file.spec.tsx | 34 ++++++++++++++++++ tests/editor-limits.spec.ts | 52 +++++++++++++++++++++++++++ tests/editor-load.spec.ts | 12 +++++++ tests/smoke.spec.ts | 27 +++++++++++++- 36 files changed, 292 insertions(+), 32 deletions(-) create mode 100644 tests/editor-limits.spec.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 1aaeeb74..45966df3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ - 🔍 **宿主接管页头时也有「查找」入口了**([#1214](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1214)):v0.32.1 给编辑器自带工具条加的查找按钮,在侧栏真实的合并式页头(`EditorHost` 以 `toolbar: 'host'` 接管)下整行不渲染,markdown / html / jsonl 预览态更是没有任何查找入口,只剩可能被第三方插件全局截获的 Cmd/Ctrl+F。现在内置编辑器把查找命令注册给宿主页头:文件可编辑时页头显示查找按钮,点击与自带工具条按钮走同一套逻辑——预览态先切到编辑,再打开 CodeMirror 的查找面板(含替换行)。没有查找面的 viewer 不注册该命令,页头也就不显示按钮。 - 🧪 测试:`tests/editor-host.spec.tsx` 用真实 `TextEditor` 走「页头查找按钮 → 搜索面板打开 + markdown 预览切编辑」;`tests/editor-find.spec.tsx` 增加 host 模式命令测试。 +- 🪲 **单行几百 KB 的压缩文件不再在点「编辑」时卡死渲染进程**([#1165](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1165)):327 KB 的压缩 HTML(整份文件只有一行)能通过 5 MB 的字节上限,但 CodeMirror 按行虚拟化——一行几百 KB 会在首帧一次性解析、着色、排版,渲染进程直接卡死,只能重启。现在 `fs.read` 在宿主侧对已解码文档的**前 64 K 字符(65,536)**做一次有界探测:任一行达到 20,000 字符就以 `{ kind: 'too-large', reason: 'line' }` 拒绝文档,客户端从不扫描字节,也不会为它创建任何 CodeMirror 状态。它走既有的只读分页预览,提示改为「文件含超长单行(大小)」,不再假装是「文件过大」,下载出口照旧。字节上限(5 MiB)与分页逻辑都不变。 + - 🧪 测试:`tests/editor-limits.spec.ts`(单行超限 / 正常多行 / 临界值 / CRLF / 有界探测)、`tests/editor-large-file.spec.tsx`(长单行提示 + 无编辑器)、`tests/editor-load.spec.ts` 与 `tests/editor-host.spec.tsx`(`reason` 沿线透传)。 + - 🎯 **侧边对话「回到最新」胶囊不再塌成细条、端部恢复正圆**([#1208](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1208)):胶囊所在的 `.sidechatToLatestSlot` 是 `height: 0` 的绝对定位 flex 槽位(抄宿主 `ui-chat` 的 `.toBottomSlot` 配方),但子项没有尺寸约束——默认 `align-items: stretch` 会在零高容器里把它拉到 0 高,内容高度被丢掉,只剩 `padding + border` 的 10px,文字于是溢出到胶囊外。宿主自己的 `.toBottom` 靠显式 `34px` 高度绕开了同一个坑,这里改用 `align-items: flex-end` + `flex: none` 让胶囊保持自身内容高度(30px)。同时补上 `corner-shape: round`:宿主 `corner-shape.css` 对 `*` 施加 `superellipse(1.5)`,会把 `border-radius: 999px` 的胶囊端部削平,宿主的全圆元素都成对声明退出该效果,本插件此前漏了这条。该按钮在 v0.31.0 引入,v0.31.0–v0.32.1 均受影响。 - 🧪 真实宿主 + Chromium 挂载冒烟确认侧边对话渲染无回归;修复前后在 Chrome 157 用宿主真实 token 样式表做过 A/B 对照(高度 10px→30px、文字溢出 true→false、`corner-shape` 恢复 `superellipse(1)`)。 diff --git a/CHANGELOG_EN.md b/CHANGELOG_EN.md index 772e53c7..d8019213 100644 --- a/CHANGELOG_EN.md +++ b/CHANGELOG_EN.md @@ -9,6 +9,9 @@ - 🔍 **Find is available when the host owns the editor header** ([#1214](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1214)): the find button added to the editor's own toolbar in v0.32.1 never rendered in the real sidebar, where `EditorHost` takes the header over with `toolbar: 'host'` and the whole self-toolbar row is skipped — and a markdown/html/jsonl preview had no find entry at all, leaving Cmd/Ctrl+F as the only way, which a third-party plugin can capture globally. The built-in text editor now registers its find command with the host header: the button appears while the file is editable and runs the same logic as the toolbar one — flip a preview to edit first, then open CodeMirror's search panel (replace row included). A viewer with no find surface omits the command, so no button appears for it. - 🧪 `tests/editor-host.spec.tsx` drives the real `TextEditor` through "header find button → panel opens + markdown preview flips to edit"; `tests/editor-find.spec.tsx` covers the host-mode command. +- 🪲 **A few-hundred-KB single-line file no longer freezes the renderer on 编辑** ([#1165](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1165)): a 327 KB minified HTML file (the whole document is one line) passes the 5 MB byte limit, but CodeMirror virtualizes by line — one line of a few hundred KB is parsed, tokenized and laid out in a single frame, hanging the renderer until the process is restarted. `fs.read` now runs a bounded probe over the **first 64K characters** (65,536) of the already-decoded document on the host: any line reaching 20,000 characters is refused with `{ kind: 'too-large', reason: 'line' }`, so the client never scans the bytes and no CodeMirror state is ever built for the file. It uses the existing read-only paged preview, whose notice now says "file has a very long line (size)" instead of pretending the file is too large, and the download escape hatch stays. The 5 MiB byte limit and the paging logic are unchanged. + - 🧪 Tests: `tests/editor-limits.spec.ts` (over-limit single line / normal multi-line / threshold / CRLF / bounded probe), `tests/editor-large-file.spec.tsx` (long-line notice + no editor), and `tests/editor-load.spec.ts` + `tests/editor-host.spec.tsx` (`reason` carried through). + - 🎯 **The side chat's "back to latest" pill no longer collapses into a sliver, and its ends are round again** ([#1208](https://github.com/omdsh-dev/DSH-better-sidebar/issues/1208)): the pill's `.sidechatToLatestSlot` is a `height: 0` absolutely-positioned flex slot (the host's `ui-chat` `.toBottomSlot` recipe), but its child carried no size constraint — the default `align-items: stretch` pulled it to zero height inside a zero-height box, discarding its content height and leaving only the `padding + border` 10px, so the label overflowed outside the capsule. The host's own `.toBottom` sidesteps the same trap with an explicit `34px` height; this uses `align-items: flex-end` + `flex: none` so the pill keeps its own content height (30px). `corner-shape: round` is added alongside: the host's `corner-shape.css` applies `superellipse(1.5)` to `*`, which squares off the ends of a `border-radius: 999px` capsule, and the host's own full-round elements opt out in pairs — this plugin had missed that. The button shipped in v0.31.0 and every release from v0.31.0 through v0.32.1 is affected. - 🧪 The real-host + Chromium mount smoke confirms side-chat rendering has no regression; the before/after was A/B-checked in Chrome 157 against the host's real token sheets (height 10px → 30px, text overflow true → false, `corner-shape` back to `superellipse(1)`). diff --git a/docs/external-plugin-guide.md b/docs/external-plugin-guide.md index 1031f4d5..cc36cd16 100644 --- a/docs/external-plugin-guide.md +++ b/docs/external-plugin-guide.md @@ -540,7 +540,7 @@ interface FileViewerProps { viewerId: string // 命中 viewer 的 id(如 'code' / 'my-plugin:csv') content?: string // fetchStrategy='fsRead' 时;超过编辑上限的文件不带它 size?: number // 本次读取量到的文件字节数 - largeFile?: { size: number } // 见下方「超大文件」 + largeFile?: { size: number; reason?: 'size' | 'line' } // 见下方「超大文件」 mtimeMs?: number // fetchStrategy='fsRead' 时:本次读取的磁盘 mtime(保存冲突基线) mediaUrl?: string // fetchStrategy='mediaUrl' 时 customData?: unknown // fetchStrategy='custom' 时(load() 的返回值) @@ -561,7 +561,7 @@ interface FileViewerProps { > **页头查找(#1214)**:当 viewer 以 `toolbar: 'host'` 运行(本插件的合并式编辑器页头),查找按钮由宿主页头渲染,而命令来自 viewer 注册的 `EditorToolbarControls.search`。**不注册 `search` 就没有查找按钮**,第三方 viewer 无需改动。内置文本编辑器的 `search()` 与自带工具条的查找按钮同一套逻辑:markdown / html / jsonl 在预览态会先切到编辑,再打开 CodeMirror 的查找面板(含替换行);纯代码文件直接打开。 > -> **超大文件(v0.26.0 起,`truncated` 已被取代)**:文件超过编辑上限(5 MiB,见 `src/editor-limits.ts` 的 `MAX_EDITABLE_BYTES`)时,`fs.read` 不再回一份被截断的文档,而是 `{ kind: 'too-large', size, mtimeMs, head }` —— **不带 `content`**。宿主把这个结果交给命中的 viewer 时,`content` 缺席、`largeFile: { size }` 到位(见 `planFsReadOutcome`)。内置的文本 viewer 据此渲染只读分页预览(`LargeFileView`),并提供下载出口;**你的 viewer 必须容忍 `content === undefined`**:要么自己处理 `largeFile`(分页预览 / 提示),要么把状态回给 `onToolbarState` 让宿主收起保存按钮。旧的 `truncated` 布尔字段与 `EditorToolbarState.truncated` 都已删除——「拿到半份文档但看起来能编辑」这个状态本身没有了,这正是它被删掉的原因(#732:保存半份文档会毁掉文件尾部)。 +> **超大文件(v0.26.0 起,`truncated` 已被取代)**:文件超过编辑上限(5 MiB,见 `src/editor-limits.ts` 的 `MAX_EDITABLE_BYTES`),**或文件头 64 K 字符内出现单行 ≥ 20,000 字符**(#1165:压缩成单行的 HTML/JS 远低于字节上限,却会让 CodeMirror 的单行虚拟化在首帧卡死)时,`fs.read` 不再回一份被截断的文档,而是 `{ kind: 'too-large', size, mtimeMs, head, reason }` —— **不带 `content`**;`reason` 为 `'size'` 或 `'line'`,让 UI 能说清是哪个上限。宿主把这个结果交给命中的 viewer 时,`content` 缺席、`largeFile: { size, reason? }` 到位(见 `planFsReadOutcome`)。内置的文本 viewer 据此渲染只读分页预览(`LargeFileView`),并提供下载出口;**你的 viewer 必须容忍 `content === undefined`**:要么自己处理 `largeFile`(分页预览 / 提示),要么把状态回给 `onToolbarState` 让宿主收起保存按钮。旧的 `truncated` 布尔字段与 `EditorToolbarState.truncated` 都已删除——「拿到半份文档但看起来能编辑」这个状态本身没有了,这正是它被删掉的原因(#732:保存半份文档会毁掉文件尾部)。 > > **分页读取**:`fs.readPage({ sessionId, cwd?, path, offset })` 按字节窗口返回一页文本(宿主按行对齐,并在同一个文件内锁定宽字符编码),返回 `{ kind:'text', text, offset, nextOffset, eof, size, mtimeMs, encoding }`。消费方式就是**只向前追加**:把每页的 `text` 接到一起,下一次请求用上一页的 `nextOffset` 作为 `offset`,直到 `eof`。因此**必须串行**(下一页的偏移就是上一页的响应),也**不能跳到末尾**——随机访问需要打开时先扫全文件建索引,而那正是分页要避免的开销。内置预览在这之上加了两个策略:滚到底自动追加下一页,以及一条按**渲染行数**设的上限(`LargeFileView` 的 `MAX_PREVIEW_ROWS`),到顶后停止追加并提示下载。 diff --git a/src/client/EditorHost.tsx b/src/client/EditorHost.tsx index c49554a1..6cc0d7bd 100644 --- a/src/client/EditorHost.tsx +++ b/src/client/EditorHost.tsx @@ -27,6 +27,7 @@ import { createElement } from 'react' import clsx from 'clsx' import { Button, IconCheckOutlineRegular, IconFolderOpenRegular, IconRefreshOutlineRegular, IconSearchOutlineRegular, writeClipboard } from '@deepseek-ai/dsh-client-ui-primitives' import type { Context } from '../context-types.ts' +import type { EditLimitReason } from '../editor-limits.ts' import { api, mediaUrl, SidebarApiError, type SessionScope } from './api.ts' import { BinaryDownload } from './binary-download.tsx' import { isDirectoryReadError, planFirstMatch, planFsReadOutcome, type EditorLoadAction } from './editor-load.ts' @@ -79,7 +80,7 @@ interface LineNav { key: string; nonce: number | null; end?: number } type EditorLoad = | { status: 'loading' } | { status: 'error'; message: string; fileNotFound?: boolean; windowsAbsolutePath?: boolean; repairStatus?: 'searching' | 'done'; repairMatches?: string[]; repairError?: string } - | { status: 'ready'; viewer: FileViewerDescriptor; content?: string; size?: number; largeFile?: { size: number }; mtimeMs?: number; mediaUrl?: string; customData?: unknown } + | { status: 'ready'; viewer: FileViewerDescriptor; content?: string; size?: number; largeFile?: { size: number; reason?: EditLimitReason }; mtimeMs?: number; mediaUrl?: string; customData?: unknown } | { status: 'binary' } /** The docked tree panel's width bounds (drag-resize clamps into them). */ diff --git a/src/client/LargeFileView.tsx b/src/client/LargeFileView.tsx index d2458f34..c892fbd5 100644 --- a/src/client/LargeFileView.tsx +++ b/src/client/LargeFileView.tsx @@ -29,7 +29,7 @@ import clsx from 'clsx' import { IconRefreshOutlineRegular, JsonTree, type JsonTreeLabels } from '@deepseek-ai/dsh-client-ui-primitives' import type { SessionScope } from './api.ts' import { downloadUrl } from './api.ts' -import { formatByteSize, MAX_EDITABLE_BYTES, MAX_EDITABLE_LABEL } from '../editor-limits.ts' +import { formatByteSize, MAX_EDITABLE_BYTES, MAX_EDITABLE_LABEL, type EditLimitReason } from '../editor-limits.ts' import { isJsonlPath, isTreeRecord, parseRecord, summarizeRecord } from './jsonl.ts' import { splitLines, useTextPager } from './text-pages.ts' import { t } from './locales.ts' @@ -75,6 +75,9 @@ function jsonLabels(): JsonTreeLabels { * @param props.scope - the session scope. * @param props.path - the file being previewed. * @param props.size - the file's size in bytes (from the read that refused it). + * @param props.reason - why the file stayed out of the edit buffer: the byte + * limit (`size`, also the fallback when absent) or the long-line probe + * (`line`, #1165). The notice names the actual limit. * @param props.visible - whether the tab is on screen; a parked tab stops reading. * @param props.revision - bumped by the host's refresh to restart from page one. * @param props.maxRows - row ceiling (defaults to {@link MAX_PREVIEW_ROWS}). @@ -84,6 +87,7 @@ export function LargeFileView(props: { scope: SessionScope path: string size: number + reason?: EditLimitReason visible: boolean revision: number maxRows?: number @@ -101,14 +105,15 @@ export function LargeFileView(props: { const labels = useMemo(() => jsonLabels(), []) /** - * Whether this file is over the EDITABLE threshold — a different question - * from "is this view paged". + * Whether this file is over an EDITABLE limit — the byte threshold, or the + * long-line probe (#1165) — a different question from "is this view paged". * * A small `.jsonl` opens straight into this record list as its PREVIEW, and * announcing "the file is too large, paged preview only" over a 2 KB file - * would be a lie. The threshold decides, not the component. + * would be a lie. The host's reason (or the byte threshold, for a caller + * that carries none) decides, not the component. */ - const tooLarge = size > MAX_EDITABLE_BYTES + const overLimit = props.reason === 'line' || size > MAX_EDITABLE_BYTES const rows = useMemo(() => pager.pages.map(splitLines), [pager.pages]) const totalRows = useMemo(() => rows.reduce((sum, page) => sum + page.length, 0), [rows]) @@ -151,9 +156,11 @@ export function LargeFileView(props: { return (
- {tooLarge && ( + {overLimit && (
- {t('previewOnlyNotice', { size: formatByteSize(size), limit: MAX_EDITABLE_LABEL })} + {props.reason === 'line' + ? t('previewLongLineNotice', { size: formatByteSize(size) }) + : t('previewOnlyNotice', { size: formatByteSize(size), limit: MAX_EDITABLE_LABEL })}