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