Skip to content

Commit

Permalink
feat: reset to the default inline code format logic (#988)
Browse files Browse the repository at this point in the history
* chore: reset to default inline code format logic

* fix: slice_attributes_test
  • Loading branch information
LucasXu0 authored Dec 11, 2024
1 parent e6971c2 commit 954ffa5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 78 deletions.
63 changes: 0 additions & 63 deletions example/lib/pages/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ class _EditorState extends State<Editor> {
EditorState? editorState;
WordCountService? wordCountService;

@override
void initState() {
super.initState();

appflowyEditorSliceAttributes = sliceAttributes;
}

@override
void didUpdateWidget(covariant Editor oldWidget) {
if (oldWidget.jsonString != widget.jsonString) {
Expand Down Expand Up @@ -163,60 +156,4 @@ class _EditorState extends State<Editor> {
],
);
}

Attributes? sliceAttributes(Delta delta, int index) {
if (index < 0) {
return null;
}

// if the index == 0, slice the attributes from the next position.
if (index == 0 && delta.isNotEmpty) {
final attributes = delta.slice(index, index + 1).firstOrNull?.attributes;
if (attributes == null) {
return null;
}

if (!isSupportSliced(attributes)) {
return null;
}

return attributes;
}

// if the index is not 0, slice the attributes from the previous position.
final prevAttributes =
delta.slice(index - 1, index).firstOrNull?.attributes;
if (prevAttributes == null) {
return null;
}
// if the prevAttributes doesn't include the code, return it.
// Otherwise, check if the nextAttributes includes the code.
if (!prevAttributes.keys.any(
(element) => element == AppFlowyRichTextKeys.code,
)) {
return prevAttributes;
}

// check if the nextAttributes includes the code.
final nextAttributes =
delta.slice(index, index + 1).firstOrNull?.attributes;
if (nextAttributes == null) {
return prevAttributes..remove(AppFlowyRichTextKeys.code);
}

// if the nextAttributes doesn't include the code, exclude the code format.
if (!nextAttributes.keys.any(
(element) => element == AppFlowyRichTextKeys.code,
)) {
return prevAttributes..remove(AppFlowyRichTextKeys.code);
}

return prevAttributes;
}

bool isSupportSliced(Attributes attributes) {
return attributes.keys.every(
(element) => AppFlowyRichTextKeys.supportSliced.contains(element),
);
}
}
47 changes: 34 additions & 13 deletions lib/src/core/document/text_delta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,50 @@ AppFlowyEditorSliceAttributes? defaultAppFlowyEditorSliceAttributes = (
return null;
}

Attributes? attributes;

// if the index == 0, slice the attributes from the next position.
if (index == 0 && delta.isNotEmpty) {
attributes = delta.slice(index, index + 1).firstOrNull?.attributes;
} else {
attributes = delta.slice(index - 1, index).firstOrNull?.attributes;
final attributes = delta.slice(index, index + 1).firstOrNull?.attributes;
if (attributes == null) {
return null;
}

// if the attributes is not supported, return null.
if (attributes.keys.any(
(element) => !AppFlowyRichTextKeys.supportSliced.contains(element),
)) {
return null;
}

return attributes;
}

if (attributes == null) {
// if the index is not 0, slice the attributes from the previous position.
final prevAttributes = delta.slice(index - 1, index).firstOrNull?.attributes;
if (prevAttributes == null) {
return null;
}
// if the prevAttributes doesn't include the code, return it.
// Otherwise, check if the nextAttributes includes the code.
if (!prevAttributes.keys.any(
(element) => element == AppFlowyRichTextKeys.code,
)) {
return prevAttributes;
}

// check if the nextAttributes includes the code.
final nextAttributes = delta.slice(index, index + 1).firstOrNull?.attributes;
if (nextAttributes == null) {
return prevAttributes..remove(AppFlowyRichTextKeys.code);
}

if (!attributes.keys.every(
(element) => AppFlowyRichTextKeys.supportSliced.contains(element),
// if the nextAttributes doesn't include the code, exclude the code format.
if (!nextAttributes.keys.any(
(element) => element == AppFlowyRichTextKeys.code,
)) {
AppFlowyEditorLog.editor.info(
'The attributes: $attributes is not supported in sliceAttributes.',
);
return null;
return prevAttributes..remove(AppFlowyRichTextKeys.code);
}

return attributes;
return prevAttributes;
};

/// Default slice attributes function.
Expand Down
5 changes: 3 additions & 2 deletions test/customer/slice_attributes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void main() async {
]);
});

testWidgets('keep attributes if pressing backspace', (tester) async {
testWidgets('don\'t keep attributes if pressing backspace', (tester) async {
final editor = tester.editor..addEmptyParagraph();
await editor.startTesting();
await tester.pumpAndSettle();
Expand All @@ -58,9 +58,10 @@ void main() async {
final delta = node?.delta;
expect(delta?.toJson(), [
{
'insert': 'HelloWorld',
'insert': 'Hello',
'attributes': {'code': true},
},
{'insert': 'World'},
]);
});

Expand Down

0 comments on commit 954ffa5

Please sign in to comment.