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