builder.rs 3.8 KB

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