Class Cursor

Cursor is a stable position representation in the doc. When expressing the position of a cursor, using "index" can be unstable because the cursor's position may change due to other deletions and insertions, requiring updates with each edit. To stably represent a position or range within a list structure, we can utilize the ID of each item/character on List CRDT or Text CRDT for expression.

Loro optimizes State metadata by not storing the IDs of deleted elements. This approach complicates tracking cursors since they rely on these IDs. The solution recalculates position by replaying relevant history to update cursors accurately. To minimize the performance impact of history replay, the system updates cursor info to reference only the IDs of currently present elements, thereby reducing the need for replay.


const doc = new LoroDoc();
const text = doc.getText("text");
text.insert(0, "123");
const pos0 = text.getCursor(0, 0);
{
const ans = doc.getCursorPos(pos0!);
expect(ans.offset).toBe(0);
}
text.insert(0, "1");
{
const ans = doc.getCursorPos(pos0!);
expect(ans.offset).toBe(1);
}

Constructors

Methods

  • Get the id of the given container.

    Returns ContainerID

  • Encode the cursor into a Uint8Array.

    Returns Uint8Array

  • Returns void

  • "Cursor"

    Returns any

  • Get the ID that represents the position.

    It can be undefined if it's not bind into a specific ID.

    Returns { counter: number; peer: `${number}` }

  • Get which side of the character/list item the cursor is on.

    Returns Side

  • Decode the cursor from a Uint8Array.

    Parameters

    • data: Uint8Array

    Returns Cursor