flotilla/src/app/components/EventReply.svelte

110 lines
3 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"
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"
2025-02-05 16:17:51 +00:00
import EditorContent from "@app/editor/EditorContent.svelte"
import {publishComment, canEnforceNip70} from "@app/core/commands"
import {PROTECTED} from "@app/core/state"
import {makeEditor} from "@app/editor"
import {DraftKey} from "@app/util/drafts"
import {pushToast} from "@app/util/toast"
2024-10-23 21:20:24 +00:00
2026-04-07 13:06:29 +00:00
type Values = {
content?: string | object
}
2026-04-07 13:06:29 +00:00
const {url, event, onClose, onSubmit} = $props()
const draftKey = new DraftKey<Values>(`reply:${event.id}`)
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
const content = ed.getText({blockSeparator: "\n"}).trim()
const 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.",
})
}
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">
2026-02-03 22:18:58 +00:00
<div class="card2 mx-2 my-2 bg-alt shadow-md">
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="tooltip tooltip-left absolute bottom-1 right-2"
onclick={selectFiles}>
2025-03-19 17:30:36 +00:00
{#if $uploading}
<span class="loading loading-spinner loading-xs"></span>
{: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">
2025-03-19 17:30:36 +00:00
<Button class="btn btn-link" onclick={onClose}>Cancel</Button>
<Button type="submit" class="btn btn-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>