Просмотр исходного кода

fix: could not remove text style when pressing enter in empty text node

Lucas.Xu 2 лет назад
Родитель
Сommit
1391d202a9

+ 8 - 9
frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart

@@ -137,17 +137,16 @@ class TransactionBuilder {
 
   replaceText(TextNode node, int index, int length, String content,
       [Attributes? attributes]) {
+    var newAttributes = attributes;
+    if (attributes == null) {
+      final ops = node.delta.slice(index, index + length).operations;
+      if (ops.isNotEmpty) {
+        newAttributes = ops.first.attributes;
+      }
+    }
     textEdit(
       node,
-      () => Delta().retain(index).delete(length).insert(
-            content,
-            attributes ??
-                node.delta
-                    .slice(index, index + length)
-                    .operations
-                    .first
-                    .attributes,
-          ),
+      () => Delta().retain(index).delete(length).insert(content, newAttributes),
     );
     afterSelection = Selection.collapsed(
       Position(

+ 1 - 0
frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/rich_text_style.dart

@@ -51,6 +51,7 @@ class StyleKey {
   ];
 
   static List<String> globalStyleKeys = [
+    StyleKey.subtype,
     StyleKey.heading,
     StyleKey.checkbox,
     StyleKey.bulletedList,

+ 25 - 10
frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart

@@ -67,16 +67,31 @@ FlowyKeyEventHandler enterWithoutShiftInTextNodesHandler =
   // If selection is collapsed and position.start.offset == 0,
   //  insert a empty text node before.
   if (selection.isCollapsed && selection.start.offset == 0) {
-    final afterSelection = Selection.collapsed(
-      Position(path: textNode.path.next, offset: 0),
-    );
-    TransactionBuilder(editorState)
-      ..insertNode(
-        textNode.path,
-        TextNode.empty(),
-      )
-      ..afterSelection = afterSelection
-      ..commit();
+    if (textNode.toRawString().isEmpty) {
+      final afterSelection = Selection.collapsed(
+        Position(path: textNode.path, offset: 0),
+      );
+      TransactionBuilder(editorState)
+        ..updateNode(
+            textNode,
+            Attributes.fromIterable(
+              StyleKey.globalStyleKeys,
+              value: (_) => null,
+            ))
+        ..afterSelection = afterSelection
+        ..commit();
+    } else {
+      final afterSelection = Selection.collapsed(
+        Position(path: textNode.path.next, offset: 0),
+      );
+      TransactionBuilder(editorState)
+        ..insertNode(
+          textNode.path,
+          TextNode.empty(),
+        )
+        ..afterSelection = afterSelection
+        ..commit();
+    }
     return KeyEventResult.handled;
   }