refactor: cleaner registerCommand fn

This commit is contained in:
tglide committed 2023-05-15 23:52:08 +01:00
1 parent afcbb5dca6
commit 05d9cb7f3a
11 files changed
+52 -77

No files matched your search

+19 -50
View File
@@ -1,16 +1,12 @@
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'
import { debounce } from './debounce';
export type Command = {
keys: string[];
/* Ctrl on Windows/Linux, Meta on Mac */
meta?: boolean;
ctrl?: boolean;
shift?: boolean;
/* Alt on Windows/Linux, Option on Mac */
alt?: boolean;
callback: () => void;
label?: string;
@@ -19,66 +15,39 @@ export type Command = {
};
type CommandCenterState = {
commandMap: Map<RegisterProxy, Command[]>;
commandMap: Map<string, Command[]>;
enabled: boolean;
};
export const commandCenter = (function init() {
const store = writable<CommandCenterState>({
commandMap: new Map(),
enabled: true
});
return {
...store
};
})();
export const commandCenter = writable<CommandCenterState>({
commandMap: new Map(),
enabled: true
});
export const commands = derived(commandCenter, ($commandCenter) => {
return Array.from($commandCenter.commandMap.values()).flat();
});
function createRegisterProxy() {
return new Proxy(
{},
{
get(_obj, field, receiver) {
if (field !== 'register') return;
return (newCommands: Command[]) => {
commandCenter.update((curr) => {
curr.commandMap.set(receiver, newCommands);
return curr;
});
};
}
}
) as { register: (newCommands: Command[]) => void };
}
export const registerCommand = {
subscribe(runner: (cb: (newCommands: Command[]) => void) => void) {
const uuid = crypto.randomUUID();
export type RegisterProxy = ReturnType<typeof createRegisterProxy>;
export const cmdRegistrant = {
subscribe(runner: (proxy: RegisterProxy) => void) {
const proxy = createRegisterProxy();
runner(proxy);
runner((newCommands: Command[]) => {
commandCenter.update((curr) => {
curr.commandMap.set(uuid, newCommands);
return curr;
});
});
return () => {
commandCenter.update((curr) => {
curr.commandMap.delete(proxy);
curr.commandMap.delete(uuid);
return curr;
});
};
}
};
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>();
@@ -92,7 +61,7 @@ export const commandCenterKeyDownHandler = derived(commandCenter, ({ commandMap,
for (const command of commandsArr) {
if (command.disabled || (!enabled && !command.forceEnable)) continue;
const { keys, meta, shift, alt, callback } = command;
const { keys, ctrl: meta, shift, alt, callback } = command;
const isMetaPressed = meta ? (isMac ? event.metaKey : event.ctrlKey) : true;
const isShiftPressed = shift ? event.shiftKey : true;
+7
View File
@@ -0,0 +1,7 @@
export function debounce(fn: () => void, ms: number) {
let timeout: NodeJS.Timeout;
return () => {
clearTimeout(timeout);
timeout = setTimeout(fn, ms);
};
}
+3 -3
View File
@@ -16,17 +16,17 @@
import Create from './createOrganization.svelte';
import { goto } from '$app/navigation';
import { cmdRegistrant, commandCenterKeyDownHandler } from '$lib/helpers/commandCenter';
import { registerCommand, commandCenterKeyDownHandler } from '$lib/helpers/commandCenter';
import CommandCenter from './commandCenter.svelte';
$: $cmdRegistrant.register([
$: $registerCommand([
{
label: 'Go to Account',
callback: () => {
goto('/console/account');
},
keys: ['a'],
meta: true,
ctrl: true,
shift: true
},
{
+4 -4
View File
@@ -1,19 +1,19 @@
<script lang="ts">
import Dialog from '$lib/components/dialog.svelte';
import { commands, commandCenter, cmdRegistrant } from '$lib/helpers/commandCenter';
import { commands, commandCenter, registerCommand } from '$lib/helpers/commandCenter';
import { isMac } from '$lib/helpers/platform';
let open = false;
let search = '';
let selected = 0;
$: $cmdRegistrant.register([
$: $registerCommand([
{
callback: () => {
open = !open;
},
keys: ['k'],
meta: true,
ctrl: true,
forceEnable: true
}
]);
@@ -66,7 +66,7 @@
{command.label}
</span>
<div class="u-flex u-gap-4">
{#if command.meta}
{#if command.ctrl}
<kbd class="kbd"> {isMac() ? '⌘' : 'ctrl'} </kbd>
{/if}
{#if command.shift}
@@ -5,7 +5,7 @@
import { CardContainer, Empty, GridItem1, Heading, PaginationWithLimit } from '$lib/components';
import { Pill } from '$lib/elements';
import { Button } from '$lib/elements/forms';
import { cmdRegistrant } from '$lib/helpers/commandCenter';
import { registerCommand } from '$lib/helpers/commandCenter';
import { Container } from '$lib/layout';
import CreateOrganization from '../createOrganization.svelte';
import type { PageData } from './$types';
@@ -46,7 +46,7 @@
);
}
$: $cmdRegistrant.register([
$: $registerCommand([
...data.projects.projects.map((project, i) => ({
label: `Project ${project.name}`,
callback: () => {
@@ -24,7 +24,7 @@
import { toLocaleDateTime } from '$lib/helpers/date';
import { Container } from '$lib/layout';
import type { Models } from '@appwrite.io/console';
import { project, projectRegistrant } from '../store';
import { project, registerProjectCommand } from '../store';
import type { PageData } from './$types';
import Create from './createUser.svelte';
@@ -36,7 +36,7 @@
await goto(`${base}/console/project-${projectId}/auth/user-${event.detail.$id}`);
}
$: $projectRegistrant.register([
$: $registerProjectCommand([
{
label: 'Create user',
callback: () => {
@@ -6,7 +6,7 @@
import { Button } from '$lib/elements/forms';
import { Container, GridHeader } from '$lib/layout';
import type { Models } from '@appwrite.io/console';
import { projectRegistrant } from '../store';
import { registerProjectCommand } from '../store';
import type { PageData } from './$types';
import Create from './create.svelte';
import Grid from './grid.svelte';
@@ -23,7 +23,7 @@
await goto(`${base}/console/project-${project}/databases/database-${event.detail.$id}`);
}
$: $projectRegistrant.register([
$: $registerProjectCommand([
{
label: 'Create database',
callback: () => {
@@ -19,7 +19,7 @@
import type { PageData } from './$types';
import Create from './createFunction.svelte';
import { projectRegistrant } from '../store';
import { registerProjectCommand } from '../store';
export let data: PageData;
@@ -35,7 +35,7 @@
wizard.hide();
});
$: $projectRegistrant.register([
$: $registerProjectCommand([
{
label: 'Create function',
callback: openWizard,
@@ -36,7 +36,7 @@
import { toLocaleDateTime } from '$lib/helpers/date';
import { app } from '$lib/stores/app';
import type { PageData } from './$types';
import { projectRegistrant } from '../../store';
import { registerProjectCommand } from '../../store';
export let data: PageData;
@@ -72,7 +72,7 @@
}
};
$: $projectRegistrant.register();
$: $registerProjectCommand();
</script>
<div class="common-section u-flex u-gap-12">
@@ -15,7 +15,7 @@
import { Button } from '$lib/elements/forms';
import { Container } from '$lib/layout';
import type { Models } from '@appwrite.io/console';
import { projectRegistrant } from '../store';
import { registerProjectCommand } from '../store';
import type { PageData } from './$types';
import Create from './create.svelte';
@@ -30,7 +30,7 @@
await goto(`${base}/console/project-${project}/storage/bucket-${event.detail.$id}`);
}
$: $projectRegistrant.register([
$: $registerProjectCommand([
{
label: 'Create bucket',
callback: () => {
@@ -1,16 +1,15 @@
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { cmdRegistrant, type Command } from '$lib/helpers/commandCenter';
import { registerCommand, type Command } from '$lib/helpers/commandCenter';
import type { Models } from '@appwrite.io/console';
import type { BarSeriesOption } from 'echarts/charts';
import { derived, writable } from 'svelte/store';
import { derived, writable, type Readable } from 'svelte/store';
export const project = derived(page, ($page) => $page.data.project as Models.Project);
export const projectRegistrant = derived([project, cmdRegistrant], ([$project, $registrant]) => {
return {
register: (c: Command[] = []) =>
$registrant.register([
export const registerProjectCommand = derived([project, registerCommand], ([$project, $register]) => {
return (c: Command[] = []) =>
$register([
...[
{
label: 'Go to Overview',
@@ -51,8 +50,8 @@ export const projectRegistrant = derived([project, cmdRegistrant], ([$project, $
],
...(c ?? [])
])
};
});
}) as Readable<(c?: Command[]) => void>;
export const onboarding = derived(
project,