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";
|
|
|
|
import { FilterOption } from "../../common/types.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,
|
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;
|
|
|
|
}) {
|
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()) {
|
2022-03-20 08:56:28 +00:00
|
|
|
options.push({
|
|
|
|
name: name,
|
|
|
|
hint: isMac && def.command.mac ? def.command.mac : def.command.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);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|