Text Document API - Client Store
The Text Document API provides a high-level client-side store for managing collaborative text documents, similar to the createGraph API for scene graphs. It's built on top of the TextRope CRDT and handles all the complexities of state management, operation journaling, undo/redo, and coalescing.
Features
- Simple API -
insert(),delete(),replace()operations with position-based editing - State Management - Automatic state tracking with subscription support
- Operation Journaling - All operations are journaled with vector clocks and Lamport timestamps
- Undo/Redo - Built-in undo/redo with proper CRDT semantics
- Coalescing - Automatic operation batching for better performance
- Server Sync - Ready for WebSocket integration with
onSendcallback
Quick Start
API Reference
Factory Functions
createTextDocument(options)
Creates a new text document store.
Parameters:
options.sessionId(string, required) - Unique session identifieroptions.initialSnapshot(TextSnapshot, optional) - Initialize from server snapshotoptions.onSend(function, optional) - Callback when messages are ready to sendoptions.onStateChange(function, optional) - Callback when state changesoptions.onMessageSent(function, optional) - Callback after each message sentoptions.coalescingEnabled(boolean, optional) - Enable automatic commit coalescing (default: false)options.coalescingDelayMs(number, optional) - Coalescing delay in milliseconds (default: 300)
Returns: TextDocumentStore
createTextDocumentFromServer(options)
Creates a text document store initialized from server state.
Parameters: Same as createTextDocument plus:
options.snapshot(TextSnapshot, required) - Server snapshotoptions.journal(TextMessage[], required) - Journal entries
Store Methods
Editing Operations
insert(position, text)
Insert text at the specified position.
delete(position, length)
Delete text at the specified position.
replace(position, length, text)
Replace text atomically (delete + insert).
commit(description?)
Commit pending edits and create a journal entry.
Note: If coalescing is enabled, edits are automatically committed after the coalescing delay. Explicit commit() calls bypass coalescing and commit immediately.
State Access
getState()
Get the current client state.
getText()
Get the current text content.
subscribe(listener)
Subscribe to state changes.
Server Communication
receive(msg)
Apply a message from another client or the server.
ack(msgId)
Mark a message as acknowledged by the server.
loadServerState(snapshot, journal)
Load state from the server (for initial sync or reconnection).
Undo/Redo
undo()
Undo the last committed change from this session.
redo()
Redo the last undone change from this session.
Coalescing Control
setCoalescingEnabled(enabled)
Enable or disable automatic operation coalescing.
setCoalescingDelay(delayMs)
Set the coalescing delay in milliseconds.
getCoalescingEnabled()
Get current coalescing enabled state.
getCoalescingDelay()
Get current coalescing delay.
Type Definitions
TextMessage
A message containing text operations.
TextOperation
A text edit operation.
TextDocumentState
The complete client-side state.
Usage Patterns
Basic Single-User Editor
Collaborative Editor with Server
React Integration
Comparison with Graph API
The Text Document API follows the same patterns as the Graph API:
| Feature | Graph API | Text Document API |
|---|---|---|
| Factory | createGraph() | createTextDocument() |
| State | ClientState with SceneGraph | TextDocumentState with TextRope |
| Edit | edit(op) | insert(), delete(), replace() |
| Commit | commit(description) | commit(description) |
| Sync | receive(msg), ack(msgId) | receive(msg), ack(msgId) |
| Undo/Redo | undo(), redo() | undo(), redo() |
| Coalescing | setCoalescingEnabled() | setCoalescingEnabled() |
| Subscribe | subscribe(listener) | subscribe(listener) |
Best Practices
Performance
- Enable Coalescing for real-time typing scenarios to batch rapid edits
- Use
replace()instead of separate delete + insert for selection replacement - Subscribe Sparingly - Only subscribe where needed to avoid unnecessary renders
Conflict Resolution
- Trust the CRDT - The rope automatically handles concurrent edits correctly
- Use Vector Clocks - They track causality and prevent duplicate application
- Preserve Journal - Keep journal entries for proper undo/redo semantics
Server Integration
- Send Messages Immediately - Use the
onSendcallback to forward operations - Acknowledge Messages - Call
ack()when the server confirms receipt - Load Initial State - Use
loadServerState()for reconnection and initial sync - Cleanup on Disconnect - Close WebSocket connections properly
See Also
- TextRope CRDT - Low-level rope operations
- React Hooks - React integration patterns
- Server API - Server-side integration
- Live Demo - Interactive demo