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">
import { page } from '$app/state'
import { profile } from '$lib/auth.svelte.js'
import CommentActions from '$lib/components/lemmy/comment/CommentActions.svelte'
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
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 { t } from '$lib/i18n/translations'
import { getClient } from '$lib/lemmy.svelte.js'
import { errorMessage } from '$lib/lemmy/error'
import { Button, Modal, toast } from 'mono-svelte'
import { onMount } from 'svelte'
import {
Bookmark,
Icon,
Microphone,
Minus,
Pencil,
Plus,
Trash,
} from 'svelte-hero-icons'
import { Bookmark, Icon, Microphone, Pencil, Trash } from 'svelte-hero-icons'
import { expoOut } from 'svelte/easing'
import type { ClassValue } from 'svelte/elements'
import { fly, slide } from 'svelte/transition'
import { slide } from 'svelte/transition'
import ShieldIcon from '../moderation/ShieldIcon.svelte'
import CommentForm from './CommentForm.svelte'
import type { CommentNodeI } from './comments.svelte'
import { errorMessage } from '$lib/lemmy/error'
interface Props {
node: CommentNodeI
@@ -60,20 +49,22 @@
let newComment = $state(node.comment_view.comment.content)
let editingLoad = $state(false)
async function save() {
async function save(type: 'reply' | 'edit') {
if (!profile.data?.jwt || newComment.length <= 0) return
editingLoad = true
try {
await getClient().editComment({
comment_id: node.comment_view.comment.id,
content: newComment,
})
if (type == 'edit') {
await getClient().editComment({
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) {
toast({
content: errorMessage(err as string),
@@ -83,56 +74,76 @@
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>
{#if editing}
<Modal bind:open={editing}>
{#if editing || replying}
<Modal
bind:open={
() => replying || editing,
v => {
replying = v
editing = v
}
}
>
{#snippet customTitle()}
<span>{$t('form.edit')}</span>
<span>{replying ? $t('comment.reply') : $t('form.edit')}</span>
{/snippet}
<form
onsubmit={e => {
e.preventDefault()
save()
save(replying ? 'reply' : 'edit')
}}
class="contents"
>
<CommentForm
bind:value={newComment}
postId={node.comment_view.comment.id}
actions={false}
preview={true}
onconfirm={save}
/>
<Button
submit
color="primary"
size="lg"
loading={editingLoad}
disabled={editingLoad}
class="w-full"
>
{$t('common.save')}
</Button>
{#if replying}
<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)}
/>
{: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>
</Modal>
{/if}
<li
class="py-3 relative {node.comment_view.comment.distinguished
? ' text-primary-900 dark:text-primary-100'
: ''} {highlight} {clazz}"
class={[
'py-3 relative',
node.comment_view.comment.distinguished &&
' text-primary-900 dark:text-primary-100',
clazz,
]}
id={node.comment_view.comment.id.toString()}
>
{#if meta}
@@ -142,28 +153,6 @@
z-0 group relative"
>
{@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">
<UserLink
inComment
@@ -222,17 +211,10 @@
class="flex flex-col whitespace-pre-wrap
max-w-full gap-1 mt-1 relative"
>
{#if node.comment_view.comment.distinguished}
<div
class="-z-10 bg-slate-100 dark:bg-zinc-900 absolute -top-9 -bottom-1.5
-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>
<Markdown
source={node.comment_view.comment.content}
class="text-[15px] text-slate-800 dark:text-zinc-200 leading-[1.3]"
/>
{#if actions}
<CommentActions
comment={node.comment_view}
@@ -243,33 +225,6 @@
/>
{/if}
</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">
{@render children?.()}
</div>
@@ -2,8 +2,8 @@
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
import { publishedToDate } from '$lib/components/util/date.js'
import { settings, type View } from '$lib/settings.svelte.js'
import { t } from '$lib/i18n/translations'
import { settings, type View } from '$lib/settings.svelte.js'
import type { CommentView } from 'lemmy-js-client'
import { Button, Material } from 'mono-svelte'
import { ArrowUturnUp, Icon } from 'svelte-hero-icons'
@@ -29,9 +29,9 @@
</script>
<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}"
color="distinct"
color="none"
padding="none"
>
{#if meta}
@@ -5,8 +5,9 @@
import { t } from '$lib/i18n/translations'
import type { Community } from 'lemmy-js-client'
import { ExclamationTriangle, Icon } from 'svelte-hero-icons'
import type { HTMLAnchorAttributes } from 'svelte/elements'
interface Props {
interface Props extends HTMLAnchorAttributes {
community: Community
avatar?: boolean
name?: boolean
+1 -1
View File
@@ -101,7 +101,7 @@
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<div
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',
view == 'compact' && 'py-3 list-type compact',
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'
interface Props {
community?: Community | undefined
community?: Community
showCommunity?: boolean
subscribed?: SubscribedType | undefined
user?: Person | undefined
published?: Date | undefined
title?: string | undefined
id?: number | undefined
subscribed?: SubscribedType
user?: Person
published?: Date
title?: string
id?: number
read?: boolean
edited?: string | undefined
edited?: string
view?: View
// Badges
badges?: {
@@ -104,12 +104,12 @@
community = $bindable(undefined),
showCommunity = true,
subscribed = $bindable(undefined),
user = undefined,
published = undefined,
title = undefined,
id = undefined,
user,
published,
title,
id,
read = false,
edited = undefined,
edited,
view = 'cozy',
badges = {
nsfw: false,
@@ -57,11 +57,11 @@
let image = $state<FileList | null | undefined>(null)
const shortcuts = {
KeyB: () => wrapSelection('**', '**'),
KeyI: () => wrapSelection('*', '*'),
KeyS: () => wrapSelection('~~', '~~'),
KeyH: () => wrapSelection('\n# ', ''),
KeyK: () => wrapSelection('[](', ')'),
b: () => wrapSelection('**', '**'),
i: () => wrapSelection('*', '*'),
s: () => wrapSelection('~~', '~~'),
h: () => wrapSelection('\n# ', ''),
k: () => wrapSelection('[](', ')'),
}
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 {
images?: boolean
value?: string
@@ -255,8 +267,9 @@ overflow-hidden transition-colors {clazz}"
onkeydown={e => {
if (disabled) return
if (e.ctrlKey || e.metaKey) {
handleKeydown(e)
// @ts-expect-error yes it can
let shortcut = shortcuts[e.code]
let shortcut = shortcuts[e.key]
if (shortcut) {
e.preventDefault()
shortcut?.(e)