builder.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. use crate::core::delta::{trim, Delta};
  2. use crate::core::operation::Attributes;
  3. use crate::core::Operation;
  4. /// A builder for creating new [Delta] objects.
  5. ///
  6. /// Note that all edit operations must be sorted; the start point of each
  7. /// interval must be no less than the end point of the previous one.
  8. ///
  9. /// # Examples
  10. ///
  11. /// ```
  12. /// use lib_ot::core::TextDeltaBuilder;
  13. /// let delta = TextDeltaBuilder::new()
  14. /// .insert("AppFlowy")
  15. /// .build();
  16. /// assert_eq!(delta.content().unwrap(), "AppFlowy");
  17. /// ```
  18. pub struct DeltaBuilder<T: Attributes> {
  19. delta: Delta<T>,
  20. }
  21. impl<T> std::default::Default for DeltaBuilder<T>
  22. where
  23. T: Attributes,
  24. {
  25. fn default() -> Self {
  26. Self { delta: Delta::new() }
  27. }
  28. }
  29. impl<T> DeltaBuilder<T>
  30. where
  31. T: Attributes,
  32. {
  33. pub fn new() -> Self {
  34. DeltaBuilder::default()
  35. }
  36. pub fn from_operations(operations: Vec<Operation<T>>) -> Delta<T> {
  37. let mut delta = DeltaBuilder::default().build();
  38. operations.into_iter().for_each(|operation| {
  39. delta.add(operation);
  40. });
  41. delta
  42. }
  43. /// Retain the 'n' characters with the attributes. Use 'retain' instead if you don't
  44. /// need any attributes.
  45. /// # Examples
  46. ///
  47. /// ```
  48. /// use lib_ot::rich_text::{TextAttribute, RichTextDelta, RichTextDeltaBuilder};
  49. ///
  50. /// let mut attribute = TextAttribute::Bold(true);
  51. /// let delta = RichTextDeltaBuilder::new().retain_with_attributes(7, attribute.into()).build();
  52. ///
  53. /// assert_eq!(delta.json_str(), r#"[{"retain":7,"attributes":{"bold":true}}]"#);
  54. /// ```
  55. pub fn retain_with_attributes(mut self, n: usize, attrs: T) -> Self {
  56. self.delta.retain(n, attrs);
  57. self
  58. }
  59. pub fn retain(mut self, n: usize) -> Self {
  60. self.delta.retain(n, T::default());
  61. self
  62. }
  63. /// Deletes the given interval. Panics if interval is not properly sorted.
  64. ///
  65. /// # Examples
  66. ///
  67. /// ```
  68. /// use lib_ot::core::{OperationTransform, TextDeltaBuilder};
  69. ///
  70. /// let delta = TextDeltaBuilder::new()
  71. /// .insert("AppFlowy...")
  72. /// .build();
  73. ///
  74. /// let changeset = TextDeltaBuilder::new()
  75. /// .retain(8)
  76. /// .delete(3)
  77. /// .build();
  78. ///
  79. /// let new_delta = delta.compose(&changeset).unwrap();
  80. /// assert_eq!(new_delta.content().unwrap(), "AppFlowy");
  81. /// ```
  82. pub fn delete(mut self, n: usize) -> Self {
  83. self.delta.delete(n);
  84. self
  85. }
  86. /// Inserts the string with attributes. Use 'insert' instead if you don't
  87. /// need any attributes.
  88. pub fn insert_with_attributes(mut self, s: &str, attrs: T) -> Self {
  89. self.delta.insert(s, attrs);
  90. self
  91. }
  92. pub fn insert(mut self, s: &str) -> Self {
  93. self.delta.insert(s, T::default());
  94. self
  95. }
  96. /// Removes trailing retain operation with empty attributes
  97. ///
  98. /// # Examples
  99. ///
  100. /// ```
  101. /// use lib_ot::core::{OperationTransform, TextDeltaBuilder};
  102. /// use lib_ot::rich_text::{TextAttribute, RichTextDeltaBuilder};
  103. /// let delta = TextDeltaBuilder::new()
  104. /// .retain(3)
  105. /// .trim()
  106. /// .build();
  107. /// assert_eq!(delta.ops.len(), 0);
  108. ///
  109. /// let delta = RichTextDeltaBuilder::new()
  110. /// .retain_with_attributes(3, TextAttribute::Bold(true).into())
  111. /// .trim()
  112. /// .build();
  113. /// assert_eq!(delta.ops.len(), 1);
  114. /// ```
  115. pub fn trim(mut self) -> Self {
  116. trim(&mut self.delta);
  117. self
  118. }
  119. /// Builds the `Delta`
  120. pub fn build(self) -> Delta<T> {
  121. self.delta
  122. }
  123. }