diff --git a/lib/src/editor/editor_component/service/shortcuts/character/character_shortcut_events.dart b/lib/src/editor/editor_component/service/shortcuts/character/character_shortcut_events.dart index ea2978552..f4cd2dac6 100644 --- a/lib/src/editor/editor_component/service/shortcuts/character/character_shortcut_events.dart +++ b/lib/src/editor/editor_component/service/shortcuts/character/character_shortcut_events.dart @@ -1,8 +1,8 @@ -export 'double/format_bold.dart'; -export 'double/format_double_characters.dart'; -export 'double/format_double_hyphen.dart'; -export 'double/format_arrow_character.dart'; -export 'double/format_strikethrough.dart'; +export 'format_double_character/format_arrow_character.dart'; +export 'format_double_character/format_bold.dart'; +export 'format_double_character/format_double_characters.dart'; +export 'format_double_character/format_double_hyphen.dart'; +export 'format_double_character/format_strikethrough.dart'; export 'format_single_character/format_code.dart'; export 'format_single_character/format_italic.dart'; export 'format_single_character/format_single_character.dart'; diff --git a/lib/src/editor/editor_component/service/shortcuts/character/double/format_arrow_character.dart b/lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_arrow_character.dart similarity index 100% rename from lib/src/editor/editor_component/service/shortcuts/character/double/format_arrow_character.dart rename to lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_arrow_character.dart diff --git a/lib/src/editor/editor_component/service/shortcuts/character/double/format_bold.dart b/lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_bold.dart similarity index 93% rename from lib/src/editor/editor_component/service/shortcuts/character/double/format_bold.dart rename to lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_bold.dart index b9732824b..130164faa 100644 --- a/lib/src/editor/editor_component/service/shortcuts/character/double/format_bold.dart +++ b/lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_bold.dart @@ -1,5 +1,5 @@ +import 'package:appflowy_editor/src/editor/editor_component/service/shortcuts/character/format_double_character/format_double_characters.dart'; import 'package:appflowy_editor/src/editor/editor_component/service/shortcuts/character_shortcut_event.dart'; -import 'package:appflowy_editor/src/editor/editor_component/service/shortcuts/character/double/format_double_characters.dart'; const _asterisk = '*'; const _underscore = '_'; diff --git a/lib/src/editor/editor_component/service/shortcuts/character/double/format_double_characters.dart b/lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_double_characters.dart similarity index 100% rename from lib/src/editor/editor_component/service/shortcuts/character/double/format_double_characters.dart rename to lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_double_characters.dart diff --git a/lib/src/editor/editor_component/service/shortcuts/character/double/format_double_hyphen.dart b/lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_double_hyphen.dart similarity index 100% rename from lib/src/editor/editor_component/service/shortcuts/character/double/format_double_hyphen.dart rename to lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_double_hyphen.dart diff --git a/lib/src/editor/editor_component/service/shortcuts/character/double/format_strikethrough.dart b/lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_strikethrough.dart similarity index 89% rename from lib/src/editor/editor_component/service/shortcuts/character/double/format_strikethrough.dart rename to lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_strikethrough.dart index 432c9cea8..62c39002b 100644 --- a/lib/src/editor/editor_component/service/shortcuts/character/double/format_strikethrough.dart +++ b/lib/src/editor/editor_component/service/shortcuts/character/format_double_character/format_strikethrough.dart @@ -1,5 +1,5 @@ +import 'package:appflowy_editor/src/editor/editor_component/service/shortcuts/character/format_double_character/format_double_characters.dart'; import 'package:appflowy_editor/src/editor/editor_component/service/shortcuts/character_shortcut_event.dart'; -import 'package:appflowy_editor/src/editor/editor_component/service/shortcuts/character/double/format_double_characters.dart'; const _tile = '~'; diff --git a/lib/src/editor/editor_component/service/shortcuts/character/format_single_character/format_single_character.dart b/lib/src/editor/editor_component/service/shortcuts/character/format_single_character/format_single_character.dart index e6d156de9..cdf484d35 100644 --- a/lib/src/editor/editor_component/service/shortcuts/character/format_single_character/format_single_character.dart +++ b/lib/src/editor/editor_component/service/shortcuts/character/format_single_character/format_single_character.dart @@ -1,4 +1,5 @@ import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:collection/collection.dart'; enum FormatStyleByWrappingWithSingleChar { code, @@ -51,7 +52,35 @@ class CheckSingleFormatFormatResult { return (false, null); } - final plainText = delta.toPlainText().substring(0, selection.end.offset); + // find the last inline code attributes + final lastInlineCode = delta.indexed.lastWhereOrNull((element) { + final (_, op) = element; + if (op.attributes?[AppFlowyRichTextKeys.code] == true) { + return true; + } + return false; + }); + int startIndex = 0; + if (lastInlineCode != null && + formatStyle != FormatStyleByWrappingWithSingleChar.code) { + final (lastInlineCodeIndex, _) = lastInlineCode; + startIndex = delta.indexed.fold(0, (sum, element) { + final (index, op) = element; + if (index <= lastInlineCodeIndex) { + return sum + op.length; + } + return sum; + }); + } + + if (startIndex >= selection.end.offset) { + return (false, null); + } + + final plainText = delta.toPlainText().substring( + startIndex, + selection.end.offset, + ); final lastCharIndex = plainText.lastIndexOf(character); final textAfterLastChar = plainText.substring(lastCharIndex + 1); bool textAfterLastCharIsEmpty = textAfterLastChar.trim().isEmpty; @@ -80,7 +109,10 @@ class CheckSingleFormatFormatResult { } // 5. If the text inbetween is empty (continuous) - if (plainText.substring(lastCharIndex + 1, selection.end.offset).isEmpty) { + final rawPlainText = delta.toPlainText(); + if (rawPlainText + .substring(startIndex + lastCharIndex + 1, selection.end.offset) + .isEmpty) { return (false, null); } @@ -88,7 +120,7 @@ class CheckSingleFormatFormatResult { true, CheckSingleFormatFormatResult( node: node, - lastCharIndex: lastCharIndex, + lastCharIndex: startIndex + lastCharIndex, path: path, delta: delta, ) diff --git a/test/new/service/shortcuts/character_shortcut_events/double_characters_shortcut_events/format_bold_test.dart b/test/new/service/shortcuts/character_shortcut_events/double_characters_shortcut_events/format_bold_test.dart index 312a08609..5015f962c 100644 --- a/test/new/service/shortcuts/character_shortcut_events/double_characters_shortcut_events/format_bold_test.dart +++ b/test/new/service/shortcuts/character_shortcut_events/double_characters_shortcut_events/format_bold_test.dart @@ -1,5 +1,6 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:flutter_test/flutter_test.dart'; + import '../../../../util/util.dart'; void main() async { diff --git a/test/new/service/shortcuts/character_shortcut_events/format_by_wrapping_with_single_char/format_code_test.dart b/test/new/service/shortcuts/character_shortcut_events/format_by_wrapping_with_single_char/format_code_test.dart index 6e2daf8cc..ab9262b8d 100644 --- a/test/new/service/shortcuts/character_shortcut_events/format_by_wrapping_with_single_char/format_code_test.dart +++ b/test/new/service/shortcuts/character_shortcut_events/format_by_wrapping_with_single_char/format_code_test.dart @@ -1,5 +1,6 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:flutter_test/flutter_test.dart'; + import '../../../../util/util.dart'; void main() async { diff --git a/test/new/service/shortcuts/character_shortcut_events/format_by_wrapping_with_single_char/format_italic_test.dart b/test/new/service/shortcuts/character_shortcut_events/format_by_wrapping_with_single_char/format_italic_test.dart index 2ba3142b6..86a1ab6e2 100644 --- a/test/new/service/shortcuts/character_shortcut_events/format_by_wrapping_with_single_char/format_italic_test.dart +++ b/test/new/service/shortcuts/character_shortcut_events/format_by_wrapping_with_single_char/format_italic_test.dart @@ -1,5 +1,6 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:flutter_test/flutter_test.dart'; + import '../../../../util/util.dart'; void main() async { @@ -210,5 +211,38 @@ void main() async { false, ); }); + + // skip the italic when the text is wrapped with code + // Before + // `App_Flowy|` + // After + // `App_Flowy_` + test('skip the italic when the text is wrapped with code', () async { + const text = 'App_Flowy'; + final document = Document.blank().addParagraphs( + 1, + builder: (index) => Delta() + ..insert( + text, + attributes: { + AppFlowyRichTextKeys.code: true, + }, + ), + ); + final editorState = EditorState(document: document); + final selection = Selection.collapsed( + Position(path: [0], offset: text.length), + ); + editorState.selection = selection; + + final result = await formatUnderscoreToItalic.execute(editorState); + expect(result, false); + final after = editorState.getNodeAtPath([0])!; + expect(after.delta!.toPlainText(), text); + final isItalic = after.delta!.any( + (element) => element.attributes?[AppFlowyRichTextKeys.italic] == true, + ); + expect(isItalic, false); + }); }); }