Ver código fonte

feat: make following lines incremental

Vincent Chan 2 anos atrás
pai
commit
69f04d0958

+ 4 - 7
frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart

@@ -4,6 +4,7 @@ import 'package:flutter/services.dart';
 
 import 'package:appflowy_editor/src/extensions/path_extensions.dart';
 import 'package:appflowy_editor/src/render/rich_text/rich_text_style.dart';
+import './number_list_helper.dart';
 
 /// Handle some cases where enter is pressed and shift is not pressed.
 ///
@@ -101,8 +102,9 @@ ShortcutEventHandler enterWithoutShiftInTextNodesHandler =
   //  split the node into two nodes with style
   Attributes attributes = _attributesFromPreviousLine(textNode);
 
+  final nextPath = textNode.path.next;
   final afterSelection = Selection.collapsed(
-    Position(path: textNode.path.next, offset: 0),
+    Position(path: nextPath, offset: 0),
   );
 
   TransactionBuilder(editorState)
@@ -124,7 +126,7 @@ ShortcutEventHandler enterWithoutShiftInTextNodesHandler =
   // If the new type of a text node is number list,
   // the numbers of the following nodes should be incremental.
   if (textNode.subtype == StyleKey.numberList) {
-    _makeFollowingNodeIncremental(editorState, textNode);
+    makeFollowingNodesIncremental(editorState, nextPath, afterSelection);
   }
 
   return KeyEventResult.handled;
@@ -156,8 +158,3 @@ Attributes _nextNumberAttributesFromPreviousLine(
   copy[StyleKey.number] = prevNum == null ? 1 : prevNum + 1;
   return copy;
 }
-
-void _makeFollowingNodeIncremental(EditorState editorState, TextNode textNode) {
-  debugPrint("following nodes");
-  TransactionBuilder(editorState).commit();
-}

+ 37 - 0
frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/number_list_helper.dart

@@ -0,0 +1,37 @@
+import 'package:appflowy_editor/src/document/selection.dart';
+import 'package:appflowy_editor/src/editor_state.dart';
+import 'package:appflowy_editor/src/render/rich_text/rich_text_style.dart';
+import 'package:appflowy_editor/src/operation/transaction_builder.dart';
+import 'package:appflowy_editor/src/document/attributes.dart';
+
+void makeFollowingNodesIncremental(
+    EditorState editorState, List<int> insertPath, Selection afterSelection) {
+  final insertNode = editorState.document.nodeAtPath(insertPath);
+  if (insertNode == null) {
+    return;
+  }
+  final int beginNum = insertNode.attributes[StyleKey.number] as int;
+
+  int numPtr = beginNum + 1;
+  var ptr = insertNode.next;
+
+  final builder = TransactionBuilder(editorState);
+
+  while (ptr != null) {
+    if (ptr.subtype != StyleKey.numberList) {
+      break;
+    }
+    final currentNum = ptr.attributes[StyleKey.number] as int;
+    if (currentNum != numPtr) {
+      Attributes updateAttributes = {};
+      updateAttributes[StyleKey.number] = numPtr;
+      builder.updateNode(ptr, updateAttributes);
+    }
+
+    ptr = ptr.next;
+    numPtr++;
+  }
+
+  builder.afterSelection = afterSelection;
+  builder.commit();
+}

+ 12 - 6
frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/whitespace_handler.dart

@@ -8,6 +8,7 @@ import 'package:appflowy_editor/src/document/selection.dart';
 import 'package:appflowy_editor/src/editor_state.dart';
 import 'package:appflowy_editor/src/operation/transaction_builder.dart';
 import 'package:appflowy_editor/src/render/rich_text/rich_text_style.dart';
+import './number_list_helper.dart';
 
 @visibleForTesting
 List<String> get checkboxListSymbols => _checkboxListSymbols;
@@ -76,17 +77,22 @@ KeyEventResult _toNumberList(EditorState editorState, TextNode textNode,
     return KeyEventResult.ignored;
   }
 
+  final afterSelection = Selection.collapsed(Position(
+    path: textNode.path,
+    offset: 0,
+  ));
+
+  final insertPath = textNode.path;
+
   TransactionBuilder(editorState)
     ..deleteText(textNode, 0, matchText.length)
     ..updateNode(textNode,
         {StyleKey.subtype: StyleKey.numberList, StyleKey.number: numValue})
-    ..afterSelection = Selection.collapsed(
-      Position(
-        path: textNode.path,
-        offset: 0,
-      ),
-    )
+    ..afterSelection = afterSelection
     ..commit();
+
+  makeFollowingNodesIncremental(editorState, insertPath, afterSelection);
+
   return KeyEventResult.handled;
 }