mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: transaction built der
This commit is contained in:
parent
2466b3eebc
commit
aa90613bf6
@ -1,5 +1,5 @@
|
|||||||
use crate::core::document::position::Position;
|
use crate::core::document::position::Position;
|
||||||
use crate::core::NodeData;
|
use crate::core::{NodeData, Transaction};
|
||||||
use indextree::{Arena, NodeId};
|
use indextree::{Arena, NodeId};
|
||||||
|
|
||||||
pub struct DocumentTree {
|
pub struct DocumentTree {
|
||||||
@ -35,6 +35,42 @@ impl DocumentTree {
|
|||||||
Some(iterate_node)
|
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> {
|
fn child_at_index_of_path(&self, at_node: NodeId, index: usize) -> Option<NodeId> {
|
||||||
let children = at_node.children(&self.arena);
|
let children = at_node.children(&self.arena);
|
||||||
|
|
||||||
@ -49,4 +85,8 @@ impl DocumentTree {
|
|||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn apply(&self, _transaction: Transaction) {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,10 @@ mod document;
|
|||||||
mod document_operation;
|
mod document_operation;
|
||||||
mod node;
|
mod node;
|
||||||
mod position;
|
mod position;
|
||||||
|
mod transaction;
|
||||||
|
|
||||||
pub use attributes::*;
|
pub use attributes::*;
|
||||||
pub use document::*;
|
pub use document::*;
|
||||||
pub use document_operation::*;
|
pub use document_operation::*;
|
||||||
pub use node::*;
|
pub use node::*;
|
||||||
|
pub use transaction::*;
|
||||||
|
25
shared-lib/lib-ot/src/core/document/transaction.rs
Normal file
25
shared-lib/lib-ot/src/core/document/transaction.rs
Normal file
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,14 @@
|
|||||||
use lib_ot::core::DocumentTree;
|
use lib_ot::core::{DocumentTree, TransactionBuilder};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn main() {
|
fn main() {
|
||||||
// Create a new arena
|
// Create a new arena
|
||||||
let _document = DocumentTree::new();
|
let _document = DocumentTree::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_documents() {
|
||||||
|
let document = DocumentTree::new();
|
||||||
|
let tb = TransactionBuilder::new();
|
||||||
|
document.apply(tb.finalize());
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user