Skip to content

Commit

Permalink
feat: add partialSliced to support partial sliced attributes (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Dec 12, 2024
1 parent 954ffa5 commit ea1bcd3
Show file tree
Hide file tree
Showing 2 changed files with 28 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.
if (!prevAttributes.keys.any(
(element) => element == AppFlowyRichTextKeys.code,
(element) => AppFlowyRichTextKeys.partialSliced.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, _) => AppFlowyRichTextKeys.partialSliced.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) => AppFlowyRichTextKeys.partialSliced.contains(element),
)) {
return prevAttributes..remove(AppFlowyRichTextKeys.code);
return prevAttributes
..removeWhere(
(key, _) => AppFlowyRichTextKeys.partialSliced.contains(key),
);
}

return prevAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AppFlowyRichTextKeys {
static String autoComplete = 'auto_complete';
static String transparent = 'transparent';

/// The attributes supported sliced.
static List<String> supportSliced = [
bold,
italic,
Expand All @@ -23,6 +24,14 @@ class AppFlowyRichTextKeys {
code,
];

/// The attributes is partially supported sliced.
///
/// For the code and href attributes, the slice attributes function will only work if the index is in the range of the code or href.
static List<String> partialSliced = [
code,
href,
];

// The values supported toggled even if the selection is collapsed.
static List<String> supportToggled = [
bold,
Expand Down

0 comments on commit ea1bcd3

Please sign in to comment.