Revert "feat: add customizable keyboard navigation"
This commit is contained in:
1 parent
4e7c37bd89
commit
f9a6e775f3
17 files changed
+39
-306
No files matched your search
@@ -25,7 +25,6 @@
|
||||
import CommentForm from './CommentForm.svelte'
|
||||
import type { CommentNodeI } from './comments.svelte'
|
||||
import { page } from '$app/state'
|
||||
import { keybindComment } from '$lib/ui/keybinds.svelte'
|
||||
import { settings } from '$lib/settings.svelte'
|
||||
|
||||
interface Props {
|
||||
@@ -119,15 +118,9 @@
|
||||
</Modal>
|
||||
{/if}
|
||||
|
||||
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
|
||||
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
|
||||
<li
|
||||
class={['py-3 relative focus:outline-0', clazz]}
|
||||
class={['py-3 relative', 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,7 +29,6 @@
|
||||
replying?: boolean
|
||||
disabled?: boolean
|
||||
onedit?: (comment: CommentView) => void
|
||||
voteComponent?: { castVote: (vote: number) => void }
|
||||
}
|
||||
|
||||
let {
|
||||
@@ -37,7 +36,6 @@
|
||||
replying = $bindable(false),
|
||||
disabled = false,
|
||||
onedit,
|
||||
voteComponent,
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
@@ -50,7 +48,6 @@
|
||||
downvotes={comment.counts.downvotes}
|
||||
vote={comment.my_vote}
|
||||
commentId={comment.comment.id}
|
||||
bind:this={voteComponent}
|
||||
/>
|
||||
<Button
|
||||
size="sm"
|
||||
@@ -76,7 +73,7 @@
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
>
|
||||
{#snippet prefix()}
|
||||
<Icon src={EllipsisHorizontal} size="16" micro />
|
||||
<Icon src={EllipsisHorizontal} width={16} height={16} mini />
|
||||
{/snippet}
|
||||
</Button>
|
||||
{/snippet}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
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[]
|
||||
@@ -14,8 +13,8 @@
|
||||
|
||||
onMount(() => {
|
||||
if (scrollTo) {
|
||||
const element = document?.getElementById(scrollTo)
|
||||
setTimeout(() => {
|
||||
const element = document?.getElementById(scrollTo)
|
||||
element?.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
||||
}, 100)
|
||||
}
|
||||
@@ -39,8 +38,6 @@
|
||||
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 { client } from '$lib/lemmy.svelte'
|
||||
import { getClient } from '$lib/lemmy.svelte'
|
||||
import { buttonColor, toast } from 'mono-svelte'
|
||||
import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons'
|
||||
import { backOut } from 'svelte/easing'
|
||||
@@ -24,37 +24,18 @@
|
||||
commentId,
|
||||
}: Props = $props()
|
||||
|
||||
export const castVote = async (newVote: number) => {
|
||||
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
|
||||
client()
|
||||
.likeComment({
|
||||
comment_id: commentId,
|
||||
score: vote,
|
||||
})
|
||||
.then(res => ({ upvotes, downvotes } = res.comment_view.counts))
|
||||
const res = await getClient().likeComment({
|
||||
comment_id: commentId,
|
||||
score: vote,
|
||||
})
|
||||
;({ upvotes, downvotes } = res.comment_view.counts)
|
||||
}
|
||||
|
||||
let voteRatio = $derived(
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
import type { PostView } from 'lemmy-js-client'
|
||||
import type { ClassValue } from 'svelte/elements'
|
||||
import PostBody from './PostBody.svelte'
|
||||
import { keybindPost } from '$lib/ui/keybinds.svelte'
|
||||
|
||||
function getTagRule(tags: Tag[]): 'blur-sm' | 'hide' | undefined {
|
||||
const tagContent = tags.map(t => t.content.toLowerCase())
|
||||
@@ -29,9 +28,9 @@
|
||||
return rule
|
||||
}
|
||||
|
||||
function onclick(e: Event) {
|
||||
function onClick(e: Event) {
|
||||
const event = e as Event
|
||||
const parent = document.getElementById(`item-${post.post.id.toString()}`)
|
||||
const parent = document.getElementById(post.post.id.toString())
|
||||
|
||||
if (event.target == parent) {
|
||||
goto(postLink(post.post))
|
||||
@@ -80,8 +79,6 @@
|
||||
admin: post.creator_is_admin,
|
||||
moderator: post.creator_is_moderator,
|
||||
})
|
||||
|
||||
let voteComponent = $state<{ castVote: (vote: number) => void }>()
|
||||
</script>
|
||||
|
||||
<!--
|
||||
@@ -99,11 +96,12 @@
|
||||
view == 'cozy' && 'py-5 flex flex-col gap-2',
|
||||
clazz,
|
||||
]}
|
||||
id="item-{post.post.id.toString()}"
|
||||
{onclick}
|
||||
id={post.post.id.toString()}
|
||||
onclick={e => {
|
||||
onClick(e)
|
||||
}}
|
||||
onkeydown={e => {
|
||||
if (e.key === 'Enter') onclick(e)
|
||||
keybindPost(e, post, voteComponent?.castVote)
|
||||
if (e.key == 'Enter') onClick(e)
|
||||
}}
|
||||
tabindex="0"
|
||||
{style}
|
||||
@@ -160,13 +158,7 @@
|
||||
/>
|
||||
{/if}
|
||||
{#if actions}
|
||||
<PostActions
|
||||
{onhide}
|
||||
bind:voteComponent
|
||||
bind:post
|
||||
style="grid-area: actions;"
|
||||
{view}
|
||||
/>
|
||||
<PostActions {onhide} bind:post style="grid-area: actions;" {view} />
|
||||
{/if}
|
||||
<div
|
||||
class="absolute overflow-hidden inset-0
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
|
||||
let loading = $state(false)
|
||||
|
||||
export const castVote = async (newVote: number) => {
|
||||
const castVote = async (newVote: number) => {
|
||||
if (!profile.current?.jwt) {
|
||||
toast({ content: $t('toast.loginVoteGate'), type: 'warning' })
|
||||
return
|
||||
|
||||
@@ -34,7 +34,6 @@
|
||||
style?: string
|
||||
onedit?: (post: PostView) => void
|
||||
onhide?: (hide: boolean) => void
|
||||
voteComponent?: { castVote: (vote: number) => void }
|
||||
}
|
||||
|
||||
let {
|
||||
@@ -42,7 +41,6 @@
|
||||
view = 'cozy',
|
||||
debug = $bindable(false),
|
||||
style = '',
|
||||
voteComponent = $bindable(),
|
||||
onedit,
|
||||
onhide,
|
||||
}: Props = $props()
|
||||
@@ -79,7 +77,7 @@
|
||||
|
||||
<footer
|
||||
class={[
|
||||
'flex flex-row gap-2 items-center shrink-0 text-slate-600 dark:text-zinc-300',
|
||||
'flex flex-row gap-2 items-center shrink-0 text-slate-600 dark:text-zinc-400',
|
||||
buttonHeight,
|
||||
]}
|
||||
class:flex-row-reverse={settings.posts.reverseActions}
|
||||
@@ -91,7 +89,6 @@
|
||||
bind:score={post.counts.score}
|
||||
bind:upvotes={post.counts.upvotes}
|
||||
bind:downvotes={post.counts.downvotes}
|
||||
bind:this={voteComponent}
|
||||
showCounts={profile.current?.user?.local_user_view?.local_user
|
||||
?.show_scores ?? true}
|
||||
/>
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
type PostFeedID,
|
||||
} from '$lib/lemmy/postfeed.svelte'
|
||||
import { settings } from '$lib/settings.svelte.js'
|
||||
import { keybindFeed } from '$lib/ui/keybinds.svelte'
|
||||
import type { PostView } from 'lemmy-js-client'
|
||||
import { Button } from 'mono-svelte'
|
||||
import { onDestroy, onMount, untrack } from 'svelte'
|
||||
@@ -171,8 +170,6 @@
|
||||
})
|
||||
</script>
|
||||
|
||||
<svelte:body onkeydown={keybindFeed()} />
|
||||
|
||||
<ul class="flex flex-col list-none" bind:this={listEl}>
|
||||
{#key posts}
|
||||
{#if posts?.length == 0}
|
||||
@@ -203,9 +200,10 @@
|
||||
>
|
||||
{#snippet item(row)}
|
||||
<li
|
||||
data-index={row}
|
||||
style={row < 7 ? `--anim-delay: ${row * 50}ms` : ''}
|
||||
class={[
|
||||
'relative post-container px-3 sm:px-6 z-0',
|
||||
'relative post-container px-3 sm:px-6',
|
||||
row < 7 && 'pop-in opacity-0',
|
||||
]}
|
||||
>
|
||||
|
||||
@@ -34,15 +34,15 @@
|
||||
{@render title?.(open)}
|
||||
</div>
|
||||
{#if icon}
|
||||
<div
|
||||
<Icon
|
||||
src={open ? Minus : Plus}
|
||||
size="16"
|
||||
micro
|
||||
class={[
|
||||
!open && 'rotate-90',
|
||||
'transition-all duration-300 ease-out',
|
||||
'grid place-items-center w-4',
|
||||
'transition-transform duration-300 ease-out',
|
||||
]}
|
||||
>
|
||||
<Icon src={open ? Minus : Plus} size="16" micro />
|
||||
</div>
|
||||
/>
|
||||
{/if}
|
||||
<div
|
||||
class="inset-0 -z-10 opacity-0 absolute bg-slate-200 dark:bg-zinc-900 rounded-full
|
||||
|
||||
@@ -29,21 +29,17 @@
|
||||
let { alt = '', class: clazz = '', children }: Props = $props()
|
||||
</script>
|
||||
|
||||
<svelte:body
|
||||
onkeydown={e => {
|
||||
if (e.key == 'Escape' && (page.state.openImage || '') != '') history.back()
|
||||
}}
|
||||
/>
|
||||
|
||||
{#if page.state.openImage || '' != ''}
|
||||
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
||||
<div
|
||||
class="fixed top-0 left-0 w-screen h-svh overflow-auto bg-white/50 dark:bg-black/50
|
||||
flex flex-col z-100 backdrop-blur-xs"
|
||||
transition:fade={{ duration: 150 }}
|
||||
use:focusTrap
|
||||
onclick={() => history.back()}
|
||||
onkeydown={e => {
|
||||
if (e.key == 'Escape') history.back()
|
||||
}}
|
||||
use:focusTrap
|
||||
>
|
||||
<img
|
||||
width={800}
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
children?: Snippet
|
||||
animate?: boolean
|
||||
size?: 'lg' | 'md' | 'sm'
|
||||
depth?: 1 | 2
|
||||
}
|
||||
|
||||
let {
|
||||
@@ -22,13 +21,12 @@
|
||||
children,
|
||||
animate = true,
|
||||
size = 'sm',
|
||||
depth = 1,
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<ul class={['space-y-[3px]', depth == 2 && 'secondary']}>
|
||||
<ul class="divide-y-3 divide-slate-50 dark:divide-zinc-925">
|
||||
{#if items}
|
||||
{#each items as item, index}
|
||||
{#each items as item, index (item)}
|
||||
<li
|
||||
class={['group/li', animate && 'animate']}
|
||||
style="--i: {index < 10 ? index * sizeDelay[size] : 0}ms;"
|
||||
@@ -83,22 +81,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
ul.secondary > :global(li) {
|
||||
background-color: var(--color-slate-25);
|
||||
|
||||
&:hover {
|
||||
background-color: var(--color-slate-50);
|
||||
}
|
||||
}
|
||||
|
||||
:global(.dark) ul.secondary > :global(li) {
|
||||
background-color: var(--color-zinc-925);
|
||||
|
||||
&:hover {
|
||||
background-color: var(--color-zinc-800);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes pop-in {
|
||||
from {
|
||||
transform: translateY(16px);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { t } from '$lib/i18n/translations'
|
||||
import { settings } from '$lib/settings.svelte'
|
||||
import { Modal, Spinner } from 'mono-svelte'
|
||||
|
||||
interface Props {
|
||||
@@ -11,8 +10,8 @@
|
||||
|
||||
function handleKeydown(event: KeyboardEvent) {
|
||||
if (
|
||||
event.key == settings.keybinds.commandPalette.open.key &&
|
||||
document.activeElement == document.body
|
||||
(event.ctrlKey && event.key === 'p') ||
|
||||
(event.key == '/' && document.activeElement == document.body)
|
||||
) {
|
||||
event.preventDefault()
|
||||
open = !open
|
||||
|
||||
@@ -833,11 +833,6 @@
|
||||
"voteRatioBar": {
|
||||
"title": "Vote ratio bar",
|
||||
"description": "When the controversiality of a post reaches greater than 15%, a bar will be displayed in the vote buttons to help visualize sentiment."
|
||||
},
|
||||
"keybinds": {
|
||||
"title": "Keybinds",
|
||||
"description": "Customize keybinds for certain actions within the app.",
|
||||
"key": "Key"
|
||||
}
|
||||
},
|
||||
"embeds": {
|
||||
|
||||
@@ -7,10 +7,6 @@ console.log('Using the following default settings from the environment:')
|
||||
console.log(env)
|
||||
|
||||
export type View = 'cozy' | 'compact'
|
||||
type Keybind = {
|
||||
key: string
|
||||
ctrl: boolean
|
||||
}
|
||||
|
||||
export const SSR_ENABLED = env.PUBLIC_SSR_ENABLED?.toLowerCase() == 'true'
|
||||
|
||||
@@ -109,29 +105,10 @@ interface Settings {
|
||||
fullMarkdown: boolean
|
||||
}
|
||||
voteRatioBar: boolean
|
||||
|
||||
keybinds: {
|
||||
lists: {
|
||||
next: Keybind
|
||||
previous: Keybind
|
||||
}
|
||||
posts: {
|
||||
upvote: Keybind
|
||||
downvote: Keybind
|
||||
save: Keybind
|
||||
expand: Keybind
|
||||
}
|
||||
comments: {
|
||||
collapse: Keybind
|
||||
}
|
||||
commandPalette: {
|
||||
open: Keybind
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const defaultSettings: Settings = {
|
||||
settingsVer: 7,
|
||||
settingsVer: 6,
|
||||
expandableImages: toBool(env.PUBLIC_EXPANDABLE_IMAGES) ?? true,
|
||||
markReadPosts: toBool(env.PUBLIC_MARK_READ_POSTS) ?? true,
|
||||
showInstances: {
|
||||
@@ -209,24 +186,6 @@ export const defaultSettings: Settings = {
|
||||
fullMarkdown: toBool(env.PUBLIC_FULL_MARKDOWN) ?? false,
|
||||
},
|
||||
voteRatioBar: false,
|
||||
keybinds: {
|
||||
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: {
|
||||
collapse: { ctrl: false, key: 'l' },
|
||||
},
|
||||
commandPalette: {
|
||||
open: { ctrl: false, key: '/' },
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
function createSettingsState(initial: Settings): Settings {
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
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'
|
||||
|
||||
const binds = $derived(settings.keybinds)
|
||||
|
||||
export const keybindFeed = (
|
||||
selector: string = '[id^="item"]',
|
||||
block: 'center' | 'start' = 'center',
|
||||
) => {
|
||||
return (event: KeyboardEvent) => {
|
||||
console.log(event)
|
||||
const direction =
|
||||
event.key == binds.lists.previous.key
|
||||
? 'up'
|
||||
: event.key == binds.lists.next.key
|
||||
? 'down'
|
||||
: null
|
||||
if (!direction) return
|
||||
|
||||
const focusedPost =
|
||||
document.activeElement?.closest('[data-index]') ??
|
||||
document
|
||||
.elementFromPoint(window.innerWidth / 2, window.innerHeight / 2)
|
||||
?.closest('[data-index]') ??
|
||||
document.querySelector('[data-index]')
|
||||
if (!focusedPost) return
|
||||
|
||||
const sibling =
|
||||
direction == 'up'
|
||||
? focusedPost.previousElementSibling
|
||||
: focusedPost.nextElementSibling
|
||||
if (sibling instanceof HTMLElement) {
|
||||
const postDiv = sibling.querySelector(selector)
|
||||
if (postDiv instanceof HTMLElement) {
|
||||
postDiv.scrollIntoView({ behavior: 'smooth', block: block })
|
||||
postDiv.focus({})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const keybindPost = (
|
||||
e: KeyboardEvent,
|
||||
post: PostView,
|
||||
castVote?: (vote: number) => void,
|
||||
) => {
|
||||
if ((e.target as HTMLDivElement).id.split('-')[1] != post.post.id.toString())
|
||||
return
|
||||
|
||||
const event = e as KeyboardEvent
|
||||
|
||||
switch (event.key) {
|
||||
case binds.posts.upvote.key:
|
||||
castVote?.(post.my_vote == 1 ? 0 : 1)
|
||||
break
|
||||
case binds.posts.downvote.key:
|
||||
castVote?.(post.my_vote == -1 ? 0 : -1)
|
||||
break
|
||||
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