Skip to content

Commit

Permalink
fix: move cursor up/down issue (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Jan 9, 2024
1 parent bdc0dc1 commit 87eabfb
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion lib/src/extensions/position_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 87eabfb

Please sign in to comment.