Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improves cursor left word delete #88

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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