feat: basic command prompt support

This commit is contained in:
Xylight committed 2024-07-19 15:11:37 -07:00
1 parent 0179859427
commit 59bb6dde89
4 files changed
+181 -1

No files matched your search

@@ -38,10 +38,12 @@
import { LINKED_INSTANCE_URL } from '$lib/instance'
import SiteCard from '$lib/components/lemmy/SiteCard.svelte'
import { t } from '$lib/translations'
import Commands from './commands/Commands.svelte'
let searching = false
</script>
<Commands />
<nav
class="flex flex-row gap-2 items-center w-full mx-auto z-50 box-border p-0.5
duration-150
@@ -0,0 +1,164 @@
<script lang="ts">
import { resumables } from '$lib/lemmy/item'
import {
Button,
buttonColor,
buttonSize,
Modal,
TextInput,
} from 'mono-svelte'
import { createEventDispatcher, onMount } from 'svelte'
import {
Icon,
PencilSquare,
QuestionMarkCircle,
type IconSource,
} from 'svelte-hero-icons'
import Avatar from '../../Avatar.svelte'
let open = true
$: if (open) search = ''
onMount(() => {
window.addEventListener('keydown', (e) => {
if (e.key == '/') {
e.preventDefault()
open = !open
}
})
})
interface Group {
name: string
actions: Action[]
}
interface Action {
name: string
desc?: string
handle?: () => any
href?: string
shortcut?: string
icon: string | IconSource
}
export let groups: Group[] = []
$: groups = [
{
name: 'Recents',
actions: $resumables.map((r) => ({
name: r.name,
icon: r.avatar ?? PencilSquare,
href: r.url,
})),
},
]
let search = ''
const dispatch = createEventDispatcher()
let selectedIndex = 0
$: flattenedActions = groups.flatMap((group) =>
group.actions.map((action) => ({ ...action, group: group.name })),
)
$: filteredActions = flattenedActions.filter(
(action) =>
action.name.toLowerCase().includes(search.toLowerCase()) ||
action.group.toLowerCase().includes(search.toLowerCase()),
)
function togglePalette() {
open = !open
if (open) {
search = ''
selectedIndex = 0
}
}
function handleKeydown(event: KeyboardEvent) {
if (event.ctrlKey && event.key === 'k') {
event.preventDefault()
togglePalette()
}
if (!open) return
switch (event.key) {
case 'ArrowDown':
event.preventDefault()
selectedIndex = (selectedIndex + 1) % filteredActions.length
break
case 'ArrowUp':
event.preventDefault()
selectedIndex =
(selectedIndex - 1 + filteredActions.length) % filteredActions.length
break
case 'Enter':
event.preventDefault()
if (filteredActions[selectedIndex]) {
dispatch('select', filteredActions[selectedIndex])
togglePalette()
}
break
case 'Escape':
event.preventDefault()
togglePalette()
break
}
}
function handleSelect(action: Action & { group: string }) {
dispatch('select', action)
togglePalette()
}
</script>
<Modal title="Command Prompt" bind:open>
<TextInput bind:value={search} autofocus />
{#if search == ''}
{#each groups as group}
<div class="space-y-1">
<span class="text-slate-700 dark:text-zinc-300 font-medium text-sm">
{group.name}
</span>
<ul class="flex flex-col">
{#each group.actions as action}
<Button
href={action.href}
on:click={action.handle}
alignment="left"
color="tertiary"
size="lg"
>
{#if typeof action.icon === 'string'}
<Avatar url={action.icon} width={24} />
{:else}
<Icon src={action.icon ?? QuestionMarkCircle} size="24" mini />
{/if}
{action.name}
</Button>
{/each}
</ul>
</div>
{/each}
{:else}
{#each filteredActions as action}
<Button
href={action.href}
on:click={action.handle}
alignment="left"
color="tertiary"
size="lg"
>
{#if typeof action.icon === 'string'}
<Avatar url={action.icon} width={24} />
{:else}
<Icon src={action.icon ?? QuestionMarkCircle} size="24" mini />
{/if}
{action.name}
</Button>
{/each}
{/if}
</Modal>
+3 -1
View File
@@ -12,7 +12,7 @@ import type {
PrivateMessageView,
Community,
} from 'lemmy-js-client'
import { get } from 'svelte/store'
import { get, writable } from 'svelte/store'
export type Result =
| PostView
@@ -82,6 +82,8 @@ export interface ResumableItem {
}
}
export let resumables = writable<ResumableItem[]>([])
export function addFavorite(item: Community, add: boolean = true) {
const pd = get(profileData)
const p = pd.profiles.find((p) => p.id == get(profile)?.id)
@@ -43,6 +43,7 @@
import { Popover } from 'mono-svelte'
import { t } from '$lib/translations.js'
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
import { resumables } from '$lib/lemmy/item.js'
export let data
@@ -62,6 +63,17 @@
post_id: post.post_view.post.id,
})
}
$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 () => {