R
C

DocumentCrdt

The editor's document CRDT: a fractional-ordered set of blocks, each a text RGA + a mark store (or an attribute-only atom). It translates the editor's offset-based {@link Step}s into id-based CRDT ops ({@link translateStep}), integrates ops from any replica ({@link applyOp}), and materializes an {@link EditorDocument} ({@link toDocument}).

Signature

ts
class DocumentCrdt

Methods

setIdFactory

Wire the replica's id generator (called once by the provider).

ts
setIdFactory(factory: () => OpId): void
ParameterTypeDescription
factory() => OpId
applyOp

Apply one op (local or remote). Returns false if a causal dependency is missing.

ts
applyOp(op: EditorOp): boolean
ParameterTypeDescription
opEditorOp
Returnsboolean
toDocument
ts
toDocument(): EditorDocument
ReturnsEditorDocument
gc

Compact the CRDT: drop tombstoned characters and fully-removed blocks that are covered by `stable`. Mark-span endpoints are preserved so formatting survives. Call ONLY at quiescence — every replica fully synced, nothing in flight — or a late op referencing dropped content can no longer integrate.

ts
gc(stable: VersionVector):
ParameterTypeDescription
stableVersionVector
Returns{ blocks: number; chars: number; }
hasBlock

Whether a block currently exists and is visible.

ts
hasBlock(blockId: string): boolean
ParameterTypeDescription
blockIdstring
Returnsboolean
toAnchor

Convert a model selection into a char-id anchor (for presence broadcast).

ts
toAnchor(selection: Selection): SelectionAnchor
ParameterTypeDescription
selectionSelection
ReturnsSelectionAnchor
resolveAnchor

Resolve an anchor back into a model selection against the current document.

ts
resolveAnchor(anchor: SelectionAnchor | null): Selection | null
ParameterTypeDescription
anchorSelectionAnchor | null
ReturnsSelection | null
translateStep

Generate the ops for a local step, reading current state for ids/positions.

ts
translateStep(step: Step): EditorOp[]
ParameterTypeDescription
stepStep
ReturnsEditorOp[]
seedFromDocument

Ops to seed the CRDT from an initial document.

ts
seedFromDocument(doc: EditorDocument): EditorOp[]
ParameterTypeDescription
docEditorDocument
ReturnsEditorOp[]