feat: add comment keybind navigation

This commit is contained in:
James Xylight committed 2025-08-07 21:21:23 -07:00
1 parent 0ca8f7474e
commit 7e2e7c513c
10 files changed
+152 -52

No files matched your search

@@ -25,6 +25,7 @@
import CommentForm from './CommentForm.svelte'
import type { CommentNodeI } from './comments.svelte'
import { page } from '$app/state'
import { keybindComment } from '$lib/ui/keybinds.svelte'
interface Props {
node: CommentNodeI
@@ -117,9 +118,15 @@
</Modal>
{/if}
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<li
class={['py-3 relative', clazz]}
class={['py-3 relative focus:outline-0', clazz]}
id={node.comment_view.comment.id.toString()}
data-type="comment"
onkeydown={e =>
keybindComment(e, node.comment_view.comment.id, () => (open = !open))}
tabindex="0"
>
{#if meta}
<button
@@ -29,6 +29,7 @@
replying?: boolean
disabled?: boolean
onedit?: (comment: CommentView) => void
voteComponent?: { castVote: (vote: number) => void }
}
let {
@@ -36,6 +37,7 @@
replying = $bindable(false),
disabled = false,
onedit,
voteComponent,
}: Props = $props()
</script>
@@ -48,6 +50,7 @@
downvotes={comment.counts.downvotes}
vote={comment.my_vote}
commentId={comment.comment.id}
bind:this={voteComponent}
/>
<Button
size="sm"
@@ -73,7 +76,7 @@
class="text-slate-600 dark:text-zinc-400"
>
{#snippet prefix()}
<Icon src={EllipsisHorizontal} width={16} height={16} mini />
<Icon src={EllipsisHorizontal} size="16" micro />
{/snippet}
</Button>
{/snippet}
@@ -4,6 +4,7 @@
import type { CommentNodeI } from './comments.svelte'
import Comments from './CommentTree.svelte'
import { onMount } from 'svelte'
import { keybindFeed } from '$lib/ui/keybinds.svelte'
interface Props {
nodes: CommentNodeI[]
@@ -13,8 +14,8 @@
onMount(() => {
if (scrollTo) {
const element = document?.getElementById(scrollTo)
setTimeout(() => {
const element = document?.getElementById(scrollTo)
element?.scrollIntoView({ behavior: 'smooth', block: 'start' })
}, 100)
}
@@ -38,6 +39,8 @@
let initialOffset = $derived(offsetEl?.offsetTop)
</script>
<svelte:body onkeydown={keybindFeed('li[data-type^="comment"]', 'start')} />
<div bind:this={offsetEl}>
{#if offsetEl}
<VirtualList
@@ -2,7 +2,7 @@
import { profile } from '$lib/auth.svelte.js'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import { t } from '$lib/i18n/translations'
import { getClient } from '$lib/lemmy.svelte'
import { client } from '$lib/lemmy.svelte'
import { buttonColor, toast } from 'mono-svelte'
import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons'
import { backOut } from 'svelte/easing'
@@ -24,18 +24,37 @@
commentId,
}: Props = $props()
const castVote = async (newVote: number) => {
export const castVote = async (newVote: number) => {
if (!profile.current?.jwt) {
toast({ content: $t('toast.loginVoteGate'), type: 'warning' })
return
}
switch (vote ?? 0) {
case 0:
// nothing was removed
if (newVote == 1) upvotes++
else downvotes++
break
case 1:
// removed an upvote
upvotes--
if (newVote == -1) downvotes++
break
case -1:
// removed a downvote
downvotes--
if (newVote == 1) upvotes++
break
}
vote = newVote
const res = await getClient().likeComment({
comment_id: commentId,
score: vote,
})
;({ upvotes, downvotes } = res.comment_view.counts)
client()
.likeComment({
comment_id: commentId,
score: vote,
})
.then(res => ({ upvotes, downvotes } = res.comment_view.counts))
}
let voteRatio = $derived(
+2 -2
View File
@@ -31,7 +31,7 @@
function onclick(e: Event) {
const event = e as Event
const parent = document.getElementById(`post-${post.post.id.toString()}`)
const parent = document.getElementById(`item-${post.post.id.toString()}`)
if (event.target == parent) {
goto(postLink(post.post))
@@ -99,7 +99,7 @@
view == 'cozy' && 'py-5 flex flex-col gap-2',
clazz,
]}
id="post-{post.post.id.toString()}"
id="item-{post.post.id.toString()}"
{onclick}
onkeydown={e => {
if (e.key === 'Enter') onclick(e)
+1 -1
View File
@@ -31,7 +31,7 @@
<svelte:body
onkeydown={e => {
if (e.key == 'Escape' && page.state.openImage != '') history.back()
if (e.key == 'Escape' && (page.state.openImage || '') != '') history.back()
}}
/>
@@ -1,5 +1,6 @@
<script lang="ts">
import { t } from '$lib/i18n/translations'
import { settings } from '$lib/settings.svelte'
import { Modal, Spinner } from 'mono-svelte'
interface Props {
@@ -10,8 +11,8 @@
function handleKeydown(event: KeyboardEvent) {
if (
(event.ctrlKey && event.key === 'p') ||
(event.key == '/' && document.activeElement == document.body)
event.key == settings.keybinds.commandPalette.open.key &&
document.activeElement == document.body
) {
event.preventDefault()
open = !open
+14 -11
View File
@@ -111,20 +111,21 @@ interface Settings {
voteRatioBar: boolean
keybinds: {
post: {
lists: {
next: Keybind
previous: Keybind
}
posts: {
upvote: Keybind
downvote: Keybind
save: Keybind
expand: Keybind
}
comments: {
next: Keybind
previous: Keybind
upvote: Keybind
downvote: Keybind
collapse: Keybind
}
commandPalette: {
open: Keybind
}
}
}
@@ -209,19 +210,21 @@ export const defaultSettings: Settings = {
},
voteRatioBar: false,
keybinds: {
post: {
lists: {
next: { ctrl: false, key: 'k' },
previous: { ctrl: false, key: 'j' },
},
posts: {
upvote: { ctrl: false, key: 'n' },
downvote: { ctrl: false, key: 'm' },
save: { ctrl: true, key: 's' },
expand: { ctrl: false, key: 'h' },
},
comments: {
next: { ctrl: false, key: 'k' },
previous: { ctrl: false, key: 'j' },
upvote: { ctrl: false, key: 'n' },
downvote: { ctrl: false, key: 'm' },
collapse: { ctrl: false, key: 'l' },
},
commandPalette: {
open: { ctrl: false, key: '/' },
},
},
}
+30 -11
View File
@@ -1,17 +1,20 @@
import { showImage } from '$lib/components/ui/ExpandableImage.svelte'
import { settings } from '$lib/settings.svelte'
import type { PostView } from 'lemmy-js-client'
import { isImage } from './image'
import { showImage } from '$lib/components/ui/ExpandableImage.svelte'
const binds = $derived(settings.keybinds)
export const keybindFeed = () => {
export const keybindFeed = (
selector: string = '[id^="item"]',
block: 'center' | 'start' = 'center',
) => {
return (event: KeyboardEvent) => {
console.log(event)
const direction =
event.key == binds.post.previous.key
event.key == binds.lists.previous.key
? 'up'
: event.key == binds.post.next.key
: event.key == binds.lists.next.key
? 'down'
: null
if (!direction) return
@@ -20,17 +23,18 @@ export const keybindFeed = () => {
document.activeElement?.closest('[data-index]') ??
document
.elementFromPoint(window.innerWidth / 2, window.innerHeight / 2)
?.closest('[data-index]')
?.closest('[data-index]') ??
document.querySelector('[data-index]')
if (!focusedPost) return
const sibling =
direction == 'up'
? focusedPost.previousSibling
? focusedPost.previousElementSibling
: focusedPost.nextElementSibling
if (sibling instanceof HTMLElement) {
const postDiv = sibling.querySelector('[id^="post"]')
const postDiv = sibling.querySelector(selector)
if (postDiv instanceof HTMLElement) {
postDiv.scrollIntoView({ behavior: 'smooth', block: 'center' })
postDiv.scrollIntoView({ behavior: 'smooth', block: block })
postDiv.focus({})
}
}
@@ -48,14 +52,29 @@ export const keybindPost = (
const event = e as KeyboardEvent
switch (event.key) {
case binds.post.upvote.key:
case binds.posts.upvote.key:
castVote?.(post.my_vote == 1 ? 0 : 1)
break
case binds.post.downvote.key:
case binds.posts.downvote.key:
castVote?.(post.my_vote == -1 ? 0 : -1)
break
case binds.post.expand.key:
case binds.posts.expand.key:
if (post.post.url && isImage(post.post.url)) showImage(post.post.url)
break
}
}
export const keybindComment = (
e: KeyboardEvent,
id: number,
toggle: () => void,
) => {
if ((e.target as HTMLDivElement).id == id.toString()) return
const event = e as KeyboardEvent
switch (event.key) {
case binds.comments.collapse.key:
toggle()
break
}
}