feat: multi key commands

This commit is contained in:
tglide committed 2023-05-15 19:55:26 +01:00
1 parent fc2932bef8
commit a7c53ae522
3 files changed
+43 -7

No files matched your search

+20 -7
View File
@@ -1,6 +1,12 @@
import { onDestroy } from 'svelte';
import { derived, writable } from 'svelte/store';
// type Key = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k'
// | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x'
// | 'y' | 'z' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K'
// | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X'
// | 'Y' | 'Z' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
type Command = {
keys: string[];
/* Ctrl on Windows/Linux, Meta on Mac */
@@ -65,30 +71,37 @@ export function ExtendCommandRegistrant(baseCommands: Command[]) {
};
}
const debounce = (fn: () => void, ms: number) => {
let timeout: NodeJS.Timeout;
return () => {
clearTimeout(timeout);
timeout = setTimeout(fn, ms);
};
};
export const commandCenterKeyDownHandler = derived(commandCenter, ({ commandMap, enabled }) => {
const commandsArr = Array.from(commandMap.values()).flat();
const recentKeyCodes = new Set<number>();
return (event: KeyboardEvent) => {
const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
recentKeyCodes.add(event.keyCode);
debounce(() => recentKeyCodes.clear(), 1000)();
for (const command of commandsArr) {
if (command.disabled || (!enabled && !command.forceEnable)) continue;
const { keys, meta, shift, alt, callback } = command;
const keyCode = event.keyCode;
const isMetaPressed = meta ? (isMac ? event.metaKey : event.ctrlKey) : true;
const isShiftPressed = shift ? event.shiftKey : true;
const isAltPressed = alt ? event.altKey : true;
const commandKeyCodes = keys.map((key) => key.toUpperCase().charCodeAt(0));
const allKeysPressed = commandKeyCodes.every((keyCode) => recentKeyCodes.has(keyCode));
if (
commandKeyCodes.includes(keyCode) &&
isMetaPressed &&
isShiftPressed &&
isAltPressed
) {
if (allKeysPressed && isMetaPressed && isShiftPressed && isAltPressed) {
event.preventDefault();
callback();
return;
@@ -13,6 +13,8 @@
import CreateOrganization from '../../createOrganization.svelte';
import { sdk } from '$lib/stores/sdk';
import type { PageData } from './$types';
import { CommandRegistrant } from '$lib/helpers/commandCenter';
import { goto } from '$app/navigation';
export let data: PageData;
@@ -8,6 +8,8 @@
import CreateOrganization from '../createOrganization.svelte';
import type { PageData } from './$types';
import { page } from '$app/stores';
import { CommandRegistrant } from '$lib/helpers/commandCenter';
import { goto } from '$app/navigation';
export let data: PageData;
@@ -43,6 +45,25 @@
(value, index, self) => index === self.findIndex((t) => t.name === value.name)
);
}
const { register } = CommandRegistrant();
$: register([
...data.projects.projects.map((project, i) => ({
label: `Project ${project.name}`,
callback: () => {
goto(`/console/project-${project.$id}`);
},
keys: ['p', `${i + 1}`]
})),
{
label: 'Create project',
callback: () => {
showCreate = true;
},
keys: ['c'],
disabled: showCreate
}
]);
</script>
<Container>