Every extension
autosave
Save the document once typing pauses.
Every change marks the document dirty and starts the clock; the save runs `delay` after the last one, so a sentence costs one save rather than one per letter. `save()` saves now. When the page is hidden or unloaded a dirty document is saved before it goes, since a pause that never comes is the usual way an autosave loses the last paragraph.
The state is reduced from transactions like any other: a change marks it dirty, and the save reports back through `markSaved`, which is a command so that the report is a transaction too. A save that fails leaves the document dirty with `error` set and is not retried on its own — the next change, or `save()`, tries again — because retrying a server that is down every second is how a server that is down stays down.
A save runs outside any command, after the transaction that asked for it has landed, because `save` is application code and may do anything at all. `editor.can.save()` asks without saving. One `autosave()` per editor: it holds the editor it was mounted in.
autosave({
delay: 800,
save: (doc) => localStorage.setItem('draft', JSON.stringify(doc)),
restore: () => JSON.parse(localStorage.getItem('draft') ?? 'null'),
})
Adding it
import { createEditor, document, paragraph, text, autosave } from '@matrajs/core'
const editor = createEditor({
extensions: [document, paragraph, text, autosave({ … })],
element: document.querySelector('#editor'),
})
There is no separate package · autosave is already in @matrajs/core, and an extension you do not import is not in your bundle. document, paragraph and text are the floor every document needs.
Options
| Field | Type | |
|---|---|---|
save required | (doc: DocNode, editor: Editor) => void | Promise<void> | Persist the document. A returned promise is waited for; whatever it rejects with, or the function throws, is reported and never thrown on. |
delay | number | How long typing has to pause before a save. Default 1000ms. |
restore | () => DocNode | string | null | undefined | Content to load when the editor mounts — what the last save left behind. Loading it is not an edit: the document is not dirty afterwards. |
onError | (error: unknown) => void | Told about every save that failed. |
flushOnHide | boolean | Save at once when the page is hidden or unloaded. Default true. |
Commands
-
editor.commands.save() -
editor.commands.markSaved()