1. Why offsets become stale
Suppose Alice inserts a word at an offset and Bob inserts text before her edit. Undoing Alice's edit by deleting at its original offset may target Bob's text. An editor can track edits using document-relative identifiers and resolve positions against the current document.
2. Designing local undo with Yjs
Yjs provides an UndoManager that scopes changes to shared types and can track selected transaction origins. An integration can tag local edits with a distinct origin, group related local edits, and leave remote synchronization updates outside the local undo history. Relative positions can help preserve cursor anchors as the document changes.
Interleaved insertions, remote deletion of locally inserted text, and redo after a remote update are cases to test with multiple connected clients.
3. Parallel Code
The VS Code extension integrates Yjs document synchronization through a WebSocket server. The selective undo design above is illustrative. The repository provides the extension and connection code.