Skip to content

Commit

Permalink
feat: support customize supported node types for slash menu (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Dec 9, 2024
1 parent 157ded3 commit f32762e
Showing 1 changed file with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> _defaultSupportSlashMenuNodeTypes = {
ParagraphBlockKeys.type,
HeadingBlockKeys.type,
TodoListBlockKeys.type,
BulletedListBlockKeys.type,
NumberedListBlockKeys.type,
QuoteBlockKeys.type,
};

/// Show the slash menu
///
/// - support
Expand All @@ -26,6 +35,7 @@ CharacterShortcutEvent customSlashCommand(
bool deleteKeywordsByDefault = false,
bool singleColumn = true,
SelectionMenuStyle style = SelectionMenuStyle.light,
Set<String> supportSlashMenuNodeTypes = _defaultSupportSlashMenuNodeTypes,
}) {
return CharacterShortcutEvent(
key: 'show the slash menu',
Expand All @@ -37,19 +47,11 @@ CharacterShortcutEvent customSlashCommand(
deleteKeywordsByDefault: deleteKeywordsByDefault,
singleColumn: singleColumn,
style: style,
supportSlashMenuNodeTypes: supportSlashMenuNodeTypes,
),
);
}

final Set<String> supportSlashMenuNodeWhiteList = {
ParagraphBlockKeys.type,
HeadingBlockKeys.type,
TodoListBlockKeys.type,
BulletedListBlockKeys.type,
NumberedListBlockKeys.type,
QuoteBlockKeys.type,
};

SelectionMenuService? _selectionMenuService;
Future<bool> _showSlashMenu(
EditorState editorState,
Expand All @@ -58,6 +60,7 @@ Future<bool> _showSlashMenu(
bool singleColumn = true,
bool deleteKeywordsByDefault = false,
SelectionMenuStyle style = SelectionMenuStyle.light,
Set<String> supportSlashMenuNodeTypes = _defaultSupportSlashMenuNodeTypes,
}) async {
if (PlatformExtension.isMobile) {
return false;
Expand All @@ -82,7 +85,8 @@ Future<bool> _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;
}

Expand Down Expand Up @@ -121,10 +125,22 @@ Future<bool> _showSlashMenu(
return true;
}

bool _isSupportSlashMenuNode(Node node) {
var result = supportSlashMenuNodeWhiteList.contains(node.type);
bool _isSupportSlashMenuNode(
Node node,
Set<String> 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;
}

0 comments on commit f32762e

Please sign in to comment.