chore: private the root node

This commit is contained in:
appflowy 2022-09-08 20:43:06 +08:00
parent 9974539946
commit ac23f81e24
5 changed files with 14 additions and 10 deletions

View File

@ -3,11 +3,11 @@ use crate::core::{
DocumentOperation, NodeAttributes, NodeData, NodeSubTree, OperationTransform, TextDelta, Transaction,
};
use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
use indextree::{Arena, Children, FollowingSiblings, Node, NodeId};
use indextree::{Arena, Children, FollowingSiblings, NodeId};
pub struct DocumentTree {
arena: Arena<NodeData>,
pub root: NodeId,
root: NodeId,
}
impl Default for DocumentTree {
@ -19,6 +19,7 @@ impl Default for DocumentTree {
impl DocumentTree {
pub fn new() -> DocumentTree {
let mut arena = Arena::new();
let root = arena.new_node(NodeData::new("root"));
DocumentTree { arena, root }
}
@ -104,7 +105,7 @@ impl DocumentTree {
///
/// let inserted_note = document.node_at_path(&inserted_path).unwrap();
/// let inserted_data = document.get_node_data(inserted_note).unwrap();
/// assert_eq!(inserted_data.node_type, node.node_type);
/// assert_eq!(inserted_data.node_type, node.note_type);
/// ```
pub fn child_from_node_with_index(&self, at_node: NodeId, index: usize) -> Option<NodeId> {
let children = at_node.children(&self.arena);

View File

@ -169,7 +169,7 @@ mod tests {
let insert = DocumentOperation::Insert {
path: Path(vec![0, 1]),
nodes: vec![NodeSubTree {
node_type: "text".into(),
note_type: "text".into(),
attributes: NodeAttributes::new(),
delta: None,
children: vec![NodeSubTree::new("text")],

View File

@ -20,10 +20,13 @@ impl NodeData {
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct NodeSubTree {
#[serde(rename = "type")]
pub node_type: String,
pub note_type: String,
pub attributes: NodeAttributes,
#[serde(skip_serializing_if = "Option::is_none")]
pub delta: Option<TextDelta>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub children: Vec<NodeSubTree>,
}
@ -31,7 +34,7 @@ pub struct NodeSubTree {
impl NodeSubTree {
pub fn new(node_type: &str) -> NodeSubTree {
NodeSubTree {
node_type: node_type.into(),
note_type: node_type.into(),
attributes: NodeAttributes::new(),
delta: None,
children: Vec::new(),
@ -40,7 +43,7 @@ impl NodeSubTree {
pub fn to_node_data(&self) -> NodeData {
NodeData {
node_type: self.node_type.clone(),
node_type: self.note_type.clone(),
attributes: self.attributes.clone(),
delta: self.delta.clone(),
}

View File

@ -134,7 +134,7 @@ impl<'a> TransactionBuilder<'a> {
});
NodeSubTree {
node_type: node_data.node_type.clone(),
note_type: node_data.node_type.clone(),
attributes: node_data.attributes.clone(),
delta: node_data.delta.clone(),
children,

View File

@ -1,4 +1,4 @@
use lib_ot::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree, Path, TransactionBuilder};
use lib_ot::core::{DocumentTree, NodeAttributes, NodeSubTree, Path, TransactionBuilder};
use lib_ot::errors::OTErrorCode;
use std::collections::HashMap;
@ -70,7 +70,7 @@ fn test_inserts_subtrees() {
tb.insert_node_at_path(
0,
NodeSubTree {
node_type: "text".into(),
note_type: "text".into(),
attributes: NodeAttributes::new(),
delta: None,
children: vec![NodeSubTree::new("image")],