diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/document/state_tree.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/document/state_tree.dart index a17b2fbf98..a4a9869df5 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/document/state_tree.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/document/state_tree.dart @@ -62,10 +62,17 @@ class StateTree { } return false; } - for (var i = 0; i < nodes.length; i++) { - final node = nodes[i]; - insertedNode!.insertAfter(node); - insertedNode = node; + if (path.last <= 0) { + for (var i = 0; i < nodes.length; i++) { + final node = nodes[i]; + insertedNode.insertBefore(node); + } + } else { + for (var i = 0; i < nodes.length; i++) { + final node = nodes[i]; + insertedNode!.insertAfter(node); + insertedNode = node; + } } return true; } diff --git a/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler_test.dart index ee21dfa455..5bfe1ada67 100644 --- a/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler_test.dart +++ b/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler_test.dart @@ -116,6 +116,27 @@ void main() async { (tester) async { _testMultipleSelection(tester, false); }); + + testWidgets('Presses enter key in the first line', (tester) async { + // Before + // + // Welcome to Appflowy 😁 + // + // After + // + // [Empty Line] + // Welcome to Appflowy 😁 + // + const text = 'Welcome to Appflowy 😁'; + final editor = tester.editor..insertTextNode(text); + await editor.startTesting(); + await editor.updateSelection( + Selection.single(path: [0], startOffset: 0), + ); + await editor.pressLogicKey(LogicalKeyboardKey.enter); + expect(editor.documentLength, 2); + expect((editor.nodeAtPath([1]) as TextNode).toRawString(), text); + }); }); }