flotilla/src/app/components/ChatCompose.svelte

63 lines
2 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"
import {NProfileExtension, ImageExtension} from "nostr-editor"
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-30 16:25:39 +00:00
import {findNodes} from "@lib/tiptap"
2024-08-26 21:43:43 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
2024-08-29 16:23:54 +00:00
import {makeMention, makeIMeta} from "@app/commands"
import {getChatEditorOptions, addFile} from "@app/editor"
2024-09-23 17:28:33 +00:00
import {ROOM, MESSAGE, GENERAL} from "@app/state"
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-08-29 16:23:54 +00:00
const uploading = 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-08-29 16:23:54 +00:00
const sendMessage = () => {
2024-08-23 20:33:36 +00:00
const json = $editor.getJSON()
const mentionTags = findNodes(NProfileExtension.name, json).map(m =>
makeMention(m.attrs!.pubkey, m.attrs!.relays),
)
const imetaTags = findNodes(ImageExtension.name, json).map(({attrs: {src, sha256: x}}: any) =>
makeIMeta(src, {x, ox: x}),
)
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 17:28:33 +00:00
tags: [[ROOM, room], ...mentionTags, ...imetaTags],
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(() => {
2024-08-29 16:23:54 +00:00
editor = createEditor(getChatEditorOptions({uploading, sendMessage}))
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"
class="center h-10 w-10 rounded-box bg-base-300 transition-colors hover:bg-base-200 tooltip"
on:click={() => addFile($editor)}>
2024-08-29 16:23:54 +00:00
{#if $uploading}
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-12 23:56:33 +00:00
<div class="flex-grow overflow-hidden chat-editor">
2024-08-22 23:16:35 +00:00
<EditorContent editor={$editor} />
</div>
2024-08-19 21:22:16 +00:00
</div>