fix: disabling commands

This commit is contained in:
tglide committed 2023-05-16 09:04:48 +01:00
1 parent 41ce56e870
commit bf77043c8e
4 files changed
+59 -30

No files matched your search

+1
View File
@@ -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;
+53 -24
View File
@@ -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<string, Command[]>;
enabled: boolean;
disabledMap: Map<string, boolean>;
};
export const commandCenter = writable<CommandCenterState>({
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<number>();
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<number>();
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;
}
}
}
};
});
};
}
);
+3 -3
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import Dialog from '$lib/components/dialog.svelte';
import { commands, commandCenter, registerCommand } from '$lib/helpers/commandCenter';
import { commands, disableCommands, registerCommand } from '$lib/helpers/commandCenter';
import { isMac } from '$lib/helpers/platform';
let open = false;
@@ -48,7 +48,7 @@
selected = 0;
}
$: $commandCenter.enabled = !open;
$: $disableCommands(open);
</script>
<svelte:window on:keydown={handleKeyDown} />
@@ -98,7 +98,7 @@
}
.result {
border-radius: 0.25rem;
border-radius: 0.75rem;
padding: 0.5rem 0.75rem;
}
@@ -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);
</script>
<Container>