document.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. use crate::{
  2. client::{History, UndoResult},
  3. core::{
  4. Attribute,
  5. Attributes,
  6. AttributesDataRule,
  7. AttrsBuilder,
  8. Delta,
  9. Interval,
  10. OpBuilder,
  11. Operation,
  12. },
  13. };
  14. pub struct Document {
  15. data: Delta,
  16. history: History,
  17. }
  18. impl Document {
  19. pub fn new() -> Self {
  20. Document {
  21. data: Delta::new(),
  22. history: History::new(),
  23. }
  24. }
  25. pub fn edit(&mut self, index: usize, text: &str) {
  26. if self.data.target_len < index {
  27. log::error!(
  28. "{} out of bounds. should 0..{}",
  29. index,
  30. self.data.target_len
  31. );
  32. }
  33. let probe = Interval::new(index, index + 1);
  34. let mut attributes = self.data.get_attributes(probe);
  35. if attributes == Attributes::Empty {
  36. attributes = Attributes::Follow;
  37. }
  38. let insert = OpBuilder::insert(text).attributes(attributes).build();
  39. let interval = Interval::new(index, index);
  40. self.update_with_op(insert, interval);
  41. }
  42. pub fn format(&mut self, interval: Interval, attribute: Attribute, enable: bool) {
  43. let attributes = match enable {
  44. true => AttrsBuilder::new().add_attribute(attribute).build(),
  45. false => AttrsBuilder::new().remove_attribute(attribute).build(),
  46. };
  47. self.update_with_attribute(attributes, interval);
  48. }
  49. pub fn undo(&mut self) -> UndoResult { unimplemented!() }
  50. pub fn redo(&mut self) -> UndoResult { unimplemented!() }
  51. pub fn delete(&mut self, interval: Interval) {
  52. let delete = OpBuilder::delete(interval.size() as u64).build();
  53. self.update_with_op(delete, interval);
  54. }
  55. pub fn to_json(&self) -> String { self.data.to_json() }
  56. pub fn data(&self) -> &Delta { &self.data }
  57. pub fn set_data(&mut self, data: Delta) { self.data = data; }
  58. fn update_with_op(&mut self, op: Operation, interval: Interval) {
  59. let mut new_delta = Delta::default();
  60. let (prefix, interval, suffix) = split_length_with_interval(self.data.target_len, interval);
  61. // prefix
  62. if prefix.is_empty() == false && prefix != interval {
  63. let intervals = split_interval_with_delta(&self.data, &prefix);
  64. intervals.into_iter().for_each(|i| {
  65. let attributes = self.data.get_attributes(i);
  66. log::debug!("prefix attribute: {:?}, interval: {:?}", attributes, i);
  67. new_delta.retain(i.size() as u64, attributes);
  68. });
  69. }
  70. log::debug!("add new op: {:?}", op);
  71. new_delta.add(op);
  72. // suffix
  73. if suffix.is_empty() == false {
  74. let intervals = split_interval_with_delta(&self.data, &suffix);
  75. intervals.into_iter().for_each(|i| {
  76. let attributes = self.data.get_attributes(i);
  77. log::debug!("suffix attribute: {:?}, interval: {:?}", attributes, i);
  78. new_delta.retain(i.size() as u64, attributes);
  79. });
  80. }
  81. let new_data = self.data.compose(&new_delta).unwrap();
  82. self.data = new_data;
  83. }
  84. pub fn update_with_attribute(&mut self, mut attributes: Attributes, interval: Interval) {
  85. let old_attributes = self.data.get_attributes(interval);
  86. log::debug!(
  87. "merge attributes: {:?}, with old: {:?}",
  88. attributes,
  89. old_attributes
  90. );
  91. let new_attributes = match &mut attributes {
  92. Attributes::Follow => old_attributes,
  93. Attributes::Custom(attr_data) => {
  94. attr_data.merge(old_attributes.data());
  95. attr_data.clone().into_attributes()
  96. },
  97. Attributes::Empty => Attributes::Empty,
  98. };
  99. log::debug!("new attributes: {:?}", new_attributes);
  100. let retain = OpBuilder::retain(interval.size() as u64)
  101. .attributes(new_attributes)
  102. .build();
  103. log::debug!(
  104. "Update delta with new attributes: {:?} at: {:?}",
  105. retain,
  106. interval
  107. );
  108. self.update_with_op(retain, interval);
  109. }
  110. }
  111. pub fn transform(left: &Document, right: &Document) -> (Document, Document) {
  112. let (a_prime, b_prime) = left.data.transform(&right.data).unwrap();
  113. log::trace!("a:{:?},b:{:?}", a_prime, b_prime);
  114. let data_left = left.data.compose(&b_prime).unwrap();
  115. let data_right = right.data.compose(&a_prime).unwrap();
  116. (
  117. Document {
  118. data: data_left,
  119. history: left.history.clone(),
  120. },
  121. Document {
  122. data: data_right,
  123. history: right.history.clone(),
  124. },
  125. )
  126. }
  127. fn split_length_with_interval(length: usize, interval: Interval) -> (Interval, Interval, Interval) {
  128. let original_interval = Interval::new(0, length);
  129. let prefix = original_interval.prefix(interval);
  130. let suffix = original_interval.suffix(interval);
  131. (prefix, interval, suffix)
  132. }
  133. fn split_interval_with_delta(delta: &Delta, interval: &Interval) -> Vec<Interval> {
  134. let mut start = 0;
  135. let mut new_intervals = vec![];
  136. delta.ops.iter().for_each(|op| match op {
  137. Operation::Delete(_) => {},
  138. Operation::Retain(_) => {},
  139. Operation::Insert(insert) => {
  140. let len = insert.num_chars() as usize;
  141. let end = start + len;
  142. let insert_interval = Interval::new(start, end);
  143. let new_interval = interval.intersect(insert_interval);
  144. if !new_interval.is_empty() {
  145. new_intervals.push(new_interval)
  146. }
  147. start += len;
  148. },
  149. });
  150. new_intervals
  151. }