Skip to content

Commit

Permalink
fix: delete the divider on mobile will raise an error
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Sep 26, 2023
1 parent b422187 commit a68efbd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ Future<void> onDelete(

// IME
if (selection.isSingle) {
if (deletion.composing.isValid || !deletion.deletedRange.isCollapsed) {
final node = editorState.getNodeAtPath(selection.start.path);
if (node?.delta != null &&
(deletion.composing.isValid || !deletion.deletedRange.isCollapsed)) {
final node = editorState.getNodesInSelection(selection).first;
final start = deletion.deletedRange.start;
final length = deletion.deletedRange.end - start;
// final firstNodeContainsDelta = editorState.document.root.children
// .firstWhereOrNull((element) => element.delta != null);
// if (firstNodeContainsDelta != null &&
// node.path.equals(firstNodeContainsDelta.path) && start == 0) {

// }
final transaction = editorState.transaction;

transaction.deleteText(node, start, length);
await editorState.apply(transaction);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,29 @@ CommandShortcutEventHandler _backspaceInCollapsedSelection = (editorState) {

final position = selection.start;
final node = editorState.getNodeAtPath(position.path);
if (node == null || node.delta == null) {
if (node == null) {
return KeyEventResult.ignored;
}

final transaction = editorState.transaction;

// delete the entire node if the delta is empty
if (node.delta == null) {
transaction.deleteNode(node);
transaction.afterSelection = Selection.collapsed(
Position(
path: position.path,

Check warning on line 54 in lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/backspace_command.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/backspace_command.dart#L51-L54

Added lines #L51 - L54 were not covered by tests
offset: 0,
),
);
editorState.apply(transaction);

Check warning on line 58 in lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/backspace_command.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/backspace_command.dart#L58

Added line #L58 was not covered by tests
return KeyEventResult.handled;
}

// Why do we use prevRunPosition instead of the position start offset?
// Because some character's length > 1, for example, emoji.
final index = node.delta!.prevRunePosition(position.offset);
final transaction = editorState.transaction;

if (index < 0) {
// move this node to it's parent in below case.
// the node's next is null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ final dividerMobileToolbarItem = MobileToolbarItem.action(
return;
}
final insertedPath = delta.isEmpty ? path : path.next;
final transaction = editorState.transaction
..insertNode(insertedPath, dividerNode())
..insertNode(insertedPath, paragraphNode())
..afterSelection = Selection.collapsed(Position(path: insertedPath.next));
final transaction = editorState.transaction;
transaction.insertNode(insertedPath, dividerNode());
// only insert a new paragraph node when the next node is not a paragraph node
// and its delta is not empty.
final next = node.next;
if (next == null ||
next.type != ParagraphBlockKeys.type ||
next.delta?.isNotEmpty == true) {
transaction.insertNode(insertedPath, paragraphNode());
}
transaction.afterSelection =
Selection.collapsed(Position(path: insertedPath.next));
editorState.apply(transaction);
}),
);

0 comments on commit a68efbd

Please sign in to comment.