From bdd2f107b0d8ec1203d51e027bd2e7e1d2e243fa Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Mon, 18 Sep 2023 11:31:54 +0200 Subject: [PATCH] Improve behavior of moveVertically on line wrap boundaries FIX: Fix some issues with the way `moveVertically` behaved for positions on line wrap points. Closes https://github.com/codemirror/dev/issues/1255 --- src/cursor.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cursor.ts b/src/cursor.ts index 0d6619c..8b29bd1 100644 --- a/src/cursor.ts +++ b/src/cursor.ts @@ -300,7 +300,7 @@ export function moveVertically(view: EditorView, start: SelectionRange, forward: if (startPos == (forward ? view.state.doc.length : 0)) return EditorSelection.cursor(startPos, start.assoc) let goal = start.goalColumn, startY let rect = view.contentDOM.getBoundingClientRect() - let startCoords = view.coordsAtPos(startPos), docTop = view.documentTop + let startCoords = view.coordsAtPos(startPos, start.assoc || -1), docTop = view.documentTop if (startCoords) { if (goal == null) goal = startCoords.left - rect.left startY = dir < 0 ? startCoords.top : startCoords.bottom @@ -314,8 +314,11 @@ export function moveVertically(view: EditorView, start: SelectionRange, forward: for (let extra = 0;; extra += 10) { let curY = startY + (dist + extra) * dir let pos = posAtCoords(view, {x: resolvedGoal, y: curY}, false, dir)! - if (curY < rect.top || curY > rect.bottom || (dir < 0 ? pos < startPos : pos > startPos)) - return EditorSelection.cursor(pos, start.assoc, undefined, goal) + if (curY < rect.top || curY > rect.bottom || (dir < 0 ? pos < startPos : pos > startPos)) { + let charRect = view.docView.coordsForChar(pos) + let assoc = !charRect || curY < charRect.top ? -1 : 1 + return EditorSelection.cursor(pos, assoc, undefined, goal) + } } }