Merge pull request #637 from Xyphyn/revert-635-keybinds

Revert "feat: add customizable keyboard navigation"
This commit is contained in:
Xylight authored and GitHub committed 2025-08-15 06:31:36 -07:00
commit 66f202f5e0
17 files changed
+39 -306

No files matched your search

@@ -25,7 +25,6 @@
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'
import { settings } from '$lib/settings.svelte' import { settings } from '$lib/settings.svelte'
interface Props { interface Props {
@@ -119,15 +118,9 @@
</Modal> </Modal>
{/if} {/if}
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<li <li
class={['py-3 relative focus:outline-0', clazz]} class={['py-3 relative', 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,7 +29,6 @@
replying?: boolean replying?: boolean
disabled?: boolean disabled?: boolean
onedit?: (comment: CommentView) => void onedit?: (comment: CommentView) => void
voteComponent?: { castVote: (vote: number) => void }
} }
let { let {
@@ -37,7 +36,6 @@
replying = $bindable(false), replying = $bindable(false),
disabled = false, disabled = false,
onedit, onedit,
voteComponent,
}: Props = $props() }: Props = $props()
</script> </script>
@@ -50,7 +48,6 @@
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"
@@ -76,7 +73,7 @@
class="text-slate-600 dark:text-zinc-400" class="text-slate-600 dark:text-zinc-400"
> >
{#snippet prefix()} {#snippet prefix()}
<Icon src={EllipsisHorizontal} size="16" micro /> <Icon src={EllipsisHorizontal} width={16} height={16} mini />
{/snippet} {/snippet}
</Button> </Button>
{/snippet} {/snippet}
@@ -4,7 +4,6 @@
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[]
@@ -14,8 +13,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)
} }
@@ -39,8 +38,6 @@
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 { client } from '$lib/lemmy.svelte' import { getClient } 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,37 +24,18 @@
commentId, commentId,
}: Props = $props() }: Props = $props()
export const castVote = async (newVote: number) => { 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
client() const res = await getClient().likeComment({
.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(
+8 -16
View File
@@ -14,7 +14,6 @@
import type { PostView } from 'lemmy-js-client' import type { PostView } from 'lemmy-js-client'
import type { ClassValue } from 'svelte/elements' import type { ClassValue } from 'svelte/elements'
import PostBody from './PostBody.svelte' import PostBody from './PostBody.svelte'
import { keybindPost } from '$lib/ui/keybinds.svelte'
function getTagRule(tags: Tag[]): 'blur-sm' | 'hide' | undefined { function getTagRule(tags: Tag[]): 'blur-sm' | 'hide' | undefined {
const tagContent = tags.map(t => t.content.toLowerCase()) const tagContent = tags.map(t => t.content.toLowerCase())
@@ -29,9 +28,9 @@
return rule return rule
} }
function onclick(e: Event) { function onClick(e: Event) {
const event = e as 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) { if (event.target == parent) {
goto(postLink(post.post)) goto(postLink(post.post))
@@ -80,8 +79,6 @@
admin: post.creator_is_admin, admin: post.creator_is_admin,
moderator: post.creator_is_moderator, moderator: post.creator_is_moderator,
}) })
let voteComponent = $state<{ castVote: (vote: number) => void }>()
</script> </script>
<!-- <!--
@@ -99,11 +96,12 @@
view == 'cozy' && 'py-5 flex flex-col gap-2', view == 'cozy' && 'py-5 flex flex-col gap-2',
clazz, clazz,
]} ]}
id="item-{post.post.id.toString()}" id={post.post.id.toString()}
{onclick} onclick={e => {
onClick(e)
}}
onkeydown={e => { onkeydown={e => {
if (e.key === 'Enter') onclick(e) if (e.key == 'Enter') onClick(e)
keybindPost(e, post, voteComponent?.castVote)
}} }}
tabindex="0" tabindex="0"
{style} {style}
@@ -160,13 +158,7 @@
/> />
{/if} {/if}
{#if actions} {#if actions}
<PostActions <PostActions {onhide} bind:post style="grid-area: actions;" {view} />
{onhide}
bind:voteComponent
bind:post
style="grid-area: actions;"
{view}
/>
{/if} {/if}
<div <div
class="absolute overflow-hidden inset-0 class="absolute overflow-hidden inset-0
@@ -51,7 +51,7 @@
let loading = $state(false) let loading = $state(false)
export const castVote = async (newVote: number) => { 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
@@ -34,7 +34,6 @@
style?: string style?: string
onedit?: (post: PostView) => void onedit?: (post: PostView) => void
onhide?: (hide: boolean) => void onhide?: (hide: boolean) => void
voteComponent?: { castVote: (vote: number) => void }
} }
let { let {
@@ -42,7 +41,6 @@
view = 'cozy', view = 'cozy',
debug = $bindable(false), debug = $bindable(false),
style = '', style = '',
voteComponent = $bindable(),
onedit, onedit,
onhide, onhide,
}: Props = $props() }: Props = $props()
@@ -79,7 +77,7 @@
<footer <footer
class={[ 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, buttonHeight,
]} ]}
class:flex-row-reverse={settings.posts.reverseActions} class:flex-row-reverse={settings.posts.reverseActions}
@@ -91,7 +89,6 @@
bind:score={post.counts.score} bind:score={post.counts.score}
bind:upvotes={post.counts.upvotes} bind:upvotes={post.counts.upvotes}
bind:downvotes={post.counts.downvotes} bind:downvotes={post.counts.downvotes}
bind:this={voteComponent}
showCounts={profile.current?.user?.local_user_view?.local_user showCounts={profile.current?.user?.local_user_view?.local_user
?.show_scores ?? true} ?.show_scores ?? true}
/> />
@@ -12,7 +12,6 @@
type PostFeedID, type PostFeedID,
} from '$lib/lemmy/postfeed.svelte' } from '$lib/lemmy/postfeed.svelte'
import { settings } from '$lib/settings.svelte.js' import { settings } from '$lib/settings.svelte.js'
import { keybindFeed } from '$lib/ui/keybinds.svelte'
import type { PostView } from 'lemmy-js-client' import type { PostView } from 'lemmy-js-client'
import { Button } from 'mono-svelte' import { Button } from 'mono-svelte'
import { onDestroy, onMount, untrack } from 'svelte' import { onDestroy, onMount, untrack } from 'svelte'
@@ -171,8 +170,6 @@
}) })
</script> </script>
<svelte:body onkeydown={keybindFeed()} />
<ul class="flex flex-col list-none" bind:this={listEl}> <ul class="flex flex-col list-none" bind:this={listEl}>
{#key posts} {#key posts}
{#if posts?.length == 0} {#if posts?.length == 0}
@@ -203,9 +200,10 @@
> >
{#snippet item(row)} {#snippet item(row)}
<li <li
data-index={row}
style={row < 7 ? `--anim-delay: ${row * 50}ms` : ''} style={row < 7 ? `--anim-delay: ${row * 50}ms` : ''}
class={[ 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', row < 7 && 'pop-in opacity-0',
]} ]}
> >
+6 -6
View File
@@ -34,15 +34,15 @@
{@render title?.(open)} {@render title?.(open)}
</div> </div>
{#if icon} {#if icon}
<div <Icon
src={open ? Minus : Plus}
size="16"
micro
class={[ class={[
!open && 'rotate-90', !open && 'rotate-90',
'transition-all duration-300 ease-out', 'transition-transform duration-300 ease-out',
'grid place-items-center w-4',
]} ]}
> />
<Icon src={open ? Minus : Plus} size="16" micro />
</div>
{/if} {/if}
<div <div
class="inset-0 -z-10 opacity-0 absolute bg-slate-200 dark:bg-zinc-900 rounded-full class="inset-0 -z-10 opacity-0 absolute bg-slate-200 dark:bg-zinc-900 rounded-full
+4 -8
View File
@@ -29,21 +29,17 @@
let { alt = '', class: clazz = '', children }: Props = $props() let { alt = '', class: clazz = '', children }: Props = $props()
</script> </script>
<svelte:body
onkeydown={e => {
if (e.key == 'Escape' && (page.state.openImage || '') != '') history.back()
}}
/>
{#if page.state.openImage || '' != ''} {#if page.state.openImage || '' != ''}
<!-- svelte-ignore a11y_click_events_have_key_events -->
<!-- svelte-ignore a11y_no_static_element_interactions --> <!-- svelte-ignore a11y_no_static_element_interactions -->
<div <div
class="fixed top-0 left-0 w-screen h-svh overflow-auto bg-white/50 dark:bg-black/50 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" flex flex-col z-100 backdrop-blur-xs"
transition:fade={{ duration: 150 }} transition:fade={{ duration: 150 }}
use:focusTrap
onclick={() => history.back()} onclick={() => history.back()}
onkeydown={e => {
if (e.key == 'Escape') history.back()
}}
use:focusTrap
> >
<img <img
width={800} width={800}
+2 -20
View File
@@ -13,7 +13,6 @@
children?: Snippet children?: Snippet
animate?: boolean animate?: boolean
size?: 'lg' | 'md' | 'sm' size?: 'lg' | 'md' | 'sm'
depth?: 1 | 2
} }
let { let {
@@ -22,13 +21,12 @@
children, children,
animate = true, animate = true,
size = 'sm', size = 'sm',
depth = 1,
}: Props = $props() }: Props = $props()
</script> </script>
<ul class={['space-y-[3px]', depth == 2 && 'secondary']}> <ul class="divide-y-3 divide-slate-50 dark:divide-zinc-925">
{#if items} {#if items}
{#each items as item, index} {#each items as item, index (item)}
<li <li
class={['group/li', animate && 'animate']} class={['group/li', animate && 'animate']}
style="--i: {index < 10 ? index * sizeDelay[size] : 0}ms;" 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 { @keyframes pop-in {
from { from {
transform: translateY(16px); transform: translateY(16px);
@@ -1,6 +1,5 @@
<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 {
@@ -11,8 +10,8 @@
function handleKeydown(event: KeyboardEvent) { function handleKeydown(event: KeyboardEvent) {
if ( if (
event.key == settings.keybinds.commandPalette.open.key && (event.ctrlKey && event.key === 'p') ||
document.activeElement == document.body (event.key == '/' && document.activeElement == document.body)
) { ) {
event.preventDefault() event.preventDefault()
open = !open open = !open
-5
View File
@@ -833,11 +833,6 @@
"voteRatioBar": { "voteRatioBar": {
"title": "Vote ratio bar", "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." "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": { "embeds": {
+1 -42
View File
@@ -7,10 +7,6 @@ console.log('Using the following default settings from the environment:')
console.log(env) console.log(env)
export type View = 'cozy' | 'compact' export type View = 'cozy' | 'compact'
type Keybind = {
key: string
ctrl: boolean
}
export const SSR_ENABLED = env.PUBLIC_SSR_ENABLED?.toLowerCase() == 'true' export const SSR_ENABLED = env.PUBLIC_SSR_ENABLED?.toLowerCase() == 'true'
@@ -109,29 +105,10 @@ interface Settings {
fullMarkdown: boolean fullMarkdown: boolean
} }
voteRatioBar: 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 = { export const defaultSettings: Settings = {
settingsVer: 7, settingsVer: 6,
expandableImages: toBool(env.PUBLIC_EXPANDABLE_IMAGES) ?? true, expandableImages: toBool(env.PUBLIC_EXPANDABLE_IMAGES) ?? true,
markReadPosts: toBool(env.PUBLIC_MARK_READ_POSTS) ?? true, markReadPosts: toBool(env.PUBLIC_MARK_READ_POSTS) ?? true,
showInstances: { showInstances: {
@@ -209,24 +186,6 @@ export const defaultSettings: Settings = {
fullMarkdown: toBool(env.PUBLIC_FULL_MARKDOWN) ?? false, fullMarkdown: toBool(env.PUBLIC_FULL_MARKDOWN) ?? false,
}, },
voteRatioBar: 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 { function createSettingsState(initial: Settings): Settings {
-80
View File
@@ -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
}
}
-71
View File
@@ -66,13 +66,10 @@
User, User,
VideoCamera, VideoCamera,
ViewColumns, ViewColumns,
ViewfinderCircle,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import Section from './Section.svelte' import Section from './Section.svelte'
import Setting from './Setting.svelte' import Setting from './Setting.svelte'
import ToggleSetting from './ToggleSetting.svelte' import ToggleSetting from './ToggleSetting.svelte'
import Expandable from '$lib/components/ui/Expandable.svelte'
import CommonList from '$lib/components/ui/layout/CommonList.svelte'
let importing = $state(false) let importing = $state(false)
let importText = $state('') let importText = $state('')
@@ -464,74 +461,6 @@
description={$t('settings.app.titleTags.description')} description={$t('settings.app.titleTags.description')}
bind:checked={settings.parseTags} bind:checked={settings.parseTags}
/> />
<Setting icon={ViewfinderCircle} adaptive={false}>
{#snippet title()}
{$t('settings.app.keybinds.title')}
{/snippet}
{#snippet description()}
{$t('settings.app.keybinds.description')}
{/snippet}
</Setting>
<li>
<Expandable>
{#snippet title()}
{$t('nav.commands.feeds')}
{/snippet}
<CommonList depth={2}>
{#each Object.entries(settings.keybinds.lists) 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.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')}>
+2 -4
View File
@@ -22,7 +22,6 @@
description?: import('svelte').Snippet description?: import('svelte').Snippet
children?: import('svelte').Snippet children?: import('svelte').Snippet
icon?: IconSource icon?: IconSource
adaptive?: boolean
} }
let { let {
@@ -38,7 +37,6 @@
description, description,
children, children,
icon, icon,
adaptive = true,
}: Props = $props() }: Props = $props()
</script> </script>
@@ -76,7 +74,7 @@
{/if} {/if}
</div> </div>
{/if} {/if}
<div class={['setting-grid items-center', adaptive && 'adaptive', clazz]}> <div class={['setting-grid items-center', clazz]}>
{#if icon} {#if icon}
<Icon <Icon
src={icon} src={icon}
@@ -126,7 +124,7 @@
} }
@media screen and (min-width: 768px) { @media screen and (min-width: 768px) {
.setting-grid.adaptive { .setting-grid {
display: grid; display: grid;
grid-template-columns: auto 1fr auto; grid-template-columns: auto 1fr auto;
grid-template-rows: auto auto; grid-template-rows: auto auto;