Skip to content

Commit d6ef13a

Browse files
committed
feat: get deleted subtrees from the document
1 parent 9d1475d commit d6ef13a

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

shared-lib/lib-ot/src/core/document/document.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ impl DocumentTree {
108108
let parent_node = self
109109
.node_at_path(&Position(parent_path.to_vec()))
110110
.ok_or(ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
111-
// let mut inserted_nodes = Vec::new();
112-
//
113-
// for node in nodes {
114-
// inserted_nodes.push(self.arena.new_node(node.to_node_data()));
115-
// }
116111

117112
self.insert_child_at_index(parent_node, last_index, nodes.as_ref())
118113
}
@@ -167,10 +162,6 @@ impl DocumentTree {
167162
.node_at_path(path)
168163
.ok_or(ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
169164
let node_data = self.arena.get_mut(update_node).unwrap();
170-
// let new_node = NodeData {
171-
// ..node_data.get().clone()
172-
// attributes:
173-
// };
174165
let new_node = {
175166
let old_attributes = &node_data.get().attributes;
176167
let new_attributes = NodeAttributes::compose(&old_attributes, attributes);

shared-lib/lib-ot/src/core/document/transaction.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::core::document::position::Position;
22
use crate::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree};
33
use std::collections::HashMap;
4+
use indextree::NodeId;
45

56
pub struct Transaction {
67
pub operations: Vec<DocumentOperation>,
@@ -62,14 +63,7 @@ impl<'a> TransactionBuilder<'a> {
6263
let mut deleted_nodes: Vec<Box<NodeSubTree>> = Vec::new();
6364

6465
for _ in 0..length {
65-
let node_data = self.document.arena.get(node).unwrap();
66-
let data = node_data.get();
67-
deleted_nodes.push(Box::new(NodeSubTree {
68-
node_type: data.node_type.clone(),
69-
attributes: data.attributes.clone(),
70-
delta: data.delta.clone(),
71-
children: vec![],
72-
}));
66+
deleted_nodes.push(self.get_deleted_nodes(node.clone()));
7367
node = node.following_siblings(&self.document.arena).next().unwrap();
7468
}
7569

@@ -79,6 +73,29 @@ impl<'a> TransactionBuilder<'a> {
7973
})
8074
}
8175

76+
fn get_deleted_nodes(&self, node_id: NodeId) -> Box<NodeSubTree> {
77+
let node = self.document.arena.get(node_id.clone()).unwrap();
78+
let node_data = node.get();
79+
let mut children: Vec<Box<NodeSubTree>> = vec![];
80+
81+
let mut children_iterators = node_id.children(&self.document.arena);
82+
loop {
83+
let next_child = children_iterators.next();
84+
if let Some(child_id) = next_child {
85+
children.push(self.get_deleted_nodes(child_id));
86+
} else {
87+
break;
88+
}
89+
};
90+
91+
Box::new(NodeSubTree {
92+
node_type: node_data.node_type.clone(),
93+
attributes: node_data.attributes.clone(),
94+
delta: node_data.delta.clone(),
95+
children,
96+
})
97+
}
98+
8299
pub fn push(&mut self, op: DocumentOperation) {
83100
self.operations.push(op);
84101
}

0 commit comments

Comments
 (0)