feat: add customizing keybinds
This commit is contained in:
9 files changed
+113
-63
No files matched your search
@@ -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}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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}
|
||||
/>
|
||||
<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 id="embeds" title={$t('settings.embeds.title')}>
|
||||
|
||||
@@ -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()
|
||||
</script>
|
||||
|
||||
@@ -74,7 +76,7 @@
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
<div class={['setting-grid items-center', clazz]}>
|
||||
<div class={['setting-grid items-center', adaptive && 'adaptive', clazz]}>
|
||||
{#if icon}
|
||||
<Icon
|
||||
src={icon}
|
||||
@@ -124,7 +126,7 @@
|
||||
}
|
||||
|
||||
@media screen and (min-width: 768px) {
|
||||
.setting-grid {
|
||||
.setting-grid.adaptive {
|
||||
display: grid;
|
||||
grid-template-columns: auto 1fr auto;
|
||||
grid-template-rows: auto auto;
|
||||
|
||||
Reference in new issue
Block a user