feat: add individual post keybinds
This commit is contained in:
6 files changed
+111
-34
No files matched your search
@@ -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}
|
||||
|
||||
Reference in new issue
Block a user