flotilla/src/app/components/ChatCompose.svelte

146 lines
3.9 KiB
Svelte
Raw Normal View History

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"
import type {MaybeAsync} from "@welshman/lib"
2025-05-05 17:11:02 +00:00
import type {EventContent} from "@welshman/util"
import {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"
import Spinner from "@lib/components/Spinner.svelte"
2025-05-05 17:11:02 +00:00
import EditorContent from "@app/editor/EditorContent.svelte"
import {makeEditor} from "@app/editor"
2026-08-11 21:54:05 +00:00
import {type DraftKey, type Draft} from "@app/drafts"
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-08-11 21:54:05 +00:00
draftKey?: DraftKey<Draft>
2026-03-10 22:13:33 +00:00
onEscape?: () => void
onEditPrevious?: () => void
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,
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())
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
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
draftKey?.clear()
2025-05-05 17:11:02 +00:00
ed.chain().clearContent().run()
}
2026-08-11 21:54:05 +00:00
let content = $state(
initialValues?.type === "text" ? initialValues.value : (draftKey?.get()?.content ?? ""),
)
2026-04-07 13:06:29 +00:00
const onChange = (json: object) => {
content = json
}
2025-05-05 17:11:02 +00:00
const editor = makeEditor({
2026-04-07 13:06:29 +00:00
content,
2025-05-05 17:11:02 +00:00
submit,
uploading,
onChange,
2025-05-05 17:11:02 +00:00
aggressive: true,
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(() => {
draftKey?.set({content})
})
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"
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}
<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>
<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"
2026-03-23 21:21:04 +00:00
disabled={$uploading || disabled}
2025-05-05 17:11:02 +00:00
onclick={submit}>
2025-09-10 22:26:39 +00:00
<Icon icon={Plane} />
2025-05-05 17:11:02 +00:00
</Button>
</form>