persist drafts in memory (#155)

Co-authored-by: userAdityaa <aditya.chaudhary1558@gmail.com>
Co-committed-by: userAdityaa <aditya.chaudhary1558@gmail.com>
This commit is contained in:
2026-04-07 12:06:29 +00:00
committed by hodlbod
parent 0547e9513f
commit 30c2a6ef79
12 changed files with 257 additions and 55 deletions
+9 -5
View File
@@ -11,13 +11,17 @@
let element: HTMLElement
onMount(() => {
editor.then(({options}) => {
if (options.element) {
element?.append(options.element)
editor.then(ed => {
if (ed.options.element) {
element?.append(ed.options.element)
}
if (options.autofocus) {
;(element?.querySelector("[contenteditable]") as HTMLElement)?.focus()
if ((ed as any)._shouldAutofocus) {
const hasContent = ed.getText().trim().length > 0
requestAnimationFrame(() => {
ed.commands.focus(hasContent ? "end" : "start")
})
}
})
})
+11 -4
View File
@@ -28,6 +28,7 @@ export const makeEditor = async ({
autofocus = false,
charCount,
content = "",
onChange,
placeholder = "",
url,
submit,
@@ -38,7 +39,8 @@ export const makeEditor = async ({
aggressive?: boolean
autofocus?: boolean
charCount?: Writable<number>
content?: string
content?: string | object
onChange?: (json: unknown) => void
placeholder?: string
url?: string
submit: () => void
@@ -82,9 +84,9 @@ export const makeEditor = async ({
},
)
return new Editor({
content: escapeHtml(content),
autofocus,
const ed = new Editor({
content: typeof content === "string" ? escapeHtml(content) : content,
autofocus: false,
editorProps,
element: document.createElement("div"),
extensions: [
@@ -142,6 +144,11 @@ export const makeEditor = async ({
onUpdate({editor}) {
wordCount?.set(editor.storage.wordCount.words)
charCount?.set(editor.storage.wordCount.chars)
onChange?.(editor.getJSON())
},
})
;(ed as any)._shouldAutofocus = autofocus
return ed
}