document.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. use crate::core::document::position::Position;
  2. use crate::core::{
  3. DeleteOperation, DocumentOperation, InsertOperation, NodeAttributes, NodeData, TextEditOperation, Transaction,
  4. UpdateOperation,
  5. };
  6. use indextree::{Arena, NodeId};
  7. pub struct DocumentTree {
  8. pub arena: Arena<NodeData>,
  9. pub root: NodeId,
  10. }
  11. impl DocumentTree {
  12. pub fn new() -> DocumentTree {
  13. let mut arena = Arena::new();
  14. let root = arena.new_node(NodeData::new("root".into()));
  15. DocumentTree { arena, root }
  16. }
  17. pub fn node_at_path(&self, position: &Position) -> Option<NodeId> {
  18. if position.is_empty() {
  19. return Some(self.root);
  20. }
  21. let mut iterate_node = self.root;
  22. for id in &position.0 {
  23. let child = self.child_at_index_of_path(iterate_node, id.clone());
  24. iterate_node = match child {
  25. Some(node) => node,
  26. None => return None,
  27. };
  28. }
  29. Some(iterate_node)
  30. }
  31. pub fn path_of_node(&self, node_id: NodeId) -> Position {
  32. let mut path: Vec<usize> = Vec::new();
  33. let mut ancestors = node_id.ancestors(&self.arena);
  34. let mut current_node = node_id;
  35. let mut parent = ancestors.next();
  36. while parent.is_some() {
  37. let parent_node = parent.unwrap();
  38. let counter = self.index_of_node(parent_node, current_node);
  39. path.push(counter);
  40. current_node = parent_node;
  41. parent = ancestors.next();
  42. }
  43. Position(path)
  44. }
  45. fn index_of_node(&self, parent_node: NodeId, child_node: NodeId) -> usize {
  46. let mut counter: usize = 0;
  47. let mut children_iterator = parent_node.children(&self.arena);
  48. let mut node = children_iterator.next();
  49. while node.is_some() {
  50. if node.unwrap() == child_node {
  51. return counter;
  52. }
  53. node = children_iterator.next();
  54. counter += 1;
  55. }
  56. counter
  57. }
  58. fn child_at_index_of_path(&self, at_node: NodeId, index: usize) -> Option<NodeId> {
  59. let children = at_node.children(&self.arena);
  60. let mut counter = 0;
  61. for child in children {
  62. if counter == index {
  63. return Some(child);
  64. }
  65. counter += 1;
  66. }
  67. None
  68. }
  69. pub fn apply(&mut self, transaction: Transaction) {
  70. for op in &transaction.operations {
  71. self.apply_op(op);
  72. }
  73. }
  74. fn apply_op(&mut self, op: &DocumentOperation) {
  75. match op {
  76. DocumentOperation::Insert(op) => self.apply_insert(op),
  77. DocumentOperation::Update(op) => self.apply_update(op),
  78. DocumentOperation::Delete(op) => self.apply_delete(op),
  79. DocumentOperation::TextEdit(op) => self.apply_text_edit(op),
  80. }
  81. }
  82. fn apply_insert(&mut self, op: &InsertOperation) {
  83. let parent_path = &op.path.0[0..(op.path.0.len() - 1)];
  84. let last_index = op.path.0[op.path.0.len() - 1];
  85. let parent_node = self.node_at_path(&Position(parent_path.to_vec()));
  86. if let Some(parent_node) = parent_node {
  87. let mut inserted_nodes = Vec::new();
  88. for node in &op.nodes {
  89. inserted_nodes.push(self.arena.new_node(node.clone()));
  90. }
  91. self.insert_child_at_index(parent_node, last_index, &inserted_nodes);
  92. }
  93. }
  94. fn insert_child_at_index(&mut self, parent: NodeId, index: usize, insert_children: &[NodeId]) {
  95. if index == 0 && parent.children(&self.arena).next().is_none() {
  96. for id in insert_children {
  97. parent.append(*id, &mut self.arena);
  98. }
  99. return;
  100. }
  101. let children_length = parent.children(&self.arena).fold(0, |counter, _| counter + 1);
  102. if index == children_length {
  103. for id in insert_children {
  104. parent.append(*id, &mut self.arena);
  105. }
  106. return;
  107. }
  108. let node_to_insert = self.child_at_index_of_path(parent, index).unwrap();
  109. for id in insert_children {
  110. node_to_insert.insert_before(*id, &mut self.arena);
  111. }
  112. }
  113. fn apply_update(&self, op: &UpdateOperation) {
  114. let update_node = self.node_at_path(&op.path).unwrap();
  115. let node_data = self.arena.get(update_node).unwrap();
  116. let new_attributes = {
  117. let old_attributes = node_data.get().attributes.borrow();
  118. NodeAttributes::compose(&old_attributes, &op.attributes)
  119. };
  120. node_data.get().attributes.replace(new_attributes);
  121. }
  122. fn apply_delete(&mut self, op: &DeleteOperation) {
  123. let update_node = self.node_at_path(&op.path).unwrap();
  124. update_node.remove_subtree(&mut self.arena);
  125. }
  126. fn apply_text_edit(&self, _op: &TextEditOperation) {
  127. unimplemented!()
  128. }
  129. }