flotilla/src/app/components/RoomCompose.svelte

195 lines
5.5 KiB
Svelte
Raw Normal View History

2024-08-19 21:22:16 +00:00
<script lang="ts">
2025-01-02 18:04:28 +00:00
import {writable} from "svelte/store"
2026-08-29 17:20:39 +00:00
import type {Maybe} from "@welshman/lib"
2026-09-07 16:48:36 +00:00
import {MESSAGE} from "@welshman/util"
2025-04-15 18:03:27 +00:00
import type {EventContent} from "@welshman/util"
2026-09-01 04:56:37 +00:00
import {escapeHtml, isMobile, preventDefault} from "@lib/html"
2025-09-10 22:26:39 +00:00
import GallerySend from "@assets/icons/gallery-send.svg?dataurl"
2025-10-06 18:23:19 +00:00
import WidgetAdd from "@assets/icons/widget-add.svg?dataurl"
2025-09-10 22:26:39 +00:00
import Plane from "@assets/icons/plane-2.svg?dataurl"
2024-08-26 21:43:43 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
2025-10-06 18:23:19 +00:00
import Tippy from "@lib/components/Tippy.svelte"
2026-08-29 17:20:39 +00:00
import type {TippyController} from "@lib/components/Tippy.svelte"
import Spinner from "@lib/components/Spinner.svelte"
2025-10-06 18:23:19 +00:00
import ComposeMenu from "@app/components/ComposeMenu.svelte"
2026-09-01 04:56:37 +00:00
import DictationButton from "@app/components/DictationButton.svelte"
2026-09-07 16:48:36 +00:00
import CommandArgBar from "@app/components/CommandArgBar.svelte"
2025-02-05 16:17:51 +00:00
import EditorContent from "@app/editor/EditorContent.svelte"
import {makeEditor} from "@app/editor"
2026-09-07 16:48:36 +00:00
import {app} from "@app/core"
2026-08-11 21:54:05 +00:00
import {DraftKey, type Draft} from "@app/drafts"
import type {Share} from "@app/share"
import {onDestroy, onMount} from "svelte"
2024-08-23 20:33:36 +00:00
2025-04-15 18:03:27 +00:00
type Props = {
url?: string
2025-10-30 22:33:34 +00:00
h?: string
2025-11-05 00:59:17 +00:00
onEscape?: () => void
onEditPrevious?: () => void
2025-04-15 18:03:27 +00:00
onSubmit: (event: EventContent) => void
2026-08-11 21:54:05 +00:00
initialValues?: Share
2025-02-04 00:37:14 +00:00
}
2026-08-11 21:54:05 +00:00
const {url, h, initialValues, onEscape, onEditPrevious, onSubmit}: Props = $props()
2026-08-21 15:31:53 +00:00
const draftKey =
(url || h) && !initialValues ? new DraftKey<Draft>(`room:${url ?? ""}:${h ?? ""}`) : undefined
2025-02-04 04:57:47 +00:00
const autofocus = !isMobile
2025-01-02 18:04:28 +00:00
const uploading = writable(false)
2025-01-02 18:04:28 +00:00
2026-09-07 16:48:36 +00:00
const text = writable("")
const commandTarget = {
url,
kind: MESSAGE,
pubkey: $app.user?.pubkey,
tags: h ? [["h", h]] : [],
}
export const focus = () => editor.then(ed => ed.chain().focus().run())
2025-02-04 04:57:47 +00:00
export const canEnterEditPrevious = () =>
editor.then(ed => ed.getText({blockSeparator: "\n"}) === "")
const handleKeyDown = async (event: KeyboardEvent) => {
2025-11-05 00:59:17 +00:00
if (event.key === "Escape") {
onEscape?.()
}
if (event.key === "ArrowUp" && (await canEnterEditPrevious())) {
onEditPrevious?.()
}
}
const uploadFiles = () => editor.then(ed => ed.chain().selectFiles().run())
2025-01-02 18:04:28 +00:00
2026-09-01 04:56:37 +00:00
// Tiptap parses a string handed to insertContent as html, so an angle bracket in the
// transcript would eat the rest of the sentence.
2026-09-07 16:48:36 +00:00
const insertTranscript = async (transcript: string) => {
const ed = await editor
ed.chain().focus().insertContent(escapeHtml(transcript)).run()
}
// Argument tokens are whitespace-delimited, so separate one from whatever precedes it.
const insertCommandToken = async (token: string) => {
2026-09-01 04:56:37 +00:00
const ed = await editor
2026-09-07 16:48:36 +00:00
const separator = /\s$/.test($text) ? "" : " "
2026-09-01 04:56:37 +00:00
2026-09-07 16:48:36 +00:00
ed.chain()
.focus()
.insertContent(escapeHtml(separator + token))
.run()
2026-09-01 04:56:37 +00:00
}
2026-08-29 17:20:39 +00:00
const showPopover = () => tippy?.show()
const hidePopover = () => tippy?.hide()
2025-10-06 18:23:19 +00:00
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()
2025-01-15 17:07:12 +00:00
onSubmit({content, tags})
2024-08-23 20:33:36 +00:00
draftKey?.clear()
2026-09-01 04:56:37 +00:00
ed.chain().clearContent(true).run()
2024-08-22 23:16:35 +00:00
}
2026-08-29 17:20:39 +00:00
let tippy: Maybe<TippyController> = $state()
2026-08-11 21:54:05 +00:00
let content = $state(
initialValues?.type === "text" ? initialValues.value : (draftKey?.get()?.content ?? ""),
)
2026-09-01 04:56:37 +00:00
let recording = $state(false)
2026-04-07 13:06:29 +00:00
const onChange = (json: object) => {
content = json
}
2026-09-01 04:56:37 +00:00
const empty = writable(true)
const editor = makeEditor({
url,
2026-04-07 13:06:29 +00:00
content,
2026-09-01 04:56:37 +00:00
empty,
submit,
2026-09-07 16:48:36 +00:00
text,
uploading,
onChange,
2026-09-07 16:48:36 +00:00
commandTarget,
aggressive: true,
})
2025-10-06 18:23:19 +00:00
2026-04-07 13:06:29 +00:00
$effect(() => {
2026-09-01 04:56:37 +00:00
if ($empty) {
draftKey?.clear()
} else {
draftKey?.set({content})
}
2026-04-07 13:06:29 +00:00
})
onMount(async () => {
const ed = await editor
ed.view.dom.addEventListener("keydown", handleKeyDown)
2026-08-11 21:54:05 +00:00
if (initialValues?.type === "file") {
ed.chain()
.addFile(initialValues.value, ed.state.selection.from + 1)
.run()
}
})
onDestroy(async () => {
const ed = await editor
ed?.view?.dom.removeEventListener("keydown", handleKeyDown)
})
2024-08-19 21:22:16 +00:00
</script>
2026-09-07 16:48:36 +00:00
<CommandArgBar target={commandTarget} content={$text} insert={insertCommandToken} />
<form class="relative flex gap-2 py-2" onsubmit={preventDefault(submit)}>
2025-10-06 18:23:19 +00:00
<div class="join">
<Button
class="join-item h-10 w-10 min-w-10 button button-neutral"
2025-10-06 18:23:19 +00:00
disabled={$uploading}
onclick={uploadFiles}>
{#if $uploading}
<Spinner size="xs" />
2025-10-06 18:23:19 +00:00
{:else}
<Icon icon={GallerySend} />
{/if}
</Button>
<Button
disabled={$uploading}
onclick={showPopover}
class="join-item h-10 w-10 min-w-10 button button-neutral">
<Tippy
2026-08-29 17:20:39 +00:00
bind:controller={tippy}
component={ComposeMenu}
props={{url, h, onClick: hidePopover}}
params={{trigger: "manual", interactive: true}}>
2025-10-06 18:23:19 +00:00
<Icon icon={WidgetAdd} />
</Tippy>
</Button>
2025-10-06 18:23:19 +00:00
</div>
<div class="chat-editor grow overflow-hidden">
2026-04-07 13:06:29 +00:00
<EditorContent {autofocus} {editor} />
2024-08-22 23:16:35 +00:00
</div>
2026-09-01 04:56:37 +00:00
{#if recording || $empty}
<DictationButton bind:recording onTranscript={insertTranscript} />
{:else}
<Button
data-tip="{window.navigator.platform.includes('Mac') ? 'cmd' : 'ctrl'}+enter to send"
class="button button-primary button-circle flex justify-center items-center tip tip-left h-10 w-10 min-w-10"
disabled={$uploading}
onclick={submit}>
<Icon icon={Plane} />
</Button>
{/if}
2024-11-16 16:04:09 +00:00
</form>