2025-05-05 17:11:02 +00:00
|
|
|
<script lang="ts">
|
2026-03-10 22:13:33 +00:00
|
|
|
import {onDestroy, onMount} from "svelte"
|
2025-05-05 17:11:02 +00:00
|
|
|
import {writable} from "svelte/store"
|
2026-03-23 21:21:04 +00:00
|
|
|
import cx from "classnames"
|
2026-09-01 03:17:52 +00:00
|
|
|
import type {MaybeAsync} from "@welshman/lib"
|
2025-05-05 17:11:02 +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"
|
|
|
|
|
import Plane from "@assets/icons/plane-2.svg?dataurl"
|
2025-05-05 17:11:02 +00:00
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
|
|
|
|
import Button from "@lib/components/Button.svelte"
|
2026-06-24 20:56:53 +00:00
|
|
|
import Spinner from "@lib/components/Spinner.svelte"
|
2026-09-01 04:56:37 +00:00
|
|
|
import DictationButton from "@app/components/DictationButton.svelte"
|
2025-05-05 17:11:02 +00:00
|
|
|
import EditorContent from "@app/editor/EditorContent.svelte"
|
|
|
|
|
import {makeEditor} from "@app/editor"
|
2026-09-11 00:48:11 +00:00
|
|
|
import {getDictation} from "@app/dictation"
|
2026-08-11 21:54:05 +00:00
|
|
|
import {type DraftKey, type Draft} from "@app/drafts"
|
2026-09-01 03:17:52 +00:00
|
|
|
import {pushToast} from "@app/toast"
|
2026-08-11 21:54:05 +00:00
|
|
|
import type {Share} from "@app/share"
|
2026-04-07 13:06:29 +00:00
|
|
|
|
2025-05-05 17:11:02 +00:00
|
|
|
type Props = {
|
2026-03-23 21:21:04 +00:00
|
|
|
disabled?: boolean
|
2026-09-11 00:48:11 +00:00
|
|
|
dictationKey: string
|
2026-08-11 21:54:05 +00:00
|
|
|
draftKey?: DraftKey<Draft>
|
2026-03-10 22:13:33 +00:00
|
|
|
onEscape?: () => void
|
|
|
|
|
onEditPrevious?: () => void
|
2026-09-01 03:17:52 +00:00
|
|
|
onSubmit: (event: EventContent) => MaybeAsync<void>
|
2026-08-11 21:54:05 +00:00
|
|
|
initialValues?: Share
|
2025-05-05 17:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
2026-08-11 21:54:05 +00:00
|
|
|
const {
|
2026-04-07 13:06:29 +00:00
|
|
|
initialValues,
|
|
|
|
|
disabled = false,
|
2026-09-11 00:48:11 +00:00
|
|
|
dictationKey,
|
2026-04-07 13:06:29 +00:00
|
|
|
draftKey,
|
|
|
|
|
onEscape,
|
|
|
|
|
onEditPrevious,
|
|
|
|
|
onSubmit,
|
|
|
|
|
}: Props = $props()
|
|
|
|
|
|
2026-03-23 21:21:04 +00:00
|
|
|
const autofocus = !isMobile && !disabled
|
2025-05-05 17:11:02 +00:00
|
|
|
|
|
|
|
|
const uploading = writable(false)
|
|
|
|
|
|
2026-03-23 21:21:04 +00:00
|
|
|
const editorClass = $derived(
|
2026-04-07 21:53:19 +00:00
|
|
|
cx("chat-editor grow overflow-hidden", {
|
2026-03-23 21:21:04 +00:00
|
|
|
"pointer-events-none opacity-50": disabled,
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
|
2025-05-05 17:11:02 +00:00
|
|
|
export const focus = () => editor.then(ed => ed.chain().focus().run())
|
|
|
|
|
|
2026-03-10 22:13:33 +00:00
|
|
|
export const canEnterEditPrevious = () =>
|
|
|
|
|
editor.then(ed => ed.getText({blockSeparator: "\n"}) === "")
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = async (event: KeyboardEvent) => {
|
|
|
|
|
if (event.key === "Escape") {
|
|
|
|
|
onEscape?.()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.key === "ArrowUp" && (await canEnterEditPrevious())) {
|
|
|
|
|
onEditPrevious?.()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-03 17:41:38 +00:00
|
|
|
const uploadFiles = () => editor.then(ed => ed.chain().selectFiles().run())
|
|
|
|
|
|
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.
|
|
|
|
|
const insertTranscript = async (text: string) => {
|
|
|
|
|
const ed = await editor
|
|
|
|
|
|
|
|
|
|
ed.chain().focus().insertContent(escapeHtml(text)).run()
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 17:11:02 +00:00
|
|
|
const submit = async () => {
|
2026-03-23 21:21:04 +00:00
|
|
|
if ($uploading || disabled) return
|
2025-05-05 17:11:02 +00:00
|
|
|
|
|
|
|
|
const ed = await editor
|
|
|
|
|
const content = ed.getText({blockSeparator: "\n"}).trim()
|
|
|
|
|
const tags = ed.storage.nostr.getEditorTags()
|
|
|
|
|
|
|
|
|
|
if (!content) return
|
|
|
|
|
|
2026-09-01 03:17:52 +00:00
|
|
|
try {
|
|
|
|
|
await onSubmit({content, tags})
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to send message", error)
|
|
|
|
|
|
|
|
|
|
return pushToast({theme: "error", message: "Failed to send your message."})
|
|
|
|
|
}
|
2025-05-05 17:11:02 +00:00
|
|
|
|
2026-04-07 12:06:29 +00:00
|
|
|
draftKey?.clear()
|
2026-09-01 04:56:37 +00:00
|
|
|
ed.chain().clearContent(true).run()
|
2025-05-05 17:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
2026-08-11 21:54:05 +00:00
|
|
|
let content = $state(
|
|
|
|
|
initialValues?.type === "text" ? initialValues.value : (draftKey?.get()?.content ?? ""),
|
|
|
|
|
)
|
2026-09-11 00:48:11 +00:00
|
|
|
let dictating = $state(Boolean(getDictation(dictationKey)))
|
2026-04-07 13:06:29 +00:00
|
|
|
|
|
|
|
|
const onChange = (json: object) => {
|
|
|
|
|
content = json
|
2026-04-07 12:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
2026-09-01 04:56:37 +00:00
|
|
|
const empty = writable(true)
|
|
|
|
|
|
2025-05-05 17:11:02 +00:00
|
|
|
const editor = makeEditor({
|
2026-04-07 13:06:29 +00:00
|
|
|
content,
|
2026-09-01 04:56:37 +00:00
|
|
|
empty,
|
2025-05-05 17:11:02 +00:00
|
|
|
submit,
|
|
|
|
|
uploading,
|
2026-04-07 12:06:29 +00:00
|
|
|
onChange,
|
2025-05-05 17:11:02 +00:00
|
|
|
aggressive: true,
|
2025-09-29 21:24:42 +00:00
|
|
|
encryptFiles: true,
|
2025-05-05 17:11:02 +00:00
|
|
|
})
|
2026-03-10 22:13:33 +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
|
|
|
})
|
|
|
|
|
|
2026-03-10 22:13:33 +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()
|
|
|
|
|
}
|
2026-03-10 22:13:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
onDestroy(async () => {
|
|
|
|
|
const ed = await editor
|
|
|
|
|
ed?.view?.dom.removeEventListener("keydown", handleKeyDown)
|
|
|
|
|
})
|
2025-05-05 17:11:02 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<form class="relative z-feature flex gap-2 p-2" onsubmit={preventDefault(submit)}>
|
2025-06-03 17:41:38 +00:00
|
|
|
<Button
|
|
|
|
|
data-tip="Add an image"
|
2026-06-24 20:56:53 +00:00
|
|
|
class="button button-neutral button-square tip tip-right h-10 w-10 min-w-10 rounded-2xl transition-colors"
|
2026-03-23 21:21:04 +00:00
|
|
|
disabled={$uploading || disabled}
|
2025-06-03 17:41:38 +00:00
|
|
|
onclick={uploadFiles}>
|
|
|
|
|
{#if $uploading}
|
2026-06-24 20:56:53 +00:00
|
|
|
<Spinner size="xs" />
|
2025-06-03 17:41:38 +00:00
|
|
|
{:else}
|
2025-09-10 22:26:39 +00:00
|
|
|
<Icon icon={GallerySend} />
|
2025-06-03 17:41:38 +00:00
|
|
|
{/if}
|
|
|
|
|
</Button>
|
2026-03-23 21:21:04 +00:00
|
|
|
<div class={editorClass} aria-disabled={disabled}>
|
2026-04-07 13:06:29 +00:00
|
|
|
<EditorContent {autofocus} {editor} />
|
2025-05-05 17:11:02 +00:00
|
|
|
</div>
|
2026-09-11 00:48:11 +00:00
|
|
|
{#if dictating || ($empty && !disabled)}
|
|
|
|
|
<DictationButton key={dictationKey} bind:dictating onTranscript={insertTranscript} />
|
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 tip tip-left h-10 w-10 min-w-10"
|
|
|
|
|
disabled={$uploading || disabled}
|
|
|
|
|
onclick={submit}>
|
|
|
|
|
<Icon icon={Plane} />
|
|
|
|
|
</Button>
|
|
|
|
|
{/if}
|
2025-05-05 17:11:02 +00:00
|
|
|
</form>
|