Skip to content

Commit 0fe9681

Browse files
authored
fix: handle default values for fractionalIndex and index in LoroTreeNode (#792)
1 parent b3aeeb5 commit 0fe9681

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

.changeset/yellow-cobras-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"loro-crdt": patch
3+
---
4+
5+
fix: handle default values for fractionalIndex and index in LoroTreeNode

crates/loro-wasm/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4120,8 +4120,11 @@ impl LoroTreeNode {
41204120
let node = TreeNodeWithChildren {
41214121
id: self.id,
41224122
parent: self.tree.get_node_parent(&self.id).unwrap(),
4123-
fractional_index: self.tree.get_position_by_tree_id(&self.id).unwrap(),
4124-
index: self.tree.get_index_by_tree_id(&self.id).unwrap(),
4123+
fractional_index: self
4124+
.tree
4125+
.get_position_by_tree_id(&self.id)
4126+
.unwrap_or_default(),
4127+
index: self.tree.get_index_by_tree_id(&self.id).unwrap_or(0),
41254128
children: value,
41264129
};
41274130
LoroTree {

crates/loro-wasm/tests/basic.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,3 +1708,17 @@ it("update text after switching to a version", () => {
17081708
newDoc.setDetachedEditing(true);
17091709
newDoc.getText("text").update("123");
17101710
});
1711+
1712+
it("tree deleted node to json", () => {
1713+
const doc = new LoroDoc();
1714+
doc.setPeerId(1);
1715+
const tree = doc.getTree("tree");
1716+
const root = tree.createNode();
1717+
tree.delete(root.id);
1718+
doc.commit();
1719+
const node = tree.getNodes({ withDeleted: true })[0].toJSON();
1720+
expect(node.parent).toBe('2147483647@18446744073709551615');
1721+
// default value
1722+
expect(node.fractionalIndex).toBe('80');
1723+
expect(node.index).toBe(0);
1724+
});

0 commit comments

Comments
 (0)