Skip to content

Commit

Permalink
fix: underscore works should not be interpreted in inline-code (#984)
Browse files Browse the repository at this point in the history
* fix: underscore works should not be interpreted in inline-code

* test: underscore works should not be interpreted in inline-code
  • Loading branch information
LucasXu0 authored Dec 9, 2024
1 parent 4034ebf commit 157ded3
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = '_';
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = '~';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:collection/collection.dart';

enum FormatStyleByWrappingWithSingleChar {
code,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -80,15 +109,18 @@ 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);
}

return (
true,
CheckSingleFormatFormatResult(
node: node,
lastCharIndex: lastCharIndex,
lastCharIndex: startIndex + lastCharIndex,
path: path,
delta: delta,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
});
});
}

0 comments on commit 157ded3

Please sign in to comment.