Files
plainleaf/client/components/command_palette.tsx
T
Zef Hemel 840e0c5fea Tuned the filterbox and fuse-based filtering
* Upgraded fuse library
* Removed the category support in the filterbox, it was buggy and hides
  command names
* Tweaked fuse parameters for (hopefully) better matches in the page
  picker (and everywhere where the filter box is used)
2026-01-15 09:17:34 +01:00

68 lines
1.7 KiB
TypeScript

import { FilterList } from "./filter.tsx";
import type {
CompletionContext,
CompletionResult,
} from "@codemirror/autocomplete";
import { Terminal } from "preact-feather";
import type { Command } from "../types/command.ts";
import type { FilterOption } from "@silverbulletmd/silverbullet/type/client";
export function CommandPalette({
commands,
onTrigger,
vimMode,
darkMode,
completer,
}: {
commands: Map<string, Command>;
vimMode: boolean;
darkMode?: boolean;
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
onTrigger: (command: Command | undefined) => void;
}) {
const options: FilterOption[] = [];
const isMac = isMacLike();
for (const [name, def] of commands.entries()) {
if (def.hide) {
continue;
}
options.push({
name: name,
hint: isMac && def.mac ? def.mac : def.key,
orderId: def.lastRun !== undefined
? -def.lastRun
: def.priority || Infinity,
});
// console.log("Options", options);
}
return (
<FilterList
label="Run"
placeholder="Command"
options={options}
allowNew={false}
icon={Terminal}
completer={completer}
vimMode={vimMode}
darkMode={darkMode}
helpText="Start typing the command name to filter results, press <code>Enter</code> to run."
onSelect={(opt) => {
if (opt) {
onTrigger(commands.get(opt.name));
} else {
onTrigger(undefined);
}
}}
/>
);
}
/**
* Checks if the current platform is Mac-like (Mac, iPhone, iPod, iPad).
* @returns A boolean indicating if the platform is Mac-like.
*/
function isMacLike() {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
}