feat: add individual post keybinds

This commit is contained in:
James Xylight committed 2025-08-07 18:30:08 -07:00
1 parent 04bd862d59
commit 2342e89f3d
6 files changed
+111 -34

No files matched your search

+15 -5
View File
@@ -14,6 +14,7 @@
import type { PostView } from 'lemmy-js-client'
import type { ClassValue } from 'svelte/elements'
import PostBody from './PostBody.svelte'
import { keybindPost } from '$lib/ui/keybinds.svelte'
function getTagRule(tags: Tag[]): 'blur-sm' | 'hide' | undefined {
const tagContent = tags.map(t => t.content.toLowerCase())
@@ -28,7 +29,7 @@
return rule
}
function onClick(e: Event) {
function onclick(e: Event) {
const event = e as Event
const parent = document.getElementById(`post-${post.post.id.toString()}`)
@@ -79,6 +80,8 @@
admin: post.creator_is_admin,
moderator: post.creator_is_moderator,
})
let voteComponent = $state<{ castVote: (vote: number) => void }>()
</script>
<!--
@@ -97,12 +100,13 @@
clazz,
]}
id="post-{post.post.id.toString()}"
onclick={onClick}
onkeydown={e => {
if (e.key == 'Enter') onClick(e)
{onclick}
onkeydown={(e) => {
if (e.key === 'Enter') onclick(e)
}}
tabindex="0"
{style}
{@attach keybindPost(post, voteComponent?.castVote)}
>
<PostMeta
community={post.community}
@@ -156,7 +160,13 @@
/>
{/if}
{#if actions}
<PostActions {onhide} bind:post style="grid-area: actions;" {view} />
<PostActions
{onhide}
bind:voteComponent
bind:post
style="grid-area: actions;"
{view}
/>
{/if}
<div
class="absolute overflow-hidden inset-0
@@ -51,7 +51,7 @@
let loading = $state(false)
const castVote = async (newVote: number) => {
export const castVote = async (newVote: number) => {
if (!profile.current?.jwt) {
toast({ content: $t('toast.loginVoteGate'), type: 'warning' })
return
@@ -34,6 +34,7 @@
style?: string
onedit?: (post: PostView) => void
onhide?: (hide: boolean) => void
voteComponent?: { castVote: (vote: number) => void }
}
let {
@@ -41,6 +42,7 @@
view = 'cozy',
debug = $bindable(false),
style = '',
voteComponent = $bindable(),
onedit,
onhide,
}: Props = $props()
@@ -89,6 +91,7 @@
bind:score={post.counts.score}
bind:upvotes={post.counts.upvotes}
bind:downvotes={post.counts.downvotes}
bind:this={voteComponent}
showCounts={profile.current?.user?.local_user_view?.local_user
?.show_scores ?? true}
/>
@@ -12,6 +12,7 @@
type PostFeedID,
} from '$lib/lemmy/postfeed.svelte'
import { settings } from '$lib/settings.svelte.js'
import { keybindFeed } from '$lib/ui/keybinds.svelte'
import type { PostView } from 'lemmy-js-client'
import { Button } from 'mono-svelte'
import { onDestroy, onMount, untrack } from 'svelte'
@@ -168,35 +169,9 @@
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} />
<svelte:body {@attach keybindFeed} />
<ul class="flex flex-col list-none" bind:this={listEl}>
{#key posts}
+26 -1
View File
@@ -7,6 +7,10 @@ console.log('Using the following default settings from the environment:')
console.log(env)
export type View = 'cozy' | 'compact'
type Keybind = {
key: string
ctrl: boolean
}
export const SSR_ENABLED = env.PUBLIC_SSR_ENABLED?.toLowerCase() == 'true'
@@ -105,10 +109,21 @@ interface Settings {
fullMarkdown: boolean
}
voteRatioBar: boolean
keybinds: {
post: {
next: Keybind
previous: Keybind
upvote: Keybind
downvote: Keybind
save: Keybind
expand: Keybind
}
}
}
export const defaultSettings: Settings = {
settingsVer: 6,
settingsVer: 7,
expandableImages: toBool(env.PUBLIC_EXPANDABLE_IMAGES) ?? true,
markReadPosts: toBool(env.PUBLIC_MARK_READ_POSTS) ?? true,
showInstances: {
@@ -186,6 +201,16 @@ export const defaultSettings: Settings = {
fullMarkdown: toBool(env.PUBLIC_FULL_MARKDOWN) ?? false,
},
voteRatioBar: false,
keybinds: {
post: {
next: { ctrl: false, key: 'k' },
previous: { ctrl: false, key: 'j' },
upvote: { ctrl: false, key: 'n' },
downvote: { ctrl: false, key: 'm' },
save: { ctrl: true, key: 's' },
expand: { ctrl: false, key: 'h' },
},
},
}
function createSettingsState(initial: Settings): Settings {
+64
View File
@@ -0,0 +1,64 @@
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
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({})
}
}
})
}
}
export const keybindPost = (
post: PostView,
castVote?: (vote: number) => void,
): Attachment => {
return (element: Element) => {
if (element! instanceof HTMLButtonElement) return
element.addEventListener('keydown', e => {
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)
}
})
}
}