Class LoroTree<T>

The handler of a tree(forest) container.

Learn more at https://loro.dev/docs/tutorial/tree

Type Parameters

  • T extends Record<string, unknown> = Record<string, unknown>

Constructors

  • Create a new detached LoroTree (not attached to any LoroDoc).

    The edits on a detached container will not be persisted. To attach the container to the document, please insert it into an attached container.

    Type Parameters

    • T extends Record<string, unknown> = Record<string, unknown>

    Returns LoroTree<T>

  • Returns LoroTree<T>

Properties

Get the id of the container.

Methods

  • Create a new tree node as the child of parent and return a LoroTreeNode instance. If the parent is undefined, the tree node will be a root node.

    If the index is not provided, the new node will be appended to the end.

    Parameters

    • Optionalparent: `${number}@${number}`
    • Optionalindex: number

    Returns LoroTreeNode<T>

    import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const tree = doc.getTree("tree");
    const root = tree.createNode();
    const node = tree.createNode(undefined, 0);

    // undefined
    // / \
    // node root
  • Parameters

    • target: `${number}@${number}`

    Returns void

  • Delete a tree node from the forest.

    Parameters

    • target: `${number}@${number}`

    Returns void

    import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const tree = doc.getTree("tree");
    const root = tree.createNode();
    const node = root.createNode();
    tree.delete(node.id);
  • Disable the fractional index generation when you don't need the Tree's siblings to be sorted. The fractional index will always be set to the same default value 0.

    After calling this, you cannot use tree.moveTo(), tree.moveBefore(), tree.moveAfter(), and tree.createAt().

    Returns void

  • Set whether to generate a fractional index for moving and creating.

    A fractional index can be used to determine the position of tree nodes among their siblings.

    The jitter is used to avoid conflicts when multiple users are creating a node at the same position. A value of 0 is the default, which means no jitter; any value larger than 0 will enable jitter.

    Generally speaking, higher jitter value will increase the size of the operation Read more about it

    Parameters

    • jitter: number

    Returns void

  • Returns void

  • Get the attached container associated with this.

    Returns an attached Container that equals to this or created by this, otherwise undefined.

    Returns LoroTree<Record<string, unknown>>

  • Get LoroTreeNode by the TreeID.

    Parameters

    • target: `${number}@${number}`

    Returns LoroTreeNode<T>

  • Parameters

    • Optionaloptions: { withDeleted?: boolean }

    Returns LoroTreeNode<T>[]

  • Get the shallow value of the tree.

    Unlike toJSON() which recursively resolves nested containers to their values, getShallowValue() returns container IDs as strings for any nested containers.

    Returns TreeNodeShallowValue[]

    const doc = new LoroDoc();
    doc.setPeerId("1");
    const tree = doc.getTree("tree");
    const root = tree.createNode();
    root.data.set("name", "root");
    const text = root.data.setContainer("content", new LoroText());
    text.insert(0, "Hello");

    console.log(tree.getShallowValue());
    // [{
    // id: "0@1",
    // parent: null,
    // index: 0,
    // fractional_index: "80",
    // meta: "cid:0@1:Map",
    // children: []
    // }]

    console.log(tree.toJSON());
    // [{
    // id: "0@1",
    // parent: null,
    // index: 0,
    // fractional_index: "80",
    // meta: {
    // name: "root",
    // content: "Hello"
    // },
    // children: []
    // }]
  • Parameters

    • target: `${number}@${number}`

    Returns boolean

  • Return true if the tree contains the TreeID, include deleted node.

    Parameters

    • target: `${number}@${number}`

    Returns boolean

  • Whether the container is attached to a document.

    If it's detached, the operations on the container will not be persisted.

    Returns boolean

  • Check if the container is deleted

    Returns boolean

  • Whether the tree enables the fractional index generation.

    Returns boolean

  • Return None if the node is not exist, otherwise return Some(true) if the node is deleted.

    Parameters

    • target: `${number}@${number}`

    Returns boolean

  • "Tree"

    Returns "Tree"

  • Parameters

    • target: `${number}@${number}`
    • Optionalparent: `${number}@${number}`
    • Optionalindex: number

    Returns void

  • Move the target tree node to be a child of the parent. It's not allowed that the target is an ancestor of the parent or the target and the parent are the same node.

    Parameters

    • target: `${number}@${number}`
    • parent: `${number}@${number}`
    • Optionalindex: number

    Returns void

    import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const tree = doc.getTree("tree");
    const root = tree.createNode();
    const node = root.createNode();
    const node2 = node.createNode();
    tree.move(node2.id, root.id);
    // Error will be thrown if move operation creates a cycle
    // tree.move(root.id, node.id);
  • Get all tree nodes of the forest, including deleted nodes.

    Returns LoroTreeNode<Record<string, unknown>>[]

    import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const tree = doc.getTree("tree");
    const root = tree.createNode();
    const node = root.createNode();
    const node2 = node.createNode();
    console.log(tree.nodes());
  • Get the parent container of the tree container.

    • The parent container of the root tree is undefined.
    • The object returned is a new js object each time because it need to cross the WASM boundary.

    Returns Container

  • Get the root nodes of the forest.

    Returns LoroTreeNode<Record<string, unknown>>[]

  • Returns TreeNodeValue[]

  • Get the hierarchy array with metadata of the forest.

    Returns any

    import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const tree = doc.getTree("tree");
    const root = tree.createNode();
    root.data.set("color", "red");
    // [ { id: '0@F2462C4159C4C8D1', parent: null, meta: { color: 'red' }, children: [] } ]
    console.log(tree.toJSON());