From f32762eae0a703e15ae681947ad1ff110d2e477a Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 9 Dec 2024 16:55:59 +0800 Subject: [PATCH] feat: support customize supported node types for slash menu (#982) --- .../shortcuts/character/slash_command.dart | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/lib/src/editor/editor_component/service/shortcuts/character/slash_command.dart b/lib/src/editor/editor_component/service/shortcuts/character/slash_command.dart index 2fb95fa3d..57fb32072 100644 --- a/lib/src/editor/editor_component/service/shortcuts/character/slash_command.dart +++ b/lib/src/editor/editor_component/service/shortcuts/character/slash_command.dart @@ -5,6 +5,15 @@ import 'package:appflowy_editor/src/editor/util/platform_extension.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +const Set _defaultSupportSlashMenuNodeTypes = { + ParagraphBlockKeys.type, + HeadingBlockKeys.type, + TodoListBlockKeys.type, + BulletedListBlockKeys.type, + NumberedListBlockKeys.type, + QuoteBlockKeys.type, +}; + /// Show the slash menu /// /// - support @@ -26,6 +35,7 @@ CharacterShortcutEvent customSlashCommand( bool deleteKeywordsByDefault = false, bool singleColumn = true, SelectionMenuStyle style = SelectionMenuStyle.light, + Set supportSlashMenuNodeTypes = _defaultSupportSlashMenuNodeTypes, }) { return CharacterShortcutEvent( key: 'show the slash menu', @@ -37,19 +47,11 @@ CharacterShortcutEvent customSlashCommand( deleteKeywordsByDefault: deleteKeywordsByDefault, singleColumn: singleColumn, style: style, + supportSlashMenuNodeTypes: supportSlashMenuNodeTypes, ), ); } -final Set supportSlashMenuNodeWhiteList = { - ParagraphBlockKeys.type, - HeadingBlockKeys.type, - TodoListBlockKeys.type, - BulletedListBlockKeys.type, - NumberedListBlockKeys.type, - QuoteBlockKeys.type, -}; - SelectionMenuService? _selectionMenuService; Future _showSlashMenu( EditorState editorState, @@ -58,6 +60,7 @@ Future _showSlashMenu( bool singleColumn = true, bool deleteKeywordsByDefault = false, SelectionMenuStyle style = SelectionMenuStyle.light, + Set supportSlashMenuNodeTypes = _defaultSupportSlashMenuNodeTypes, }) async { if (PlatformExtension.isMobile) { return false; @@ -82,7 +85,8 @@ Future _showSlashMenu( final node = editorState.getNodeAtPath(selection.start.path); // only enable in white-list nodes - if (node == null || !_isSupportSlashMenuNode(node)) { + if (node == null || + !_isSupportSlashMenuNode(node, supportSlashMenuNodeTypes)) { return false; } @@ -121,10 +125,22 @@ Future _showSlashMenu( return true; } -bool _isSupportSlashMenuNode(Node node) { - var result = supportSlashMenuNodeWhiteList.contains(node.type); +bool _isSupportSlashMenuNode( + Node node, + Set supportSlashMenuNodeWhiteList, +) { + // Check if current node type is supported + if (!supportSlashMenuNodeWhiteList.contains(node.type)) { + return false; + } + + // If node has a parent and level > 1, recursively check parent nodes if (node.level > 1 && node.parent != null) { - return result && _isSupportSlashMenuNode(node.parent!); + return _isSupportSlashMenuNode( + node.parent!, + supportSlashMenuNodeWhiteList, + ); } - return result; + + return true; }