From 94c3d7e3d2ba7fb69d19a1930805d783b625623b Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 9 Jan 2024 11:42:21 +0800 Subject: [PATCH] fix: move cursor up/down issus --- 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; } }