|
@@ -1,5 +1,6 @@
|
|
|
use crate::core::delta::{trim, Delta};
|
|
|
use crate::core::operation::{Attributes, PhantomAttributes};
|
|
|
+use crate::core::Operation;
|
|
|
|
|
|
pub type PlainTextDeltaBuilder = DeltaBuilder<PhantomAttributes>;
|
|
|
|
|
@@ -7,6 +8,16 @@ pub type PlainTextDeltaBuilder = DeltaBuilder<PhantomAttributes>;
|
|
|
///
|
|
|
/// Note that all edit operations must be sorted; the start point of each
|
|
|
/// interval must be no less than the end point of the previous one.
|
|
|
+///
|
|
|
+/// # Examples
|
|
|
+///
|
|
|
+/// ```
|
|
|
+/// use lib_ot::core::PlainTextDeltaBuilder;
|
|
|
+/// let delta = PlainTextDeltaBuilder::new()
|
|
|
+/// .insert("AppFlowy")
|
|
|
+/// .build();
|
|
|
+/// assert_eq!(delta.content_str().unwrap(), "AppFlowy");
|
|
|
+/// ```
|
|
|
pub struct DeltaBuilder<T: Attributes> {
|
|
|
delta: Delta<T>,
|
|
|
}
|
|
@@ -28,8 +39,26 @@ where
|
|
|
DeltaBuilder::default()
|
|
|
}
|
|
|
|
|
|
+ pub fn from_operations(operations: Vec<Operation<T>>) -> Delta<T> {
|
|
|
+ let mut delta = DeltaBuilder::default().build();
|
|
|
+ operations.into_iter().for_each(|operation| {
|
|
|
+ delta.add(operation);
|
|
|
+ });
|
|
|
+ delta
|
|
|
+ }
|
|
|
+
|
|
|
/// Retain the 'n' characters with the attributes. Use 'retain' instead if you don't
|
|
|
/// need any attributes.
|
|
|
+ /// # Examples
|
|
|
+ ///
|
|
|
+ /// ```
|
|
|
+ /// use lib_ot::rich_text::{RichTextAttribute, RichTextDelta, RichTextDeltaBuilder};
|
|
|
+ ///
|
|
|
+ /// let mut attribute = RichTextAttribute::Bold(true);
|
|
|
+ /// let delta = RichTextDeltaBuilder::new().retain_with_attributes(7, attribute.into()).build();
|
|
|
+ ///
|
|
|
+ /// assert_eq!(delta.to_json_str(), r#"[{"retain":7,"attributes":{"bold":true}}]"#);
|
|
|
+ /// ```
|
|
|
pub fn retain_with_attributes(mut self, n: usize, attrs: T) -> Self {
|
|
|
self.delta.retain(n, attrs);
|
|
|
self
|
|
@@ -41,6 +70,24 @@ where
|
|
|
}
|
|
|
|
|
|
/// Deletes the given interval. Panics if interval is not properly sorted.
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ ///
|
|
|
+ /// ```
|
|
|
+ /// use lib_ot::core::{OperationTransform, PlainTextDeltaBuilder};
|
|
|
+ ///
|
|
|
+ /// let delta = PlainTextDeltaBuilder::new()
|
|
|
+ /// .insert("AppFlowy...")
|
|
|
+ /// .build();
|
|
|
+ ///
|
|
|
+ /// let changeset = PlainTextDeltaBuilder::new()
|
|
|
+ /// .retain(8)
|
|
|
+ /// .delete(3)
|
|
|
+ /// .build();
|
|
|
+ ///
|
|
|
+ /// let new_delta = delta.compose(&changeset).unwrap();
|
|
|
+ /// assert_eq!(new_delta.content_str().unwrap(), "AppFlowy");
|
|
|
+ /// ```
|
|
|
pub fn delete(mut self, n: usize) -> Self {
|
|
|
self.delta.delete(n);
|
|
|
self
|
|
@@ -58,6 +105,25 @@ where
|
|
|
self
|
|
|
}
|
|
|
|
|
|
+ /// Removes trailing retain operation with empty attributes
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ ///
|
|
|
+ /// ```
|
|
|
+ /// use lib_ot::core::{OperationTransform, PlainTextDeltaBuilder};
|
|
|
+ /// use lib_ot::rich_text::{RichTextAttribute, RichTextDeltaBuilder};
|
|
|
+ /// let delta = PlainTextDeltaBuilder::new()
|
|
|
+ /// .retain(3)
|
|
|
+ /// .trim()
|
|
|
+ /// .build();
|
|
|
+ /// assert_eq!(delta.ops.len(), 0);
|
|
|
+ ///
|
|
|
+ /// let delta = RichTextDeltaBuilder::new()
|
|
|
+ /// .retain_with_attributes(3, RichTextAttribute::Bold(true).into())
|
|
|
+ /// .trim()
|
|
|
+ /// .build();
|
|
|
+ /// assert_eq!(delta.ops.len(), 1);
|
|
|
+ /// ```
|
|
|
pub fn trim(mut self) -> Self {
|
|
|
trim(&mut self.delta);
|
|
|
self
|