Browse Source

fix: cancel number list style after enter in empty line

Vincent Chan 2 năm trước cách đây
mục cha
commit
06bd6064ac

+ 35 - 10
frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart

@@ -76,9 +76,7 @@ ShortcutEventHandler enterWithoutShiftInTextNodesHandler =
 
   // If selection is collapsed and position.start.offset == 0,
   //  insert a empty text node before.
-  if (selection.isCollapsed &&
-      selection.start.offset == 0 &&
-      textNode.subtype != StyleKey.numberList) {
+  if (selection.isCollapsed && selection.start.offset == 0) {
     if (textNode.toRawString().isEmpty && textNode.subtype != null) {
       final afterSelection = Selection.collapsed(
         Position(path: textNode.path, offset: 0),
@@ -92,17 +90,44 @@ ShortcutEventHandler enterWithoutShiftInTextNodesHandler =
             ))
         ..afterSelection = afterSelection
         ..commit();
+
+      final nextNode = textNode.next;
+      if (nextNode is TextNode && nextNode.subtype == StyleKey.numberList) {
+        makeFollowingNodesIncremental(
+            editorState, textNode.path, afterSelection,
+            beginNum: 0);
+      }
     } else {
+      final subtype = textNode.subtype;
       final afterSelection = Selection.collapsed(
         Position(path: textNode.path.next, offset: 0),
       );
-      TransactionBuilder(editorState)
-        ..insertNode(
-          textNode.path,
-          TextNode.empty(),
-        )
-        ..afterSelection = afterSelection
-        ..commit();
+
+      if (subtype == StyleKey.numberList) {
+        final prevNumber = textNode.attributes[StyleKey.number] as int;
+        final newNode = TextNode.empty();
+        newNode.attributes[StyleKey.subtype] = StyleKey.numberList;
+        newNode.attributes[StyleKey.number] = prevNumber;
+        final insertPath = textNode.path;
+        TransactionBuilder(editorState)
+          ..insertNode(
+            insertPath,
+            newNode,
+          )
+          ..afterSelection = afterSelection
+          ..commit();
+
+        makeFollowingNodesIncremental(editorState, insertPath, afterSelection,
+            beginNum: prevNumber);
+      } else {
+        TransactionBuilder(editorState)
+          ..insertNode(
+            textNode.path,
+            TextNode.empty(),
+          )
+          ..afterSelection = afterSelection
+          ..commit();
+      }
     }
     return KeyEventResult.handled;
   }

+ 2 - 9
frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler_test.dart

@@ -176,15 +176,8 @@ Future<void> _testStyleNeedToBeCopy(WidgetTester tester, String style) async {
   await editor.pressLogicKey(
     LogicalKeyboardKey.enter,
   );
-  if (style == StyleKey.numberList) {
-    expect(
-        editor.documentSelection, Selection.single(path: [5], startOffset: 0));
-    expect(editor.nodeAtPath([4])?.subtype, StyleKey.numberList);
-  } else {
-    expect(
-        editor.documentSelection, Selection.single(path: [4], startOffset: 0));
-    expect(editor.nodeAtPath([4])?.subtype, null);
-  }
+  expect(editor.documentSelection, Selection.single(path: [4], startOffset: 0));
+  expect(editor.nodeAtPath([4])?.subtype, null);
 }
 
 Future<void> _testMultipleSelection(