builder.rs 3.8 KB

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