From 08df140a55c7bdb8322be1334c816b11df1a97c5 Mon Sep 17 00:00:00 2001 From: kurorinto <394460456@qq.com> Date: Thu, 15 Aug 2024 14:53:36 +0800 Subject: [PATCH] feat: `command palette` supports searching by shortcut --- .../CommandPalette/CommandPalette.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/excalidraw/components/CommandPalette/CommandPalette.tsx b/packages/excalidraw/components/CommandPalette/CommandPalette.tsx index 36c9a9a687..dcec6575d6 100644 --- a/packages/excalidraw/components/CommandPalette/CommandPalette.tsx +++ b/packages/excalidraw/components/CommandPalette/CommandPalette.tsx @@ -780,13 +780,29 @@ function CommandPaletteInner({ const _query = deburr( commandSearch.toLocaleLowerCase().replace(/[<>_| -]/g, ""), ); - matchingCommands = fuzzy + + const matchingCommandsHaystack = fuzzy .filter(_query, matchingCommands, { extract: (command) => command.haystack, }) .sort((a, b) => b.score - a.score) .map((item) => item.original); + const matchingCommandsShortcut = fuzzy + .filter(_query, matchingCommands, { + extract: (command) => command.shortcut || "", + }) + .sort((a, b) => b.score - a.score) + .map((item) => item.original) + .filter( + (item) => + !matchingCommandsHaystack.some((item2) => item.label === item2.label), + ); + + matchingCommands = matchingCommandsHaystack.concat( + matchingCommandsShortcut, + ); + setCommandsByCategory(getNextCommandsByCategory(matchingCommands)); setCurrentCommand(matchingCommands[0] ?? null); }, [commandSearch, allCommands, isCommandAvailable, lastUsed]);