Skip to content

Commit 9c2e8d0

Browse files
fix: scroll-then-click places cursor at clicked position
Capture document position at mousedown time via posAtCoords and use it when collapsing accidental selections. Previously used selection.to which could be the old cursor position when scrolling upward, causing the view to jump back to the wrong location. Also removed aggressive scroll restoration that could conflict with CodeMirror's internal scroll management. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e28188c commit 9c2e8d0

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

frontend/src/app/components/editor/editor.component.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ export class EditorComponent implements AfterViewInit, OnDestroy {
355355
const hadSelection = !view.state.selection.main.empty;
356356
const downX = event.clientX;
357357
const downY = event.clientY;
358+
// Capture the document position the user actually clicked on
359+
const clickedPos = view.posAtCoords({ x: event.clientX, y: event.clientY });
358360

359361
// Track whether the user dragged (intentional selection)
360362
const onMouseUp = (upEvent: MouseEvent) => {
@@ -365,14 +367,8 @@ export class EditorComponent implements AfterViewInit, OnDestroy {
365367

366368
// Use setTimeout to check after CodeMirror processes the click
367369
setTimeout(() => {
368-
const newScrollTop = view.scrollDOM.scrollTop;
369370
const selection = view.state.selection.main;
370371

371-
// If scroll jumped more than 100px, restore it
372-
if (Math.abs(newScrollTop - scrollTop) > 100) {
373-
view.scrollDOM.scrollTop = scrollTop;
374-
}
375-
376372
// If a selection was created but there wasn't one before,
377373
// and user didn't drag, collapse it (accidental scroll+click selection)
378374
if (!wasDrag && !hadSelection && !selection.empty) {
@@ -386,8 +382,12 @@ export class EditorComponent implements AfterViewInit, OnDestroy {
386382
toLineNumber: toLine
387383
})
388384
) {
385+
// Use the captured click position so cursor goes where the
386+
// user actually clicked, not selection.to which may be the
387+
// old cursor position if the user scrolled upward.
388+
const targetPos = clickedPos ?? selection.head;
389389
view.dispatch({
390-
selection: { anchor: selection.to }
390+
selection: { anchor: targetPos }
391391
});
392392
}
393393
}

0 commit comments

Comments
 (0)