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.
Optional
parent: `${number}@${number}`Optional
index: numberDisable 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()
.
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
Get the attached container associated with this.
Returns an attached Container
that equals to this or created by this, otherwise undefined
.
Optional
options: { withDeleted?: boolean }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.
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: []
// }]
Return true
if the tree contains the TreeID, include deleted node.
Whether the container is attached to a document.
If it's detached, the operations on the container will not be persisted.
Check if the container is deleted
Whether the tree enables the fractional index generation.
Return None
if the node is not exist, otherwise return Some(true)
if the node is deleted.
"Tree"
Optional
parent: `${number}@${number}`Optional
index: numberMove 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.
Optional
index: numberimport { 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.
Get the parent container of the tree container.
undefined
.Get the root nodes of the forest.
Get the hierarchy array with metadata of the forest.
The handler of a tree(forest) container.
Learn more at https://loro.dev/docs/tutorial/tree