Procházet zdrojové kódy

feat: get deleted subtrees from the document

Vincent Chan před 2 roky
rodič
revize
d6ef13adae

+ 0 - 9
shared-lib/lib-ot/src/core/document/document.rs

@@ -108,11 +108,6 @@ impl DocumentTree {
         let parent_node = self
             .node_at_path(&Position(parent_path.to_vec()))
             .ok_or(ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
-        // let mut inserted_nodes = Vec::new();
-        //
-        // for node in nodes {
-        //     inserted_nodes.push(self.arena.new_node(node.to_node_data()));
-        // }
 
         self.insert_child_at_index(parent_node, last_index, nodes.as_ref())
     }
@@ -167,10 +162,6 @@ impl DocumentTree {
             .node_at_path(path)
             .ok_or(ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
         let node_data = self.arena.get_mut(update_node).unwrap();
-        // let new_node = NodeData {
-        //     ..node_data.get().clone()
-        //     attributes:
-        // };
         let new_node = {
             let old_attributes = &node_data.get().attributes;
             let new_attributes = NodeAttributes::compose(&old_attributes, attributes);

+ 25 - 8
shared-lib/lib-ot/src/core/document/transaction.rs

@@ -1,6 +1,7 @@
 use crate::core::document::position::Position;
 use crate::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree};
 use std::collections::HashMap;
+use indextree::NodeId;
 
 pub struct Transaction {
     pub operations: Vec<DocumentOperation>,
@@ -62,14 +63,7 @@ impl<'a> TransactionBuilder<'a> {
         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(Box::new(NodeSubTree {
-                node_type: data.node_type.clone(),
-                attributes: data.attributes.clone(),
-                delta: data.delta.clone(),
-                children: vec![],
-            }));
+            deleted_nodes.push(self.get_deleted_nodes(node.clone()));
             node = node.following_siblings(&self.document.arena).next().unwrap();
         }
 
@@ -79,6 +73,29 @@ impl<'a> TransactionBuilder<'a> {
         })
     }
 
+    fn get_deleted_nodes(&self, node_id: NodeId) -> Box<NodeSubTree> {
+        let node = self.document.arena.get(node_id.clone()).unwrap();
+        let node_data = node.get();
+        let mut children: Vec<Box<NodeSubTree>> = vec![];
+
+        let mut children_iterators = node_id.children(&self.document.arena);
+        loop {
+            let next_child = children_iterators.next();
+            if let Some(child_id) = next_child {
+                children.push(self.get_deleted_nodes(child_id));
+            } else {
+                break;
+            }
+        };
+
+        Box::new(NodeSubTree {
+            node_type: node_data.node_type.clone(),
+            attributes: node_data.attributes.clone(),
+            delta: node_data.delta.clone(),
+            children,
+        })
+    }
+
     pub fn push(&mut self, op: DocumentOperation) {
         self.operations.push(op);
     }