fix(editor): 落地到行时把光标亮出来,并让引用赢过预览的阅读位置(#826 收尾之四)
第一次真机手测反馈:跳转确实进了编辑视图,但**看不到光标**,所以不知道落在哪一行。
两个原因:
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;去掉清空那行即红)。
This commit is contained in:
1 parent
6dd0486f5c
commit
776e777da7
2 files changed
+60
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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<FileViewerProps> = {}): 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)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user