فهرست منبع

feat: compose attributes

Vincent Chan 2 سال پیش
والد
کامیت
ba160c8026

+ 12 - 0
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)
+    }
 }

+ 12 - 5
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) {

+ 6 - 3
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),
         }
     }
 }

+ 19 - 5
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) {

+ 3 - 2
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);
 }