Drafts
Staging the edits of a gesture locally and committing them as one transaction, or discarding them.
A draft holds edits that are not submitted yet. Use one for a gesture that produces many intermediate states, such as dragging a shape or scrubbing a slider: the user sees every step, other users see only the result, and an abandoned gesture leaves no trace. This page explains how drafts behave and how to use them.
How a draft behaves
A draft does not freeze the document. It always shows the document's current value with the draft's edits applied on top, so other users' edits keep appearing while the gesture is in progress. If one of those edits removes something the draft edited, that staged edit is left out.
Nothing a draft holds reaches the server until you commit it. Committing applies the draft's net effect as one transaction; discarding drops it.
Using a draft
const draft = document.createDraft();
// While the pointer moves: stage edits and render the preview.
draft.update((root) => {
root.findByIdAcrossTree(shapeId)?.as(Shape).update({ x, y });
});
render(document.getPreviewSnapshot());
// On pointer up: one transaction with the final position.
draft.commit({ label: "Move shape" });
// Or, on Escape:
draft.discard();getPreviewSnapshot() on the document returns the value as the newest open
draft shows it, or the ordinary snapshot when no draft is open. Render from
it and your UI shows the gesture's preview without special cases.
| Member | Purpose |
|---|---|
id | The draft's id |
root | A proxy whose writes are staged in the draft |
update(fn) | Stages the writes fn makes |
transaction(fn) | The same, returning the function's result |
applyCommands(commands) | Stages commands you already have; throws when they do not apply |
getSnapshot(), getValue() | The document with the staged edits on top |
commit(meta?) | Submits the staged edits that still apply as one transaction |
discard() | Drops the staged edits |
isActive() | false once committed or discarded |
document.getActiveDraftIds() lists the open drafts, and the onDraftChange
listener runs when a draft is created, changes or ends.
Committing
commit submits what is left of the draft as one transaction. Staged edits
whose target another user removed in the meantime are skipped and the rest
commits, the same way the server treats a transaction.
A draft that stages the same field many times commits only the final value, so a long drag produces a small transaction.
When the commit fails, because the result would violate the schema or the
session is read-only, commit throws and the draft stays active. You can
change it and commit again, or discard it. Using a draft after it was
committed or discarded throws InvalidStateError.
When the document's collection is migrated to a new schema version while a draft is open, the draft is discarded: its staged edits address the old schema.
Drafts and undo
A committed draft is an ordinary local transaction, so an undo
history records it as one step; the label you pass
to commit names the step. With the zustand command layer the rule is
different; see Commands with undo.
With React
useDraft from @voidhash/mimic/zustand owns one draft for a component's
gesture and discards it when the component unmounts. See
Zustand bindings.