Browse Source

fix: Should not add a new line below after pressing enter at the front of the first line of text.

Lucas.Xu 2 years ago
parent
commit
c07af9007c

+ 11 - 4
frontend/app_flowy/packages/appflowy_editor/lib/src/document/state_tree.dart

@@ -62,10 +62,17 @@ class StateTree {
       }
       }
       return false;
       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;
     return true;
   }
   }

+ 21 - 0
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 {
         (tester) async {
       _testMultipleSelection(tester, false);
       _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);
+    });
   });
   });
 }
 }