feat: add basic forward/backward feed keybinds
This commit is contained in:
3 files changed
+33
-8
No files matched your search
@@ -30,7 +30,7 @@
|
||||
|
||||
function onClick(e: Event) {
|
||||
const event = e as Event
|
||||
const parent = document.getElementById(post.post.id.toString())
|
||||
const parent = document.getElementById(`post-${post.post.id.toString()}`)
|
||||
|
||||
if (event.target == parent) {
|
||||
goto(postLink(post.post))
|
||||
@@ -96,10 +96,8 @@
|
||||
view == 'cozy' && 'py-5 flex flex-col gap-2',
|
||||
clazz,
|
||||
]}
|
||||
id={post.post.id.toString()}
|
||||
onclick={e => {
|
||||
onClick(e)
|
||||
}}
|
||||
id="post-{post.post.id.toString()}"
|
||||
onclick={onClick}
|
||||
onkeydown={e => {
|
||||
if (e.key == 'Enter') onClick(e)
|
||||
}}
|
||||
|
||||
@@ -77,7 +77,7 @@
|
||||
|
||||
<footer
|
||||
class={[
|
||||
'flex flex-row gap-2 items-center shrink-0 text-slate-600 dark:text-zinc-400',
|
||||
'flex flex-row gap-2 items-center shrink-0 text-slate-600 dark:text-zinc-300',
|
||||
buttonHeight,
|
||||
]}
|
||||
class:flex-row-reverse={settings.posts.reverseActions}
|
||||
|
||||
@@ -168,8 +168,36 @@
|
||||
onDestroy(() => {
|
||||
abortLoad?.abort()
|
||||
})
|
||||
|
||||
function onkeydown(
|
||||
event: KeyboardEvent & { currentTarget: EventTarget & HTMLElement },
|
||||
) {
|
||||
const direction = event.key == 'j' ? 'up' : event.key == 'k' ? 'down' : null
|
||||
if (!direction) return
|
||||
|
||||
const focusedPost =
|
||||
document.activeElement?.closest('[data-index]') ??
|
||||
document
|
||||
.elementFromPoint(window.innerWidth / 2, window.innerHeight / 2)
|
||||
?.closest('[data-index]')
|
||||
if (!focusedPost) return
|
||||
|
||||
const sibling =
|
||||
direction == 'up'
|
||||
? focusedPost.previousSibling
|
||||
: focusedPost.nextElementSibling
|
||||
if (sibling instanceof HTMLElement) {
|
||||
const postDiv = sibling.querySelector('[id^="post"]')
|
||||
if (postDiv instanceof HTMLElement) {
|
||||
postDiv.scrollIntoView({ behavior: 'smooth', block: 'center' })
|
||||
postDiv.focus({})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:body {onkeydown} />
|
||||
|
||||
<ul class="flex flex-col list-none" bind:this={listEl}>
|
||||
{#key posts}
|
||||
{#if posts?.length == 0}
|
||||
@@ -200,10 +228,9 @@
|
||||
>
|
||||
{#snippet item(row)}
|
||||
<li
|
||||
data-index={row}
|
||||
style={row < 7 ? `--anim-delay: ${row * 50}ms` : ''}
|
||||
class={[
|
||||
'relative post-container px-3 sm:px-6',
|
||||
'relative post-container px-3 sm:px-6 z-0',
|
||||
row < 7 && 'pop-in opacity-0',
|
||||
]}
|
||||
>
|
||||
|
||||
Reference in new issue
Block a user