mirror of
https://github.com/loro-dev/loro.git
synced 2025-01-22 12:57:20 +00:00
fix: two tree move (inside the same parent) issues (#450)
* fix: two tree move (inside the same parent) issues * chore: add release note
This commit is contained in:
parent
622c881605
commit
46e21fccc7
5 changed files with 38 additions and 5 deletions
6
.changeset/lemon-maps-smash.md
Normal file
6
.changeset/lemon-maps-smash.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
"loro-crdt": patch
|
||||
"loro-wasm": patch
|
||||
---
|
||||
|
||||
Fix tree move issues
|
|
@ -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;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue