Skip to content

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.

feat: transaction built der

Vincent Chanaa90613bf65a
diff --git a/shared-lib/lib-ot/src/core/document/document.rs b/shared-lib/lib-ot/src/core/document/document.rs
index fa8e2aea6a..7acd357159 100644
--- a/shared-lib/lib-ot/src/core/document/document.rs
+++ b/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!()
+ }
}
diff --git a/shared-lib/lib-ot/src/core/document/mod.rs b/shared-lib/lib-ot/src/core/document/mod.rs
index 81008fa50e..968c78bc2b 100644
--- a/shared-lib/lib-ot/src/core/document/mod.rs
+++ b/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::*;
diff --git a/shared-lib/lib-ot/src/core/document/transaction.rs b/shared-lib/lib-ot/src/core/document/transaction.rs
new file mode 100644
index 0000000000..7e48a4c544
--- /dev/null
+++ b/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,
+ }
+ }
+}
diff --git a/shared-lib/lib-ot/tests/main.rs b/shared-lib/lib-ot/tests/main.rs
index 2c766017e4..cf2a6a94f3 100644
--- a/shared-lib/lib-ot/tests/main.rs
+++ b/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());
+}