2026-08-17 15:42:14 +00:00
|
|
|
<script lang="ts">
|
|
|
|
|
import {writable} from "svelte/store"
|
|
|
|
|
import type {TrustedEvent} from "@welshman/util"
|
|
|
|
|
import {publishToRelays} from "@welshman/app"
|
|
|
|
|
import {Comment} from "@welshman/domain"
|
|
|
|
|
import {isMobile, preventDefault} from "@lib/html"
|
|
|
|
|
import Paperclip from "@assets/icons/paperclip-2.svg?dataurl"
|
|
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
|
|
|
|
import Button from "@lib/components/Button.svelte"
|
|
|
|
|
import Spinner from "@lib/components/Spinner.svelte"
|
|
|
|
|
import EditorContent from "@app/editor/EditorContent.svelte"
|
|
|
|
|
import {command, relays, writer} from "@app/core"
|
|
|
|
|
import {DraftKey} from "@app/drafts"
|
|
|
|
|
import {makeEditor} from "@app/editor"
|
|
|
|
|
import {pushToast} from "@app/toast"
|
|
|
|
|
|
|
|
|
|
type Values = {
|
|
|
|
|
content?: string | object
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
url: string
|
|
|
|
|
event: TrustedEvent
|
|
|
|
|
parent?: TrustedEvent
|
|
|
|
|
onCancel: () => void
|
|
|
|
|
onSubmit: () => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {url, event, parent, onCancel, onSubmit}: Props = $props()
|
|
|
|
|
|
|
|
|
|
const draftKey = new DraftKey<Values>(`comment:${event.id}:${parent?.id ?? ""}`)
|
|
|
|
|
const initialValues = draftKey.get()
|
|
|
|
|
const uploading = writable(false)
|
|
|
|
|
|
|
|
|
|
const selectFiles = () => editor.then(ed => ed.commands.selectFiles())
|
|
|
|
|
|
|
|
|
|
const submit = async () => {
|
|
|
|
|
if ($uploading || loading) return
|
|
|
|
|
|
|
|
|
|
const ed = await editor
|
|
|
|
|
const content = ed.getText({blockSeparator: "\n"}).trim()
|
|
|
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
|
return pushToast({theme: "error", message: "Please write something first."})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loading = true
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const eventWriter = writer(Comment)
|
|
|
|
|
.setContent(content)
|
|
|
|
|
.addTags(...ed.storage.nostr.getEditorTags())
|
|
|
|
|
.setRootFromEvent(event)
|
|
|
|
|
.setParentFromEvent(parent ?? event)
|
|
|
|
|
.setProtected(await $relays.hasNip(url, 70))
|
|
|
|
|
|
|
|
|
|
const thunk = await command(eventWriter).then(publishToRelays([url]))
|
|
|
|
|
const error = await thunk.waitForError()
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return pushToast({theme: "error", message: error})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draftKey.clear()
|
|
|
|
|
onSubmit()
|
|
|
|
|
} finally {
|
|
|
|
|
loading = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let loading = $state(false)
|
|
|
|
|
let content = $state(initialValues?.content ?? "")
|
|
|
|
|
|
|
|
|
|
const onChange = (json: object) => {
|
|
|
|
|
content = json
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-01 04:56:37 +00:00
|
|
|
const empty = writable(true)
|
|
|
|
|
|
2026-08-17 15:42:14 +00:00
|
|
|
const editor = makeEditor({
|
|
|
|
|
url,
|
|
|
|
|
submit,
|
|
|
|
|
uploading,
|
|
|
|
|
onChange,
|
|
|
|
|
content,
|
2026-09-01 04:56:37 +00:00
|
|
|
empty,
|
2026-08-17 15:42:14 +00:00
|
|
|
placeholder: parent ? "Write a reply..." : "Write a comment...",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
$effect(() => {
|
2026-09-01 04:56:37 +00:00
|
|
|
if ($empty) {
|
|
|
|
|
draftKey.clear()
|
|
|
|
|
} else {
|
|
|
|
|
draftKey.set({content})
|
|
|
|
|
}
|
2026-08-17 15:42:14 +00:00
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
2026-08-18 21:18:10 +00:00
|
|
|
<form
|
|
|
|
|
onsubmit={preventDefault(submit)}
|
|
|
|
|
class="border-line flex flex-col gap-2 rounded-2xl border border-solid p-3">
|
2026-08-17 15:42:14 +00:00
|
|
|
<div class="relative">
|
|
|
|
|
<div class="note-editor grow overflow-hidden">
|
|
|
|
|
<EditorContent autofocus={!isMobile} {editor} />
|
|
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
data-tip="Add an image"
|
|
|
|
|
class="tip tip-left absolute bottom-1 right-2"
|
|
|
|
|
onclick={selectFiles}
|
|
|
|
|
disabled={loading}>
|
|
|
|
|
{#if $uploading}
|
|
|
|
|
<Spinner size="xs" />
|
|
|
|
|
{:else}
|
|
|
|
|
<Icon icon={Paperclip} size={3} />
|
|
|
|
|
{/if}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="flex justify-end gap-2">
|
|
|
|
|
<Button class="button button-link" onclick={onCancel} disabled={loading}>Cancel</Button>
|
|
|
|
|
<Button type="submit" class="button button-primary button-sm" disabled={$uploading || loading}>
|
|
|
|
|
<Spinner {loading}>
|
|
|
|
|
{#if parent}
|
|
|
|
|
Reply
|
|
|
|
|
{:else}
|
|
|
|
|
Comment
|
|
|
|
|
{/if}
|
|
|
|
|
</Spinner>
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|