mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: recursive append children
This commit is contained in:
@ -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 parent_path = &path.0[0..(path.0.len() - 1)];
|
||||||
let last_index = path.0[path.0.len() - 1];
|
let last_index = path.0[path.0.len() - 1];
|
||||||
let parent_node = self
|
let parent_node = self
|
||||||
@ -121,7 +121,7 @@ impl DocumentTree {
|
|||||||
&mut self,
|
&mut self,
|
||||||
parent: NodeId,
|
parent: NodeId,
|
||||||
index: usize,
|
index: usize,
|
||||||
insert_children: &[NodeSubTree],
|
insert_children: &[Box<NodeSubTree>],
|
||||||
) -> Result<(), OTError> {
|
) -> Result<(), OTError> {
|
||||||
if index == 0 && parent.children(&self.arena).next().is_none() {
|
if index == 0 && parent.children(&self.arena).next().is_none() {
|
||||||
self.append_subtree(&parent, insert_children);
|
self.append_subtree(&parent, insert_children);
|
||||||
@ -143,17 +143,22 @@ impl DocumentTree {
|
|||||||
Ok(())
|
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 {
|
for child in insert_children {
|
||||||
let child_id = self.arena.new_node(child.to_node_data());
|
let child_id = self.arena.new_node(child.to_node_data());
|
||||||
parent.append(child_id, &mut self.arena);
|
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]) {
|
fn insert_subtree_before(&mut self, before: &NodeId, insert_children: &[Box<NodeSubTree>]) {
|
||||||
for id in insert_children {
|
for child in insert_children {
|
||||||
let child_id = self.arena.new_node(id.to_node_data());
|
let child_id = self.arena.new_node(child.to_node_data());
|
||||||
before.insert_before(child_id, &mut self.arena);
|
before.insert_before(child_id, &mut self.arena);
|
||||||
|
|
||||||
|
self.append_subtree(&child_id, child.children.as_ref());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ use crate::core::{NodeAttributes, NodeSubTree, TextDelta};
|
|||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum DocumentOperation {
|
pub enum DocumentOperation {
|
||||||
#[serde(rename = "insert-operation")]
|
#[serde(rename = "insert-operation")]
|
||||||
Insert { path: Position, nodes: Vec<NodeSubTree> },
|
Insert { path: Position, nodes: Vec<Box<NodeSubTree>> },
|
||||||
#[serde(rename = "update-operation")]
|
#[serde(rename = "update-operation")]
|
||||||
Update {
|
Update {
|
||||||
path: Position,
|
path: Position,
|
||||||
@ -14,7 +14,7 @@ pub enum DocumentOperation {
|
|||||||
old_attributes: NodeAttributes,
|
old_attributes: NodeAttributes,
|
||||||
},
|
},
|
||||||
#[serde(rename = "delete-operation")]
|
#[serde(rename = "delete-operation")]
|
||||||
Delete { path: Position, nodes: Vec<NodeSubTree> },
|
Delete { path: Position, nodes: Vec<Box<NodeSubTree>> },
|
||||||
#[serde(rename = "text-edit-operation")]
|
#[serde(rename = "text-edit-operation")]
|
||||||
TextEdit {
|
TextEdit {
|
||||||
path: Position,
|
path: Position,
|
||||||
@ -155,7 +155,7 @@ mod tests {
|
|||||||
fn test_serialize_insert_operation() {
|
fn test_serialize_insert_operation() {
|
||||||
let insert = DocumentOperation::Insert {
|
let insert = DocumentOperation::Insert {
|
||||||
path: Position(vec![0, 1]),
|
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();
|
let result = serde_json::to_string(&insert).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -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 {
|
self.push(DocumentOperation::Insert {
|
||||||
path: path.clone(),
|
path: path.clone(),
|
||||||
nodes: nodes.to_vec(),
|
nodes: nodes.to_vec(),
|
||||||
@ -59,17 +59,17 @@ impl<'a> TransactionBuilder<'a> {
|
|||||||
|
|
||||||
pub fn delete_nodes_at_path(&mut self, path: &Position, length: usize) {
|
pub fn delete_nodes_at_path(&mut self, path: &Position, length: usize) {
|
||||||
let mut node = self.document.node_at_path(path).unwrap();
|
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 {
|
for _ in 0..length {
|
||||||
let node_data = self.document.arena.get(node).unwrap();
|
let node_data = self.document.arena.get(node).unwrap();
|
||||||
let data = node_data.get();
|
let data = node_data.get();
|
||||||
deleted_nodes.push(NodeSubTree {
|
deleted_nodes.push(Box::new(NodeSubTree {
|
||||||
node_type: data.node_type.clone(),
|
node_type: data.node_type.clone(),
|
||||||
attributes: data.attributes.clone(),
|
attributes: data.attributes.clone(),
|
||||||
delta: data.delta.clone(),
|
delta: data.delta.clone(),
|
||||||
children: vec![],
|
children: vec![],
|
||||||
});
|
}));
|
||||||
node = node.following_siblings(&self.document.arena).next().unwrap();
|
node = node.following_siblings(&self.document.arena).next().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user