Skip to content

Commit

Permalink
fix: delete node does not propagate non null selection (AppFlowy-IO#45)
Browse files Browse the repository at this point in the history
* fix: delete node does not propagate non null selection

* test: delete node does not propagate non null selection

* refactor: lucas's suggestion

* refactor: suggestions

* refactor: revert inSelection, is not able to differentiate between overlays and selection.

* test: updated tests

* refactor: code formatting

* refactor: code formatting

* chore: dart format

---------

Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io>
  • Loading branch information
2 people authored and Xazin committed Apr 13, 2023
1 parent a3c34bc commit b828c49
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/src/core/transform/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class Transaction {
/// Deletes the [Node] in the document.
void deleteNode(Node node) {
deleteNodesAtPath(node.path);
if (beforeSelection != null) {
final nodePath = node.path;
final selectionPath = beforeSelection!.start.path;
if (!(nodePath.equals(selectionPath))) {
afterSelection = beforeSelection;
}
}
}

/// Deletes the [Node]s in the document.
Expand Down
67 changes: 67 additions & 0 deletions test/core/transform/transaction_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,72 @@ void main() async {
expect(textNodes[3].toPlainText(), 'ABC4');
expect(textNodes[4].toPlainText(), 'ABC5 AppFlowy!');
});

testWidgets('test selection propagates if non-selected node is deleted',
(tester) async {
TestWidgetsFlutterBinding.ensureInitialized();

final editor = tester.editor
..insertTextNode('Welcome to AppFlowy!')
..insertTextNode('Testing selection on this');

await editor.startTesting();
await tester.pumpAndSettle();

expect(editor.documentLength, 2);

await editor.updateSelection(
Selection.single(
path: [0],
startOffset: 0,
endOffset: 20,
),
);
await tester.pumpAndSettle();

final transaction = editor.editorState.transaction;
transaction.deleteNode(editor.nodeAtPath([1])!);
editor.editorState.apply(transaction);
await tester.pumpAndSettle();

expect(editor.documentLength, 1);
expect(
editor.editorState.cursorSelection,
Selection.single(
path: [0],
startOffset: 0,
endOffset: 20,
),
);
});

testWidgets('test selection does not propagate if selected node is deleted',
(tester) async {
TestWidgetsFlutterBinding.ensureInitialized();

final editor = tester.editor
..insertTextNode('Welcome to AppFlowy!')
..insertTextNode('Testing selection on this');

await editor.startTesting();
await tester.pumpAndSettle();

expect(editor.documentLength, 2);

await editor.updateSelection(Selection.single(
path: [0],
startOffset: 0,
endOffset: 20,
));
await tester.pumpAndSettle();

final transaction = editor.editorState.transaction;
transaction.deleteNode(editor.nodeAtPath([0])!);
editor.editorState.apply(transaction);
await tester.pumpAndSettle();

expect(editor.documentLength, 1);
expect(editor.editorState.cursorSelection, null);
});
});
}

0 comments on commit b828c49

Please sign in to comment.