From a7c53ae522df6166d02686f9c018b3fa2ccb56d1 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Mon, 15 May 2023 19:55:26 +0100 Subject: [PATCH] feat: multi key commands --- src/lib/helpers/commandCenter.ts | 27 ++++++++++++++----- .../account/organizations/+page.svelte | 2 ++ .../organization-[organization]/+page.svelte | 21 +++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/lib/helpers/commandCenter.ts b/src/lib/helpers/commandCenter.ts index ced51ded4..90a9fb775 100644 --- a/src/lib/helpers/commandCenter.ts +++ b/src/lib/helpers/commandCenter.ts @@ -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(); 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; diff --git a/src/routes/console/account/organizations/+page.svelte b/src/routes/console/account/organizations/+page.svelte index f43f7c46c..a2435cdf8 100644 --- a/src/routes/console/account/organizations/+page.svelte +++ b/src/routes/console/account/organizations/+page.svelte @@ -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; diff --git a/src/routes/console/organization-[organization]/+page.svelte b/src/routes/console/organization-[organization]/+page.svelte index d039ac7d3..0928668d1 100644 --- a/src/routes/console/organization-[organization]/+page.svelte +++ b/src/routes/console/organization-[organization]/+page.svelte @@ -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 + } + ]);