Clean up drafts implementation (#164)

This commit is contained in:
2026-04-07 13:06:29 +00:00
parent 30c2a6ef79
commit 17fb4e780b
13 changed files with 173 additions and 162 deletions
+20 -11
View File
@@ -15,21 +15,26 @@
import {DraftKey} from "@app/util/drafts"
import {onDestroy, onMount} from "svelte"
type Values = {
content?: string | object
}
type Props = {
url?: string
h?: string
content?: string
onEscape?: () => void
onEditPrevious?: () => void
onSubmit: (event: EventContent) => void
initialValues?: Values
}
const {url, h, content, onEscape, onEditPrevious, onSubmit}: Props = $props()
let {url, h, initialValues, onEscape, onEditPrevious, onSubmit}: Props = $props()
type ComposeDraft = {content?: unknown}
const draftKey = url || h ? new DraftKey<Values>(`room:${url ?? ""}:${h ?? ""}`) : undefined
const draftKey = url || h ? new DraftKey<ComposeDraft>(`room:${url ?? ""}:${h ?? ""}`) : undefined
const draft = draftKey?.get()
if (!initialValues) {
initialValues = draftKey?.get()
}
const autofocus = !isMobile
@@ -71,21 +76,25 @@
ed.chain().clearContent().run()
}
const onChange = (json: unknown) => {
draftKey?.set({content: json})
let popover: Instance | undefined = $state()
let content = $state(initialValues?.content ?? "")
const onChange = (json: object) => {
content = json
}
const editor = makeEditor({
url,
content: content ?? (draft?.content as string | object | undefined),
autofocus,
content,
submit,
uploading,
onChange,
aggressive: true,
})
let popover: Instance | undefined = $state()
$effect(() => {
draftKey?.set({content})
})
onMount(async () => {
const ed = await editor
@@ -124,7 +133,7 @@
</Tippy>
</div>
<div class="chat-editor flex-grow overflow-hidden">
<EditorContent {editor} />
<EditorContent {autofocus} {editor} />
</div>
<Button
data-tip="{window.navigator.platform.includes('Mac') ? 'cmd' : 'ctrl'}+enter to send"