fix: two tree move (inside the same parent) issues (#450)
Some checks are pending
Release WASM / Release (push) Waiting to run
Test All / build (push) Waiting to run

* fix: two tree move (inside the same parent) issues

* chore: add release note
This commit is contained in:
Zixuan Chen 2024-09-07 16:49:28 +08:00 committed by GitHub
parent 622c881605
commit 46e21fccc7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 38 additions and 5 deletions

View file

@ -0,0 +1,6 @@
---
"loro-crdt": patch
"loro-wasm": patch
---
Fix tree move issues

View file

@ -518,7 +518,7 @@ impl TreeHandler {
.ok_or(LoroTreeError::TreeNodeNotExist(other))?;
let mut index = self.get_index_by_tree_id(&other).unwrap();
if self.is_parent(target, parent)
&& index > 1
&& index >= 1
&& self.get_index_by_tree_id(&target).unwrap() < index
{
index -= 1;

View file

@ -1322,9 +1322,7 @@ impl LoroTree {
/// tree.mov(root2, root).unwrap();
/// ```
pub fn mov<T: Into<Option<TreeID>>>(&self, target: TreeID, parent: T) -> LoroResult<()> {
let parent = parent.into();
let index = self.children_num(parent).unwrap_or(0);
self.handler.move_to(target, parent, index)
self.handler.mov(target, parent)
}
/// Move the `target` node to be a child of the `parent` node at the given index.

View file

@ -871,3 +871,22 @@ fn len_and_is_empty_inconsistency() {
assert_eq!(map.len(), 0);
assert!(map.is_empty());
}
#[test]
fn test_tree_move() {
let doc = LoroDoc::new();
let tree = doc.get_tree("tree");
let root1 = tree.create(None).unwrap();
let node1 = tree.create(root1).unwrap();
let node2 = tree.create(root1).unwrap();
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node1, node2]);
tree.mov_before(node2, node1).unwrap();
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node2, node1]);
tree.mov_before(node2, node1).unwrap();
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node2, node1]);
tree.mov_after(node2, node1).unwrap();
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node1, node2]);
tree.mov_after(node2, node1).unwrap();
assert_eq!(tree.children(Some(root1)).unwrap(), vec![node1, node2]);
}

View file

@ -182,6 +182,16 @@ describe("loro tree node", ()=>{
});
});
describe("LoroTree", () => {
it ("move", () => {
const loro = new Loro();
const tree = loro.getTree("root");
const root = tree.createNode();
const child = tree.createNode(root.id);
tree.move(child.id, root.id);
})
})
function one_ms(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 1));
}
}