From c0b93f65a976440a8afae98c4569348c223f6eae Mon Sep 17 00:00:00 2001 From: Xylight Date: Fri, 19 Jul 2024 17:30:28 -0700 Subject: [PATCH] misc: clean up actions --- .../ui/navbar/commands/CommandItem.svelte | 3 +- .../ui/navbar/commands/Commands.svelte | 134 ++++++++++++++---- .../components/ui/navbar/commands/actions.ts | 0 src/lib/i18n/en.json | 6 + .../post/[instance]/[id=integer]/+page.svelte | 21 +-- 5 files changed, 124 insertions(+), 40 deletions(-) create mode 100644 src/lib/components/ui/navbar/commands/actions.ts diff --git a/src/lib/components/ui/navbar/commands/CommandItem.svelte b/src/lib/components/ui/navbar/commands/CommandItem.svelte index 9eaca2d9..6ab9be36 100644 --- a/src/lib/components/ui/navbar/commands/CommandItem.svelte +++ b/src/lib/components/ui/navbar/commands/CommandItem.svelte @@ -19,12 +19,13 @@ alignment="left" color="tertiary" size="lg" - class="focus:bg-slate-200 focus:dark:bg-zinc-800" + class="focus:bg-slate-200 focus:dark:bg-zinc-800 block {$$props.class}" > {#if typeof action.icon === 'string'} {:else} {/if} + {action.name} diff --git a/src/lib/components/ui/navbar/commands/Commands.svelte b/src/lib/components/ui/navbar/commands/Commands.svelte index beb79e21..bd51754f 100644 --- a/src/lib/components/ui/navbar/commands/Commands.svelte +++ b/src/lib/components/ui/navbar/commands/Commands.svelte @@ -9,19 +9,25 @@ } from 'mono-svelte' import { createEventDispatcher, onMount } from 'svelte' import { + ArrowRightOnRectangle, Bookmark, + Cog6Tooth, Icon, + Identification, Inbox, + MagnifyingGlass, + Newspaper, PencilSquare, QuestionMarkCircle, UserCircle, + UserGroup, type IconSource, } from 'svelte-hero-icons' import Avatar from '../../Avatar.svelte' import { t } from '$lib/translations' import CommandItem from './CommandItem.svelte' import { browser } from '$app/environment' - import { goto } from '$app/navigation' + import { afterNavigate, goto } from '$app/navigation' export let open = false $: if (open) search = '' @@ -44,7 +50,7 @@ $: groups = [ { - name: 'Recents', + name: $t('nav.commands.recents'), actions: $resumables.map((r) => ({ name: r.name, icon: r.avatar ?? PencilSquare, @@ -69,6 +75,46 @@ name: $t('profile.saved'), icon: Bookmark, }, + { + href: '/accounts', + name: $t('account.accounts'), + icon: UserGroup, + }, + { + href: '/login', + name: $t('account.login'), + icon: ArrowRightOnRectangle, + }, + { + href: '/signup', + name: $t('account.signup'), + icon: Identification, + }, + ], + }, + { + name: $t('nav.menu.app'), + actions: [ + { + href: '/settings', + name: $t('nav.menu.settings'), + icon: Cog6Tooth, + }, + ], + }, + { + name: $t('nav.commands.content'), + actions: [ + { + href: '/create/post', + name: $t('routes.createPost'), + icon: PencilSquare, + }, + { + href: '/create/community', + name: $t('routes.createCommunity'), + icon: Newspaper, + }, ], }, ] @@ -78,6 +124,15 @@ const dispatch = createEventDispatcher() let selectedIndex = 0 + $: filteredGroups = groups + .map((group) => ({ + ...group, + actions: group.actions.filter((action) => + action.name.toLowerCase().includes(search.toLowerCase()), + ), + })) + .filter((group) => group.actions.length > 0) + $: flattenedActions = groups.flatMap((group) => group.actions.map((action) => ({ ...action, group: group.name })), ) @@ -87,7 +142,7 @@ ) function togglePalette() { - open = false + open = !open if (open) { search = '' selectedIndex = 0 @@ -97,7 +152,7 @@ function handleKeydown(event: KeyboardEvent) { if ((event.ctrlKey && event.key === 'k') || event.key == '/') { event.preventDefault() - open = true + togglePalette() } if (!open) return @@ -106,18 +161,16 @@ case 'ArrowDown': event.preventDefault() selectedIndex = (selectedIndex + 1) % filteredActions.length + focusItem(selectedIndex) break case 'ArrowUp': event.preventDefault() selectedIndex = (selectedIndex - 1 + filteredActions.length) % filteredActions.length + focusItem(selectedIndex) break case 'Enter': - event.preventDefault() - if (filteredActions[selectedIndex]) { - handleSelect(filteredActions[selectedIndex]) - dispatch('select', filteredActions[selectedIndex]) - } + handleSelect(filteredActions[selectedIndex]) break case 'Escape': event.preventDefault() @@ -136,32 +189,55 @@ async function handleSelect(action: Action & { group: string }) { if (action.href) await goto(action.href) if (action.handle) action.handle() + togglePalette() dispatch('select', action) } + + afterNavigate(() => { + open = false + }) - - -
- {#if search == ''} - {#each groups as group} - - {group.name} - -
    - {#each group.actions as action, index} - - {/each} -
+ + +
+
+ {#each filteredGroups as group, groupIndex} +
+ {group.name} +
    + {#each group.actions as action, actionIndex} + {@const globalIndex = + filteredGroups + .slice(0, groupIndex) + .reduce((sum, g) => sum + g.actions.length, 0) + actionIndex} +
  • + +
  • + {/each} +
+
{/each} - {:else} -
    - {#each filteredActions as action, index} - - {/each} -
- {/if} + + + {$t('nav.commands.search', { default: '' })} + + +
diff --git a/src/lib/components/ui/navbar/commands/actions.ts b/src/lib/components/ui/navbar/commands/actions.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/lib/i18n/en.json b/src/lib/i18n/en.json index c2aa36e1..359591ae 100644 --- a/src/lib/i18n/en.json +++ b/src/lib/i18n/en.json @@ -32,6 +32,12 @@ "source": "Source", "donate": "Donate", "instance": "Instance" + }, + "commands": { + "prompt": "Command Prompt", + "recents": "Recents", + "content": "Content", + "search": "Search for {{default}}" } }, "account": { diff --git a/src/routes/post/[instance]/[id=integer]/+page.svelte b/src/routes/post/[instance]/[id=integer]/+page.svelte index ca8e14ce..0e94994e 100644 --- a/src/routes/post/[instance]/[id=integer]/+page.svelte +++ b/src/routes/post/[instance]/[id=integer]/+page.svelte @@ -64,16 +64,17 @@ }) } - $resumables = [ - ...$resumables, - { - id: Math.floor(Math.random() * 1000000), - name: post.post_view.post.name, - type: 'post', - url: $page.url.toString(), - avatar: post.post_view.post.thumbnail_url, - }, - ] + if (!$resumables.find((r) => r.url == $page.url.toString())) + $resumables = [ + ...$resumables, + { + id: Math.floor(Math.random() * 1000000), + name: post.post_view.post.name, + type: 'post', + url: $page.url.toString(), + avatar: post.post_view.post.thumbnail_url, + }, + ] }) afterNavigate(async () => {