diff --git a/src/app/components/CalendarEventForm.svelte b/src/app/components/CalendarEventForm.svelte index fadc1a70..cacbecf2 100644 --- a/src/app/components/CalendarEventForm.svelte +++ b/src/app/components/CalendarEventForm.svelte @@ -24,28 +24,28 @@ import {pushToast} from "@app/util/toast" import {canEnforceNip70} from "@app/core/commands" - type CalendarValues = { - d?: string + type Values = { + d: string title: string - content: unknown + content: string | object location: string - start: number | undefined - end: number | undefined + start?: number + end?: number } type Props = { url: string h?: string header: Snippet - initialValues?: CalendarValues + initialValues?: Values } let {url, h, header, initialValues}: Props = $props() - const draftKey = new DraftKey(`calendar:${url}:${h ?? ""}`) - const draft = draftKey.get() + const draftKey = new DraftKey(`calendar:${url}:${h ?? ""}`) + if (!initialValues) { - initialValues = draft + initialValues = draftKey.get() } const shouldProtect = canEnforceNip70(url) @@ -83,9 +83,9 @@ const ed = await editor const content = ed.getText({blockSeparator: "\n"}).trim() const tags = [ - ["d", initialValues?.d || randomId()], + ["d", d], ["title", title], - ["location", location || ""], + ["location", location], ["start", start.toString()], ["end", end.toString()], ...daysBetween(start, end).map(D => ["D", D.toString()]), @@ -108,28 +108,22 @@ history.back() } - let editorContent = $state(initialValues?.content ?? "") - - const onChange = (json: unknown) => { - editorContent = json - } - - const editor = makeEditor({ - url, - submit, - uploading, - onChange, - content: initialValues?.content ?? "", - }) - + const d = $state(initialValues?.d ?? randomId()) let title = $state(initialValues?.title ?? "") let location = $state(initialValues?.location ?? "") let start: number | undefined = $state(initialValues?.start) let end: number | undefined = $state(initialValues?.end) let endDirty = $state(Boolean(initialValues?.end)) + let content = $state(initialValues?.content ?? "") + + const onChange = (json: object) => { + content = json + } + + const editor = makeEditor({url, submit, uploading, onChange, content}) $effect(() => { - draftKey.set({d: initialValues?.d, title, location, start, end, content: editorContent}) + draftKey.set({d, title, location, start, end, content}) }) $effect(() => { diff --git a/src/app/components/Chat.svelte b/src/app/components/Chat.svelte index 47a8dd73..b509234d 100644 --- a/src/app/components/Chat.svelte +++ b/src/app/components/Chat.svelte @@ -67,7 +67,7 @@ const {pubkeys, info}: Props = $props() const chat = deriveChat(pubkeys) - const draftKey = new DraftKey<{content?: unknown}>(`dm:${$chat?.id}`) + const draftKey = new DraftKey<{content?: string | object}>(`dm:${$chat?.id}`) const others = remove($pubkey!, pubkeys) const missingRelayLists = $derived(others.filter(pk => !$messagingRelayListsByPubkey.has(pk))) @@ -338,7 +338,7 @@ {onSubmit} {onEscape} {onEditPrevious} - content={eventToEdit?.content} + initialValues={eventToEdit} draftKey={eventToEdit ? undefined : draftKey} disabled={Boolean(missingRelayLists.length)} /> {/key} diff --git a/src/app/components/ChatCompose.svelte b/src/app/components/ChatCompose.svelte index b941c1a3..e1e26825 100644 --- a/src/app/components/ChatCompose.svelte +++ b/src/app/components/ChatCompose.svelte @@ -12,18 +12,31 @@ import {makeEditor} from "@app/editor" import {type DraftKey} from "@app/util/drafts" + type Values = { + content?: string | object + } + type Props = { - content?: string disabled?: boolean - draftKey?: DraftKey<{content?: unknown}> + draftKey?: DraftKey onEscape?: () => void onEditPrevious?: () => void onSubmit: (event: EventContent) => void + initialValues?: Values } - const {content, disabled = false, draftKey, onEscape, onEditPrevious, onSubmit}: Props = $props() + let { + initialValues, + disabled = false, + draftKey, + onEscape, + onEditPrevious, + onSubmit, + }: Props = $props() - const draft = draftKey?.get() + if (!initialValues) { + initialValues = draftKey?.get() + } const autofocus = !isMobile && !disabled @@ -67,13 +80,14 @@ ed.chain().clearContent().run() } - const onChange = (json: unknown) => { - draftKey?.set({content: json}) + let content = $state(initialValues?.content ?? "") + + const onChange = (json: object) => { + content = json } const editor = makeEditor({ - content: content ?? (draft?.content as string | object | undefined), - autofocus, + content, submit, uploading, onChange, @@ -81,6 +95,10 @@ encryptFiles: true, }) + $effect(() => { + draftKey?.set({content}) + }) + onMount(async () => { const ed = await editor ed.view.dom.addEventListener("keydown", handleKeyDown) @@ -105,7 +123,7 @@ {/if}
- +