Skip to content

Commit

Permalink
feat: support link format
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Dec 12, 2024
1 parent 954ffa5 commit 08b8c77
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/src/core/document/text_delta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ typedef AppFlowyEditorSliceAttributes = Attributes? Function(
int index,
);

/// Default slice attributes function.
///
/// For the BIUS attributes, the slice attributes function will slice the attributes from the previous position,
/// if the index is 0, it will slice the attributes from the next position.
/// For the link and code attributes, the slice attributes function will only work if the index is in the range of the link or code.
AppFlowyEditorSliceAttributes? defaultAppFlowyEditorSliceAttributes = (
delta,
index,
int index,
) {
if (index < 0) {
return null;
Expand Down Expand Up @@ -41,25 +46,31 @@ AppFlowyEditorSliceAttributes? defaultAppFlowyEditorSliceAttributes = (
if (prevAttributes == null) {
return null;
}
// if the prevAttributes doesn't include the code, return it.
// Otherwise, check if the nextAttributes includes the code.
// if the prevAttributes doesn't include the code/href, return it.
// Otherwise, check if the nextAttributes includes the code/href.
final unformattedAttributes = [
AppFlowyRichTextKeys.code,
AppFlowyRichTextKeys.href,
];
if (!prevAttributes.keys.any(
(element) => element == AppFlowyRichTextKeys.code,
(element) => unformattedAttributes.contains(element),
)) {
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);
return prevAttributes
..removeWhere((key, _) => unformattedAttributes.contains(key));
}

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

return prevAttributes;
Expand Down

0 comments on commit 08b8c77

Please sign in to comment.