Skip to content

Commit 8401fa0

Browse files
committed
feat: update attributes
1 parent c207bf3 commit 8401fa0

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

shared-lib/lib-ot/src/core/document/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22

33
#[derive(Clone)]
4-
pub struct NodeAttributes(HashMap<String, Option<String>>);
4+
pub struct NodeAttributes(pub HashMap<String, Option<String>>);
55

66
impl NodeAttributes {
77
pub fn new() -> NodeAttributes {

shared-lib/lib-ot/src/core/document/transaction.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use std::collections::HashMap;
12
use crate::core::document::position::Position;
2-
use crate::core::{DeleteOperation, DocumentOperation, DocumentTree, InsertOperation, NodeData};
3+
use crate::core::{DeleteOperation, DocumentOperation, DocumentTree, InsertOperation, NodeAttributes, NodeData, UpdateOperation};
34

45
pub struct Transaction {
56
pub operations: Vec<DocumentOperation>,
@@ -31,6 +32,27 @@ impl<'a> TransactionBuilder<'a> {
3132
}));
3233
}
3334

35+
pub fn update_attributes(&mut self, path: &Position, attributes: HashMap<String, Option<String>>) {
36+
let mut old_attributes: HashMap<String, Option<String>> = HashMap::new();
37+
let node = self.document.node_at_path(path).unwrap();
38+
let node_data = self.document.arena.get(node).unwrap().get();
39+
40+
for key in attributes.keys() {
41+
let old_attrs = node_data.attributes.borrow();
42+
let old_value = match old_attrs.0.get(key.as_str()) {
43+
Some(value) => value.clone(),
44+
None => None,
45+
};
46+
old_attributes.insert(key.clone(), old_value);
47+
}
48+
49+
self.push(DocumentOperation::Update(UpdateOperation {
50+
path: path.clone(),
51+
attributes: NodeAttributes(attributes),
52+
old_attributes: NodeAttributes(old_attributes),
53+
}))
54+
}
55+
3456
pub fn delete_node(&mut self, path: &Position) {
3557
self.delete_nodes(path, 1);
3658
}

shared-lib/lib-ot/tests/main.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use lib_ot::core::{DocumentTree, NodeData, Position, TransactionBuilder};
23

34
#[test]
@@ -10,7 +11,19 @@ fn main() {
1011
fn test_documents() {
1112
let mut document = DocumentTree::new();
1213
let mut tb = TransactionBuilder::new(&document);
13-
tb.insert_nodes(&Position(vec![0]), &vec![NodeData::new("type")]);
14+
tb.insert_nodes(&Position(vec![0]), &vec![NodeData::new("text")]);
15+
let transaction = tb.finalize();
16+
document.apply(transaction);
17+
18+
assert!(document.node_at_path(&Position(vec![0])).is_some());
19+
let node = document.node_at_path(&Position(vec![0])).unwrap();
20+
let node_data = document.arena.get(node).unwrap().get();
21+
assert_eq!(node_data.node_type, "text");
22+
23+
let mut tb = TransactionBuilder::new(&document);
24+
tb.update_attributes(&Position(vec![0]), HashMap::from([
25+
("subtype".into(), Some("bullet-list".into())),
26+
]));
1427
let transaction = tb.finalize();
1528
document.apply(transaction);
1629
}

0 commit comments

Comments
 (0)