impr: use modals for replies, close #466

This commit is contained in:
James Xylight committed 2025-06-10 11:59:00 -07:00
1 parent 8232903bbe
commit 7e2590585d
7 files changed
+112 -140

No files matched your search

+71 -116
View File
@@ -1,32 +1,21 @@
<script lang="ts"> <script lang="ts">
import { page } from '$app/state'
import { profile } from '$lib/auth.svelte.js' import { profile } from '$lib/auth.svelte.js'
import CommentActions from '$lib/components/lemmy/comment/CommentActions.svelte' import CommentActions from '$lib/components/lemmy/comment/CommentActions.svelte'
import UserLink from '$lib/components/lemmy/user/UserLink.svelte' import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte' import Markdown from '$lib/components/markdown/Markdown.svelte'
import { publishedToDate } from '$lib/components/util/date.js' import { publishedToDate } from '$lib/components/util/date.js'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import RelativeDate from '$lib/components/util/RelativeDate.svelte' import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import { t } from '$lib/i18n/translations' import { t } from '$lib/i18n/translations'
import { getClient } from '$lib/lemmy.svelte.js' import { getClient } from '$lib/lemmy.svelte.js'
import { errorMessage } from '$lib/lemmy/error'
import { Button, Modal, toast } from 'mono-svelte' import { Button, Modal, toast } from 'mono-svelte'
import { onMount } from 'svelte' import { Bookmark, Icon, Microphone, Pencil, Trash } from 'svelte-hero-icons'
import {
Bookmark,
Icon,
Microphone,
Minus,
Pencil,
Plus,
Trash,
} from 'svelte-hero-icons'
import { expoOut } from 'svelte/easing' import { expoOut } from 'svelte/easing'
import type { ClassValue } from 'svelte/elements' import type { ClassValue } from 'svelte/elements'
import { fly, slide } from 'svelte/transition' import { slide } from 'svelte/transition'
import ShieldIcon from '../moderation/ShieldIcon.svelte' import ShieldIcon from '../moderation/ShieldIcon.svelte'
import CommentForm from './CommentForm.svelte' import CommentForm from './CommentForm.svelte'
import type { CommentNodeI } from './comments.svelte' import type { CommentNodeI } from './comments.svelte'
import { errorMessage } from '$lib/lemmy/error'
interface Props { interface Props {
node: CommentNodeI node: CommentNodeI
@@ -60,20 +49,22 @@
let newComment = $state(node.comment_view.comment.content) let newComment = $state(node.comment_view.comment.content)
let editingLoad = $state(false) let editingLoad = $state(false)
async function save() { async function save(type: 'reply' | 'edit') {
if (!profile.data?.jwt || newComment.length <= 0) return if (!profile.data?.jwt || newComment.length <= 0) return
editingLoad = true editingLoad = true
try { try {
await getClient().editComment({ if (type == 'edit') {
comment_id: node.comment_view.comment.id, await getClient().editComment({
content: newComment, comment_id: node.comment_view.comment.id,
}) content: newComment,
})
node.comment_view.comment.content = newComment node.comment_view.comment.content = newComment
editing = false editing = false
}
} catch (err) { } catch (err) {
toast({ toast({
content: errorMessage(err as string), content: errorMessage(err as string),
@@ -83,56 +74,76 @@
editingLoad = false editingLoad = false
} }
onMount(() => {
if ('#' + node.comment_view.comment.id.toString() == page.url.hash) {
highlight = 'text-primary-900 dark:text-primary-100 font-medium'
setTimeout(() => (highlight = 'duration-[3s] transition-all'), 500)
setTimeout(() => (highlight = ''), 600)
}
})
let highlight = $state('')
</script> </script>
{#if editing} {#if editing || replying}
<Modal bind:open={editing}> <Modal
bind:open={
() => replying || editing,
v => {
replying = v
editing = v
}
}
>
{#snippet customTitle()} {#snippet customTitle()}
<span>{$t('form.edit')}</span> <span>{replying ? $t('comment.reply') : $t('form.edit')}</span>
{/snippet} {/snippet}
<form <form
onsubmit={e => { onsubmit={e => {
e.preventDefault() e.preventDefault()
save() save(replying ? 'reply' : 'edit')
}} }}
class="contents" class="contents"
> >
<CommentForm {#if replying}
bind:value={newComment} <CommentForm
postId={node.comment_view.comment.id} label={$t('comment.reply')}
actions={false} {postId}
preview={true} parentId={node.comment_view.comment.id}
onconfirm={save} oncomment={e => {
/> node.children = [
<Button {
submit children: [],
color="primary" comment_view: e.comment_view,
size="lg" depth: node.depth + 1,
loading={editingLoad} expanded: true,
disabled={editingLoad} },
class="w-full" ...node.children,
> ]
{$t('common.save')} replying = false
</Button> }}
oncancel={() => (replying = false)}
/>
{:else if editing}
<CommentForm
bind:value={newComment}
postId={node.comment_view.comment.id}
actions={false}
preview={true}
/>
<Button
submit
color="primary"
size="lg"
loading={editingLoad}
disabled={editingLoad}
class="w-full"
>
{$t('form.submit')}
</Button>
{/if}
</form> </form>
</Modal> </Modal>
{/if} {/if}
<li <li
class="py-3 relative {node.comment_view.comment.distinguished class={[
? ' text-primary-900 dark:text-primary-100' 'py-3 relative',
: ''} {highlight} {clazz}" node.comment_view.comment.distinguished &&
' text-primary-900 dark:text-primary-100',
clazz,
]}
id={node.comment_view.comment.id.toString()} id={node.comment_view.comment.id.toString()}
> >
{#if meta} {#if meta}
@@ -142,28 +153,6 @@
z-0 group relative" z-0 group relative"
> >
{@render metaSuffix?.()} {@render metaSuffix?.()}
<div
class="absolute opacity-0 -z-10 inset-0 group-hover:block group-hover:opacity-100
bg-slate-100 dark:bg-zinc-900 group-hover:-inset-1 group-hover:-inset-x-2 rounded-full transition-all
"
>
<div
class="h-full flex items-center justify-center gap-1 ml-auto w-max mr-2"
>
{#if node.children.length > 0}
<FormattedNumber number={node.comment_view.counts.child_count}
></FormattedNumber>
{/if}
<Icon
src={open ? Minus : Plus}
size="16"
micro
class="transition-transform duration-400 ease-out {open
? ''
: 'rotate-90'} text-primary-900 dark:text-primary-100"
/>
</div>
</div>
<span class:font-bold={op} class="flex flex-row gap-1 items-center"> <span class:font-bold={op} class="flex flex-row gap-1 items-center">
<UserLink <UserLink
inComment inComment
@@ -222,17 +211,10 @@
class="flex flex-col whitespace-pre-wrap class="flex flex-col whitespace-pre-wrap
max-w-full gap-1 mt-1 relative" max-w-full gap-1 mt-1 relative"
> >
{#if node.comment_view.comment.distinguished} <Markdown
<div source={node.comment_view.comment.content}
class="-z-10 bg-slate-100 dark:bg-zinc-900 absolute -top-9 -bottom-1.5 class="text-[15px] text-slate-800 dark:text-zinc-200 leading-[1.3]"
-inset-x-6 -right-6" />
></div>
{/if}
<div
class="max-w-full mt-0.5 break-words text-[15px] text-slate-800 dark:text-zinc-100"
>
<Markdown source={node.comment_view.comment.content} />
</div>
{#if actions} {#if actions}
<CommentActions <CommentActions
comment={node.comment_view} comment={node.comment_view}
@@ -243,33 +225,6 @@
/> />
{/if} {/if}
</div> </div>
{#if replying}
<div
class="max-w-full my-2 border-l border-slate-200 dark:border-zinc-800 pl-4"
transition:slide={{ axis: 'y', duration: 400, easing: expoOut }}
>
<div in:fly={{ duration: 500, y: -16, easing: expoOut, delay: 200 }}>
<CommentForm
label={$t('comment.reply')}
{postId}
parentId={node.comment_view.comment.id}
oncomment={e => {
node.children = [
{
children: [],
comment_view: e.comment_view,
depth: node.depth + 1,
expanded: true,
},
...node.children,
]
replying = false
}}
oncancel={() => (replying = false)}
/>
</div>
</div>
{/if}
<div class="bg-transparent dark:bg-transparent"> <div class="bg-transparent dark:bg-transparent">
{@render children?.()} {@render children?.()}
</div> </div>
@@ -2,8 +2,8 @@
import Comment from '$lib/components/lemmy/comment/Comment.svelte' import Comment from '$lib/components/lemmy/comment/Comment.svelte'
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte' import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
import { publishedToDate } from '$lib/components/util/date.js' import { publishedToDate } from '$lib/components/util/date.js'
import { settings, type View } from '$lib/settings.svelte.js'
import { t } from '$lib/i18n/translations' import { t } from '$lib/i18n/translations'
import { settings, type View } from '$lib/settings.svelte.js'
import type { CommentView } from 'lemmy-js-client' import type { CommentView } from 'lemmy-js-client'
import { Button, Material } from 'mono-svelte' import { Button, Material } from 'mono-svelte'
import { ArrowUturnUp, Icon } from 'svelte-hero-icons' import { ArrowUturnUp, Icon } from 'svelte-hero-icons'
@@ -29,9 +29,9 @@
</script> </script>
<Material <Material
class="flex flex-col flex-1 bg-transparent! border-0! rounded-none class="flex flex-col flex-1 rounded-none
{view == 'compact' ? 'py-4' : 'py-5'} {clazz}" {view == 'compact' ? 'py-4' : 'py-5'} {clazz}"
color="distinct" color="none"
padding="none" padding="none"
> >
{#if meta} {#if meta}
@@ -5,8 +5,9 @@
import { t } from '$lib/i18n/translations' import { t } from '$lib/i18n/translations'
import type { Community } from 'lemmy-js-client' import type { Community } from 'lemmy-js-client'
import { ExclamationTriangle, Icon } from 'svelte-hero-icons' import { ExclamationTriangle, Icon } from 'svelte-hero-icons'
import type { HTMLAnchorAttributes } from 'svelte/elements'
interface Props { interface Props extends HTMLAnchorAttributes {
community: Community community: Community
avatar?: boolean avatar?: boolean
name?: boolean name?: boolean
+1 -1
View File
@@ -101,7 +101,7 @@
<!-- svelte-ignore a11y_no_noninteractive_tabindex --> <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<div <div
class={[ class={[
'post relative max-w-full min-w-0 w-full cursor-pointer outline-hidden group z-10 rounded-3xl', 'post relative max-w-full min-w-0 w-full cursor-pointer outline-hidden group',
settings.leftAlign && 'left-align', settings.leftAlign && 'left-align',
view == 'compact' && 'py-3 list-type compact', view == 'compact' && 'py-3 list-type compact',
view == 'cozy' && 'py-5 flex flex-col gap-2', view == 'cozy' && 'py-5 flex flex-col gap-2',
+12 -12
View File
@@ -73,15 +73,15 @@
import Material from 'mono-svelte/materials/Material.svelte' import Material from 'mono-svelte/materials/Material.svelte'
interface Props { interface Props {
community?: Community | undefined community?: Community
showCommunity?: boolean showCommunity?: boolean
subscribed?: SubscribedType | undefined subscribed?: SubscribedType
user?: Person | undefined user?: Person
published?: Date | undefined published?: Date
title?: string | undefined title?: string
id?: number | undefined id?: number
read?: boolean read?: boolean
edited?: string | undefined edited?: string
view?: View view?: View
// Badges // Badges
badges?: { badges?: {
@@ -104,12 +104,12 @@
community = $bindable(undefined), community = $bindable(undefined),
showCommunity = true, showCommunity = true,
subscribed = $bindable(undefined), subscribed = $bindable(undefined),
user = undefined, user,
published = undefined, published,
title = undefined, title,
id = undefined, id,
read = false, read = false,
edited = undefined, edited,
view = 'cozy', view = 'cozy',
badges = { badges = {
nsfw: false, nsfw: false,
@@ -57,11 +57,11 @@
let image = $state<FileList | null | undefined>(null) let image = $state<FileList | null | undefined>(null)
const shortcuts = { const shortcuts = {
KeyB: () => wrapSelection('**', '**'), b: () => wrapSelection('**', '**'),
KeyI: () => wrapSelection('*', '*'), i: () => wrapSelection('*', '*'),
KeyS: () => wrapSelection('~~', '~~'), s: () => wrapSelection('~~', '~~'),
KeyH: () => wrapSelection('\n# ', ''), h: () => wrapSelection('\n# ', ''),
KeyK: () => wrapSelection('[](', ')'), k: () => wrapSelection('[](', ')'),
} }
async function adjustHeight() { async function adjustHeight() {
@@ -72,6 +72,18 @@
} }
} }
function handleKeydown(
event: KeyboardEvent & {
currentTarget: EventTarget & HTMLTextAreaElement
},
) {
if (event.ctrlKey && event.key === 'Enter') {
event.preventDefault()
const form = textArea?.closest('form')
if (form) form.requestSubmit() // Automatically submits the form
}
}
interface Props extends TextAreaProps { interface Props extends TextAreaProps {
images?: boolean images?: boolean
value?: string value?: string
@@ -255,8 +267,9 @@ overflow-hidden transition-colors {clazz}"
onkeydown={e => { onkeydown={e => {
if (disabled) return if (disabled) return
if (e.ctrlKey || e.metaKey) { if (e.ctrlKey || e.metaKey) {
handleKeydown(e)
// @ts-expect-error yes it can // @ts-expect-error yes it can
let shortcut = shortcuts[e.code] let shortcut = shortcuts[e.key]
if (shortcut) { if (shortcut) {
e.preventDefault() e.preventDefault()
shortcut?.(e) shortcut?.(e)
+4 -1
View File
@@ -167,7 +167,10 @@
</div> </div>
{:then object} {:then object}
{#if object} {#if object}
<div transition:slide={{ axis: 'y', easing: expoOut }}> <div
transition:slide={{ axis: 'y', easing: expoOut }}
class="divide-y divide-slate-200 dark:divide-zinc-900"
>
{#if object.community} {#if object.community}
<div <div
class="-mx-4 sm:-mx-6 px-6 hover:bg-slate-100 dark:hover:bg-zinc-800 transition-colors" class="-mx-4 sm:-mx-6 px-6 hover:bg-slate-100 dark:hover:bg-zinc-800 transition-colors"