misc: clean up actions
This commit is contained in:
1 parent
f38011e0c6
commit
c0b93f65a9
5 files changed
+112
-28
No files matched your search
@@ -19,12 +19,13 @@
|
|||||||
alignment="left"
|
alignment="left"
|
||||||
color="tertiary"
|
color="tertiary"
|
||||||
size="lg"
|
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'}
|
{#if typeof action.icon === 'string'}
|
||||||
<Avatar url={action.icon} width={20} />
|
<Avatar url={action.icon} width={20} />
|
||||||
{:else}
|
{:else}
|
||||||
<Icon src={action.icon ?? QuestionMarkCircle} size="20" />
|
<Icon src={action.icon ?? QuestionMarkCircle} size="20" />
|
||||||
{/if}
|
{/if}
|
||||||
|
<slot />
|
||||||
{action.name}
|
{action.name}
|
||||||
</Button>
|
</Button>
|
||||||
@@ -9,19 +9,25 @@
|
|||||||
} from 'mono-svelte'
|
} from 'mono-svelte'
|
||||||
import { createEventDispatcher, onMount } from 'svelte'
|
import { createEventDispatcher, onMount } from 'svelte'
|
||||||
import {
|
import {
|
||||||
|
ArrowRightOnRectangle,
|
||||||
Bookmark,
|
Bookmark,
|
||||||
|
Cog6Tooth,
|
||||||
Icon,
|
Icon,
|
||||||
|
Identification,
|
||||||
Inbox,
|
Inbox,
|
||||||
|
MagnifyingGlass,
|
||||||
|
Newspaper,
|
||||||
PencilSquare,
|
PencilSquare,
|
||||||
QuestionMarkCircle,
|
QuestionMarkCircle,
|
||||||
UserCircle,
|
UserCircle,
|
||||||
|
UserGroup,
|
||||||
type IconSource,
|
type IconSource,
|
||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
import Avatar from '../../Avatar.svelte'
|
import Avatar from '../../Avatar.svelte'
|
||||||
import { t } from '$lib/translations'
|
import { t } from '$lib/translations'
|
||||||
import CommandItem from './CommandItem.svelte'
|
import CommandItem from './CommandItem.svelte'
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { goto } from '$app/navigation'
|
import { afterNavigate, goto } from '$app/navigation'
|
||||||
|
|
||||||
export let open = false
|
export let open = false
|
||||||
$: if (open) search = ''
|
$: if (open) search = ''
|
||||||
@@ -44,7 +50,7 @@
|
|||||||
|
|
||||||
$: groups = [
|
$: groups = [
|
||||||
{
|
{
|
||||||
name: 'Recents',
|
name: $t('nav.commands.recents'),
|
||||||
actions: $resumables.map((r) => ({
|
actions: $resumables.map((r) => ({
|
||||||
name: r.name,
|
name: r.name,
|
||||||
icon: r.avatar ?? PencilSquare,
|
icon: r.avatar ?? PencilSquare,
|
||||||
@@ -69,6 +75,46 @@
|
|||||||
name: $t('profile.saved'),
|
name: $t('profile.saved'),
|
||||||
icon: Bookmark,
|
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()
|
const dispatch = createEventDispatcher()
|
||||||
let selectedIndex = 0
|
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) =>
|
$: flattenedActions = groups.flatMap((group) =>
|
||||||
group.actions.map((action) => ({ ...action, group: group.name })),
|
group.actions.map((action) => ({ ...action, group: group.name })),
|
||||||
)
|
)
|
||||||
@@ -87,7 +142,7 @@
|
|||||||
)
|
)
|
||||||
|
|
||||||
function togglePalette() {
|
function togglePalette() {
|
||||||
open = false
|
open = !open
|
||||||
if (open) {
|
if (open) {
|
||||||
search = ''
|
search = ''
|
||||||
selectedIndex = 0
|
selectedIndex = 0
|
||||||
@@ -97,7 +152,7 @@
|
|||||||
function handleKeydown(event: KeyboardEvent) {
|
function handleKeydown(event: KeyboardEvent) {
|
||||||
if ((event.ctrlKey && event.key === 'k') || event.key == '/') {
|
if ((event.ctrlKey && event.key === 'k') || event.key == '/') {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
open = true
|
togglePalette()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!open) return
|
if (!open) return
|
||||||
@@ -106,18 +161,16 @@
|
|||||||
case 'ArrowDown':
|
case 'ArrowDown':
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
selectedIndex = (selectedIndex + 1) % filteredActions.length
|
selectedIndex = (selectedIndex + 1) % filteredActions.length
|
||||||
|
focusItem(selectedIndex)
|
||||||
break
|
break
|
||||||
case 'ArrowUp':
|
case 'ArrowUp':
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
selectedIndex =
|
selectedIndex =
|
||||||
(selectedIndex - 1 + filteredActions.length) % filteredActions.length
|
(selectedIndex - 1 + filteredActions.length) % filteredActions.length
|
||||||
|
focusItem(selectedIndex)
|
||||||
break
|
break
|
||||||
case 'Enter':
|
case 'Enter':
|
||||||
event.preventDefault()
|
|
||||||
if (filteredActions[selectedIndex]) {
|
|
||||||
handleSelect(filteredActions[selectedIndex])
|
handleSelect(filteredActions[selectedIndex])
|
||||||
dispatch('select', filteredActions[selectedIndex])
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
case 'Escape':
|
case 'Escape':
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
@@ -136,32 +189,55 @@
|
|||||||
async function handleSelect(action: Action & { group: string }) {
|
async function handleSelect(action: Action & { group: string }) {
|
||||||
if (action.href) await goto(action.href)
|
if (action.href) await goto(action.href)
|
||||||
if (action.handle) action.handle()
|
if (action.handle) action.handle()
|
||||||
|
togglePalette()
|
||||||
dispatch('select', action)
|
dispatch('select', action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
afterNavigate(() => {
|
||||||
|
open = false
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:window on:keydown={handleKeydown} />
|
<svelte:window on:keydown={handleKeydown} />
|
||||||
|
|
||||||
<Modal title="Command Prompt" bind:open>
|
<Modal title={$t('nav.commands.prompt')} bind:open>
|
||||||
<TextInput bind:value={search} autofocus />
|
<TextInput bind:value={search} autofocus class="sticky" />
|
||||||
|
<div
|
||||||
|
class="h-96 overflow-auto border-t border-slate-200 dark:border-zinc-800"
|
||||||
|
>
|
||||||
<div class="space-y-1" bind:this={container}>
|
<div class="space-y-1" bind:this={container}>
|
||||||
{#if search == ''}
|
{#each filteredGroups as group, groupIndex}
|
||||||
{#each groups as group}
|
<div class="space-y-1">
|
||||||
<span class="text-slate-700 dark:text-zinc-300 font-medium text-sm">
|
<span class="text-sm font-medium">{group.name}</span>
|
||||||
{group.name}
|
<ul class="flex flex-col gap-1">
|
||||||
|
{#each group.actions as action, actionIndex}
|
||||||
|
{@const globalIndex =
|
||||||
|
filteredGroups
|
||||||
|
.slice(0, groupIndex)
|
||||||
|
.reduce((sum, g) => sum + g.actions.length, 0) + actionIndex}
|
||||||
|
<li>
|
||||||
|
<CommandItem
|
||||||
|
{action}
|
||||||
|
class="{globalIndex == selectedIndex
|
||||||
|
? 'bg-slate-200 dark:bg-zinc-800 text-inherit'
|
||||||
|
: 'text-slate-600 dark:text-zinc-400'} block"
|
||||||
|
/>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
<CommandItem
|
||||||
|
action={{
|
||||||
|
name: search,
|
||||||
|
href: `/search?q=${encodeURIComponent(search)}`,
|
||||||
|
icon: MagnifyingGlass,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span class="font-normal text-slate-600 dark:text-zinc-400">
|
||||||
|
{$t('nav.commands.search', { default: '' })}
|
||||||
</span>
|
</span>
|
||||||
<ul class="flex flex-col gap-1">
|
</CommandItem>
|
||||||
{#each group.actions as action, index}
|
</div>
|
||||||
<CommandItem {action} />
|
|
||||||
{/each}
|
|
||||||
</ul>
|
|
||||||
{/each}
|
|
||||||
{:else}
|
|
||||||
<ul class="flex flex-col gap-1">
|
|
||||||
{#each filteredActions as action, index}
|
|
||||||
<CommandItem {action} />
|
|
||||||
{/each}
|
|
||||||
</ul>
|
|
||||||
{/if}
|
|
||||||
</div>
|
</div>
|
||||||
</Modal>
|
</Modal>
|
||||||
Whitespace-only changes.
@@ -32,6 +32,12 @@
|
|||||||
"source": "Source",
|
"source": "Source",
|
||||||
"donate": "Donate",
|
"donate": "Donate",
|
||||||
"instance": "Instance"
|
"instance": "Instance"
|
||||||
|
},
|
||||||
|
"commands": {
|
||||||
|
"prompt": "Command Prompt",
|
||||||
|
"recents": "Recents",
|
||||||
|
"content": "Content",
|
||||||
|
"search": "Search for {{default}}"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"account": {
|
"account": {
|
||||||
|
|||||||
@@ -64,6 +64,7 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!$resumables.find((r) => r.url == $page.url.toString()))
|
||||||
$resumables = [
|
$resumables = [
|
||||||
...$resumables,
|
...$resumables,
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user