flotilla/src/app/components/ChatCompose.svelte

53 lines
1.6 KiB
Svelte
Raw Normal View History

2024-08-19 21:22:16 +00:00
<script lang="ts">
2024-08-26 21:43:43 +00:00
import {onMount} from "svelte"
import type {Readable} from "svelte/store"
2024-08-29 16:23:54 +00:00
import {writable} from "svelte/store"
import {createEditor, type Editor, EditorContent} from "svelte-tiptap"
2024-09-11 18:21:16 +00:00
import {createEvent} from "@welshman/util"
2024-08-30 23:25:41 +00:00
import {publishThunk, makeThunk} from "@welshman/app"
2024-08-26 21:43:43 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
2024-09-23 20:58:01 +00:00
import {getEditorOptions, getEditorTags, addFile} from "@lib/editor"
2024-09-23 17:28:33 +00:00
import {ROOM, MESSAGE, GENERAL} from "@app/state"
2024-09-23 20:58:01 +00:00
import {getPubkeyHints} from "@app/commands"
2024-08-23 20:33:36 +00:00
export let url
2024-09-23 17:28:33 +00:00
export let room = GENERAL
2024-08-19 21:22:16 +00:00
2024-09-23 20:58:01 +00:00
const loading = writable(false)
2024-08-19 21:22:16 +00:00
2024-08-29 16:23:54 +00:00
let editor: Readable<Editor>
2024-08-22 23:16:35 +00:00
2024-09-23 20:58:01 +00:00
const submit = () => {
2024-09-11 18:21:16 +00:00
const event = createEvent(MESSAGE, {
2024-08-23 20:33:36 +00:00
content: $editor.getText(),
2024-09-23 20:58:01 +00:00
tags: [[ROOM, room], ...getEditorTags($editor)],
2024-08-22 23:16:35 +00:00
})
2024-08-23 20:33:36 +00:00
publishThunk(makeThunk({event, relays: [url]}))
2024-08-23 20:33:36 +00:00
$editor.chain().clearContent().run()
2024-08-22 23:16:35 +00:00
}
2024-08-19 21:22:16 +00:00
onMount(() => {
editor = createEditor(getEditorOptions({submit, loading, getPubkeyHints, submitOnEnter: true, autofocus: true}))
2024-08-19 21:22:16 +00:00
})
</script>
2024-08-26 21:43:43 +00:00
<div
class="shadow-top-xl relative z-feature flex gap-2 border-t border-solid border-base-100 bg-base-100 p-2">
<Button
2024-09-11 22:21:25 +00:00
data-tip="Add an image"
2024-09-23 20:58:01 +00:00
class="center tooltip h-10 w-10 rounded-box bg-base-300 transition-colors hover:bg-base-200"
2024-09-11 22:21:25 +00:00
on:click={() => addFile($editor)}>
2024-09-23 20:58:01 +00:00
{#if $loading}
2024-08-22 23:16:35 +00:00
<span class="loading loading-spinner loading-xs"></span>
{:else}
<Icon icon="gallery-send" />
{/if}
</Button>
2024-09-23 20:58:01 +00:00
<div class="chat-editor flex-grow overflow-hidden">
2024-08-22 23:16:35 +00:00
<EditorContent editor={$editor} />
</div>
2024-08-19 21:22:16 +00:00
</div>