|
@@ -1,5 +1,6 @@
|
|
|
+use std::collections::HashMap;
|
|
|
use crate::core::document::position::Position;
|
|
|
-use crate::core::{DeleteOperation, DocumentOperation, DocumentTree, InsertOperation, NodeData};
|
|
|
+use crate::core::{DeleteOperation, DocumentOperation, DocumentTree, InsertOperation, NodeAttributes, NodeData, UpdateOperation};
|
|
|
|
|
|
pub struct Transaction {
|
|
|
pub operations: Vec<DocumentOperation>,
|
|
@@ -31,6 +32,27 @@ impl<'a> TransactionBuilder<'a> {
|
|
|
}));
|
|
|
}
|
|
|
|
|
|
+ pub fn update_attributes(&mut self, path: &Position, attributes: HashMap<String, Option<String>>) {
|
|
|
+ let mut old_attributes: HashMap<String, Option<String>> = HashMap::new();
|
|
|
+ let node = self.document.node_at_path(path).unwrap();
|
|
|
+ let node_data = self.document.arena.get(node).unwrap().get();
|
|
|
+
|
|
|
+ for key in attributes.keys() {
|
|
|
+ let old_attrs = node_data.attributes.borrow();
|
|
|
+ let old_value = match old_attrs.0.get(key.as_str()) {
|
|
|
+ Some(value) => value.clone(),
|
|
|
+ None => None,
|
|
|
+ };
|
|
|
+ old_attributes.insert(key.clone(), old_value);
|
|
|
+ }
|
|
|
+
|
|
|
+ self.push(DocumentOperation::Update(UpdateOperation {
|
|
|
+ path: path.clone(),
|
|
|
+ attributes: NodeAttributes(attributes),
|
|
|
+ old_attributes: NodeAttributes(old_attributes),
|
|
|
+ }))
|
|
|
+ }
|
|
|
+
|
|
|
pub fn delete_node(&mut self, path: &Position) {
|
|
|
self.delete_nodes(path, 1);
|
|
|
}
|