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()) {
|
2024-01-02 12:47:43 +00:00
|
|
|
let shortcut: { key?: string; mac?: string; priority?: number } =
|
|
|
|
def.command;
|
2023-12-27 07:14:57 +00:00
|
|
|
// Let's see if there's a shortcut override
|
|
|
|
if (settings.shortcuts) {
|
2024-01-02 12:47:43 +00:00
|
|
|
const commandOverride = settings.shortcuts.find((
|
2023-12-22 13:33:50 +00:00
|
|
|
shortcut,
|
|
|
|
) => {
|
|
|
|
const commandMatch = commandLinkRegex.exec(shortcut.command);
|
2023-12-27 07:14:57 +00:00
|
|
|
// If this is a command link, we want to match the command name but also make sure no arguments were set
|
|
|
|
return commandMatch && commandMatch[1] === name && !commandMatch[5] ||
|
|
|
|
// or if it's not a command link, let's match exactly
|
2023-12-22 13:33:50 +00:00
|
|
|
shortcut.command === name;
|
|
|
|
});
|
2024-01-02 12:47:43 +00:00
|
|
|
if (commandOverride) {
|
|
|
|
shortcut = commandOverride;
|
|
|
|
console.log(`Shortcut override for ${name}:`, shortcut);
|
2023-12-22 13:33:50 +00:00
|
|
|
}
|
|
|
|
}
|
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()
|
2024-01-02 12:47:43 +00:00
|
|
|
: shortcut.priority || Infinity,
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
2024-01-02 12:47:43 +00:00
|
|
|
// console.log("Options", options);
|
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);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|