From 7e2e7c513c727be042b27858fe471203407beaab Mon Sep 17 00:00:00 2001 From: James Xylight Date: Thu, 7 Aug 2025 21:21:23 -0700 Subject: [PATCH] feat: add comment keybind navigation --- .../components/lemmy/comment/Comment.svelte | 9 ++- .../lemmy/comment/CommentActions.svelte | 5 +- .../comment/CommentListVirtualizer.svelte | 5 +- .../lemmy/comment/CommentVote.svelte | 33 ++++++-- src/lib/components/lemmy/post/Post.svelte | 4 +- src/lib/components/ui/ExpandableImage.svelte | 2 +- .../ui/navbar/commands/CommandsWrapper.svelte | 5 +- src/lib/settings.svelte.ts | 25 ++++--- src/lib/ui/keybinds.svelte.ts | 41 +++++++--- src/routes/settings/+page.svelte | 75 +++++++++++++++---- 10 files changed, 152 insertions(+), 52 deletions(-) diff --git a/src/lib/components/lemmy/comment/Comment.svelte b/src/lib/components/lemmy/comment/Comment.svelte index 932573d9..2afca5ff 100644 --- a/src/lib/components/lemmy/comment/Comment.svelte +++ b/src/lib/components/lemmy/comment/Comment.svelte @@ -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 @@ {/if} + +
  • + keybindComment(e, node.comment_view.comment.id, () => (open = !open))} + tabindex="0" > {#if meta} {/snippet} diff --git a/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte b/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte index 676951dd..6fd0d78b 100644 --- a/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte +++ b/src/lib/components/lemmy/comment/CommentListVirtualizer.svelte @@ -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) + +
    {#if offsetEl} { + 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( diff --git a/src/lib/components/lemmy/post/Post.svelte b/src/lib/components/lemmy/post/Post.svelte index 49f7ae76..edb0ace3 100644 --- a/src/lib/components/lemmy/post/Post.svelte +++ b/src/lib/components/lemmy/post/Post.svelte @@ -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) diff --git a/src/lib/components/ui/ExpandableImage.svelte b/src/lib/components/ui/ExpandableImage.svelte index e3c23d90..8c83ef1c 100644 --- a/src/lib/components/ui/ExpandableImage.svelte +++ b/src/lib/components/ui/ExpandableImage.svelte @@ -31,7 +31,7 @@ { - if (e.key == 'Escape' && page.state.openImage != '') history.back() + if (e.key == 'Escape' && (page.state.openImage || '') != '') history.back() }} /> diff --git a/src/lib/components/ui/navbar/commands/CommandsWrapper.svelte b/src/lib/components/ui/navbar/commands/CommandsWrapper.svelte index 934fc090..19eda457 100644 --- a/src/lib/components/ui/navbar/commands/CommandsWrapper.svelte +++ b/src/lib/components/ui/navbar/commands/CommandsWrapper.svelte @@ -1,5 +1,6 @@