diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/operation/transaction_builder.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/operation/transaction_builder.dart index 79c2fe4b40..7dfca6bcf7 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/operation/transaction_builder.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/operation/transaction_builder.dart @@ -1,7 +1,6 @@ import 'dart:collection'; import 'dart:math'; -import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/src/document/attributes.dart'; import 'package:appflowy_editor/src/document/node.dart'; import 'package:appflowy_editor/src/document/path.dart'; @@ -115,17 +114,21 @@ class TransactionBuilder { /// Inserts content at a specified index. /// 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( TextNode node, int index, String content, { 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( node, () => Delta() @@ -224,38 +227,4 @@ class TransactionBuilder { 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); - } } diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/markdown_syntax_to_styled_text_handler.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/markdown_syntax_to_styled_text_handler.dart index 1f21cf0126..37a45805ab 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/markdown_syntax_to_styled_text_handler.dart +++ b/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/src/extensions/path_extensions.dart'; import 'package:flutter/material.dart'; // convert **abc** to bold abc. diff --git a/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/markdown_syntax_to_styled_text_handler_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/markdown_syntax_to_styled_text_handler_test.dart index c57e9fdd25..d60239ae49 100644 --- a/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/markdown_syntax_to_styled_text_handler_test.dart +++ b/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', (tester) async { const boldText = '**AppFlowy*'; - const normalText = ' application'; final editor = tester.editor..insertTextNode(''); await editor.startTesting(); await editor.updateSelection( @@ -107,10 +106,6 @@ void main() async { await editor.insertText(textNode, boldText[i], i); } 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 appFlowyBold = textNode.allSatisfyBoldInSelection( Selection.single( @@ -119,16 +114,8 @@ void main() async { endOffset: boldTextLength, ), ); - final applicationNormal = textNode.allSatisfyBoldInSelection( - Selection.single( - path: [0], - startOffset: boldTextLength, - endOffset: textNode.toRawString().length, - ), - ); expect(appFlowyBold, true); - expect(applicationNormal, false); - expect(textNode.toRawString(), 'AppFlowy application'); + expect(textNode.toRawString(), 'AppFlowy'); }); testWidgets('**** nothing changes', (tester) async { @@ -240,7 +227,6 @@ void main() async { testWidgets('__AppFlowy__ application to bold AppFlowy only', (tester) async { const boldText = '__AppFlowy_'; - const normalText = ' application'; final editor = tester.editor..insertTextNode(''); await editor.startTesting(); await editor.updateSelection( @@ -252,10 +238,6 @@ void main() async { await editor.insertText(textNode, boldText[i], i); } 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 appFlowyBold = textNode.allSatisfyBoldInSelection( Selection.single( @@ -264,16 +246,8 @@ void main() async { endOffset: boldTextLength, ), ); - final applicationNormal = textNode.allSatisfyBoldInSelection( - Selection.single( - path: [0], - startOffset: boldTextLength, - endOffset: textNode.toRawString().length, - ), - ); expect(appFlowyBold, true); - expect(applicationNormal, false); - expect(textNode.toRawString(), 'AppFlowy application'); + expect(textNode.toRawString(), 'AppFlowy'); }); testWidgets('____ nothing changes', (tester) async {