From 776e777da71ea49573a8b3cda8ed8ac2e47be208 Mon Sep 17 00:00:00 2001 From: ymac <707337118@qq.com> Date: Sun, 11 Oct 2026 16:06:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(editor):=20=E8=90=BD=E5=9C=B0=E5=88=B0?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E6=8A=8A=E5=85=89=E6=A0=87=E4=BA=AE=E5=87=BA?= =?UTF-8?q?=E6=9D=A5=EF=BC=8C=E5=B9=B6=E8=AE=A9=E5=BC=95=E7=94=A8=E8=B5=A2?= =?UTF-8?q?=E8=BF=87=E9=A2=84=E8=A7=88=E7=9A=84=E9=98=85=E8=AF=BB=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=EF=BC=88#826=20=E6=94=B6=E5=B0=BE=E4=B9=8B=E5=9B=9B?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一次真机手测反馈:跳转确实进了编辑视图,但**看不到光标**,所以不知道落在哪一行。 两个原因: 1. **没给编辑器焦点**。落地只 `dispatch({ selection })`,而 CodeMirror 只在 **获焦**的编辑器里画光标,且零长度选择本身不画任何东西——未获焦的落地读起来 就是「文件打开了,停在某处」。现在只在**行号引用**这条路上 `view.focus()` (普通打开不动读者的焦点,读者可能是从聊天输入框点的链接)。 2. **预览的阅读位置会覆盖它**。进入编辑态时那条「按预览滚动位置恢复」的副作用 声明在落地之后,两个 effect 在同一次提交里,它会把选择与滚动重新写回读者 刚才预览的地方——读者先在预览里滚动过,再点带行号的引用,跳转就被静默抹掉。 落地前清掉 `previewSyncRef`:引用要的是源码里的那一行,不是预览里那个位置。 测试:text-editor-line-jump.spec.tsx 加 3 例(落地后 focus 被调用、普通打开不抢焦点、 markdown 翻转后仍获焦)+ 1 例判别性用例(预览滚到 90% 后再跳第 3 行,最后一次 dispatch 必须是第 3 行的 offset 8 而不是按比例算出的 14;去掉清空那行即红)。 --- src/client/TextEditor.tsx | 12 +++++++ tests/text-editor-line-jump.spec.tsx | 48 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/client/TextEditor.tsx b/src/client/TextEditor.tsx index 538e2c80..2de075a2 100644 --- a/src/client/TextEditor.tsx +++ b/src/client/TextEditor.tsx @@ -416,6 +416,11 @@ export function TextEditor(props: FileViewerProps) { return } landedAtRef.current = key + // The reference asked for a LINE, so it wins over the preview's remembered + // reading position: the flip's own "enter edit where the reader was" effect + // is declared AFTER this one and would otherwise re-dispatch (and re-scroll + // to) the preview position in the same commit, silently undoing the jump. + previewSyncRef.current = { text: null, ratio: 0 } // Out-of-range references clamp to the last line rather than throwing — // the file can have changed since the link was written. const doc = view.state.doc @@ -428,6 +433,13 @@ export function TextEditor(props: FileViewerProps) { // on the editor scroller can never touch anything outside the editor. view.requestMeasure() view.dispatch({ selection: { anchor: target } }) + // Show the caret. A selection alone is invisible here: CodeMirror draws the + // cursor only in a FOCUSED editor, and a zero-length selection paints + // nothing — so an unfocused landing read as "the file just opened + // somewhere" (reported from the first manual test of this feature). Only a + // line reference focuses; an ordinary open leaves the reader's focus where + // it was (e.g. in the chat composer they clicked the link from). + view.focus() requestAnimationFrame(() => { requestAnimationFrame(() => { const block = view.lineBlockAt(target) diff --git a/tests/text-editor-line-jump.spec.tsx b/tests/text-editor-line-jump.spec.tsx index d4bf2fe1..5b1ce50a 100644 --- a/tests/text-editor-line-jump.spec.tsx +++ b/tests/text-editor-line-jump.spec.tsx @@ -53,9 +53,18 @@ let container: HTMLDivElement | undefined const anchors: number[] = [] const originalDispatch = EditorView.prototype.dispatch +const originalFocus = EditorView.prototype.focus +/** Whether the landing showed the caret (CodeMirror draws it only when the + * editor has focus, so an unfocused jump reads as "opened somewhere"). */ +const focus = vi.fn() beforeEach(() => { anchors.length = 0 + focus.mockClear() + vi.spyOn(EditorView.prototype, 'focus').mockImplementation(function (this: EditorView) { + focus() + return originalFocus.call(this) + }) // Record the selections the component dispatches, and let the real dispatch // run: the reveal reads the document back out of the view right after // writing to it. @@ -142,4 +151,43 @@ const render = (overrides: Partial = {}): HTMLDivElement => { render({ onLineConsumed: consumed }) expect(consumed).not.toHaveBeenCalled() }) + + it('focuses the editor so the caret is visible where it landed', () => { + // Without focus CodeMirror paints no cursor at all and a zero-length + // selection paints nothing either, so the reader could not tell where the + // reference put them (reported from the first manual test). + render({ line: 3 }) + expect(focus).toHaveBeenCalled() + expect(anchors).toContain(8) + }) + + it('does not steal focus on an ordinary open', () => { + render() + expect(focus).not.toHaveBeenCalled() + }) + + it('focuses after a markdown file is flipped out of preview (the editor is display:none until then)', () => { + render({ viewerId: 'markdown', path: '/workspace/readme.md', line: 2 }) + expect(focus).toHaveBeenCalled() + }) + + it('a line reference beats the preview reading position it was scrolled to', () => { + // The flip's "enter edit where the reader was" effect is declared AFTER the + // landing and re-dispatches the preview-derived position, so a reader who + // had scrolled the preview first got their jump silently undone (the + // scroller was restored to where they came from). + const el = render({ viewerId: 'markdown', path: '/workspace/readme.md' }) + const preview = el.querySelector('[data-dsh-md-surface]')! + Object.defineProperty(preview, 'scrollHeight', { configurable: true, value: 1000 }) + Object.defineProperty(preview, 'clientHeight', { configurable: true, value: 100 }) + Object.defineProperty(preview, 'scrollTop', { configurable: true, value: 810, writable: true }) + // 90% down the preview: the proportional fallback alone would land on the + // LAST line (offset 14) — a different place from line 3 (offset 8). + act(() => { preview.dispatchEvent(new Event('scroll')) }) + + act(() => { + root!.render(createElement(TextEditor, base({ viewerId: 'markdown', path: '/workspace/readme.md', line: 3 }))) + }) + expect(anchors.at(-1)).toBe(8) + }) }) \ No newline at end of file