Skip to content

Commit

Permalink
fix: improves cursor left word delete (#88)
Browse files Browse the repository at this point in the history
* refactor: add position_extension file

* feat: move word/sentence del to new file

* test: cursor left word delete

* test: for cursor left sentence delete

* chore: formatting arrow keys test

* refactor: consolidate goLeft and goRight into one
  • Loading branch information
MayurSMahajan authored Apr 24, 2023
1 parent 32ffcf4 commit 58ea6b0
Show file tree
Hide file tree
Showing 7 changed files with 496 additions and 319 deletions.
1 change: 1 addition & 0 deletions lib/src/extensions/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export 'text_node_extensions.dart';

export 'text_style_extension.dart';
export 'url_launcher_extension.dart';
export 'position_extension.dart';
94 changes: 94 additions & 0 deletions lib/src/extensions/position_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';

enum SelectionRange {
character,
word,
}

extension PositionExtension on Position {
Position? moveHorizontal(
EditorState editorState, {
bool moveLeft = true,
SelectionRange selectionRange = SelectionRange.character,
}) {
final node = editorState.document.nodeAtPath(path);
if (node == null) {
return null;
}

if (moveLeft && offset == 0) {
final previousEnd = node.previous?.selectable?.end();
if (previousEnd != null) {
return previousEnd;
}
return null;
} else if (!moveLeft) {
final end = node.selectable?.end();
if (end != null && offset >= end.offset) {
return node.next?.selectable?.start();
}
}

switch (selectionRange) {
case SelectionRange.character:
if (node is TextNode) {
return Position(
path: path,
offset: moveLeft
? node.delta.prevRunePosition(offset)
: node.delta.nextRunePosition(offset),
);
}

return Position(path: path, offset: offset);
case SelectionRange.word:
if (node is TextNode) {
final result = moveLeft
? node.selectable?.getWordBoundaryInPosition(
Position(
path: path,
offset: node.delta.prevRunePosition(offset),
),
)
: node.selectable?.getWordBoundaryInPosition(this);
if (result != null) {
return moveLeft ? result.start : result.end;
}
}

return Position(path: path, offset: offset);
}
}

Position? moveVertical(
EditorState editorState, {
bool upwards = true,
}) {
final selection =
editorState.service.selectionService.currentSelection.value;
final rects = editorState.service.selectionService.selectionRects;
if (rects.isEmpty || selection == null) {
return null;
}

Offset offset;
if (selection.isBackward) {
final rect = rects.reduce(
(current, next) => current.bottom >= next.bottom ? current : next,
);
offset = upwards
? rect.topRight.translate(0, -rect.height)
: rect.bottomRight.translate(0, rect.height);
} else {
final rect = rects.reduce(
(current, next) => current.top <= next.top ? current : next,
);
offset = upwards
? rect.topLeft.translate(0, -rect.height)
: rect.bottomLeft.translate(0, rect.height);
}

return editorState.service.selectionService.getPositionInOffset(offset);
}
}
Loading

0 comments on commit 58ea6b0

Please sign in to comment.