Collaborative undo
Undo that reverses your own last action and leaves everyone else's work alone - which is what users expect, and what a naïve "restore the previous state" undo gets badly wrong in a shared document.
Two replicas, two undo stacks
Each replica has its own undo stack, tracking only its own edits. Undo reverses this replica's most recent step and Redo puts it back. Typing that pauses for less than half a second is one step; End the current step closes the step being typed at once, so the next keystroke starts a new one. The footer counts the steps on each stack.
Opening two replicas…
Undo mine, not yours
Undo in a single-user editor means "put the document back how it was". In a collaborative document that would be a disaster: restoring an earlier state would throw away everything your collaborators did in the meantime, and they would watch their work vanish because somebody else pressed a key.
What users actually expect is selective undo - reverse my last action, leave everybody else's alone - and that is what these two replicas implement.
Note also that consecutive keystrokes collapse into one undo step, so undo removes a word or a phrase rather than a single character. A remote edit arriving between two of your keystrokes ends the current step, which is what keeps the boundaries sensible when several people are working at once. And as in any editor, a fresh edit after an undo empties the redo stack.
Why this is not a rewind
An undo here does not restore a previous state. It creates new operations that delete what the action inserted and re-insert what it deleted, and those operations travel to peers exactly like a keystroke. That is why the effect appears on the other replica, why it merges rather than clobbering, and why it can itself be undone.
It is also why an undo can survive a disconnection: take a replica offline, undo something, bring it back, and the undo reaches its peer whenever the connection returns.
// One stack per editing surface, scoped to the text it edits. using var undo = new BcrdtUndoManager(document.Text("body")); undo.Undo(); // removes what I did, not what they did undo.Redo(); undo.StopCapturing(); // the next edit starts a step of its own
What a stack captures
A stack has a scope - the types it watches - and captures only transactions that touched
them. Here each stack is scoped to one text, which is the rule worth following: a stack over the whole
document would undo an edit made in an unrelated field minutes ago. Every document opened through the
collaboration service also comes with a default stack over its root map, document.Undo, and
document.CreateUndoManager(...) makes more.
A stack also captures only transactions whose origin it tracks. Edits made with no origin -
BcrdtTextArea's keystrokes, and any code that does not tag its transactions - are tracked by
default, while updates received from a peer carry their sync provider as origin and are never captured.
Code that tags its own edits (document.Transact(..., origin: myTool)) adds that origin to
TrackedOrigins to make them undoable.
| BcrdtUndoManagerOptions | Default | What it decides |
|---|---|---|
| CaptureTimeout | 500 ms | How long consecutive changes keep merging into one step. Zero makes every transaction a step. |
| TrackedOrigins | untagged edits | Which transaction origins the stack is responsible for. |
| MaxStackItems | 100 | How many steps each stack keeps; the oldest makes way, and the deleted content it was keeping for an undo is reclaimed. Zero keeps every step. |
| IgnoreRemoteMapChanges | false | Whether redoing a map key overwrites a value somebody else has written there since. By default the redo is abandoned instead. |
| DeleteFilter | none | A veto on removing a particular block while undoing. |
The buttons above enable and disable through the stack's own events - StackItemAdded,
StackItemUpdated and StackItemPopped - and read CanUndo,
CanRedo, UndoCount and RedoCount. A step's Meta dictionary
holds whatever an application wants to restore with it, such as the caret position.