flotilla/src/app/components/EventReply.svelte

130 lines
3.6 KiB
Svelte
Raw Normal View History

2024-10-23 21:20:24 +00:00
<script lang="ts">
2025-03-19 17:30:36 +00:00
import {onMount} from "svelte"
2025-01-02 18:04:28 +00:00
import {writable} from "svelte/store"
import type {TrustedEvent} from "@welshman/util"
2025-02-04 03:28:29 +00:00
import {isMobile, preventDefault} from "@lib/html"
2025-03-19 17:30:36 +00:00
import {fly} from "@lib/transition"
2025-09-10 22:26:39 +00:00
import Paperclip from "@assets/icons/paperclip-2.svg?dataurl"
2024-10-23 21:20:24 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
import Spinner from "@lib/components/Spinner.svelte"
2025-02-05 16:17:51 +00:00
import EditorContent from "@app/editor/EditorContent.svelte"
import ChatComposeParent from "@app/components/ChatComposeParent.svelte"
import {publishComment} from "@app/comments"
import {canEnforceNip70} from "@app/relays"
import {PROTECTED, prependParent} from "@app/groups"
import {makeEditor} from "@app/editor"
2026-06-08 21:14:40 +00:00
import {DraftKey} from "@app/drafts"
import {pushToast} from "@app/toast"
2024-10-23 21:20:24 +00:00
2026-04-07 13:06:29 +00:00
type Values = {
content?: string | object
}
type Props = {
url: string
event: TrustedEvent
parent?: TrustedEvent
onClose: () => void
onClearParent?: () => void
onSubmit: (thunk: unknown) => void
}
const {url, event, parent, onClose, onClearParent, onSubmit}: Props = $props()
const draftKey = new DraftKey<Values>(`reply:${event.id}:${parent?.id || ""}`)
2026-04-07 13:06:29 +00:00
const initialValues = draftKey.get()
const shouldProtect = canEnforceNip70(url)
2025-01-02 18:04:28 +00:00
const uploading = writable(false)
2026-04-07 13:06:29 +00:00
const autofocus = !isMobile
2025-01-02 18:04:28 +00:00
const selectFiles = () => editor.then(ed => ed.commands.selectFiles())
const submit = async () => {
2025-01-02 18:04:28 +00:00
if ($uploading) return
2024-11-16 16:04:09 +00:00
const ed = await editor
let content = ed.getText({blockSeparator: "\n"}).trim()
let tags = ed.storage.nostr.getEditorTags()
if (await shouldProtect) {
tags.push(PROTECTED)
}
2024-10-24 19:55:48 +00:00
2025-01-02 18:04:28 +00:00
if (!content) {
2024-10-24 19:55:48 +00:00
return pushToast({
2024-10-24 22:48:45 +00:00
theme: "error",
2024-10-24 19:55:48 +00:00
message: "Please provide a message for your reply.",
})
}
if (parent) {
;({content, tags} = prependParent(parent, {content, tags}, url))
}
draftKey.clear()
2024-10-24 20:05:25 +00:00
onSubmit(publishComment({event, content, tags, relays: [url]}))
2024-10-23 21:20:24 +00:00
}
2025-03-19 17:30:36 +00:00
let form: HTMLElement
let spacer: HTMLElement
2026-04-07 13:06:29 +00:00
let content = $state(initialValues?.content ?? "")
const onChange = (json: object) => {
content = json
}
const editor = makeEditor({url, submit, uploading, content, onChange})
$effect(() => {
draftKey.set({content})
})
2025-03-19 17:30:36 +00:00
onMount(() => {
setTimeout(() => {
spacer.scrollIntoView({block: "end", behavior: "smooth"})
})
const observer = new ResizeObserver(() => {
spacer!.style.minHeight = `${form!.offsetHeight + 60}px`
2025-03-19 17:30:36 +00:00
})
observer.observe(form!)
return () => {
observer.unobserve(form!)
}
})
2024-10-23 21:20:24 +00:00
</script>
2025-03-19 17:30:36 +00:00
<div bind:this={spacer}></div>
<form
in:fly
bind:this={form}
onsubmit={preventDefault(submit)}
class="left-content bottom-sai right-sai fixed z-feature mb-14 md:mb-0 w-full md:w-auto pr-2">
<div class="card mx-2 my-2 shadow-md">
{#if parent}
<ChatComposeParent event={parent} clear={() => onClearParent?.()} verb="Replying to" />
{/if}
2025-03-19 17:30:36 +00:00
<div class="relative">
2026-04-07 21:53:19 +00:00
<div class="note-editor grow overflow-hidden">
2026-04-07 13:06:29 +00:00
<EditorContent {autofocus} {editor} />
2025-03-19 17:30:36 +00:00
</div>
<Button
data-tip="Add an image"
class="tip tip-left absolute bottom-1 right-2"
onclick={selectFiles}>
2025-03-19 17:30:36 +00:00
{#if $uploading}
<Spinner size="xs" />
2025-03-19 17:30:36 +00:00
{:else}
2025-09-10 22:26:39 +00:00
<Icon icon={Paperclip} size={3} />
2025-03-19 17:30:36 +00:00
{/if}
</Button>
2024-10-23 21:20:24 +00:00
</div>
2026-02-03 22:18:58 +00:00
<div class="flex justify-between pt-3">
<Button class="button button-link" onclick={onClose}>Cancel</Button>
<Button type="submit" class="button button-primary">Post Reply</Button>
2026-02-03 22:18:58 +00:00
</div>
2024-10-23 21:20:24 +00:00
</div>
2024-10-23 23:16:28 +00:00
</form>