flotilla/src/app/components/EventReply.svelte

92 lines
2.5 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"
2024-10-23 21:20:24 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
2025-02-05 16:17:51 +00:00
import EditorContent from "@app/editor/EditorContent.svelte"
import {publishComment, canEnforceNip70} from "@app/commands"
2025-05-27 22:04:44 +00:00
import {PROTECTED} from "@app/state"
import {makeEditor} from "@app/editor"
2024-10-24 22:48:45 +00:00
import {pushToast} from "@app/toast"
2024-10-23 21:20:24 +00:00
const {url, event, onClose, onSubmit} = $props()
2024-10-23 21:20:24 +00:00
2025-01-02 18:04:28 +00:00
const uploading = writable(false)
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 canEnforceNip70(url)) {
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.",
})
}
2024-10-24 20:05:25 +00:00
onSubmit(publishComment({event, content, tags, relays: [url]}))
2024-10-23 21:20:24 +00:00
}
2025-04-15 18:03:27 +00:00
const editor = makeEditor({url, submit, uploading, autofocus: !isMobile})
2025-03-19 17:30:36 +00:00
let form: HTMLElement
let spacer: HTMLElement
onMount(() => {
setTimeout(() => {
spacer.scrollIntoView({block: "end", behavior: "smooth"})
})
const observer = new ResizeObserver(() => {
spacer!.style.minHeight = `${form!.offsetHeight}px`
})
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>
2024-10-23 23:16:28 +00:00
<form
in:fly
2025-03-19 17:30:36 +00:00
bind:this={form}
2025-02-04 00:37:14 +00:00
onsubmit={preventDefault(submit)}
2025-03-19 17:30:36 +00:00
class="cb cw fixed z-feature -mx-2 pt-3">
<div class="card2 mx-2 my-2 bg-neutral">
<div class="relative">
<div class="note-editor flex-grow overflow-hidden">
<EditorContent {editor} />
</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}
<Icon icon="paperclip" size={3} />
{/if}
</Button>
2024-10-23 21:20:24 +00:00
</div>
2025-03-19 17:30:36 +00:00
<ModalFooter>
<Button class="btn btn-link" onclick={onClose}>Cancel</Button>
<Button type="submit" class="btn btn-primary">Post Reply</Button>
</ModalFooter>
2024-10-23 21:20:24 +00:00
</div>
2024-10-23 23:16:28 +00:00
</form>