flotilla/src/app/components/Compose.svelte

239 lines
6.5 KiB
Svelte
Raw Normal View History

2024-08-19 21:22:16 +00:00
<script lang="ts">
import {onDestroy, onMount} from "svelte"
2025-01-02 18:04:28 +00:00
import {writable} from "svelte/store"
import cx from "classnames"
import type {MaybeAsync} 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"
import MenuButton from "@lib/components/MenuButton.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-25 20:32:44 +00:00
import EditorEmojiButton from "@app/components/EditorEmojiButton.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"
import {getDictation} from "@app/dictation"
import {pushToast} from "@app/toast"
2026-08-11 21:54:05 +00:00
import {DraftKey, type Draft} from "@app/drafts"
import type {Share} from "@app/share"
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
draftKey: string
encryptFiles?: boolean
disabled?: boolean
initialValues?: Share
2025-11-05 00:59:17 +00:00
onEscape?: () => void
onEditPrevious?: () => void
onSubmit: (event: EventContent) => MaybeAsync<void>
2025-02-04 00:37:14 +00:00
}
const {
url,
h,
draftKey,
encryptFiles = false,
disabled = false,
initialValues,
onEscape,
onEditPrevious,
onSubmit,
}: Props = $props()
const draft = initialValues ? undefined : new DraftKey<Draft>(draftKey)
const autofocus = !isMobile && !disabled
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
? {url, kind: MESSAGE, pubkey: $app.user?.pubkey, tags: h ? [["h", h]] : []}
: undefined
const buttonClass = url
? "join-item h-10 w-10 min-w-10 button button-neutral"
: "button button-neutral button-square tip tip-right h-10 w-10 min-w-10 rounded-2xl transition-colors"
const editorClass = $derived(
cx("chat-editor grow overflow-hidden", {
"pointer-events-none opacity-50": disabled,
}),
)
2026-09-07 16:48:36 +00:00
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
// Tiptap parses a string handed to insertContent as html, so an angle bracket would eat 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()
}
const attachVoiceNote = async (audio: File) => {
const ed = await editor
ed.chain()
.addFile(audio, ed.state.selection.from + 1)
.run()
}
2026-09-07 16:48:36 +00:00
// 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
}
const submit = async () => {
if ($uploading || disabled || sending) {
2026-09-21 16:48:35 +00:00
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
sending = true
try {
await onSubmit({content, tags})
} catch (error) {
console.error("Failed to send message", error)
return pushToast({theme: "error", message: "Failed to send your message."})
} finally {
sending = false
}
2024-08-23 20:33:36 +00:00
draft?.clear()
2026-09-01 04:56:37 +00:00
ed.chain().clearContent(true).run()
2024-08-22 23:16:35 +00:00
}
let sending = false
2026-08-11 21:54:05 +00:00
let content = $state(
initialValues?.type === "text" ? initialValues.value : (draft?.get()?.content ?? ""),
2026-08-11 21:54:05 +00:00
)
let dictating = $state(Boolean(getDictation(draftKey)))
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,
encryptFiles,
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) {
draft?.clear()
2026-09-01 04:56:37 +00:00
} else {
draft?.set({content})
2026-09-01 04:56:37 +00:00
}
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>
{#if commandTarget}
<CommandArgBar target={commandTarget} content={$text} insert={insertCommandToken} />
{/if}
<form class="relative flex gap-2 py-2" onsubmit={preventDefault(submit)}>
<div class={cx({join: url, "flex gap-2": !url})}>
2025-10-06 18:23:19 +00:00
<Button
data-tip="Add an image"
class={buttonClass}
disabled={$uploading || disabled}
2025-10-06 18:23:19 +00:00
onclick={uploadFiles}>
{#if $uploading}
<Spinner size="xs" />
2025-10-06 18:23:19 +00:00
{:else}
<Icon icon={GallerySend} />
{/if}
</Button>
{#if url}
<MenuButton
component={ComposeMenu}
componentProps={{url, h}}
placement="top"
disabled={$uploading}
class={buttonClass}>
<Icon icon={WidgetAdd} />
</MenuButton>
{/if}
<EditorEmojiButton {editor} disabled={$uploading || disabled} class={buttonClass} />
2025-10-06 18:23:19 +00:00
</div>
<div class={editorClass} aria-disabled={disabled}>
2026-04-07 13:06:29 +00:00
<EditorContent {autofocus} {editor} />
2024-08-22 23:16:35 +00:00
</div>
{#if dictating || ($empty && !disabled)}
<DictationButton
key={draftKey}
bind:dictating
onTranscript={insertTranscript}
onVoiceNote={attachVoiceNote} />
2026-09-01 04:56:37 +00:00
{: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 || disabled}
2026-09-01 04:56:37 +00:00
onclick={submit}>
<Icon icon={Plane} />
</Button>
{/if}
2024-11-16 16:04:09 +00:00
</form>