skip to content

In a team wiki's collaborative editor, how should undo revert only your own changes when a colleague has since edited the same paragraph?

level: seniorimportance: should knowfreq 33%

answer

  1. my stack holds only my operations
  2. undo is a new forward edit
  3. inverse adjusted past later edits
  4. already deleted by someone: skip it
  5. group by intent, not by packet

basics

~20 s

Keep an undo stack of only your own operations; undo builds the inverse of your latest group, adjusted for every edit applied since, and sends it as a new operation, so colleagues' later work survives and nobody's document rewinds.

solid answer

~50 s

Keep an undo stack of your own operations only, grouped by intent — a run of typing, a paste, a formatting command — and store each group's inverse. Remote operations are applied to the document but never pushed onto the stack. Because colleagues keep editing, the inverse must be re-expressed against the current document: in an OT editor it is transformed against every later operation; in a sequence CRDT it names the identities of the characters you inserted or deleted, which never shift. Undo then applies that inverse and sends it as an ordinary new operation, so a colleague's word typed inside your sentence survives while your characters go. If a colleague already deleted your text, the entry is a no-op and undo moves on to your next group. Never restore a snapshot: that would erase everyone's later work.

code

pseudocode · 18 lines
pseudocode
onLocalEdit(op):
  apply(op); send(op)
  group = currentGroup()                 // closes after a pause or a caret jump
  group.inverse = compose(inverseOf(op), group.inverse)   // newest undone first
  redoStack.clear()

onRemoteOp(remote):
  apply(remote)
  for entry in undoStack + redoStack:    // OT only; a CRDT inverse names identities
    entry.inverse = transform(entry.inverse, remote)

undo():
  while undoStack is not empty:
    entry = undoStack.pop()
    if isNoOp(entry.inverse): continue   // a colleague already removed that text
    apply(entry.inverse); send(entry.inverse)
    redoStack.push(inverseOf(entry.inverse))
    return

go deeper

for a junior

Recall that in a shared document undo reverts your own changes, not whatever change anyone made last.

for a middle

Explain what goes on the stack — your own grouped operations with their inverses — and why remote operations and cursor moves never do.

for a senior

Show how the inverse stays aimed at your text — transformed in OT, identity-based in a CRDT — and walk the cases where colleagues edited inside or deleted it.

for a principal

Judge which undo edge cases deserve product rules — skipped no-ops, restoring around deleted neighbours, reverting others via history — and how much engineering shared undo is worth.

## Why single-user undo breaks in a shared page In a single-user editor, undo can restore an earlier state or pop the last operation off one history. In a **team wiki's collaborative editor**, both go wrong. Restoring the state from before your last edit also erases whatever colleagues typed since. Popping the last operation in the shared history undoes someone else's work whenever they typed after you. What a user expects is narrower: **undo reverts my own most recent change and leaves everyone else's work alone**, even when theirs landed in the same paragraph. One user's undo history and where it lives belong to the single-user undo question, and the command and memento patterns to their own questions; this one is about the shared page. ## The stack holds only my operations - Push **local** operations only; operations from the network are applied to the document but never pushed. - **Group** operations by intent: a run of typing until a pause or a caret jump, one paste, one formatting command. Undo grouping follows intent and is unrelated to how operations are batched on the wire. - Store each group's **inverse**: for an insert, a delete of exactly those characters; for a delete, a re-insert of the deleted text with its formatting at its original place; for a formatting change, the attributes it replaced. - Presence — cursor moves and selections — never enters the stack. - **Cap** the stack at a few hundred groups: in an OT editor that adjusts entries as edits arrive, every stored entry is transformed on each remote operation, so an unbounded stack costs work on every incoming edit. ## Making the inverse land on my text The inverse was built against the document as it was then. By the time you press undo, others have edited, so it must be expressed against the current document: - With **OT**, each stored inverse is transformed against every operation applied after it, so its positions still point at your characters. Many published OT algorithms support no undo at all, and the ones that do need extra transformation properties — which is why shared undo is a design item, not a free feature. - With a **sequence CRDT**, the inverse names **element identities** rather than positions: "delete these characters I inserted", "re-insert this text after the character with this identity". Identities do not shift, so the inverse stays aimed at your text whatever happened around it. Either way, undo applies the inverse locally and **sends it as a new operation**. Collaborators see an ordinary edit; nobody's document rewinds. ## The cases interviewers probe | Situation | What your undo does | |---|---| | A colleague typed a word inside your sentence | removes your characters; their word stays | | A colleague already deleted your sentence | nothing left to remove: skip to your next group | | A colleague bolded part of your sentence | removing your text removes it, bold and all | | You undo a delete whose neighbours were removed | re-insert at the nearest surviving position | | You undo while offline | applied locally; it syncs with your other edits | The no-op case is the one users notice: pressing undo and seeing nothing happen reads as a bug. Skip to the next entry that still has an effect, or show a brief note that someone else already removed that text. ## Redo and what the user sees 1. Redo is the inverse of the undo, kept on a redo stack and adjusted against later operations exactly like undo entries. 2. A new local edit clears the redo stack, as in any editor. 3. Move the caret to where the undone change was, and scroll there if it is off-screen, so the user sees what changed. 4. When an entry has become a no-op because others removed the text, skip it rather than leaving undo looking broken. 5. Keep the stack per page session; after a reload, start empty unless the product deliberately persists it. ## When the user really wants someone else's change gone Reverting a colleague's edit should never be the keyboard shortcut. Offer it through version history ("restore this paragraph as of 10:40"), which creates a new, attributed operation everyone can see. That keeps undo predictable — it only ever touches what you did — while still giving people a way back.

  • Should undo ever revert a collaborator's change?
    Not from the shortcut. Reverting someone else's work is a deliberate act, so offer it through version history — restore a paragraph as of a time — which creates a new operation visible to everyone and attributed to whoever did it. Undo stays predictable because it only touches your own work.
  • How does undo grouping relate to sending operations in batches?
    They are independent. Network batching groups keystrokes by time to cut message count; undo grouping follows intent — a run of typing until a pause, one paste, one formatting command. One undo group can span many network batches, and one batch can cross a group boundary.

Shared undo is like correcting your own entry in a shared ledger by writing a new reversing line, rather than tearing out the page that also holds everyone else's later entries.

  • Undo restores the document to the state before my last edit.
  • One shared undo stack lets anyone undo the latest change, whoever made it.
  • Undo sends a rollback request so the server rewinds the page to an earlier version.
  • Remote operations should be pushed onto my undo stack so undo stays in order.
  • An inverse stored as positions stays valid however much others edited since.