fix: pasting keeps current node type (#689)

This commit is contained in:
Juliano Penna 2024-01-25 23:45:41 -03:00 • committed by GitHub
commit 7d8147c78d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 56 additions and 3 deletions

View file

@ -3,7 +3,7 @@
The directory structure of test files mirrors that of the code files, making it easy for us to map a file with the corresponding test and check if the test is updated. The directory structure of test files mirrors that of the code files, making it easy for us to map a file with the corresponding test and check if the test is updated.
For an overview of testing best practices in Flutter applications, please refer to Flutter's [introduction to widget testing](https://docs.flutter.dev/cookbook/testing/widget/introduction) as well as their [introduction to unit testing](https://docs.flutter.dev/cookbook/testing/unit/introduction). For an overview of testing best practices in Flutter applications, please refer to Flutter's [introduction to widget testing](https://docs.flutter.dev/cookbook/testing/widget/introduction) as well as their [introduction to unit testing](https://docs.flutter.dev/cookbook/testing/unit/introduction).
There you will learn how to do such things as such as simulate a click as well as leverage the `test` and `expect` functions. There you will learn how to do such things as simulate a click as well as leverage the `test` and `expect` functions.
## Testing Basic Editor Functions ## Testing Basic Editor Functions

View file

@ -13,8 +13,8 @@ extension EditorCopyPaste on EditorState {
} }
final transaction = this.transaction; final transaction = this.transaction;
final insertedDelta = insertedNode.delta; final insertedDelta = insertedNode.delta;
// if the node is empty, replace it with the inserted node. // if the node is empty paragraph (default), replace it with the inserted node.
if (delta.isEmpty) { if (delta.isEmpty && node.type == ParagraphBlockKeys.type) {
transaction.insertNode( transaction.insertNode(
selection.end.path.next, selection.end.path.next,
insertedNode, insertedNode,

View file

@ -75,6 +75,59 @@ void main() async {
); );
}); });
}); });
group('copy_paste_extension.dart', () {
testWidgets('Keep current node if current node is empty but not paragraph',
(tester) async {
final initialNode = quoteNode();
final pasteNode = paragraphNode(text: 'hello');
final nodeType = await _testPasteNode(tester, initialNode, pasteNode);
expect(nodeType, initialNode.type);
});
testWidgets('Replace node with pasted node if current is empty paragraph',
(tester) async {
final initialNode = paragraphNode();
final pasteNode = headingNode(level: 2, delta: Delta()..insert('hello'));
final nodeType = await _testPasteNode(tester, initialNode, pasteNode);
expect(nodeType, pasteNode.type);
});
});
}
Future<String> _testPasteNode(
WidgetTester tester,
Node initialNode,
Node pasteNode,
) async {
final editor = tester.editor..addNode(initialNode);
await editor.startTesting();
await editor.updateSelection(
Selection.collapsed(Position(path: [0])),
);
AppFlowyClipboard.mockSetData(
AppFlowyClipboardData(
text: pasteNode.delta!.toPlainText(),
html: documentToHTML(Document.blank()..insert([0], [pasteNode])),
),
);
pasteCommand.execute(editor.editorState);
await tester.pumpAndSettle();
final node = editor.nodeAtPath([0])!;
final delta = node.delta!;
expect(delta.toPlainText(), pasteNode.delta!.toPlainText());
AppFlowyClipboard.mockSetData(null);
await editor.dispose();
return node.type;
} }
Future<void> _testHandleCopyMultiplePaste( Future<void> _testHandleCopyMultiplePaste(