From 87eabfb8f58c4974873eef09b1f7749458d07778 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 9 Jan 2024 20:59:43 +0700 Subject: [PATCH] fix: move cursor up/down issue (#657) --- lib/src/extensions/position_extension.dart | 39 +++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/src/extensions/position_extension.dart b/lib/src/extensions/position_extension.dart index aabf5925d..826541514 100644 --- a/lib/src/extensions/position_extension.dart +++ b/lib/src/extensions/position_extension.dart @@ -90,6 +90,43 @@ extension PositionExtension on Position { : rect.centerLeft.translate(0, rect.height); } - return editorState.service.selectionService.getPositionInOffset(offset); + final position = + editorState.service.selectionService.getPositionInOffset(offset); + + if (position != null && !position.path.equals(path)) { + return position; + } + + if (upwards) { + final previous = selection.start.path.previous; + if (previous.isNotEmpty && !previous.equals(selection.start.path)) { + final node = editorState.document.nodeAtPath(previous); + final selectable = node?.selectable; + var offset = selection.startIndex; + if (selectable != null) { + offset = offset.clamp( + selectable.start().offset, + selectable.end().offset, + ); + return Position(path: previous, offset: offset); + } + } + } else { + final next = selection.end.path.next; + if (next.isNotEmpty && !next.equals(selection.end.path)) { + final node = editorState.document.nodeAtPath(next); + final selectable = node?.selectable; + var offset = selection.endIndex; + if (selectable != null) { + offset = offset.clamp( + selectable.start().offset, + selectable.end().offset, + ); + return Position(path: next, offset: offset); + } + } + } + + return this; } }