feat: add comment keybind navigation
This commit is contained in:
10 files changed
+152
-52
No files matched your search
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user