Browse Source

feat: recursive append children

Vincent Chan 2 năm trước cách đây
mục cha
commit
9d1475df2b

+ 11 - 6
shared-lib/lib-ot/src/core/document/document.rs

@@ -102,7 +102,7 @@ impl DocumentTree {
         }
     }
 
-    fn apply_insert(&mut self, path: &Position, nodes: &Vec<NodeSubTree>) -> Result<(), OTError> {
+    fn apply_insert(&mut self, path: &Position, nodes: &[Box<NodeSubTree>]) -> 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<NodeSubTree>],
     ) -> 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<NodeSubTree>]) {
         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<NodeSubTree>]) {
+        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());
         }
     }
 

+ 3 - 3
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<NodeSubTree> },
+    Insert { path: Position, nodes: Vec<Box<NodeSubTree>> },
     #[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<NodeSubTree> },
+    Delete { path: Position, nodes: Vec<Box<NodeSubTree>> },
     #[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!(

+ 4 - 4
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<NodeSubTree>]) {
         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<NodeSubTree> = Vec::new();
+        let mut deleted_nodes: Vec<Box<NodeSubTree>> = 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();
         }