This is a C# port of the Quill Delta. It is a library for creating and applying deltas to rich text. It is useful for creating collaborative editors like Google Docs or Microsoft Word Online.
// Document with text "Gandalf the Grey"
// with "Gandalf" bolded, and "Grey" in grey
var delta = new Delta()
.Insert("Gandalf", new AttributeMap {{"bold", true}})
.Insert(" the ")
.Insert("Grey", new AttributeMap {{"color", "#ccc"}});
// Change intended to be applied to above:
// Keep the first 12 characters, insert a white 'White'
// and delete the next four characters ('Grey')
var death = new Delta()
.Retain(12)
.Insert("White", new AttributeMap {{ color: '#fff' }})
.Delete(4);
// {
// "ops": [
// { "retain": 12 },
// { "insert": "White", "attributes": { "color": "#fff" } },
// { "delete": 4 }
// ]
// }
// Applying the above:
var restored = delta.Compose(death);
// {
// "ops": [
// { "insert": "Gandalf", "attributes": { "bold": true } },
// { "insert": " the " },
// { "insert": "White", "attributes": { "color": "#fff" } }
// ]
// }
The following is a brief description of the supported operations on Delta objects.
Deltas can be used to represent both content (aka Documents) as well as individual changes. A Document is defined as a list of Insert operations. The following methods should be applied only to Documents. These methods called on or with non-document Deltas will result in undefined behavior.
These are the core methods for OT (Operational Transform). These are useful for implementating collaborative software, as individual Deltas could be applied to a Document being edited, and that individual Delta could be the only piece of information being shared among collaborating parties.
Insert operations have an insert
key defined. A String value represents inserting text. Any other type represents inserting an embed (however only one level of object comparison will be performed for equality).
In both cases of text and embeds, an optional attributes
key can be defined with an Object to describe additonal formatting information. Formats can be changed by the retain operation.
// Insert a bolded "Text"
{ insert: "Text", attributes: { bold: true } }
// Insert a link
{ insert: "Google", attributes: { link: 'https://www.google.com' } }
// Insert an embed
{
insert: { image: 'https://octodex.github.com/images/labtocat.png' },
attributes: { alt: "Lab Octocat" }
}
// Insert another embed
{
insert: { video: 'https://www.youtube.com/watch?v=dMH0bHeiRNg' },
attributes: {
width: 420,
height: 315
}
}
Delete operations have a Number delete
key defined representing the number of characters to delete. All embeds have a
length of 1.
// Delete the next 10 characters
{ delete: 10 }
Retain operations have a Number retain
key defined representing the number of characters to keep (other libraries
might use the name keep or skip). 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.
Note: It is not necessary to retain the last characters of a document as this is implied.
// Keep the next 5 characters
{ retain: 5 }
// Keep and bold the next 5 characters
{ retain: 5, attributes: { bold: true } }
// Keep and unbold the next 5 characters
// More specifically, remove the bold key in the attributes Object
// in the next 5 characters
{ retain: 5, attributes: { bold: null } }
Creates a new Delta object.
new Delta()
new Delta(ops)
new Delta(delta)
Delta.FromJson(deltaAsJson)
ops
- List of operationsdelta
- Another Delta objectdeltaAsJson
- JSON representation of a Delta object
Note: No validity/sanity check is performed when constructed with ops or delta. The new delta's internal ops array will also be assigned from ops or delta.ops without deep copying.
var delta = Delta.fromJson(@"[
{ ""insert"": ""Hello World"" },
{ ""insert"": ""!"", ""attributes"": { ""bold"": true }}
]");
var json = delta.toJson();
var chained = new Delta().Insert("Hello World").Insert("!", new AttributeMap {{ "bold", true }});
Appends an insert operation. Returns this
for chainability.
Insert(string text, AttributeMap? attributes = null)
Insert(object embed, AttributeMap? attributes = null)
text
- String representing text to insertembed
- Object representing embed type to insertattributes
- Optional attributes to apply
delta.Insert("Text", new AttributeMap {{ "bold", true }, { "color", "#ccc" }});
delta.Insert(new AttributeMap {{ "image", "https://octodex.github.com/images/labtocat.png" }});
Appends a delete operation. Returns this
for chainability.
Delete(int length)
length
- Number of characters to delete
delta.Delete(5);
Appends a retain operation. Returns this
for chainability.
Retain(int length, AttributeMap? attributes = null)
Retain(object retainObject, AttributeMap? attributes = null)
length
- Number of characters to retainattributes
- Optional attributes to applyretainObject
- Embed type to retain, to which some attributes are applied
delta.Retain(4).Retain(5, new AttributeMap {{ "color", "#0c6" }});
Returns a new Delta representing the concatenation of this and another document Delta's operations.
Concat(Delta other)
other
- Document Delta to concatenate
Delta
- Concatenated document Delta
Delta a = new Delta().Insert("Hello");
Delta b = new Delta().Insert("!", new AttributeMap {{ "bold", true }});
// {
// ops: [
// { "insert": "Hello" },
// { "insert": "!", "attributes": { "bold": true } }
// ]
// }
Delta concat = a.Concat(b);
Not implemented yet.
Iterates through document Delta, calling a given function with a Delta and attributes object, representing the line segment.
ForEach(Action<Op, int> action)
predicate
- function to call on each line groupnewline
- newline character, defaults to\n
var delta = new Delta().Insert("Hello\n\n")
.Insert("World")
.Insert(new AttributeMap {{ "image": "octocat.png" }})
.Insert("\n", new AttributeMap {{ "align": "right" })
.Insert("!");
delta.ForEach((op, i) => {
Console.WriteLine(op.ToString() + ", " + i);
// Can return false to exit loop early
});
Not implemented yet.
Returns an array of operations that passes a given function.
Filter(Func<Op, int, bool> predicate)
predicate
- Function to test each operation against. Returntrue
to keep the operation,false
otherwise.
List<Op>
- Filtered resulting List of operations
var delta = new Delta().Insert("Hello", new AttributeMap {{ "bold", true }})
.Insert(new AttributeMap {{ "image", "https://octodex.github.com/images/labtocat.png" }})
.Insert("World!");
string textOnly = string.Join("", delta
.Filter((op, i) => op.insert is string)
.Map((op) => op.insert)));
Returns length of a Delta, which is the sum of the lengths of its operations.
Length()
new Delta().Insert("Hello").Length(); // Returns 5
new Delta().Insert("A").Retain(2).Delete(1).Length(); // Returns 4
Returns a new List with the results of calling the provided function on each operation.
Map<T>(Func<Op, int, T> mappingFunction)
mappingFunction
- Function to call, passing in the current operation, returning an element of the new List to be returned
List<T>
- A new List with each element being the result of the given function.
var delta = new Delta().Insert("Hello", new AttributeMap {{ "bold", true }})
.Insert(new AttributeMap {{ "image", "https://octodex.github.com/images/labtocat.png" }})
.Insert("World!");
string text = string.Join("", delta.Map<string>((op, i) => (string)(op.insert is string ? op.insert : "")));
Returns a two Lists, the first with operations that pass the given function, the other that failed.
Partition(Func<Op, bool> predicate)
predicate
- Function to call, passing in the current operation, returning whether that operation passed
(List<Op>, List<Op>)
- A tuple of two lists, the first with passed operations, the other with failed operations
var delta = new Delta().Insert("Hello", new AttributeMap {{ "bold", true }})
.Insert(new AttributeMap {{ "image", "https://octodex.github.com/images/labtocat.png" }})
.Insert("World!");
var (passed, failed) = delta.Partition(op => op.insert is string);
// passed is [{ insert: 'Hello', attributes: { bold: true }}, { insert: 'World'}]
// failed [{ insert: { image: 'https://octodex.github.com/images/labtocat.png' }}]
Applies given function against an accumulator and each operation to reduce to a single value.
Reduce<T>(Func<T, Op, int, T> reducer, T initialValue)
reducer
- Function to call per iteration, returning an accumulated valueinitialValue
- Initial value to pass to first call to predicate
T
- the accumulated value
var delta = new Delta().Insert("Hello", new AttributeMap {{ "bold", true }})
.Insert(new AttributeMap {{ "image", "https://octodex.github.com/images/labtocat.png" }})
.Insert("World!");
long totalLength = delta.reduce((length, op) => length + Op.Length(op), 0);
Returns copy of delta with subset of operations.
Slice(int start = 0, int end = int.MaxValue)
start
- Start index of subset, defaults to 0end
- End index of subset, defaults to rest of operations
var delta = new Delta().Insert("Hello", new AttributeMap {{ "bold", true }).Insert(" World");
// {
// ops: [
// { insert: 'Hello', attributes: { bold: true } },
// { insert: ' World' }
// ]
// }
var copy = delta.Slice();
// { ops: [{ insert: 'World' }] }
var world = delta.Slice(6);
// { ops: [{ insert: ' ' }] }
var space = delta.Slice(5, 6);
Returns a Delta that is equivalent to applying the operations of own Delta, followed by another Delta.
Compose(Delta other)
other
- Delta to compose
var a = new Delta().Insert("abc");
var b = new Delta().Retain(1).Delete(1);
var composed = a.Compose(b); // composed == new Delta().Insert('ac');
Not implemented yet.
Not implemented yet.