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: compose attributes

Vincent Chanba160c8026de
diff --git a/shared-lib/lib-ot/src/core/document/attributes.rs b/shared-lib/lib-ot/src/core/document/attributes.rs
index 1b898ccbb3..011c4cec43 100644
--- a/shared-lib/lib-ot/src/core/document/attributes.rs
+++ b/shared-lib/lib-ot/src/core/document/attributes.rs
@@ -7,4 +7,16 @@ impl NodeAttributes {
pub fn new() -> NodeAttributes {
NodeAttributes(HashMap::new())
}
+
+ pub fn compose(a: &NodeAttributes, b: &NodeAttributes) -> NodeAttributes {
+ let mut new_map: HashMap<String, Option<String>> = b.0.clone();
+
+ for (key, value) in &a.0 {
+ if b.0.contains_key(key.as_str()) {
+ new_map.insert(key.into(), value.clone());
+ }
+ }
+
+ NodeAttributes(new_map)
+ }
}
diff --git a/shared-lib/lib-ot/src/core/document/document.rs b/shared-lib/lib-ot/src/core/document/document.rs
index bc62491c82..4200d4e398 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::{DeleteOperation, DocumentOperation, InsertOperation, NodeData, TextEditOperation, Transaction, UpdateOperation};
+use crate::core::{DeleteOperation, DocumentOperation, InsertOperation, NodeAttributes, NodeData, TextEditOperation, Transaction, UpdateOperation};
use indextree::{Arena, NodeId};
pub struct DocumentTree {
@@ -125,12 +125,19 @@ impl DocumentTree {
}
}
- fn apply_update(&self, _op: &UpdateOperation) {
- unimplemented!()
+ fn apply_update(&self, op: &UpdateOperation) {
+ let update_node = self.node_at_path(&op.path).unwrap();
+ let node_data = self.arena.get(update_node).unwrap();
+ let new_attributes = {
+ let old_attributes = node_data.get().attributes.borrow();
+ NodeAttributes::compose(&old_attributes, &op.attributes)
+ };
+ node_data.get().attributes.replace(new_attributes);
}
- fn apply_delete(&self, _op: &DeleteOperation) {
- unimplemented!()
+ fn apply_delete(&mut self, op: &DeleteOperation) {
+ let update_node = self.node_at_path(&op.path).unwrap();
+ update_node.remove_subtree(&mut self.arena);
}
fn apply_text_edit(&self, _op: &TextEditOperation) {
diff --git a/shared-lib/lib-ot/src/core/document/node.rs b/shared-lib/lib-ot/src/core/document/node.rs
index 7f0f172cd6..2fa9706ce3 100644
--- a/shared-lib/lib-ot/src/core/document/node.rs
+++ b/shared-lib/lib-ot/src/core/document/node.rs
@@ -1,15 +1,18 @@
-use crate::core::NodeAttributes;
+use std::cell::RefCell;
+use crate::core::{TextDelta, NodeAttributes};
pub struct NodeData {
pub node_type: String,
- pub attributes: NodeAttributes,
+ pub attributes: RefCell<NodeAttributes>,
+ pub delta: RefCell<Option<TextDelta>>,
}
impl NodeData {
pub fn new(node_type: &str) -> NodeData {
NodeData {
node_type: node_type.into(),
- attributes: NodeAttributes::new(),
+ attributes: RefCell::new(NodeAttributes::new()),
+ delta: RefCell::new(None),
}
}
}
diff --git a/shared-lib/lib-ot/src/core/document/transaction.rs b/shared-lib/lib-ot/src/core/document/transaction.rs
index 7e48a4c544..c75cb16bea 100644
--- a/shared-lib/lib-ot/src/core/document/transaction.rs
+++ b/shared-lib/lib-ot/src/core/document/transaction.rs
@@ -1,16 +1,30 @@
-use crate::core::DocumentOperation;
+use crate::core::{DocumentOperation, DocumentTree};
pub struct Transaction {
pub operations: Vec<DocumentOperation>,
}
-pub struct TransactionBuilder {
+impl Transaction {
+
+ fn new(operations: Vec<DocumentOperation>) -> Transaction {
+ Transaction {
+ operations,
+ }
+ }
+
+}
+
+pub struct TransactionBuilder<'a> {
+ document: &'a DocumentTree,
operations: Vec<DocumentOperation>,
}
-impl TransactionBuilder {
- pub fn new() -> TransactionBuilder {
- TransactionBuilder { operations: Vec::new() }
+impl<'a> TransactionBuilder<'a> {
+ pub fn new(document: &'a DocumentTree) -> TransactionBuilder {
+ TransactionBuilder {
+ document,
+ operations: Vec::new()
+ }
}
pub fn push(&mut self, op: DocumentOperation) {
diff --git a/shared-lib/lib-ot/tests/main.rs b/shared-lib/lib-ot/tests/main.rs
index d554c12cfb..ecfdfb4da2 100644
--- a/shared-lib/lib-ot/tests/main.rs
+++ b/shared-lib/lib-ot/tests/main.rs
@@ -9,6 +9,7 @@ fn main() {
#[test]
fn test_documents() {
let mut document = DocumentTree::new();
- let tb = TransactionBuilder::new();
- document.apply(tb.finalize());
+ let tb = TransactionBuilder::new(&document);
+ let transaction = tb.finalize();
+ document.apply(transaction);
}