feat: add comment keybind navigation
This commit is contained in:
10 files changed
+139
-39
No files matched your search
@@ -25,6 +25,7 @@
|
|||||||
import CommentForm from './CommentForm.svelte'
|
import CommentForm from './CommentForm.svelte'
|
||||||
import type { CommentNodeI } from './comments.svelte'
|
import type { CommentNodeI } from './comments.svelte'
|
||||||
import { page } from '$app/state'
|
import { page } from '$app/state'
|
||||||
|
import { keybindComment } from '$lib/ui/keybinds.svelte'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
node: CommentNodeI
|
node: CommentNodeI
|
||||||
@@ -117,9 +118,15 @@
|
|||||||
</Modal>
|
</Modal>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
|
||||||
|
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
|
||||||
<li
|
<li
|
||||||
class={['py-3 relative', clazz]}
|
class={['py-3 relative focus:outline-0', clazz]}
|
||||||
id={node.comment_view.comment.id.toString()}
|
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}
|
{#if meta}
|
||||||
<button
|
<button
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
replying?: boolean
|
replying?: boolean
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
onedit?: (comment: CommentView) => void
|
onedit?: (comment: CommentView) => void
|
||||||
|
voteComponent?: { castVote: (vote: number) => void }
|
||||||
}
|
}
|
||||||
|
|
||||||
let {
|
let {
|
||||||
@@ -36,6 +37,7 @@
|
|||||||
replying = $bindable(false),
|
replying = $bindable(false),
|
||||||
disabled = false,
|
disabled = false,
|
||||||
onedit,
|
onedit,
|
||||||
|
voteComponent,
|
||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -48,6 +50,7 @@
|
|||||||
downvotes={comment.counts.downvotes}
|
downvotes={comment.counts.downvotes}
|
||||||
vote={comment.my_vote}
|
vote={comment.my_vote}
|
||||||
commentId={comment.comment.id}
|
commentId={comment.comment.id}
|
||||||
|
bind:this={voteComponent}
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
@@ -73,7 +76,7 @@
|
|||||||
class="text-slate-600 dark:text-zinc-400"
|
class="text-slate-600 dark:text-zinc-400"
|
||||||
>
|
>
|
||||||
{#snippet prefix()}
|
{#snippet prefix()}
|
||||||
<Icon src={EllipsisHorizontal} width={16} height={16} mini />
|
<Icon src={EllipsisHorizontal} size="16" micro />
|
||||||
{/snippet}
|
{/snippet}
|
||||||
</Button>
|
</Button>
|
||||||
{/snippet}
|
{/snippet}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
import type { CommentNodeI } from './comments.svelte'
|
import type { CommentNodeI } from './comments.svelte'
|
||||||
import Comments from './CommentTree.svelte'
|
import Comments from './CommentTree.svelte'
|
||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
|
import { keybindFeed } from '$lib/ui/keybinds.svelte'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
nodes: CommentNodeI[]
|
nodes: CommentNodeI[]
|
||||||
@@ -13,8 +14,8 @@
|
|||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
if (scrollTo) {
|
if (scrollTo) {
|
||||||
const element = document?.getElementById(scrollTo)
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
const element = document?.getElementById(scrollTo)
|
||||||
element?.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
element?.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
||||||
}, 100)
|
}, 100)
|
||||||
}
|
}
|
||||||
@@ -38,6 +39,8 @@
|
|||||||
let initialOffset = $derived(offsetEl?.offsetTop)
|
let initialOffset = $derived(offsetEl?.offsetTop)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<svelte:body onkeydown={keybindFeed('li[data-type^="comment"]', 'start')} />
|
||||||
|
|
||||||
<div bind:this={offsetEl}>
|
<div bind:this={offsetEl}>
|
||||||
{#if offsetEl}
|
{#if offsetEl}
|
||||||
<VirtualList
|
<VirtualList
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import { profile } from '$lib/auth.svelte.js'
|
import { profile } from '$lib/auth.svelte.js'
|
||||||
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
|
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
|
||||||
import { t } from '$lib/i18n/translations'
|
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 { buttonColor, toast } from 'mono-svelte'
|
||||||
import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons'
|
import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons'
|
||||||
import { backOut } from 'svelte/easing'
|
import { backOut } from 'svelte/easing'
|
||||||
@@ -24,18 +24,37 @@
|
|||||||
commentId,
|
commentId,
|
||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
vote = newVote
|
||||||
const res = await getClient().likeComment({
|
client()
|
||||||
|
.likeComment({
|
||||||
comment_id: commentId,
|
comment_id: commentId,
|
||||||
score: vote,
|
score: vote,
|
||||||
})
|
})
|
||||||
;({ upvotes, downvotes } = res.comment_view.counts)
|
.then(res => ({ upvotes, downvotes } = res.comment_view.counts))
|
||||||
}
|
}
|
||||||
|
|
||||||
let voteRatio = $derived(
|
let voteRatio = $derived(
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
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(`item-${post.post.id.toString()}`)
|
||||||
|
|
||||||
if (event.target == parent) {
|
if (event.target == parent) {
|
||||||
goto(postLink(post.post))
|
goto(postLink(post.post))
|
||||||
@@ -99,7 +99,7 @@
|
|||||||
view == 'cozy' && 'py-5 flex flex-col gap-2',
|
view == 'cozy' && 'py-5 flex flex-col gap-2',
|
||||||
clazz,
|
clazz,
|
||||||
]}
|
]}
|
||||||
id="post-{post.post.id.toString()}"
|
id="item-{post.post.id.toString()}"
|
||||||
{onclick}
|
{onclick}
|
||||||
onkeydown={e => {
|
onkeydown={e => {
|
||||||
if (e.key === 'Enter') onclick(e)
|
if (e.key === 'Enter') onclick(e)
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
<svelte:body
|
<svelte:body
|
||||||
onkeydown={e => {
|
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">
|
<script lang="ts">
|
||||||
import { t } from '$lib/i18n/translations'
|
import { t } from '$lib/i18n/translations'
|
||||||
|
import { settings } from '$lib/settings.svelte'
|
||||||
import { Modal, Spinner } from 'mono-svelte'
|
import { Modal, Spinner } from 'mono-svelte'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -10,8 +11,8 @@
|
|||||||
|
|
||||||
function handleKeydown(event: KeyboardEvent) {
|
function handleKeydown(event: KeyboardEvent) {
|
||||||
if (
|
if (
|
||||||
(event.ctrlKey && event.key === 'p') ||
|
event.key == settings.keybinds.commandPalette.open.key &&
|
||||||
(event.key == '/' && document.activeElement == document.body)
|
document.activeElement == document.body
|
||||||
) {
|
) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
open = !open
|
open = !open
|
||||||
|
|||||||
+14
-11
@@ -111,20 +111,21 @@ interface Settings {
|
|||||||
voteRatioBar: boolean
|
voteRatioBar: boolean
|
||||||
|
|
||||||
keybinds: {
|
keybinds: {
|
||||||
post: {
|
lists: {
|
||||||
next: Keybind
|
next: Keybind
|
||||||
previous: Keybind
|
previous: Keybind
|
||||||
|
}
|
||||||
|
posts: {
|
||||||
upvote: Keybind
|
upvote: Keybind
|
||||||
downvote: Keybind
|
downvote: Keybind
|
||||||
save: Keybind
|
save: Keybind
|
||||||
expand: Keybind
|
expand: Keybind
|
||||||
}
|
}
|
||||||
|
|
||||||
comments: {
|
comments: {
|
||||||
next: Keybind
|
collapse: Keybind
|
||||||
previous: Keybind
|
}
|
||||||
upvote: Keybind
|
commandPalette: {
|
||||||
downvote: Keybind
|
open: Keybind
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -209,19 +210,21 @@ export const defaultSettings: Settings = {
|
|||||||
},
|
},
|
||||||
voteRatioBar: false,
|
voteRatioBar: false,
|
||||||
keybinds: {
|
keybinds: {
|
||||||
post: {
|
lists: {
|
||||||
next: { ctrl: false, key: 'k' },
|
next: { ctrl: false, key: 'k' },
|
||||||
previous: { ctrl: false, key: 'j' },
|
previous: { ctrl: false, key: 'j' },
|
||||||
|
},
|
||||||
|
posts: {
|
||||||
upvote: { ctrl: false, key: 'n' },
|
upvote: { ctrl: false, key: 'n' },
|
||||||
downvote: { ctrl: false, key: 'm' },
|
downvote: { ctrl: false, key: 'm' },
|
||||||
save: { ctrl: true, key: 's' },
|
save: { ctrl: true, key: 's' },
|
||||||
expand: { ctrl: false, key: 'h' },
|
expand: { ctrl: false, key: 'h' },
|
||||||
},
|
},
|
||||||
comments: {
|
comments: {
|
||||||
next: { ctrl: false, key: 'k' },
|
collapse: { ctrl: false, key: 'l' },
|
||||||
previous: { ctrl: false, key: 'j' },
|
},
|
||||||
upvote: { ctrl: false, key: 'n' },
|
commandPalette: {
|
||||||
downvote: { ctrl: false, key: 'm' },
|
open: { ctrl: false, key: '/' },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,20 @@
|
|||||||
|
import { showImage } from '$lib/components/ui/ExpandableImage.svelte'
|
||||||
import { settings } from '$lib/settings.svelte'
|
import { settings } from '$lib/settings.svelte'
|
||||||
import type { PostView } from 'lemmy-js-client'
|
import type { PostView } from 'lemmy-js-client'
|
||||||
import { isImage } from './image'
|
import { isImage } from './image'
|
||||||
import { showImage } from '$lib/components/ui/ExpandableImage.svelte'
|
|
||||||
|
|
||||||
const binds = $derived(settings.keybinds)
|
const binds = $derived(settings.keybinds)
|
||||||
|
|
||||||
export const keybindFeed = () => {
|
export const keybindFeed = (
|
||||||
|
selector: string = '[id^="item"]',
|
||||||
|
block: 'center' | 'start' = 'center',
|
||||||
|
) => {
|
||||||
return (event: KeyboardEvent) => {
|
return (event: KeyboardEvent) => {
|
||||||
console.log(event)
|
console.log(event)
|
||||||
const direction =
|
const direction =
|
||||||
event.key == binds.post.previous.key
|
event.key == binds.lists.previous.key
|
||||||
? 'up'
|
? 'up'
|
||||||
: event.key == binds.post.next.key
|
: event.key == binds.lists.next.key
|
||||||
? 'down'
|
? 'down'
|
||||||
: null
|
: null
|
||||||
if (!direction) return
|
if (!direction) return
|
||||||
@@ -20,17 +23,18 @@ export const keybindFeed = () => {
|
|||||||
document.activeElement?.closest('[data-index]') ??
|
document.activeElement?.closest('[data-index]') ??
|
||||||
document
|
document
|
||||||
.elementFromPoint(window.innerWidth / 2, window.innerHeight / 2)
|
.elementFromPoint(window.innerWidth / 2, window.innerHeight / 2)
|
||||||
?.closest('[data-index]')
|
?.closest('[data-index]') ??
|
||||||
|
document.querySelector('[data-index]')
|
||||||
if (!focusedPost) return
|
if (!focusedPost) return
|
||||||
|
|
||||||
const sibling =
|
const sibling =
|
||||||
direction == 'up'
|
direction == 'up'
|
||||||
? focusedPost.previousSibling
|
? focusedPost.previousElementSibling
|
||||||
: focusedPost.nextElementSibling
|
: focusedPost.nextElementSibling
|
||||||
if (sibling instanceof HTMLElement) {
|
if (sibling instanceof HTMLElement) {
|
||||||
const postDiv = sibling.querySelector('[id^="post"]')
|
const postDiv = sibling.querySelector(selector)
|
||||||
if (postDiv instanceof HTMLElement) {
|
if (postDiv instanceof HTMLElement) {
|
||||||
postDiv.scrollIntoView({ behavior: 'smooth', block: 'center' })
|
postDiv.scrollIntoView({ behavior: 'smooth', block: block })
|
||||||
postDiv.focus({})
|
postDiv.focus({})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,14 +52,29 @@ export const keybindPost = (
|
|||||||
const event = e as KeyboardEvent
|
const event = e as KeyboardEvent
|
||||||
|
|
||||||
switch (event.key) {
|
switch (event.key) {
|
||||||
case binds.post.upvote.key:
|
case binds.posts.upvote.key:
|
||||||
castVote?.(post.my_vote == 1 ? 0 : 1)
|
castVote?.(post.my_vote == 1 ? 0 : 1)
|
||||||
break
|
break
|
||||||
case binds.post.downvote.key:
|
case binds.posts.downvote.key:
|
||||||
castVote?.(post.my_vote == -1 ? 0 : -1)
|
castVote?.(post.my_vote == -1 ? 0 : -1)
|
||||||
break
|
break
|
||||||
case binds.post.expand.key:
|
case binds.posts.expand.key:
|
||||||
if (post.post.url && isImage(post.post.url)) showImage(post.post.url)
|
if (post.post.url && isImage(post.post.url)) showImage(post.post.url)
|
||||||
break
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -471,13 +471,14 @@
|
|||||||
{#snippet description()}
|
{#snippet description()}
|
||||||
{$t('settings.app.keybinds.description')}
|
{$t('settings.app.keybinds.description')}
|
||||||
{/snippet}
|
{/snippet}
|
||||||
<div class="flex flex-col my-2">
|
</Setting>
|
||||||
|
<li>
|
||||||
<Expandable>
|
<Expandable>
|
||||||
{#snippet title()}
|
{#snippet title()}
|
||||||
{$t('content.posts')}
|
{$t('nav.commands.feeds')}
|
||||||
{/snippet}
|
{/snippet}
|
||||||
<CommonList depth={2}>
|
<CommonList depth={2}>
|
||||||
{#each Object.entries(settings.keybinds.post) as [key, value] (key)}
|
{#each Object.entries(settings.keybinds.lists) as [key, value] (key)}
|
||||||
<li class="flex items-center justify-between gap-2 secondary">
|
<li class="flex items-center justify-between gap-2 secondary">
|
||||||
<div class="capitalize font-medium">{key}</div>
|
<div class="capitalize font-medium">{key}</div>
|
||||||
<TextInput bind:value={value.key} size="sm" class="w-24" />
|
<TextInput bind:value={value.key} size="sm" class="w-24" />
|
||||||
@@ -485,8 +486,52 @@
|
|||||||
{/each}
|
{/each}
|
||||||
</CommonList>
|
</CommonList>
|
||||||
</Expandable>
|
</Expandable>
|
||||||
</div>
|
</li>
|
||||||
</Setting>
|
<li>
|
||||||
|
<Expandable>
|
||||||
|
{#snippet title()}
|
||||||
|
{$t('content.posts')}
|
||||||
|
{/snippet}
|
||||||
|
<CommonList depth={2}>
|
||||||
|
{#each Object.entries(settings.keybinds.posts) as [key, value] (key)}
|
||||||
|
<li class="flex items-center justify-between gap-2 secondary">
|
||||||
|
<div class="capitalize font-medium">{key}</div>
|
||||||
|
<TextInput bind:value={value.key} size="sm" class="w-24" />
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</CommonList>
|
||||||
|
</Expandable>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<Expandable>
|
||||||
|
{#snippet title()}
|
||||||
|
{$t('content.comments')}
|
||||||
|
{/snippet}
|
||||||
|
<CommonList depth={2}>
|
||||||
|
{#each Object.entries(settings.keybinds.comments) as [key, value] (key)}
|
||||||
|
<li class="flex items-center justify-between gap-2 secondary">
|
||||||
|
<div class="capitalize font-medium">{key}</div>
|
||||||
|
<TextInput bind:value={value.key} size="sm" class="w-24" />
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</CommonList>
|
||||||
|
</Expandable>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<Expandable>
|
||||||
|
{#snippet title()}
|
||||||
|
{$t('nav.commands.prompt')}
|
||||||
|
{/snippet}
|
||||||
|
<CommonList depth={2}>
|
||||||
|
{#each Object.entries(settings.keybinds.commandPalette) as [key, value] (key)}
|
||||||
|
<li class="flex items-center justify-between gap-2 secondary">
|
||||||
|
<div class="capitalize font-medium">{key}</div>
|
||||||
|
<TextInput bind:value={value.key} size="sm" class="w-24" />
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</CommonList>
|
||||||
|
</Expandable>
|
||||||
|
</li>
|
||||||
</Section>
|
</Section>
|
||||||
|
|
||||||
<Section id="embeds" title={$t('settings.embeds.title')}>
|
<Section id="embeds" title={$t('settings.embeds.title')}>
|
||||||
|
|||||||
Reference in new issue
Block a user