Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
feat: recursive append children
Vincent Chan9d1475df2bfd
diff --git a/shared-lib/lib-ot/src/core/document/document.rs b/shared-lib/lib-ot/src/core/document/document.rsindex 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<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());}}diff --git a/shared-lib/lib-ot/src/core/document/document_operation.rs b/shared-lib/lib-ot/src/core/document/document_operation.rsindex 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<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!(diff --git a/shared-lib/lib-ot/src/core/document/transaction.rs b/shared-lib/lib-ot/src/core/document/transaction.rsindex 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<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();}