refactor: improve commands

This commit is contained in:
tglide committed 2023-05-16 15:57:25 +01:00
1 parent dce2d824b9
commit 7badf3a28b
9 files changed
+65 -71

No files matched your search

+9 -6
View File
@@ -74,6 +74,10 @@ export const disableCommands = {
}
};
function isInputEvent(event: KeyboardEvent) {
return ['INPUT', 'TEXTAREA', 'SELECT'].includes((event.target as HTMLElement).tagName);
}
export const commandCenterKeyDownHandler = derived(
[commandCenter, commandsEnabled],
([{ commandMap }, enabled]) => {
@@ -81,16 +85,15 @@ export const commandCenterKeyDownHandler = derived(
let recentKeyCodes: number[] = [];
return (event: KeyboardEvent) => {
// ignore keypresses that come from input, textarea and select elements
if (['INPUT', 'TEXTAREA', 'SELECT'].includes((event.target as HTMLElement).tagName)) {
return;
}
recentKeyCodes.push(event.keyCode);
debounce(() => (recentKeyCodes = []), 1000)();
for (const command of commandsArr) {
if (command.disabled || (!enabled && !command.forceEnable)) continue;
if (!command.forceEnable) {
if (command.disabled) continue;
if (!enabled) continue;
if (isInputEvent(event)) continue;
}
const { keys, ctrl: meta, shift, alt, callback } = command;
@@ -2,7 +2,9 @@
import { UploadBox } from '$lib/components';
import { sdk } from '$lib/stores/sdk';
import { onMount } from 'svelte';
import { stats } from './store';
import { project, stats } from './store';
import { registerCommand } from '$lib/helpers/commandCenter';
import { goto } from '$app/navigation';
onMount(async () => {
return sdk.forConsole.client.subscribe(['project', 'console'], (response) => {
@@ -13,6 +15,46 @@
}
});
});
$: $registerCommand([
{
label: 'Go to Overview',
keys: ['g', 'o'],
callback: () => {
goto(`/console/project-${$project.$id}`);
}
},
{
label: 'Go to Auth',
callback: () => {
goto(`/console/project-${$project.$id}/auth`);
},
keys: ['g', 'a']
},
{
label: 'Go to Databases',
callback: () => {
goto(`/console/project-${$project.$id}/databases`);
},
keys: ['g', 'd']
},
{
label: 'Go to Functions',
callback: () => {
goto(`/console/project-${$project.$id}/functions`);
},
keys: ['g', 'f']
},
{
label: 'Go to Storage',
callback: () => {
goto(`/console/project-${$project.$id}/storage`);
},
keys: ['g', 's']
}
]);
</script>
<slot />
@@ -24,9 +24,10 @@
import { toLocaleDateTime } from '$lib/helpers/date';
import { Container } from '$lib/layout';
import type { Models } from '@appwrite.io/console';
import { project, registerProjectCommand } from '../store';
import { project } from '../store';
import type { PageData } from './$types';
import Create from './createUser.svelte';
import { registerCommand } from '$lib/helpers/commandCenter';
export let data: PageData;
@@ -36,7 +37,7 @@
await goto(`${base}/console/project-${projectId}/auth/user-${event.detail.$id}`);
}
$: $registerProjectCommand([
$: $registerCommand([
{
label: 'Create user',
callback: () => {
@@ -6,12 +6,13 @@
import { Button } from '$lib/elements/forms';
import { Container, GridHeader } from '$lib/layout';
import type { Models } from '@appwrite.io/console';
import { registerProjectCommand } from '../store';
import type { PageData } from './$types';
import Create from './create.svelte';
import Grid from './grid.svelte';
import { columns } from './store';
import Table from './table.svelte';
import { registerCommand } from '$lib/helpers/commandCenter';
export let data: PageData;
@@ -23,7 +24,7 @@
await goto(`${base}/console/project-${project}/databases/database-${event.detail.$id}`);
}
$: $registerProjectCommand([
$: $registerCommand([
{
label: 'Create database',
callback: () => {
@@ -19,7 +19,7 @@
import type { PageData } from './$types';
import Create from './createFunction.svelte';
import { registerProjectCommand } from '../store';
import { registerCommand } from '$lib/helpers/commandCenter';
export let data: PageData;
@@ -35,7 +35,7 @@
wizard.hide();
});
$: $registerProjectCommand([
$: $registerCommand([
{
label: 'Create function',
callback: openWizard,
@@ -9,7 +9,6 @@
import { app } from '$lib/stores/app';
import { wizard } from '$lib/stores/wizard';
import Wizard from './keys/wizard.svelte';
import { registerProjectCommand } from '../store';
export let projectId: string;
@@ -43,8 +42,6 @@
$: onBoardIntro = $app.themeInUse === 'dark' ? OnboardDarkIntro : OnboardLightIntro;
$: onBoardImage1 = $app.themeInUse === 'dark' ? OnboardDark1 : OnboardLight1;
$: onBoardImage2 = $app.themeInUse === 'dark' ? OnboardDark2 : OnboardLight2;
$: $registerProjectCommand();
</script>
<div class="card">
@@ -36,7 +36,7 @@
import { toLocaleDateTime } from '$lib/helpers/date';
import { app } from '$lib/stores/app';
import type { PageData } from './$types';
import { registerProjectCommand } from '../../store';
import { registerCommand } from '$lib/helpers/commandCenter';
export let data: PageData;
@@ -72,7 +72,7 @@
}
};
$: $registerProjectCommand([
$: $registerCommand([
{
label: 'Create Web App',
callback: () => addPlatform(Platform.Web),
@@ -15,7 +15,7 @@
import { Button } from '$lib/elements/forms';
import { Container } from '$lib/layout';
import type { Models } from '@appwrite.io/console';
import { registerProjectCommand } from '../store';
import { registerCommand } from '$lib/helpers/commandCenter';
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}`);
}
$: $registerProjectCommand([
$: $registerCommand([
{
label: 'Create bucket',
callback: () => {
+1 -51
View File
@@ -1,60 +1,10 @@
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { registerCommand, type Command } from '$lib/helpers/commandCenter';
import type { Models } from '@appwrite.io/console';
import type { BarSeriesOption } from 'echarts/charts';
import { derived, writable, type Readable } from 'svelte/store';
import { derived, writable } from 'svelte/store';
export const project = derived(page, ($page) => $page.data.project as Models.Project);
export const registerProjectCommand = derived(
[project, registerCommand],
([$project, $register]) => {
return (c: Command[] = []) => {
const projectCommands: Command[] = [
{
label: 'Go to Overview',
keys: ['g', 'o'],
callback: () => {
goto(`/console/project-${$project.$id}`);
}
},
{
label: 'Go to Auth',
callback: () => {
goto(`/console/project-${$project.$id}/auth`);
},
keys: ['g', 'a']
},
{
label: 'Go to Databases',
callback: () => {
goto(`/console/project-${$project.$id}/databases`);
},
keys: ['g', 'd']
},
{
label: 'Go to Functions',
callback: () => {
goto(`/console/project-${$project.$id}/functions`);
},
keys: ['g', 'f']
},
{
label: 'Go to Storage',
callback: () => {
goto(`/console/project-${$project.$id}/storage`);
},
keys: ['g', 's']
}
];
return $register([...projectCommands, ...(c ?? [])]);
};
}
) as Readable<(c?: Command[]) => void>;
export const onboarding = derived(
project,
($project) => $project.platforms.length === 0 && $project.keys.length === 0