Browse Source

chore: temporarily remove the code that automatically formats when inserting text

Lucas.Xu 2 years ago
parent
commit
1841fb293e

+ 9 - 40
frontend/app_flowy/packages/appflowy_editor/lib/src/operation/transaction_builder.dart

@@ -1,7 +1,6 @@
 import 'dart:collection';
 import 'dart:collection';
 import 'dart:math';
 import 'dart:math';
 
 
-import 'package:appflowy_editor/appflowy_editor.dart';
 import 'package:appflowy_editor/src/document/attributes.dart';
 import 'package:appflowy_editor/src/document/attributes.dart';
 import 'package:appflowy_editor/src/document/node.dart';
 import 'package:appflowy_editor/src/document/node.dart';
 import 'package:appflowy_editor/src/document/path.dart';
 import 'package:appflowy_editor/src/document/path.dart';
@@ -115,17 +114,21 @@ class TransactionBuilder {
 
 
   /// Inserts content at a specified index.
   /// Inserts content at a specified index.
   /// Optionally, you may specify formatting attributes that are applied to the inserted string.
   /// Optionally, you may specify formatting attributes that are applied to the inserted string.
-  /// When no formatting attributes specified, the formating attributes before the insert position will be used if they don't have defaultFormatting flag set
-  /// When defaultFormatting flag is set before the insert position, it will be cleared.
-  /// When insert position is within a text having defaultFormatting flag set, the flag will be ignored and clear (formatting attributes of the text will be applied)
+  /// By default, the formatting attributes before the insert position will be used.
   insertText(
   insertText(
     TextNode node,
     TextNode node,
     int index,
     int index,
     String content, {
     String content, {
     Attributes? attributes,
     Attributes? attributes,
   }) {
   }) {
-    final newAttributes = attributes ?? _getAttributesAt(node, index);
-
+    var newAttributes = attributes;
+    if (index != 0 && attributes == null) {
+      newAttributes =
+          node.delta.slice(max(index - 1, 0), index).first.attributes;
+      if (newAttributes != null) {
+        newAttributes = Attributes.from(newAttributes);
+      }
+    }
     textEdit(
     textEdit(
       node,
       node,
       () => Delta()
       () => Delta()
@@ -224,38 +227,4 @@ class TransactionBuilder {
       afterSelection: afterSelection,
       afterSelection: afterSelection,
     );
     );
   }
   }
-
-  Attributes? _getAttributesAt(TextNode node, int index) {
-    if (index == 0) {
-      return null;
-    }
-
-    final previousAttributes =
-        node.delta.slice(index - 1, index).first.attributes;
-
-    final nextAttributes = node.delta.length > index
-        ? node.delta.slice(index, index + 1).first.attributes
-        : null;
-
-    if (previousAttributes == null) {
-      return null;
-    }
-
-    if (previousAttributes.containsKey(BuiltInAttributeKey.defaultFormating)) {
-      Attributes newAttributes = Map.from(previousAttributes)
-        ..removeWhere((key, _) => key == BuiltInAttributeKey.defaultFormating);
-
-      if (node.previous != null) {
-        updateNode(node.next!, newAttributes);
-        if (previousAttributes == nextAttributes) {
-          updateNode(node.next!, newAttributes);
-          return newAttributes;
-        }
-      }
-
-      return null;
-    }
-
-    return Attributes.from(previousAttributes);
-  }
 }
 }

+ 0 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/markdown_syntax_to_styled_text_handler.dart

@@ -1,5 +1,4 @@
 import 'package:appflowy_editor/appflowy_editor.dart';
 import 'package:appflowy_editor/appflowy_editor.dart';
-import 'package:appflowy_editor/src/extensions/path_extensions.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/material.dart';
 
 
 // convert **abc** to bold abc.
 // convert **abc** to bold abc.

+ 2 - 28
frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/markdown_syntax_to_styled_text_handler_test.dart

@@ -95,7 +95,6 @@ void main() async {
       testWidgets('**AppFlowy** application to bold AppFlowy only',
       testWidgets('**AppFlowy** application to bold AppFlowy only',
           (tester) async {
           (tester) async {
         const boldText = '**AppFlowy*';
         const boldText = '**AppFlowy*';
-        const normalText = ' application';
         final editor = tester.editor..insertTextNode('');
         final editor = tester.editor..insertTextNode('');
         await editor.startTesting();
         await editor.startTesting();
         await editor.updateSelection(
         await editor.updateSelection(
@@ -107,10 +106,6 @@ void main() async {
           await editor.insertText(textNode, boldText[i], i);
           await editor.insertText(textNode, boldText[i], i);
         }
         }
         await insertAsterisk(editor);
         await insertAsterisk(editor);
-        for (var i = 0; i < normalText.length; i++) {
-          await editor.insertText(
-              textNode, normalText[i], i + boldText.length - 3);
-        }
         final boldTextLength = boldText.replaceAll('*', '').length;
         final boldTextLength = boldText.replaceAll('*', '').length;
         final appFlowyBold = textNode.allSatisfyBoldInSelection(
         final appFlowyBold = textNode.allSatisfyBoldInSelection(
           Selection.single(
           Selection.single(
@@ -119,16 +114,8 @@ void main() async {
             endOffset: boldTextLength,
             endOffset: boldTextLength,
           ),
           ),
         );
         );
-        final applicationNormal = textNode.allSatisfyBoldInSelection(
-          Selection.single(
-            path: [0],
-            startOffset: boldTextLength,
-            endOffset: textNode.toRawString().length,
-          ),
-        );
         expect(appFlowyBold, true);
         expect(appFlowyBold, true);
-        expect(applicationNormal, false);
-        expect(textNode.toRawString(), 'AppFlowy application');
+        expect(textNode.toRawString(), 'AppFlowy');
       });
       });
 
 
       testWidgets('**** nothing changes', (tester) async {
       testWidgets('**** nothing changes', (tester) async {
@@ -240,7 +227,6 @@ void main() async {
       testWidgets('__AppFlowy__ application to bold AppFlowy only',
       testWidgets('__AppFlowy__ application to bold AppFlowy only',
           (tester) async {
           (tester) async {
         const boldText = '__AppFlowy_';
         const boldText = '__AppFlowy_';
-        const normalText = ' application';
         final editor = tester.editor..insertTextNode('');
         final editor = tester.editor..insertTextNode('');
         await editor.startTesting();
         await editor.startTesting();
         await editor.updateSelection(
         await editor.updateSelection(
@@ -252,10 +238,6 @@ void main() async {
           await editor.insertText(textNode, boldText[i], i);
           await editor.insertText(textNode, boldText[i], i);
         }
         }
         await insertUnderscore(editor);
         await insertUnderscore(editor);
-        for (var i = 0; i < normalText.length; i++) {
-          await editor.insertText(
-              textNode, normalText[i], i + boldText.length - 3);
-        }
         final boldTextLength = boldText.replaceAll('_', '').length;
         final boldTextLength = boldText.replaceAll('_', '').length;
         final appFlowyBold = textNode.allSatisfyBoldInSelection(
         final appFlowyBold = textNode.allSatisfyBoldInSelection(
           Selection.single(
           Selection.single(
@@ -264,16 +246,8 @@ void main() async {
             endOffset: boldTextLength,
             endOffset: boldTextLength,
           ),
           ),
         );
         );
-        final applicationNormal = textNode.allSatisfyBoldInSelection(
-          Selection.single(
-            path: [0],
-            startOffset: boldTextLength,
-            endOffset: textNode.toRawString().length,
-          ),
-        );
         expect(appFlowyBold, true);
         expect(appFlowyBold, true);
-        expect(applicationNormal, false);
-        expect(textNode.toRawString(), 'AppFlowy application');
+        expect(textNode.toRawString(), 'AppFlowy');
       });
       });
 
 
       testWidgets('____ nothing changes', (tester) async {
       testWidgets('____ nothing changes', (tester) async {