insert_ext.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. use crate::{
  2. client::view::InsertExt,
  3. core::{AttributeKey, Attributes, CharMetric, Delta, DeltaBuilder, DeltaIter, Operation},
  4. };
  5. pub const NEW_LINE: &'static str = "\n";
  6. pub struct PreserveBlockStyleOnInsertExt {}
  7. impl InsertExt for PreserveBlockStyleOnInsertExt {
  8. fn ext_name(&self) -> &str { "PreserveBlockStyleOnInsertExt" }
  9. fn apply(
  10. &self,
  11. _delta: &Delta,
  12. _replace_len: usize,
  13. _text: &str,
  14. _index: usize,
  15. ) -> Option<Delta> {
  16. None
  17. }
  18. }
  19. pub struct PreserveLineStyleOnSplitExt {}
  20. impl InsertExt for PreserveLineStyleOnSplitExt {
  21. fn ext_name(&self) -> &str { "PreserveLineStyleOnSplitExt" }
  22. fn apply(
  23. &self,
  24. _delta: &Delta,
  25. _replace_len: usize,
  26. _text: &str,
  27. _index: usize,
  28. ) -> Option<Delta> {
  29. None
  30. }
  31. }
  32. pub struct AutoExitBlockExt {}
  33. impl InsertExt for AutoExitBlockExt {
  34. fn ext_name(&self) -> &str { "AutoExitBlockExt" }
  35. fn apply(
  36. &self,
  37. _delta: &Delta,
  38. _replace_len: usize,
  39. _text: &str,
  40. _index: usize,
  41. ) -> Option<Delta> {
  42. None
  43. }
  44. }
  45. pub struct InsertEmbedsExt {}
  46. impl InsertExt for InsertEmbedsExt {
  47. fn ext_name(&self) -> &str { "InsertEmbedsExt" }
  48. fn apply(
  49. &self,
  50. _delta: &Delta,
  51. _replace_len: usize,
  52. _text: &str,
  53. _index: usize,
  54. ) -> Option<Delta> {
  55. None
  56. }
  57. }
  58. pub struct ForceNewlineForInsertsAroundEmbedExt {}
  59. impl InsertExt for ForceNewlineForInsertsAroundEmbedExt {
  60. fn ext_name(&self) -> &str { "ForceNewlineForInsertsAroundEmbedExt" }
  61. fn apply(
  62. &self,
  63. _delta: &Delta,
  64. _replace_len: usize,
  65. _text: &str,
  66. _index: usize,
  67. ) -> Option<Delta> {
  68. None
  69. }
  70. }
  71. pub struct AutoFormatLinksExt {}
  72. impl InsertExt for AutoFormatLinksExt {
  73. fn ext_name(&self) -> &str { "AutoFormatLinksExt" }
  74. fn apply(
  75. &self,
  76. _delta: &Delta,
  77. _replace_len: usize,
  78. _text: &str,
  79. _index: usize,
  80. ) -> Option<Delta> {
  81. None
  82. }
  83. }
  84. pub struct PreserveInlineStylesExt {}
  85. impl InsertExt for PreserveInlineStylesExt {
  86. fn ext_name(&self) -> &str { "PreserveInlineStylesExt" }
  87. fn apply(&self, delta: &Delta, replace_len: usize, text: &str, index: usize) -> Option<Delta> {
  88. if text.contains(NEW_LINE) {
  89. return None;
  90. }
  91. let mut iter = DeltaIter::new(delta);
  92. let prev = iter.next_op_before(index)?;
  93. if prev.get_data().contains(NEW_LINE) {
  94. return None;
  95. }
  96. let mut attributes = prev.get_attributes();
  97. if attributes.is_empty() || !attributes.contains_key(&AttributeKey::Link) {
  98. return Some(
  99. DeltaBuilder::new()
  100. .retain(index + replace_len)
  101. .insert_with_attributes(text, attributes)
  102. .build(),
  103. );
  104. }
  105. attributes.remove(&AttributeKey::Link);
  106. let new_delta = DeltaBuilder::new()
  107. .retain(index + replace_len)
  108. .insert_with_attributes(text, attributes)
  109. .build();
  110. return Some(new_delta);
  111. }
  112. }
  113. pub struct ResetLineFormatOnNewLineExt {}
  114. impl InsertExt for ResetLineFormatOnNewLineExt {
  115. fn ext_name(&self) -> &str { "ResetLineFormatOnNewLineExt" }
  116. fn apply(&self, delta: &Delta, replace_len: usize, text: &str, index: usize) -> Option<Delta> {
  117. if text != NEW_LINE {
  118. return None;
  119. }
  120. let mut iter = DeltaIter::new(delta);
  121. iter.seek::<CharMetric>(index);
  122. let next_op = iter.next()?;
  123. if !next_op.get_data().starts_with(NEW_LINE) {
  124. return None;
  125. }
  126. let mut reset_attribute = Attributes::new();
  127. if next_op.get_attributes().contains_key(&AttributeKey::Header) {
  128. reset_attribute.add(AttributeKey::Header.value(""));
  129. }
  130. let len = index + replace_len;
  131. Some(
  132. DeltaBuilder::new()
  133. .retain(len)
  134. .insert_with_attributes(NEW_LINE, next_op.get_attributes())
  135. .retain_with_attributes(1, reset_attribute)
  136. .trim()
  137. .build(),
  138. )
  139. }
  140. }
  141. pub struct DefaultInsertExt {}
  142. impl InsertExt for DefaultInsertExt {
  143. fn ext_name(&self) -> &str { "DefaultInsertExt" }
  144. fn apply(&self, delta: &Delta, replace_len: usize, text: &str, index: usize) -> Option<Delta> {
  145. let mut iter = DeltaIter::new(delta);
  146. let mut attributes = Attributes::new();
  147. if text.ends_with(NEW_LINE) {
  148. match iter.last() {
  149. None => {},
  150. Some(op) => {
  151. if op.get_attributes().contains_key(&AttributeKey::Header) {
  152. attributes.extend(op.get_attributes());
  153. }
  154. },
  155. }
  156. }
  157. Some(
  158. DeltaBuilder::new()
  159. .retain(index + replace_len)
  160. .insert_with_attributes(text, attributes)
  161. .build(),
  162. )
  163. }
  164. }