123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- use crate::core::delta::operation::OperationAttributes;
- use crate::core::delta::{trim, DeltaOperations};
- use crate::core::DeltaOperation;
- /// A builder for creating new [Operations] objects.
- ///
- /// 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::DeltaBuilder;
- /// let delta = DeltaBuilder::new()
- /// .insert("AppFlowy")
- /// .build();
- /// assert_eq!(delta.content().unwrap(), "AppFlowy");
- /// ```
- pub struct OperationBuilder<T: OperationAttributes> {
- delta: DeltaOperations<T>,
- }
- impl<T> std::default::Default for OperationBuilder<T>
- where
- T: OperationAttributes,
- {
- fn default() -> Self {
- Self {
- delta: DeltaOperations::new(),
- }
- }
- }
- impl<T> OperationBuilder<T>
- where
- T: OperationAttributes,
- {
- pub fn new() -> Self {
- OperationBuilder::default()
- }
- pub fn from_operations(operations: Vec<DeltaOperation<T>>) -> DeltaOperations<T> {
- let mut delta = OperationBuilder::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::text_delta::{BuildInTextAttribute, TextOperations, TextOperationBuilder};
- ///
- /// let mut attribute = BuildInTextAttribute::Bold(true);
- /// let delta = TextOperationBuilder::new().retain_with_attributes(7, attribute.into()).build();
- ///
- /// assert_eq!(delta.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
- }
- pub fn retain(mut self, n: usize) -> Self {
- self.delta.retain(n, T::default());
- self
- }
- /// Deletes the given interval. Panics if interval is not properly sorted.
- ///
- /// # Examples
- ///
- /// ```
- /// use lib_ot::core::{OperationTransform, DeltaBuilder};
- ///
- /// let delta = DeltaBuilder::new()
- /// .insert("AppFlowy...")
- /// .build();
- ///
- /// let changeset = DeltaBuilder::new()
- /// .retain(8)
- /// .delete(3)
- /// .build();
- ///
- /// let new_delta = delta.compose(&changeset).unwrap();
- /// assert_eq!(new_delta.content().unwrap(), "AppFlowy");
- /// ```
- pub fn delete(mut self, n: usize) -> Self {
- self.delta.delete(n);
- self
- }
- /// Inserts the string with attributes. Use 'insert' instead if you don't
- /// need any attributes.
- pub fn insert_with_attributes(mut self, s: &str, attrs: T) -> Self {
- self.delta.insert(s, attrs);
- self
- }
- pub fn insert(mut self, s: &str) -> Self {
- self.delta.insert(s, T::default());
- self
- }
- /// Removes trailing retain operation with empty attributes
- ///
- /// # Examples
- ///
- /// ```
- /// use lib_ot::core::{OperationTransform, DeltaBuilder};
- /// use lib_ot::text_delta::{BuildInTextAttribute, TextOperationBuilder};
- /// let delta = DeltaBuilder::new()
- /// .retain(3)
- /// .trim()
- /// .build();
- /// assert_eq!(delta.ops.len(), 0);
- ///
- /// let delta = TextOperationBuilder::new()
- /// .retain_with_attributes(3, BuildInTextAttribute::Bold(true).into())
- /// .trim()
- /// .build();
- /// assert_eq!(delta.ops.len(), 1);
- /// ```
- pub fn trim(mut self) -> Self {
- trim(&mut self.delta);
- self
- }
- /// Builds the `Delta`
- pub fn build(self) -> DeltaOperations<T> {
- self.delta
- }
- }
|