A Delta contains a list of operations, which describe the changes to a document. There are three kinds of operations, insert, delete, and retain. The implementation of Delta is located in the lib-ot(shared-lib/lib-ot) crate.The format of Delta is JSON based, and is human readable, it can describe any rich text document, includes all text and formatting information.
The picture shown below is a UML diagram that describes the relations between these classes. We're going to explain them on by one.
Operation contains three types, Insert, Delete, and Retain.
Insert
Insert operations have an insert key defined. A String value represents inserting text. an optional attributes key can be defined with an Object to describe additional formatting information. Formats can be changed by the retain operation.
Delete
Delete operations have a Number delete key defined representing the number of characters to delete.
Retain
Retain operations have a Number retain key defined representing the number of characters to keep An optional attributes key can be defined with an Object to describe formatting changes to the character range. A value of null in the attributes Object represents removal of that key.
OTString
The length of strings behaves differently in different languages. For example: [Dart] string's length is calculated with UTF-16 code units. The method [utf16_len] returns the length of a String in UTF-16 code units.
let utf16_len =OTString::from("👋").utf16_len();assert_eq!(utf16_len, 2);let bytes_len =String::from("👋").len();assert_eq!(bytes_len, 4);
OTUtf16CodePointIterator
let s:OTString="👋😁👋".into(); ///letmut iter = s.utf16_code_point_iter();assert_eq!(iter.next().unwrap(), "👋".to_string());assert_eq!(iter.next().unwrap(), "😁".to_string());assert_eq!(iter.next().unwrap(), "👋".to_string());assert_eq!(iter.next(), None);letmut iter = s.utf16_code_point_iter();assert_eq!(iter.next().unwrap(), "👋".to_string());assert_eq!(iter.next().unwrap(), "1".to_string());assert_eq!(iter.next().unwrap(), "2".to_string());assert_eq!(iter.skip(OTString::from("ab一二").utf16_len()).next().unwrap(), "👋".to_string());
Attributes
Each operation can carry attributes. For example, the [RichTextAttributes] has a list of key/value attributes. Such as { bold: true, italic: true }.
Because [Operation] is generic over the T, so you must specify the T. For example, the [TextDelta] uses [PhantomAttributes] as the T. [PhantomAttributes] does nothing, just a phantom.
pubtraitOperationTransform {/// Merges the operation with `other` into one operation while preserving/// the changes of both. ////// # Arguments////// * `other`: The delta gonna to merge.////// # Examples////// ```/// use lib_ot::core::{OperationTransform, TextDeltaBuilder};/// let document = TextDeltaBuilder::new().build();/// let delta = TextDeltaBuilder::new().insert("abc").build();/// let new_document = document.compose(&delta).unwrap();/// assert_eq!(new_document.content().unwrap(), "abc".to_owned());/// ```fncompose(&self, other:&Self) ->Result<Self, OTError>where Self:Sized;/// Transforms two operations a and b that happened concurrently and/// produces two operations a' and b'./// (a', b') = a.transform(b)/// a.compose(b') = b.compose(a') ///fntransform(&self, other:&Self) ->Result<(Self, Self), OTError>where Self:Sized;/// Returns the invert delta from the other. It can be used to do the undo operation.////// # Arguments////// * `other`: Generate the undo delta for [Other]. [Other] can compose the undo delta to return/// to the previous state.////// # Examples////// ```/// use lib_ot::core::{OperationTransform, TextDeltaBuilder};/// let original_document = TextDeltaBuilder::new().build();/// let delta = TextDeltaBuilder::new().insert("abc").build();////// let undo_delta = delta.invert(&original_document);/// let new_document = original_document.compose(&delta).unwrap();/// let document = new_document.compose(&undo_delta).unwrap();////// assert_eq!(original_document, document);////// ```fninvert(&self, other:&Self) -> Self;}
Serde
Import and Export
Markdown
We can use a Markdown parser to import the document into AppFlowy or export the data to a Markdown file. AppFlowy uses Delta to represent the document content.