From 8602a360266f595e73f4fead97939004698ec30e Mon Sep 17 00:00:00 2001 From: Mihir <84044317+squidrye@users.noreply.github.com> Date: Tue, 11 Apr 2023 11:38:50 +0530 Subject: [PATCH] fix: delete node does not propagate non null selection (#45) * 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 --- lib/src/core/transform/transaction.dart | 7 +++ test/core/transform/transaction_test.dart | 67 +++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/lib/src/core/transform/transaction.dart b/lib/src/core/transform/transaction.dart index 6c369f566..97fe3a46e 100644 --- a/lib/src/core/transform/transaction.dart +++ b/lib/src/core/transform/transaction.dart @@ -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. diff --git a/test/core/transform/transaction_test.dart b/test/core/transform/transaction_test.dart index bfb9fa2b3..97e6c083d 100644 --- a/test/core/transform/transaction_test.dart +++ b/test/core/transform/transaction_test.dart @@ -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); + }); }); }