feat: compose attributes

This commit is contained in:
Vincent Chan 2022-08-18 16:19:50 +08:00
parent bb7c7e4f41
commit ba160c8026
5 changed files with 52 additions and 15 deletions

View File

@ -7,4 +7,16 @@ impl NodeAttributes {
pub fn new() -> NodeAttributes { pub fn new() -> NodeAttributes {
NodeAttributes(HashMap::new()) 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)
}
} }

View File

@ -1,5 +1,5 @@
use crate::core::document::position::Position; 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}; use indextree::{Arena, NodeId};
pub struct DocumentTree { pub struct DocumentTree {
@ -125,12 +125,19 @@ impl DocumentTree {
} }
} }
fn apply_update(&self, _op: &UpdateOperation) { fn apply_update(&self, op: &UpdateOperation) {
unimplemented!() 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) { fn apply_delete(&mut self, op: &DeleteOperation) {
unimplemented!() let update_node = self.node_at_path(&op.path).unwrap();
update_node.remove_subtree(&mut self.arena);
} }
fn apply_text_edit(&self, _op: &TextEditOperation) { fn apply_text_edit(&self, _op: &TextEditOperation) {

View File

@ -1,15 +1,18 @@
use crate::core::NodeAttributes; use std::cell::RefCell;
use crate::core::{TextDelta, NodeAttributes};
pub struct NodeData { pub struct NodeData {
pub node_type: String, pub node_type: String,
pub attributes: NodeAttributes, pub attributes: RefCell<NodeAttributes>,
pub delta: RefCell<Option<TextDelta>>,
} }
impl NodeData { impl NodeData {
pub fn new(node_type: &str) -> NodeData { pub fn new(node_type: &str) -> NodeData {
NodeData { NodeData {
node_type: node_type.into(), node_type: node_type.into(),
attributes: NodeAttributes::new(), attributes: RefCell::new(NodeAttributes::new()),
delta: RefCell::new(None),
} }
} }
} }

View File

@ -1,16 +1,30 @@
use crate::core::DocumentOperation; use crate::core::{DocumentOperation, DocumentTree};
pub struct Transaction { pub struct Transaction {
pub operations: Vec<DocumentOperation>, 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>, operations: Vec<DocumentOperation>,
} }
impl TransactionBuilder { impl<'a> TransactionBuilder<'a> {
pub fn new() -> TransactionBuilder { pub fn new(document: &'a DocumentTree) -> TransactionBuilder {
TransactionBuilder { operations: Vec::new() } TransactionBuilder {
document,
operations: Vec::new()
}
} }
pub fn push(&mut self, op: DocumentOperation) { pub fn push(&mut self, op: DocumentOperation) {

View File

@ -9,6 +9,7 @@ fn main() {
#[test] #[test]
fn test_documents() { fn test_documents() {
let mut document = DocumentTree::new(); let mut document = DocumentTree::new();
let tb = TransactionBuilder::new(); let tb = TransactionBuilder::new(&document);
document.apply(tb.finalize()); let transaction = tb.finalize();
document.apply(transaction);
} }