flotilla/src/app/components/ChatCompose.svelte

118 lines
3.3 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"
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 EditorContent from "@app/editor/EditorContent.svelte"
import {makeEditor} from "@app/editor"
import {type DraftKey} from "@app/util/drafts"
2025-05-05 17:11:02 +00:00
type Props = {
2026-03-10 22:13:33 +00:00
content?: string
2026-03-23 21:21:04 +00:00
disabled?: boolean
draftKey?: DraftKey<{content?: unknown}>
2026-03-10 22:13:33 +00:00
onEscape?: () => void
onEditPrevious?: () => void
2025-05-05 17:11:02 +00:00
onSubmit: (event: EventContent) => void
}
const {content, disabled = false, draftKey, onEscape, onEditPrevious, onSubmit}: Props = $props()
const draft = draftKey?.get()
2025-05-05 17:11:02 +00:00
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(
cx("chat-editor flex-grow overflow-hidden", {
"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
onSubmit({content, tags})
draftKey?.clear()
2025-05-05 17:11:02 +00:00
ed.chain().clearContent().run()
}
const onChange = (json: unknown) => {
draftKey?.set({content: json})
}
2025-05-05 17:11:02 +00:00
const editor = makeEditor({
content: content ?? (draft?.content as string | object | undefined),
2025-05-05 17:11:02 +00:00
autofocus,
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
onMount(async () => {
const ed = await editor
ed.view.dom.addEventListener("keydown", handleKeyDown)
})
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="center tooltip tooltip-right h-10 w-10 min-w-10 rounded-box bg-base-300 transition-colors hover:bg-base-200"
2026-03-23 21:21:04 +00:00
disabled={$uploading || disabled}
2025-06-03 17:41:38 +00:00
onclick={uploadFiles}>
{#if $uploading}
<span class="loading loading-spinner loading-xs"></span>
{: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}>
2025-05-05 17:11:02 +00:00
<EditorContent {editor} />
</div>
<Button
data-tip="{window.navigator.platform.includes('Mac') ? 'cmd' : 'ctrl'}+enter to send"
class="center tooltip tooltip-left absolute right-4 h-10 w-10 min-w-10 rounded-full"
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>