localStorage as the user types, with no extra libraries required. You configure it once at initialization and the editor handles debouncing, serialization, and storage for you — so your users never lose work if they close the tab or navigate away unexpectedly.
Enabling auto-save
Pass anautoSave object to the editor constructor with two properties: a key that names the localStorage entry, and an optional debounce delay in milliseconds.
change event. Once debounce milliseconds have elapsed since the last change, it writes the current HTML content to localStorage under the given key.
The
key is written directly to localStorage, so it shares the same namespace as every other key your page sets. Use a namespaced key like lumen:doc-123 to avoid collisions with other scripts or libraries on the same origin.Listening to the autosave event
The editor emits anautosave event each time it successfully writes to localStorage. Hook into it to display a “Saved” indicator, update a status bar, or trigger any other side effect:
localStorage or from editor.getHTML() if you need it.
Restoring saved content
On page load, checklocalStorage for a previously saved draft and pass it as the value option so the editor initializes with the restored content:
localStorage.getItem returns null, so the nullish coalescing fallback (?? '') ensures the editor starts with an empty document.
End-to-end example
The following snippet combines initialization, draft restoration, auto-save configuration, and status feedback into a complete setup you can drop into any page:Clearing a saved draft
Clearing a saved draft
To discard the auto-saved draft — for example when the user explicitly clicks a “Discard” button — remove the key from The next auto-save cycle will create a fresh entry.
localStorage directly: