The Realtime API supports undoing and redoing recent changes made by the local user. Your application does not need to keep track of the user's edits because the Realtime API does this automatically.
Your application can call the
model.undo() and
model.redo()
to undo or redo the last change.
The Realtime API automatically updates the undo and redo history when changes from other collaborators arrive so that the undo or redo changes are valid for the current state of the model.
Undo and Redo State
Changes can only be undone if there are local changes to
the model and the change is undoable. Your application must test if changes can
be undone/redone by testing the
canUndo
and
canRedo
properties. For example, to undo the most recent local change, you can use the
following code:
if (model.canUndo) {
model.undo();
} else {
console.log("No events to undo.");
}
Your application can also listen for the
UndoRedoStateChangedEvent
to be notified when the state of canUndo or canRedo change. For more
information on using this event, see
Undo and Redo State Events.
Preventing Undo
In some scenarios, you may not want the user to be able to undo a change, like
if you are using a portion of the data model to track the user’s cursor or
making other changes not based directly on user input. You can prevent these
changes from being undone by wrapping them in a compound operation marked with
opt_isUndoable set to false.
Compound operations describes
how to set these create a compound operation.
This set of changes to the model will be treated the same as a remote collaborator change. When the user next attempts to undo, the non-undoable change is skipped and the previous change will be undone.