Prechádzať zdrojové kódy

fix: #1997 Exception trhown after inserting '/' slash in middle of text (#1998)

Lucas.Xu 2 rokov pred
rodič
commit
6b14983f2b

+ 6 - 6
frontend/appflowy_flutter/packages/appflowy_editor/lib/src/core/transform/transaction.dart

@@ -264,11 +264,11 @@ extension TextTransaction on Transaction {
     if (index != 0 && attributes == null) {
       newAttributes =
           textNode.delta.slice(max(index - 1, 0), index).first.attributes;
-      if (newAttributes != null) {
-        newAttributes = {...newAttributes}; // make a copy
-      } else {
-        newAttributes =
-            textNode.delta.slice(index, index + length).first.attributes;
+      if (newAttributes == null) {
+        final slicedDelta = textNode.delta.slice(index, index + length);
+        if (slicedDelta.isNotEmpty) {
+          newAttributes = slicedDelta.first.attributes;
+        }
       }
     }
     updateText(
@@ -276,7 +276,7 @@ extension TextTransaction on Transaction {
       Delta()
         ..retain(index)
         ..delete(length)
-        ..insert(text, attributes: newAttributes),
+        ..insert(text, attributes: {...newAttributes ?? {}}),
     );
     afterSelection = Selection.collapsed(
       Position(

+ 35 - 1
frontend/appflowy_flutter/packages/appflowy_editor/test/service/internal_key_event_handlers/slash_handler_test.dart

@@ -10,7 +10,8 @@ void main() async {
   });
 
   group('slash_handler.dart', () {
-    testWidgets('Presses / to trigger selection menu', (tester) async {
+    testWidgets('Presses / to trigger selection menu in 0 index',
+        (tester) async {
       const text = 'Welcome to Appflowy 😁';
       const lines = 3;
       final editor = tester.editor;
@@ -41,5 +42,38 @@ void main() async {
         findsNothing,
       );
     });
+
+    testWidgets('Presses / to trigger selection menu in not 0 index',
+        (tester) async {
+      const text = 'Welcome to Appflowy 😁';
+      const lines = 3;
+      final editor = tester.editor;
+      for (var i = 0; i < lines; i++) {
+        editor.insertTextNode(text);
+      }
+      await editor.startTesting();
+      await editor.updateSelection(Selection.single(path: [1], startOffset: 5));
+      await editor.pressLogicKey(LogicalKeyboardKey.slash);
+
+      await tester.pumpAndSettle(const Duration(milliseconds: 1000));
+
+      expect(
+        find.byType(SelectionMenuWidget, skipOffstage: false),
+        findsOneWidget,
+      );
+
+      for (final item in defaultSelectionMenuItems) {
+        expect(find.text(item.name), findsOneWidget);
+      }
+
+      await editor.updateSelection(Selection.single(path: [1], startOffset: 0));
+
+      await tester.pumpAndSettle(const Duration(milliseconds: 200));
+
+      expect(
+        find.byType(SelectionMenuItemWidget, skipOffstage: false),
+        findsNothing,
+      );
+    });
   });
 }