diff --git a/src/lib/components/lemmy/post/Post.svelte b/src/lib/components/lemmy/post/Post.svelte index cbba5550..49f7ae76 100644 --- a/src/lib/components/lemmy/post/Post.svelte +++ b/src/lib/components/lemmy/post/Post.svelte @@ -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)} > - +
    {#key posts} diff --git a/src/lib/components/ui/Expandable.svelte b/src/lib/components/ui/Expandable.svelte index ce0323ca..cd8ac073 100644 --- a/src/lib/components/ui/Expandable.svelte +++ b/src/lib/components/ui/Expandable.svelte @@ -34,15 +34,15 @@ {@render title?.(open)} {#if icon} - + > + + {/if}
    history.back()} - onkeydown={e => { - if (e.key == 'Escape') history.back() - }} use:focusTrap > -
      +
        {#if items} - {#each items as item, index (item)} + {#each items as item, index}
      • :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); diff --git a/src/lib/i18n/en.json b/src/lib/i18n/en.json index 1d7ed7d2..14a82dfb 100644 --- a/src/lib/i18n/en.json +++ b/src/lib/i18n/en.json @@ -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": { diff --git a/src/lib/ui/keybinds.svelte.ts b/src/lib/ui/keybinds.svelte.ts index 33bc7924..565c5d52 100644 --- a/src/lib/ui/keybinds.svelte.ts +++ b/src/lib/ui/keybinds.svelte.ts @@ -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 } } diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelte index 94f7a1a7..2127813e 100644 --- a/src/routes/settings/+page.svelte +++ b/src/routes/settings/+page.svelte @@ -66,10 +66,13 @@ User, VideoCamera, ViewColumns, + ViewfinderCircle, } from 'svelte-hero-icons' import Section from './Section.svelte' import Setting from './Setting.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 importText = $state('') @@ -461,6 +464,29 @@ description={$t('settings.app.titleTags.description')} bind:checked={settings.parseTags} /> + + {#snippet title()} + {$t('settings.app.keybinds.title')} + {/snippet} + {#snippet description()} + {$t('settings.app.keybinds.description')} + {/snippet} +
        + + {#snippet title()} + {$t('content.posts')} + {/snippet} + + {#each Object.entries(settings.keybinds.post) as [key, value] (key)} +
      • +
        {key}
        + +
      • + {/each} + + +
    +
    diff --git a/src/routes/settings/Setting.svelte b/src/routes/settings/Setting.svelte index 11c34932..23d7ef6c 100644 --- a/src/routes/settings/Setting.svelte +++ b/src/routes/settings/Setting.svelte @@ -22,6 +22,7 @@ description?: import('svelte').Snippet children?: import('svelte').Snippet icon?: IconSource + adaptive?: boolean } let { @@ -37,6 +38,7 @@ description, children, icon, + adaptive = true, }: Props = $props() @@ -74,7 +76,7 @@ {/if} {/if} -
    +
    {#if icon}