mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
fix: skip root to get the right path
This commit is contained in:
parent
89a5ee4a8a
commit
d386698e97
@ -23,6 +23,21 @@ impl DocumentTree {
|
|||||||
DocumentTree { arena, root }
|
DocumentTree { arena, root }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use lib_ot::core::{DocumentOperation, DocumentTree, NodeSubTree, Path};
|
||||||
|
/// let nodes = vec![NodeSubTree::new("text")];
|
||||||
|
/// let root_path: Path = vec![0].into();
|
||||||
|
/// let op = DocumentOperation::Insert {path: root_path.clone(),nodes };
|
||||||
|
///
|
||||||
|
/// let mut document = DocumentTree::new();
|
||||||
|
/// document.apply_op(&op).unwrap();
|
||||||
|
/// let node_id = document.node_at_path(&root_path).unwrap();
|
||||||
|
/// let node_path = document.path_of_node(node_id);
|
||||||
|
/// debug_assert_eq!(node_path, root_path);
|
||||||
|
/// ```
|
||||||
pub fn node_at_path<T: Into<Path>>(&self, path: T) -> Option<NodeId> {
|
pub fn node_at_path<T: Into<Path>>(&self, path: T) -> Option<NodeId> {
|
||||||
let path = path.into();
|
let path = path.into();
|
||||||
if path.is_empty() {
|
if path.is_empty() {
|
||||||
@ -36,30 +51,9 @@ impl DocumentTree {
|
|||||||
Some(iterate_node)
|
Some(iterate_node)
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `node_id`:
|
|
||||||
///
|
|
||||||
/// returns: Path
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use lib_ot::core::{DocumentOperation, DocumentTree, NodeSubTree, Path};
|
|
||||||
/// let text_node = NodeSubTree::new("text");
|
|
||||||
/// let path: Path = vec![0].into();
|
|
||||||
/// let op = DocumentOperation::Insert {path: path.clone(),nodes: vec![text_node
|
|
||||||
/// ]};
|
|
||||||
/// let mut document = DocumentTree::new();
|
|
||||||
/// document.apply_op(&op).unwrap();
|
|
||||||
/// let node_id = document.node_at_path(&path).unwrap();
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
pub fn path_of_node(&self, node_id: NodeId) -> Path {
|
pub fn path_of_node(&self, node_id: NodeId) -> Path {
|
||||||
let mut path: Vec<usize> = Vec::new();
|
let mut path: Vec<usize> = Vec::new();
|
||||||
let mut ancestors = node_id.ancestors(&self.arena);
|
let mut ancestors = node_id.ancestors(&self.arena).skip(1);
|
||||||
let mut current_node = node_id;
|
let mut current_node = node_id;
|
||||||
let mut parent = ancestors.next();
|
let mut parent = ancestors.next();
|
||||||
|
|
||||||
@ -76,10 +70,8 @@ impl DocumentTree {
|
|||||||
|
|
||||||
fn index_of_node(&self, parent_node: NodeId, child_node: NodeId) -> usize {
|
fn index_of_node(&self, parent_node: NodeId, child_node: NodeId) -> usize {
|
||||||
let mut counter: usize = 0;
|
let mut counter: usize = 0;
|
||||||
|
|
||||||
let mut children_iterator = parent_node.children(&self.arena);
|
let mut children_iterator = parent_node.children(&self.arena);
|
||||||
let mut node = children_iterator.next();
|
let mut node = children_iterator.next();
|
||||||
|
|
||||||
while node.is_some() {
|
while node.is_some() {
|
||||||
if node.unwrap() == child_node {
|
if node.unwrap() == child_node {
|
||||||
return counter;
|
return counter;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
|
||||||
pub struct Path(pub Vec<usize>);
|
pub struct Path(pub Vec<usize>);
|
||||||
|
|
||||||
impl std::ops::Deref for Path {
|
impl std::ops::Deref for Path {
|
||||||
|
Loading…
Reference in New Issue
Block a user