Scroll directly to comment position (#28349)

* Scroll directly to comment position

* Only use EditorView.scrollIntoView when the target position is outside the viewport

* Always listen for editor:scroll-position-restored (#28352)

GitOrigin-RevId: 2da23b05ddd4ddbd2631c1da5b27dbef4757c86d
This commit is contained in:
Alf Eaton
2025-09-17 11:26:08 +01:00
committed by Copybot
parent f411ef9273
commit 4b11851738
2 changed files with 37 additions and 12 deletions

View File

@@ -402,20 +402,31 @@ export const EditorManagerProvider: FC<React.PropsWithChildren> = ({
})
)
if (hasGotoLine(options)) {
window.setTimeout(() => jumpToLine(options))
const jump = () => jumpToLine(options)
// Jump to the line again after a stored scroll position has been restored
if (isNewDoc) {
window.addEventListener(
'editor:scroll-position-restored',
() => jumpToLine(options),
{ once: true }
)
// Jump to the line after a stored scroll position has been restored
window.addEventListener('editor:scroll-position-restored', jump, {
once: true,
})
} else {
// Jump directly to the line
jump()
}
} else if (hasGotoOffset(options)) {
window.setTimeout(() => {
const jump = () => {
eventEmitter.emit('editor:gotoOffset', options)
})
}
if (isNewDoc) {
// Jump to the offset after a stored scroll position has been restored
window.addEventListener('editor:scroll-position-restored', jump, {
once: true,
})
} else {
// Jump directly to the offset
jump()
}
}
resolve(doc)

View File

@@ -15,6 +15,7 @@ import { EditorSelection } from '@codemirror/state'
import { OFFSET_FOR_ENTRIES_ABOVE } from '../utils/position-items'
import useReviewPanelLayout from '../hooks/use-review-panel-layout'
import { EntryIndicator } from './review-panel-entry-indicator'
import { EditorView } from '@codemirror/view'
export const ReviewPanelEntry: FC<
React.PropsWithChildren<{
@@ -86,9 +87,22 @@ export const ReviewPanelEntry: FC<
openDocWithId(docId, { gotoOffset: position, keepCurrentView: true })
} else {
setTimeout(() => {
view.dispatch({
selection: EditorSelection.cursor(position),
})
const selection = EditorSelection.cursor(position)
// if the position is outside the viewport, so could be estimated,
// use EditorView.scrollIntoView (accurate, not smooth)
if (position < view.viewport.from || view.viewport.to < position) {
view.dispatch({
selection,
effects: EditorView.scrollIntoView(selection, { y: 'center' }),
})
return
}
// if the position is inside the viewport, so accurate,
// use scrollDOM.scrollTo (smooth)
view.dispatch({ selection })
// scroll to line (centered)
const blockInfo = view.lineBlockAt(position)