feat: add customizing keybinds

This commit is contained in:
James Xylight committed 2025-08-07 19:11:51 -07:00
1 parent 2342e89f3d
commit e75cd00901
9 files changed
+113 -63

No files matched your search

+2 -2
View File
@@ -101,12 +101,12 @@
]}
id="post-{post.post.id.toString()}"
{onclick}
onkeydown={(e) => {
onkeydown={e => {
if (e.key === 'Enter') onclick(e)
keybindPost(e, post, voteComponent?.castVote)
}}
tabindex="0"
{style}
{@attach keybindPost(post, voteComponent?.castVote)}
>
<PostMeta
community={post.community}
@@ -171,7 +171,7 @@
})
</script>
<svelte:body {@attach keybindFeed} />
<svelte:body onkeydown={keybindFeed()} />
<ul class="flex flex-col list-none" bind:this={listEl}>
{#key posts}
+6 -6
View File
@@ -34,15 +34,15 @@
{@render title?.(open)}
</div>
{#if icon}
<Icon
src={open ? Minus : Plus}
size="16"
micro
<div
class={[
!open && 'rotate-90',
'transition-transform duration-300 ease-out',
'transition-all duration-300 ease-out',
'grid place-items-center w-4',
]}
/>
>
<Icon src={open ? Minus : Plus} size="16" micro />
</div>
{/if}
<div
class="inset-0 -z-10 opacity-0 absolute bg-slate-200 dark:bg-zinc-900 rounded-full
+7 -5
View File
@@ -29,16 +29,18 @@
let { alt = '', class: clazz = '', children }: Props = $props()
</script>
<svelte:body
onclick={() => history.back()}
onkeydown={e => {
if (e.key == 'Escape') history.back()
}}
/>
{#if page.state.openImage || '' != ''}
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
class="fixed top-0 left-0 w-screen h-svh overflow-auto bg-white/50 dark:bg-black/50
flex flex-col z-100 backdrop-blur-xs"
transition:fade={{ duration: 150 }}
onclick={() => history.back()}
onkeydown={e => {
if (e.key == 'Escape') history.back()
}}
use:focusTrap
>
<img
+20 -2
View File
@@ -13,6 +13,7 @@
children?: Snippet
animate?: boolean
size?: 'lg' | 'md' | 'sm'
depth?: 1 | 2
}
let {
@@ -21,12 +22,13 @@
children,
animate = true,
size = 'sm',
depth = 1,
}: Props = $props()
</script>
<ul class="divide-y-3 divide-slate-50 dark:divide-zinc-925">
<ul class={['space-y-[3px]', depth == 2 && 'secondary']}>
{#if items}
{#each items as item, index (item)}
{#each items as item, index}
<li
class={['group/li', animate && 'animate']}
style="--i: {index < 10 ? index * sizeDelay[size] : 0}ms;"
@@ -77,6 +79,22 @@
}
}
ul.secondary > :global(li) {
background-color: var(--color-slate-25);
&:hover {
background-color: var(--color-slate-50);
}
}
:global(.dark) ul.secondary > :global(li) {
background-color: var(--color-zinc-925);
&:hover {
background-color: var(--color-zinc-800);
}
}
@keyframes pop-in {
from {
transform: translateY(16px);
+5
View File
@@ -833,6 +833,11 @@
"voteRatioBar": {
"title": "Vote ratio bar",
"description": "When the controversiality of a post reaches greater than 15%, a bar will be displayed in the vote buttons to help visualize sentiment."
},
"keybinds": {
"title": "Keybinds",
"description": "Customize keybinds for certain actions within the app.",
"key": "Key"
}
},
"embeds": {
+42 -45
View File
@@ -1,64 +1,61 @@
import { settings } from '$lib/settings.svelte'
import type { PostView } from 'lemmy-js-client'
import type { Attachment } from 'svelte/attachments'
import { isImage } from './image'
import { showImage } from '$lib/components/ui/ExpandableImage.svelte'
const binds = $derived(settings.keybinds)
export const keybindFeed = (): Attachment => {
return () => {
window.addEventListener('keydown', event => {
console.log(event)
const direction =
event.key == binds.post.previous.key
? 'up'
: event.key == binds.post.next.key
? 'down'
: null
if (!direction) return
export const keybindFeed = () => {
return (event: KeyboardEvent) => {
console.log(event)
const direction =
event.key == binds.post.previous.key
? 'up'
: event.key == binds.post.next.key
? '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 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({})
}
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({})
}
})
}
}
}
export const keybindPost = (
e: KeyboardEvent,
post: PostView,
castVote?: (vote: number) => void,
): Attachment => {
return (element: Element) => {
if (element! instanceof HTMLButtonElement) return
element.addEventListener('keydown', e => {
const event = e as KeyboardEvent
) => {
if ((e.target as HTMLDivElement).id.split('-')[1] != post.post.id.toString())
return
switch (event.key) {
case binds.post.upvote.key:
castVote?.(post.my_vote == 1 ? 0 : 1)
break
case binds.post.downvote.key:
castVote?.(post.my_vote == -1 ? 0 : -1)
break
case binds.post.expand.key:
if (post.post.url && isImage(post.post.url)) showImage(post.post.url)
}
})
const event = e as KeyboardEvent
switch (event.key) {
case binds.post.upvote.key:
castVote?.(post.my_vote == 1 ? 0 : 1)
break
case binds.post.downvote.key:
castVote?.(post.my_vote == -1 ? 0 : -1)
break
case binds.post.expand.key:
if (post.post.url && isImage(post.post.url)) showImage(post.post.url)
break
}
}