152 lines
4.5 KiB
Rust
Raw Normal View History

use crate::core::document::position::Path;
2022-08-23 11:15:11 +08:00
use crate::core::{DocumentOperation, DocumentTree, NodeAttributes, NodeSubTree};
use indextree::NodeId;
2022-08-23 19:39:00 +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
}
2022-09-08 17:38:11 +08:00
///
///
/// # Arguments
///
/// * `path`: the path that is used to save the nodes
/// * `nodes`: the nodes you will be save in the path
///
/// # Examples
///
/// ```
2022-09-08 19:27:15 +08:00
/// // -- 0 (root)
/// // 0 -- text_1
/// // 1 -- text_2
2022-09-08 17:38:11 +08:00
/// use lib_ot::core::{DocumentTree, NodeSubTree, TransactionBuilder};
/// let mut document = DocumentTree::new();
/// let transaction = {
/// let mut tb = TransactionBuilder::new(&document);
/// tb.insert_nodes_at_path(0,vec![ NodeSubTree::new("text_1"), NodeSubTree::new("text_2")]);
/// tb.finalize()
/// };
/// document.apply(transaction).unwrap();
///
/// document.node_at_path(vec![0, 0]);
/// ```
///
pub fn insert_nodes_at_path<T: Into<Path>>(&mut self, path: T, nodes: Vec<NodeSubTree>) {
2022-08-22 16:46:24 +08:00
self.push(DocumentOperation::Insert {
2022-09-08 17:38:11 +08:00
path: path.into(),
nodes,
2022-08-22 16:46:24 +08:00
});
2022-08-18 17:49:20 +08:00
}
2022-09-08 17:38:11 +08:00
///
///
/// # Arguments
///
/// * `path`: the path that is used to save the nodes
/// * `node`: the node data will be saved in the path
///
/// # Examples
///
/// ```
/// // 0
/// // -- 0
/// // |-- text
/// use lib_ot::core::{DocumentTree, NodeSubTree, TransactionBuilder};
/// let mut document = DocumentTree::new();
/// let transaction = {
/// let mut tb = TransactionBuilder::new(&document);
/// tb.insert_node_at_path(0, NodeSubTree::new("text"));
/// tb.finalize()
/// };
/// document.apply(transaction).unwrap();
/// ```
///
pub fn insert_node_at_path<T: Into<Path>>(&mut self, path: T, node: NodeSubTree) {
self.insert_nodes_at_path(path, vec![node]);
}
pub fn update_attributes_at_path(&mut self, path: &Path, 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();
2022-09-08 19:27:15 +08:00
let node_data = self.document.get_node_data(node).unwrap();
2022-08-18 20:15:34 +08:00
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: &Path) {
self.delete_nodes_at_path(path, 1);
2022-08-18 17:49:20 +08:00
}
pub fn delete_nodes_at_path(&mut self, path: &Path, 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![];
2022-08-18 17:49:20 +08:00
for _ in 0..length {
deleted_nodes.push(self.get_deleted_nodes(node));
2022-09-08 19:27:15 +08:00
node = self.document.following_siblings(node).next().unwrap();
2022-08-18 17:49:20 +08:00
}
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
}
fn get_deleted_nodes(&self, node_id: NodeId) -> NodeSubTree {
2022-09-08 19:27:15 +08:00
let node_data = self.document.get_node_data(node_id).unwrap();
let mut children = vec![];
2022-09-08 19:27:15 +08:00
self.document.children_from_node(node_id).for_each(|child_id| {
children.push(self.get_deleted_nodes(child_id));
});
NodeSubTree {
node_type: node_data.node_type.clone(),
attributes: node_data.attributes.clone(),
delta: node_data.delta.clone(),
children,
}
}
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
}
}