Преглед изворни кода

feat: transaction built der

Vincent Chan пре 2 година
родитељ
комит
aa90613bf6

+ 41 - 1
shared-lib/lib-ot/src/core/document/document.rs

@@ -1,5 +1,5 @@
 use crate::core::document::position::Position;
-use crate::core::NodeData;
+use crate::core::{NodeData, Transaction};
 use indextree::{Arena, NodeId};
 
 pub struct DocumentTree {
@@ -35,6 +35,42 @@ impl DocumentTree {
         Some(iterate_node)
     }
 
+    pub fn path_of_node(&self, node_id: NodeId) -> Position {
+        let mut path: Vec<usize> = Vec::new();
+
+        let mut ancestors = node_id.ancestors(&self.arena);
+        let mut current_node = node_id;
+        let mut parent = ancestors.next();
+
+        while parent.is_some() {
+            let parent_node = parent.unwrap();
+            let counter = self.index_of_node(parent_node, current_node);
+            path.push(counter);
+            current_node = parent_node;
+            parent = ancestors.next();
+        }
+
+        Position(path)
+    }
+
+    fn index_of_node(&self, parent_node: NodeId, child_node: NodeId) -> usize {
+        let mut counter: usize = 0;
+
+        let mut children_iterator = parent_node.children(&self.arena);
+        let mut node = children_iterator.next();
+
+        while node.is_some() {
+            if node.unwrap() == child_node {
+                return counter;
+            }
+
+            node = children_iterator.next();
+            counter += 1;
+        }
+
+        counter
+    }
+
     fn child_at_index_of_path(&self, at_node: NodeId, index: usize) -> Option<NodeId> {
         let children = at_node.children(&self.arena);
 
@@ -49,4 +85,8 @@ impl DocumentTree {
 
         None
     }
+
+    pub fn apply(&self, _transaction: Transaction) {
+        unimplemented!()
+    }
 }

+ 2 - 0
shared-lib/lib-ot/src/core/document/mod.rs

@@ -3,8 +3,10 @@ mod document;
 mod document_operation;
 mod node;
 mod position;
+mod transaction;
 
 pub use attributes::*;
 pub use document::*;
 pub use document_operation::*;
 pub use node::*;
+pub use transaction::*;

+ 25 - 0
shared-lib/lib-ot/src/core/document/transaction.rs

@@ -0,0 +1,25 @@
+use crate::core::DocumentOperation;
+
+pub struct Transaction {
+    pub operations: Vec<DocumentOperation>,
+}
+
+pub struct TransactionBuilder {
+    operations: Vec<DocumentOperation>,
+}
+
+impl TransactionBuilder {
+    pub fn new() -> TransactionBuilder {
+        TransactionBuilder { operations: Vec::new() }
+    }
+
+    pub fn push(&mut self, op: DocumentOperation) {
+        self.operations.push(op);
+    }
+
+    pub fn finalize(self) -> Transaction {
+        Transaction {
+            operations: self.operations,
+        }
+    }
+}

+ 8 - 1
shared-lib/lib-ot/tests/main.rs

@@ -1,7 +1,14 @@
-use lib_ot::core::DocumentTree;
+use lib_ot::core::{DocumentTree, TransactionBuilder};
 
 #[test]
 fn main() {
     // Create a new arena
     let _document = DocumentTree::new();
 }
+
+#[test]
+fn test_documents() {
+    let document = DocumentTree::new();
+    let tb = TransactionBuilder::new();
+    document.apply(tb.finalize());
+}