Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add partialSliced to support partial sliced attributes #989

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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),

Check warning on line 68 in lib/src/core/document/text_delta.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/core/document/text_delta.dart#L68

Added line #L68 was not covered by tests
)) {
return prevAttributes..remove(AppFlowyRichTextKeys.code);
return prevAttributes
..removeWhere(
(key, _) => AppFlowyRichTextKeys.partialSliced.contains(key),

Check warning on line 72 in lib/src/core/document/text_delta.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/core/document/text_delta.dart#L71-L72

Added lines #L71 - L72 were not covered by tests
);
}

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
Loading