2022-10-10 12:50:21 +00:00
|
|
|
import { isMacLike } from "../../common/util.ts";
|
|
|
|
import { FilterList } from "./filter.tsx";
|
2022-12-21 13:55:24 +00:00
|
|
|
import { CompletionContext, CompletionResult, TerminalIcon } from "../deps.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { AppCommand } from "../hooks/command.ts";
|
2023-12-22 13:33:50 +00:00
|
|
|
import { BuiltinSettings, FilterOption } from "../types.ts";
|
|
|
|
import { commandLinkRegex } from "../../common/markdown_parser/parser.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
export function CommandPalette({
|
|
|
|
commands,
|
2022-05-16 13:09:36 +00:00
|
|
|
recentCommands,
|
2022-03-20 08:56:28 +00:00
|
|
|
onTrigger,
|
2022-12-21 13:55:24 +00:00
|
|
|
vimMode,
|
|
|
|
darkMode,
|
|
|
|
completer,
|
2023-12-22 13:33:50 +00:00
|
|
|
settings,
|
2022-03-20 08:56:28 +00:00
|
|
|
}: {
|
|
|
|
commands: Map<string, AppCommand>;
|
2022-05-16 13:09:36 +00:00
|
|
|
recentCommands: Map<string, Date>;
|
2022-12-21 13:55:24 +00:00
|
|
|
vimMode: boolean;
|
|
|
|
darkMode: boolean;
|
|
|
|
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
|
2022-03-20 08:56:28 +00:00
|
|
|
onTrigger: (command: AppCommand | undefined) => void;
|
2023-12-22 13:33:50 +00:00
|
|
|
settings: BuiltinSettings;
|
2022-03-20 08:56:28 +00:00
|
|
|
}) {
|
2022-12-21 13:55:24 +00:00
|
|
|
const options: FilterOption[] = [];
|
2022-03-20 08:56:28 +00:00
|
|
|
const isMac = isMacLike();
|
2022-12-21 13:55:24 +00:00
|
|
|
for (const [name, def] of commands.entries()) {
|
2023-12-22 13:33:50 +00:00
|
|
|
let shortcut: { key?: string; mac?: string } = def.command;
|
|
|
|
// Let's see if there's a keyboard shortcut override
|
|
|
|
if (settings.keyboardShortcuts) {
|
|
|
|
const commandKeyboardOverride = settings.keyboardShortcuts.find((
|
|
|
|
shortcut,
|
|
|
|
) => {
|
|
|
|
const commandMatch = commandLinkRegex.exec(shortcut.command);
|
|
|
|
return commandMatch && commandMatch[1] === name ||
|
|
|
|
shortcut.command === name;
|
|
|
|
});
|
|
|
|
if (commandKeyboardOverride) {
|
|
|
|
shortcut = commandKeyboardOverride;
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
options.push({
|
|
|
|
name: name,
|
2023-12-22 13:33:50 +00:00
|
|
|
hint: isMac && shortcut.mac ? shortcut.mac : shortcut.key,
|
2022-05-16 13:09:36 +00:00
|
|
|
orderId: recentCommands.has(name)
|
|
|
|
? -recentCommands.get(name)!.getTime()
|
|
|
|
: 0,
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<FilterList
|
|
|
|
label="Run"
|
|
|
|
placeholder="Command"
|
|
|
|
options={options}
|
|
|
|
allowNew={false}
|
2022-12-08 16:04:07 +00:00
|
|
|
icon={TerminalIcon}
|
2022-12-21 13:55:24 +00:00
|
|
|
completer={completer}
|
|
|
|
vimMode={vimMode}
|
|
|
|
darkMode={darkMode}
|
2022-03-20 08:56:28 +00:00
|
|
|
helpText="Start typing the command name to filter results, press <code>Return</code> to run."
|
|
|
|
onSelect={(opt) => {
|
|
|
|
if (opt) {
|
|
|
|
onTrigger(commands.get(opt.name));
|
|
|
|
} else {
|
|
|
|
onTrigger(undefined);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|