From bf77043c8e5cdf5a52eb855658304c72a779007d Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Tue, 16 May 2023 09:04:48 +0100 Subject: [PATCH] fix: disabling commands --- src/lib/components/dialog.svelte | 1 + src/lib/helpers/commandCenter.ts | 77 +++++++++++++------ src/routes/console/commandCenter.svelte | 6 +- .../project-[project]/databases/+page.svelte | 5 +- 4 files changed, 59 insertions(+), 30 deletions(-) diff --git a/src/lib/components/dialog.svelte b/src/lib/components/dialog.svelte index aa055883e..6578ebef9 100644 --- a/src/lib/components/dialog.svelte +++ b/src/lib/components/dialog.svelte @@ -7,6 +7,7 @@ export let error: string = null; export let closable = true; export let headerDivider = true; + export let animate = true; let dialog: HTMLDialogElement; let alert: HTMLElement; diff --git a/src/lib/helpers/commandCenter.ts b/src/lib/helpers/commandCenter.ts index a5196b241..1289b6f57 100644 --- a/src/lib/helpers/commandCenter.ts +++ b/src/lib/helpers/commandCenter.ts @@ -1,5 +1,6 @@ import { derived, writable } from 'svelte/store'; import { debounce } from './debounce'; +import { isMac } from './platform'; export type Command = { keys: string[]; @@ -16,18 +17,23 @@ export type Command = { type CommandCenterState = { commandMap: Map; - enabled: boolean; + disabledMap: Map; }; export const commandCenter = writable({ commandMap: new Map(), - enabled: true + disabledMap: new Map() }); export const commands = derived(commandCenter, ($commandCenter) => { return Array.from($commandCenter.commandMap.values()).flat(); }); +const commandsEnabled = derived(commandCenter, ($commandCenter) => { + // If there's an item on the disabledMap that's true, then disable the command center + return Array.from($commandCenter.disabledMap.values()).every((disabled) => !disabled); +}); + export const registerCommand = { subscribe(runner: (cb: (newCommands: Command[]) => void) => void) { const uuid = crypto.randomUUID(); @@ -48,33 +54,56 @@ export const registerCommand = { } }; -export const commandCenterKeyDownHandler = derived(commandCenter, ({ commandMap, enabled }) => { - const commandsArr = Array.from(commandMap.values()).flat(); - const recentKeyCodes = new Set(); +export const disableCommands = { + subscribe(runner: (cb: (disabled: boolean) => void) => void) { + const uuid = crypto.randomUUID(); - return (event: KeyboardEvent) => { - const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0; + runner((disabled: boolean) => { + commandCenter.update((curr) => { + curr.disabledMap.set(uuid, disabled); + return curr; + }); + }); - recentKeyCodes.add(event.keyCode); - debounce(() => recentKeyCodes.clear(), 1000)(); + return () => { + commandCenter.update((curr) => { + curr.disabledMap.delete(uuid); + return curr; + }); + }; + } +}; - for (const command of commandsArr) { - if (command.disabled || (!enabled && !command.forceEnable)) continue; +export const commandCenterKeyDownHandler = derived( + [commandCenter, commandsEnabled], + ([{ commandMap }, enabled]) => { + const commandsArr = Array.from(commandMap.values()).flat(); + const recentKeyCodes = new Set(); - const { keys, ctrl: meta, shift, alt, callback } = command; + return (event: KeyboardEvent) => { + recentKeyCodes.add(event.keyCode); + debounce(() => recentKeyCodes.clear(), 1000)(); - const isMetaPressed = meta ? (isMac ? event.metaKey : event.ctrlKey) : true; - const isShiftPressed = shift ? event.shiftKey : true; - const isAltPressed = alt ? event.altKey : true; + for (const command of commandsArr) { + if (command.disabled || (!enabled && !command.forceEnable)) continue; - const commandKeyCodes = keys.map((key) => key.toUpperCase().charCodeAt(0)); - const allKeysPressed = commandKeyCodes.every((keyCode) => recentKeyCodes.has(keyCode)); + const { keys, ctrl: meta, shift, alt, callback } = command; - if (allKeysPressed && isMetaPressed && isShiftPressed && isAltPressed) { - event.preventDefault(); - callback(); - return; + 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 (allKeysPressed && isMetaPressed && isShiftPressed && isAltPressed) { + event.preventDefault(); + callback(); + return; + } } - } - }; -}); + }; + } +); diff --git a/src/routes/console/commandCenter.svelte b/src/routes/console/commandCenter.svelte index e39c113ca..e1857455d 100644 --- a/src/routes/console/commandCenter.svelte +++ b/src/routes/console/commandCenter.svelte @@ -1,6 +1,6 @@ @@ -98,7 +98,7 @@ } .result { - border-radius: 0.25rem; + border-radius: 0.75rem; padding: 0.5rem 0.75rem; } diff --git a/src/routes/console/project-[project]/databases/+page.svelte b/src/routes/console/project-[project]/databases/+page.svelte index dc3eb69ca..b76e9a840 100644 --- a/src/routes/console/project-[project]/databases/+page.svelte +++ b/src/routes/console/project-[project]/databases/+page.svelte @@ -4,6 +4,7 @@ import { page } from '$app/stores'; import { Empty, PaginationWithLimit } from '$lib/components'; import { Button } from '$lib/elements/forms'; + import { disableCommands } from '$lib/helpers/commandCenter'; import { Container, GridHeader } from '$lib/layout'; import type { Models } from '@appwrite.io/console'; import { registerProjectCommand } from '../store'; @@ -12,7 +13,6 @@ import Grid from './grid.svelte'; import { columns } from './store'; import Table from './table.svelte'; - import { commandCenter } from '$lib/helpers/commandCenter'; export let data: PageData; @@ -31,12 +31,11 @@ showCreate = true; }, keys: ['c'], - ctrl: true, disabled: showCreate } ]); - $: $commandCenter.enabled = !showCreate; + $: $disableCommands(showCreate);