From c6e6da76080b9449fad06adb23743cb2f8a1ace6 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Thu, 12 Dec 2024 14:24:52 +0800 Subject: [PATCH] feat: support link format --- lib/src/core/document/text_delta.dart | 27 +++++++++++++------ .../rich_text/appflowy_rich_text_keys.dart | 9 +++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/src/core/document/text_delta.dart b/lib/src/core/document/text_delta.dart index 8aa15443f..cf23f20df 100644 --- a/lib/src/core/document/text_delta.dart +++ b/lib/src/core/document/text_delta.dart @@ -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; @@ -41,10 +46,10 @@ 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; } @@ -52,14 +57,20 @@ AppFlowyEditorSliceAttributes? defaultAppFlowyEditorSliceAttributes = ( // 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; diff --git a/lib/src/editor/block_component/rich_text/appflowy_rich_text_keys.dart b/lib/src/editor/block_component/rich_text/appflowy_rich_text_keys.dart index 5a5dfecc4..39f6ee130 100644 --- a/lib/src/editor/block_component/rich_text/appflowy_rich_text_keys.dart +++ b/lib/src/editor/block_component/rich_text/appflowy_rich_text_keys.dart @@ -13,6 +13,7 @@ class AppFlowyRichTextKeys { static String autoComplete = 'auto_complete'; static String transparent = 'transparent'; + /// The attributes supported sliced. static List supportSliced = [ bold, italic, @@ -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 partialSliced = [ + code, + href, + ]; + // The values supported toggled even if the selection is collapsed. static List supportToggled = [ bold,