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