84 lines
2.6 KiB
Rust
Raw Normal View History

2022-08-18 17:49:20 +08:00
use crate::core::document::position::Position;
2022-08-22 16:46:24 +08:00
use crate::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeData};
2022-08-19 14:43:11 +08:00
use std::collections::HashMap;
2022-08-17 16:48:45 +08:00
pub struct Transaction {
pub operations: Vec<DocumentOperation>,
}
2022-08-18 16:19:50 +08:00
impl Transaction {
fn new(operations: Vec<DocumentOperation>) -> Transaction {
2022-08-18 17:49:20 +08:00
Transaction { operations }
2022-08-18 16:19:50 +08:00
}
}
pub struct TransactionBuilder<'a> {
document: &'a DocumentTree,
2022-08-17 16:48:45 +08:00
operations: Vec<DocumentOperation>,
}
2022-08-18 16:19:50 +08:00
impl<'a> TransactionBuilder<'a> {
pub fn new(document: &'a DocumentTree) -> TransactionBuilder {
TransactionBuilder {
document,
2022-08-18 17:49:20 +08:00
operations: Vec::new(),
2022-08-18 16:19:50 +08:00
}
2022-08-17 16:48:45 +08:00
}
pub fn insert_nodes_at_path(&mut self, path: &Position, nodes: &[NodeData]) {
2022-08-22 16:46:24 +08:00
self.push(DocumentOperation::Insert {
2022-08-18 17:49:20 +08:00
path: path.clone(),
nodes: nodes.to_vec(),
2022-08-22 16:46:24 +08:00
});
2022-08-18 17:49:20 +08:00
}
pub fn update_attributes_at_path(&mut self, path: &Position, attributes: HashMap<String, Option<String>>) {
2022-08-18 20:15:34 +08:00
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() {
2022-08-22 16:46:24 +08:00
let old_attrs = &node_data.attributes;
2022-08-18 20:15:34 +08:00
let old_value = match old_attrs.0.get(key.as_str()) {
Some(value) => value.clone(),
None => None,
};
old_attributes.insert(key.clone(), old_value);
}
2022-08-22 16:46:24 +08:00
self.push(DocumentOperation::Update {
2022-08-18 20:15:34 +08:00
path: path.clone(),
attributes: NodeAttributes(attributes),
old_attributes: NodeAttributes(old_attributes),
2022-08-22 16:46:24 +08:00
})
2022-08-18 20:15:34 +08:00
}
pub fn delete_node_at_path(&mut self, path: &Position) {
self.delete_nodes_at_path(path, 1);
2022-08-18 17:49:20 +08:00
}
pub fn delete_nodes_at_path(&mut self, path: &Position, length: usize) {
2022-08-18 17:49:20 +08:00
let mut node = self.document.node_at_path(path).unwrap();
let mut deleted_nodes: Vec<NodeData> = Vec::new();
for _ in 0..length {
let data = self.document.arena.get(node).unwrap();
deleted_nodes.push(data.get().clone());
node = node.following_siblings(&self.document.arena).next().unwrap();
}
2022-08-22 16:46:24 +08:00
self.operations.push(DocumentOperation::Delete {
2022-08-18 17:49:20 +08:00
path: path.clone(),
nodes: deleted_nodes,
2022-08-22 16:46:24 +08:00
})
2022-08-18 17:49:20 +08:00
}
2022-08-17 16:48:45 +08:00
pub fn push(&mut self, op: DocumentOperation) {
self.operations.push(op);
}
pub fn finalize(self) -> Transaction {
2022-08-18 17:49:20 +08:00
Transaction::new(self.operations)
2022-08-17 16:48:45 +08:00
}
}