flotilla/src/app/components/ThreadReply.svelte

69 lines
2 KiB
Svelte
Raw Normal View History

2024-10-23 21:20:24 +00:00
<script lang="ts">
import {onMount} from "svelte"
import type {Readable} from "svelte/store"
import {writable} from "svelte/store"
import {createEditor, type Editor, EditorContent} from "svelte-tiptap"
2024-10-23 23:16:28 +00:00
import {fly, slideAndFade} 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"
2024-10-24 20:05:25 +00:00
import {getPubkeyHints, publishComment} from "@app/commands"
2024-10-23 21:20:24 +00:00
import {getEditorOptions, addFile, uploadFiles, getEditorTags} from "@lib/editor"
2024-10-24 19:55:48 +00:00
import {pushToast} from '@app/toast'
2024-10-23 21:20:24 +00:00
export let url
export let event
2024-10-23 23:16:28 +00:00
export let onClose
export let onSubmit
2024-10-23 21:20:24 +00:00
const startSubmit = () => uploadFiles($editor)
const loading = writable(false)
const submit = () => {
2024-10-24 19:55:48 +00:00
const content = $editor.getText()
const tags = getEditorTags($editor)
if (!content.trim()) {
return pushToast({
theme: 'error',
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
}
let editor: Readable<Editor>
onMount(() => {
editor = createEditor(getEditorOptions({submit, loading, getPubkeyHints, autofocus: true}))
})
</script>
2024-10-23 23:16:28 +00:00
<form
in:fly
out:slideAndFade
2024-10-24 19:55:48 +00:00
on:submit|preventDefault={startSubmit}
2024-10-23 23:16:28 +00:00
class="card2 sticky bottom-2 z-feature mx-2 mt-2 bg-neutral">
<div class="relative">
<div class="note-editor flex-grow overflow-hidden">
<EditorContent editor={$editor} />
2024-10-23 21:20:24 +00:00
</div>
2024-10-23 23:16:28 +00:00
<Button
data-tip="Add an image"
class="tooltip tooltip-left absolute bottom-1 right-2"
on:click={() => addFile($editor)}>
{#if $loading}
<span class="loading loading-spinner loading-xs"></span>
{:else}
<Icon icon="paperclip" size={3} />
{/if}
2024-10-23 21:20:24 +00:00
</Button>
</div>
2024-10-23 23:16:28 +00:00
<ModalFooter>
<Button class="btn btn-link" on:click={onClose}>Cancel</Button>
<Button type="submit" class="btn btn-primary">Post Reply</Button>
</ModalFooter>
</form>