Bläddra i källkod

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
Alf Eaton 11 månader sedan
förälder
incheckning
ff1c4cf2f5

+ 20 - 9
services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx

@@ -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)

+ 17 - 3
services/web/frontend/js/features/review-panel/components/review-panel-entry.tsx

@@ -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)