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()}" id="post-{post.post.id.toString()}"
{onclick} {onclick}
onkeydown={(e) => { onkeydown={e => {
if (e.key === 'Enter') onclick(e) if (e.key === 'Enter') onclick(e)
keybindPost(e, post, voteComponent?.castVote)
}} }}
tabindex="0" tabindex="0"
{style} {style}
{@attach keybindPost(post, voteComponent?.castVote)}
> >
<PostMeta <PostMeta
community={post.community} community={post.community}
@@ -171,7 +171,7 @@
}) })
</script> </script>
<svelte:body {@attach keybindFeed} /> <svelte:body onkeydown={keybindFeed()} />
<ul class="flex flex-col list-none" bind:this={listEl}> <ul class="flex flex-col list-none" bind:this={listEl}>
{#key posts} {#key posts}
+6 -6
View File
@@ -34,15 +34,15 @@
{@render title?.(open)} {@render title?.(open)}
</div> </div>
{#if icon} {#if icon}
<Icon <div
src={open ? Minus : Plus}
size="16"
micro
class={[ class={[
!open && 'rotate-90', !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} {/if}
<div <div
class="inset-0 -z-10 opacity-0 absolute bg-slate-200 dark:bg-zinc-900 rounded-full 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() let { alt = '', class: clazz = '', children }: Props = $props()
</script> </script>
<svelte:body
onclick={() => history.back()}
onkeydown={e => {
if (e.key == 'Escape') history.back()
}}
/>
{#if page.state.openImage || '' != ''} {#if page.state.openImage || '' != ''}
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div <div
class="fixed top-0 left-0 w-screen h-svh overflow-auto bg-white/50 dark:bg-black/50 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" flex flex-col z-100 backdrop-blur-xs"
transition:fade={{ duration: 150 }} transition:fade={{ duration: 150 }}
onclick={() => history.back()}
onkeydown={e => {
if (e.key == 'Escape') history.back()
}}
use:focusTrap use:focusTrap
> >
<img <img
+20 -2
View File
@@ -13,6 +13,7 @@
children?: Snippet children?: Snippet
animate?: boolean animate?: boolean
size?: 'lg' | 'md' | 'sm' size?: 'lg' | 'md' | 'sm'
depth?: 1 | 2
} }
let { let {
@@ -21,12 +22,13 @@
children, children,
animate = true, animate = true,
size = 'sm', size = 'sm',
depth = 1,
}: Props = $props() }: Props = $props()
</script> </script>
<ul class="divide-y-3 divide-slate-50 dark:divide-zinc-925"> <ul class={['space-y-[3px]', depth == 2 && 'secondary']}>
{#if items} {#if items}
{#each items as item, index (item)} {#each items as item, index}
<li <li
class={['group/li', animate && 'animate']} class={['group/li', animate && 'animate']}
style="--i: {index < 10 ? index * sizeDelay[size] : 0}ms;" 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 { @keyframes pop-in {
from { from {
transform: translateY(16px); transform: translateY(16px);
+5
View File
@@ -833,6 +833,11 @@
"voteRatioBar": { "voteRatioBar": {
"title": "Vote ratio bar", "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." "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": { "embeds": {
+42 -45
View File
@@ -1,64 +1,61 @@
import { settings } from '$lib/settings.svelte' import { settings } from '$lib/settings.svelte'
import type { PostView } from 'lemmy-js-client' import type { PostView } from 'lemmy-js-client'
import type { Attachment } from 'svelte/attachments'
import { isImage } from './image' import { isImage } from './image'
import { showImage } from '$lib/components/ui/ExpandableImage.svelte' import { showImage } from '$lib/components/ui/ExpandableImage.svelte'
const binds = $derived(settings.keybinds) const binds = $derived(settings.keybinds)
export const keybindFeed = (): Attachment => { export const keybindFeed = () => {
return () => { return (event: KeyboardEvent) => {
window.addEventListener('keydown', event => { console.log(event)
console.log(event) const direction =
const direction = event.key == binds.post.previous.key
event.key == binds.post.previous.key ? 'up'
? 'up' : event.key == binds.post.next.key
: event.key == binds.post.next.key ? 'down'
? 'down' : null
: null if (!direction) return
if (!direction) return
const focusedPost = const focusedPost =
document.activeElement?.closest('[data-index]') ?? document.activeElement?.closest('[data-index]') ??
document document
.elementFromPoint(window.innerWidth / 2, window.innerHeight / 2) .elementFromPoint(window.innerWidth / 2, window.innerHeight / 2)
?.closest('[data-index]') ?.closest('[data-index]')
if (!focusedPost) return if (!focusedPost) return
const sibling = const sibling =
direction == 'up' direction == 'up'
? focusedPost.previousSibling ? focusedPost.previousSibling
: focusedPost.nextElementSibling : focusedPost.nextElementSibling
if (sibling instanceof HTMLElement) { if (sibling instanceof HTMLElement) {
const postDiv = sibling.querySelector('[id^="post"]') const postDiv = sibling.querySelector('[id^="post"]')
if (postDiv instanceof HTMLElement) { if (postDiv instanceof HTMLElement) {
postDiv.scrollIntoView({ behavior: 'smooth', block: 'center' }) postDiv.scrollIntoView({ behavior: 'smooth', block: 'center' })
postDiv.focus({}) postDiv.focus({})
}
} }
}) }
} }
} }
export const keybindPost = ( export const keybindPost = (
e: KeyboardEvent,
post: PostView, post: PostView,
castVote?: (vote: number) => void, castVote?: (vote: number) => void,
): Attachment => { ) => {
return (element: Element) => { if ((e.target as HTMLDivElement).id.split('-')[1] != post.post.id.toString())
if (element! instanceof HTMLButtonElement) return return
element.addEventListener('keydown', e => {
const event = e as KeyboardEvent
switch (event.key) { const event = e as KeyboardEvent
case binds.post.upvote.key:
castVote?.(post.my_vote == 1 ? 0 : 1) switch (event.key) {
break case binds.post.upvote.key:
case binds.post.downvote.key: castVote?.(post.my_vote == 1 ? 0 : 1)
castVote?.(post.my_vote == -1 ? 0 : -1) break
break case binds.post.downvote.key:
case binds.post.expand.key: castVote?.(post.my_vote == -1 ? 0 : -1)
if (post.post.url && isImage(post.post.url)) showImage(post.post.url) break
} case binds.post.expand.key:
}) if (post.post.url && isImage(post.post.url)) showImage(post.post.url)
break
} }
} }
+26
View File
@@ -66,10 +66,13 @@
User, User,
VideoCamera, VideoCamera,
ViewColumns, ViewColumns,
ViewfinderCircle,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import Section from './Section.svelte' import Section from './Section.svelte'
import Setting from './Setting.svelte' import Setting from './Setting.svelte'
import ToggleSetting from './ToggleSetting.svelte' import ToggleSetting from './ToggleSetting.svelte'
import Expandable from '$lib/components/ui/Expandable.svelte'
import CommonList from '$lib/components/ui/layout/CommonList.svelte'
let importing = $state(false) let importing = $state(false)
let importText = $state('') let importText = $state('')
@@ -461,6 +464,29 @@
description={$t('settings.app.titleTags.description')} description={$t('settings.app.titleTags.description')}
bind:checked={settings.parseTags} bind:checked={settings.parseTags}
/> />
<Setting icon={ViewfinderCircle} adaptive={false}>
{#snippet title()}
{$t('settings.app.keybinds.title')}
{/snippet}
{#snippet description()}
{$t('settings.app.keybinds.description')}
{/snippet}
<div class="flex flex-col my-2">
<Expandable>
{#snippet title()}
{$t('content.posts')}
{/snippet}
<CommonList depth={2}>
{#each Object.entries(settings.keybinds.post) as [key, value] (key)}
<li class="flex items-center justify-between gap-2 secondary">
<div class="capitalize font-medium">{key}</div>
<TextInput bind:value={value.key} size="sm" class="w-24" />
</li>
{/each}
</CommonList>
</Expandable>
</div>
</Setting>
</Section> </Section>
<Section id="embeds" title={$t('settings.embeds.title')}> <Section id="embeds" title={$t('settings.embeds.title')}>
+4 -2
View File
@@ -22,6 +22,7 @@
description?: import('svelte').Snippet description?: import('svelte').Snippet
children?: import('svelte').Snippet children?: import('svelte').Snippet
icon?: IconSource icon?: IconSource
adaptive?: boolean
} }
let { let {
@@ -37,6 +38,7 @@
description, description,
children, children,
icon, icon,
adaptive = true,
}: Props = $props() }: Props = $props()
</script> </script>
@@ -74,7 +76,7 @@
{/if} {/if}
</div> </div>
{/if} {/if}
<div class={['setting-grid items-center', clazz]}> <div class={['setting-grid items-center', adaptive && 'adaptive', clazz]}>
{#if icon} {#if icon}
<Icon <Icon
src={icon} src={icon}
@@ -124,7 +126,7 @@
} }
@media screen and (min-width: 768px) { @media screen and (min-width: 768px) {
.setting-grid { .setting-grid.adaptive {
display: grid; display: grid;
grid-template-columns: auto 1fr auto; grid-template-columns: auto 1fr auto;
grid-template-rows: auto auto; grid-template-rows: auto auto;