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: support customize supported node types for slash menu #982

Merged
merged 1 commit into from
Dec 9, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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!,

Check warning on line 140 in lib/src/editor/editor_component/service/shortcuts/character/slash_command.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/shortcuts/character/slash_command.dart#L139-L140

Added lines #L139 - L140 were not covered by tests
supportSlashMenuNodeWhiteList,
);
}
return result;

return true;
}
Loading