diff --git a/shared-lib/lib-ot/src/core/document/document.rs b/shared-lib/lib-ot/src/core/document/document.rs index 17cd1d7989..340f4fdc57 100644 --- a/shared-lib/lib-ot/src/core/document/document.rs +++ b/shared-lib/lib-ot/src/core/document/document.rs @@ -102,7 +102,7 @@ impl DocumentTree { } } - fn apply_insert(&mut self, path: &Position, nodes: &Vec) -> Result<(), OTError> { + fn apply_insert(&mut self, path: &Position, nodes: &[Box]) -> Result<(), OTError> { let parent_path = &path.0[0..(path.0.len() - 1)]; let last_index = path.0[path.0.len() - 1]; let parent_node = self @@ -121,7 +121,7 @@ impl DocumentTree { &mut self, parent: NodeId, index: usize, - insert_children: &[NodeSubTree], + insert_children: &[Box], ) -> Result<(), OTError> { if index == 0 && parent.children(&self.arena).next().is_none() { self.append_subtree(&parent, insert_children); @@ -143,17 +143,22 @@ impl DocumentTree { Ok(()) } - fn append_subtree(&mut self, parent: &NodeId, insert_children: &[NodeSubTree]) { + // recursive append the subtrees to the node + fn append_subtree(&mut self, parent: &NodeId, insert_children: &[Box]) { for child in insert_children { let child_id = self.arena.new_node(child.to_node_data()); parent.append(child_id, &mut self.arena); + + self.append_subtree(&child_id, child.children.as_ref()); } } - fn insert_subtree_before(&mut self, before: &NodeId, insert_children: &[NodeSubTree]) { - for id in insert_children { - let child_id = self.arena.new_node(id.to_node_data()); + fn insert_subtree_before(&mut self, before: &NodeId, insert_children: &[Box]) { + for child in insert_children { + let child_id = self.arena.new_node(child.to_node_data()); before.insert_before(child_id, &mut self.arena); + + self.append_subtree(&child_id, child.children.as_ref()); } } diff --git a/shared-lib/lib-ot/src/core/document/document_operation.rs b/shared-lib/lib-ot/src/core/document/document_operation.rs index 6a2e97e976..77c74127bd 100644 --- a/shared-lib/lib-ot/src/core/document/document_operation.rs +++ b/shared-lib/lib-ot/src/core/document/document_operation.rs @@ -5,7 +5,7 @@ use crate::core::{NodeAttributes, NodeSubTree, TextDelta}; #[serde(tag = "type")] pub enum DocumentOperation { #[serde(rename = "insert-operation")] - Insert { path: Position, nodes: Vec }, + Insert { path: Position, nodes: Vec> }, #[serde(rename = "update-operation")] Update { path: Position, @@ -14,7 +14,7 @@ pub enum DocumentOperation { old_attributes: NodeAttributes, }, #[serde(rename = "delete-operation")] - Delete { path: Position, nodes: Vec }, + Delete { path: Position, nodes: Vec> }, #[serde(rename = "text-edit-operation")] TextEdit { path: Position, @@ -155,7 +155,7 @@ mod tests { fn test_serialize_insert_operation() { let insert = DocumentOperation::Insert { path: Position(vec![0, 1]), - nodes: vec![NodeSubTree::new("text")], + nodes: vec![Box::new(NodeSubTree::new("text"))], }; let result = serde_json::to_string(&insert).unwrap(); assert_eq!( diff --git a/shared-lib/lib-ot/src/core/document/transaction.rs b/shared-lib/lib-ot/src/core/document/transaction.rs index b24a085eae..86448820bc 100644 --- a/shared-lib/lib-ot/src/core/document/transaction.rs +++ b/shared-lib/lib-ot/src/core/document/transaction.rs @@ -25,7 +25,7 @@ impl<'a> TransactionBuilder<'a> { } } - pub fn insert_nodes_at_path(&mut self, path: &Position, nodes: &[NodeSubTree]) { + pub fn insert_nodes_at_path(&mut self, path: &Position, nodes: &[Box]) { self.push(DocumentOperation::Insert { path: path.clone(), nodes: nodes.to_vec(), @@ -59,17 +59,17 @@ impl<'a> TransactionBuilder<'a> { pub fn delete_nodes_at_path(&mut self, path: &Position, length: usize) { let mut node = self.document.node_at_path(path).unwrap(); - let mut deleted_nodes: Vec = Vec::new(); + let mut deleted_nodes: Vec> = Vec::new(); for _ in 0..length { let node_data = self.document.arena.get(node).unwrap(); let data = node_data.get(); - deleted_nodes.push(NodeSubTree { + deleted_nodes.push(Box::new(NodeSubTree { node_type: data.node_type.clone(), attributes: data.attributes.clone(), delta: data.delta.clone(), children: vec![], - }); + })); node = node.following_siblings(&self.document.arena).next().unwrap(); }