Skip to content

Commit 9d1475d

Browse files
committed
feat: recursive append children
1 parent bb8e048 commit 9d1475d

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl DocumentTree {
102102
}
103103
}
104104

105-
fn apply_insert(&mut self, path: &Position, nodes: &Vec<NodeSubTree>) -> Result<(), OTError> {
105+
fn apply_insert(&mut self, path: &Position, nodes: &[Box<NodeSubTree>]) -> Result<(), OTError> {
106106
let parent_path = &path.0[0..(path.0.len() - 1)];
107107
let last_index = path.0[path.0.len() - 1];
108108
let parent_node = self
@@ -121,7 +121,7 @@ impl DocumentTree {
121121
&mut self,
122122
parent: NodeId,
123123
index: usize,
124-
insert_children: &[NodeSubTree],
124+
insert_children: &[Box<NodeSubTree>],
125125
) -> Result<(), OTError> {
126126
if index == 0 && parent.children(&self.arena).next().is_none() {
127127
self.append_subtree(&parent, insert_children);
@@ -143,17 +143,22 @@ impl DocumentTree {
143143
Ok(())
144144
}
145145

146-
fn append_subtree(&mut self, parent: &NodeId, insert_children: &[NodeSubTree]) {
146+
// recursive append the subtrees to the node
147+
fn append_subtree(&mut self, parent: &NodeId, insert_children: &[Box<NodeSubTree>]) {
147148
for child in insert_children {
148149
let child_id = self.arena.new_node(child.to_node_data());
149150
parent.append(child_id, &mut self.arena);
151+
152+
self.append_subtree(&child_id, child.children.as_ref());
150153
}
151154
}
152155

153-
fn insert_subtree_before(&mut self, before: &NodeId, insert_children: &[NodeSubTree]) {
154-
for id in insert_children {
155-
let child_id = self.arena.new_node(id.to_node_data());
156+
fn insert_subtree_before(&mut self, before: &NodeId, insert_children: &[Box<NodeSubTree>]) {
157+
for child in insert_children {
158+
let child_id = self.arena.new_node(child.to_node_data());
156159
before.insert_before(child_id, &mut self.arena);
160+
161+
self.append_subtree(&child_id, child.children.as_ref());
157162
}
158163
}
159164

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::core::{NodeAttributes, NodeSubTree, TextDelta};
55
#[serde(tag = "type")]
66
pub enum DocumentOperation {
77
#[serde(rename = "insert-operation")]
8-
Insert { path: Position, nodes: Vec<NodeSubTree> },
8+
Insert { path: Position, nodes: Vec<Box<NodeSubTree>> },
99
#[serde(rename = "update-operation")]
1010
Update {
1111
path: Position,
@@ -14,7 +14,7 @@ pub enum DocumentOperation {
1414
old_attributes: NodeAttributes,
1515
},
1616
#[serde(rename = "delete-operation")]
17-
Delete { path: Position, nodes: Vec<NodeSubTree> },
17+
Delete { path: Position, nodes: Vec<Box<NodeSubTree>> },
1818
#[serde(rename = "text-edit-operation")]
1919
TextEdit {
2020
path: Position,
@@ -155,7 +155,7 @@ mod tests {
155155
fn test_serialize_insert_operation() {
156156
let insert = DocumentOperation::Insert {
157157
path: Position(vec![0, 1]),
158-
nodes: vec![NodeSubTree::new("text")],
158+
nodes: vec![Box::new(NodeSubTree::new("text"))],
159159
};
160160
let result = serde_json::to_string(&insert).unwrap();
161161
assert_eq!(

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'a> TransactionBuilder<'a> {
2525
}
2626
}
2727

28-
pub fn insert_nodes_at_path(&mut self, path: &Position, nodes: &[NodeSubTree]) {
28+
pub fn insert_nodes_at_path(&mut self, path: &Position, nodes: &[Box<NodeSubTree>]) {
2929
self.push(DocumentOperation::Insert {
3030
path: path.clone(),
3131
nodes: nodes.to_vec(),
@@ -59,17 +59,17 @@ impl<'a> TransactionBuilder<'a> {
5959

6060
pub fn delete_nodes_at_path(&mut self, path: &Position, length: usize) {
6161
let mut node = self.document.node_at_path(path).unwrap();
62-
let mut deleted_nodes: Vec<NodeSubTree> = Vec::new();
62+
let mut deleted_nodes: Vec<Box<NodeSubTree>> = Vec::new();
6363

6464
for _ in 0..length {
6565
let node_data = self.document.arena.get(node).unwrap();
6666
let data = node_data.get();
67-
deleted_nodes.push(NodeSubTree {
67+
deleted_nodes.push(Box::new(NodeSubTree {
6868
node_type: data.node_type.clone(),
6969
attributes: data.attributes.clone(),
7070
delta: data.delta.clone(),
7171
children: vec![],
72-
});
72+
}));
7373
node = node.following_siblings(&self.document.arena).next().unwrap();
7474
}
7575

0 commit comments

Comments
 (0)