refactor: add at_path suffix to transaction builder

This commit is contained in:
Vincent Chan 2022-08-22 14:58:44 +08:00
parent 1801a47b1d
commit ef185cd5d5
2 changed files with 54 additions and 27 deletions

View File

@ -27,14 +27,14 @@ impl<'a> TransactionBuilder<'a> {
}
}
pub fn insert_nodes(&mut self, path: &Position, nodes: &[NodeData]) {
pub fn insert_nodes_at_path(&mut self, path: &Position, nodes: &[NodeData]) {
self.push(DocumentOperation::Insert(InsertOperation {
path: path.clone(),
nodes: nodes.to_vec(),
}));
}
pub fn update_attributes(&mut self, path: &Position, attributes: HashMap<String, Option<String>>) {
pub fn update_attributes_at_path(&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();
@ -55,11 +55,11 @@ impl<'a> TransactionBuilder<'a> {
}))
}
pub fn delete_node(&mut self, path: &Position) {
self.delete_nodes(path, 1);
pub fn delete_node_at_path(&mut self, path: &Position) {
self.delete_nodes_at_path(path, 1);
}
pub fn delete_nodes(&mut self, path: &Position, length: usize) {
pub fn delete_nodes_at_path(&mut self, path: &Position, length: usize) {
let mut node = self.document.node_at_path(path).unwrap();
let mut deleted_nodes: Vec<NodeData> = Vec::new();

View File

@ -10,9 +10,11 @@ fn main() {
#[test]
fn test_documents() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.insert_nodes(&vec![0].into(), &vec![NodeData::new("text")]);
let transaction = tb.finalize();
tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
tb.finalize()
};
document.apply(transaction);
assert!(document.node_at_path(&vec![0].into()).is_some());
@ -20,17 +22,21 @@ fn test_documents() {
let node_data = document.arena.get(node).unwrap().get();
assert_eq!(node_data.node_type, "text");
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.update_attributes(
tb.update_attributes_at_path(
&vec![0].into(),
HashMap::from([("subtype".into(), Some("bullet-list".into()))]),
);
let transaction = tb.finalize();
tb.finalize()
};
document.apply(transaction);
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.delete_node(&vec![0].into());
let transaction = tb.finalize();
tb.delete_node_at_path(&vec![0].into());
tb.finalize()
};
document.apply(transaction);
assert!(document.node_at_path(&vec![0].into()).is_none());
}
@ -40,16 +46,16 @@ fn test_inserts_nodes() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.insert_nodes(&vec![0].into(), &vec![NodeData::new("text")]);
tb.insert_nodes(&vec![1].into(), &vec![NodeData::new("text")]);
tb.insert_nodes(&vec![2].into(), &vec![NodeData::new("text")]);
tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
tb.finalize()
};
document.apply(transaction);
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.insert_nodes(&vec![1].into(), &vec![NodeData::new("text")]);
tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
tb.finalize()
};
document.apply(transaction);
@ -60,18 +66,16 @@ fn test_update_nodes() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.insert_nodes(&vec![0].into(), &vec![NodeData::new("text")]);
tb.insert_nodes(&vec![1].into(), &vec![NodeData::new("text")]);
tb.insert_nodes(&vec![2].into(), &vec![NodeData::new("text")]);
tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
tb.finalize()
};
document.apply(transaction);
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.update_attributes(&vec![1].into(), HashMap::from([
("bolded".into(), Some("true".into())),
]));
tb.update_attributes_at_path(&vec![1].into(), HashMap::from([("bolded".into(), Some("true".into()))]));
tb.finalize()
};
document.apply(transaction);
@ -81,3 +85,26 @@ fn test_update_nodes() {
let is_bold = node_data.attributes.borrow().0.get("bolded").unwrap().clone();
assert_eq!(is_bold.unwrap(), "true");
}
#[test]
fn test_delete_nodes() {
let mut document = DocumentTree::new();
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.insert_nodes_at_path(&vec![0].into(), &vec![NodeData::new("text")]);
tb.insert_nodes_at_path(&vec![1].into(), &vec![NodeData::new("text")]);
tb.insert_nodes_at_path(&vec![2].into(), &vec![NodeData::new("text")]);
tb.finalize()
};
document.apply(transaction);
let transaction = {
let mut tb = TransactionBuilder::new(&document);
tb.delete_node_at_path(&Position(vec![1]));
tb.finalize()
};
document.apply(transaction);
let len = document.root.children(&document.arena).fold(0, |count, _| count + 1);
assert_eq!(len, 2);
}