Skip to content

Commit 689b8fd

Browse files
CatHood0CatHood0
andauthored
doc(Delta): more documentation about Delta (#2042)
* doc(Delta): added documentation about Deltas * fix: bad display of retain examples * fix bad display of transformation examples --------- Co-authored-by: CatHood0 <[email protected]>
1 parent 1fb757c commit 689b8fd

File tree

1 file changed

+252
-0
lines changed

1 file changed

+252
-0
lines changed

doc/delta_introduction.md

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
# What is Delta?
2+
3+
`Delta` is a structured format used to represent text editing operations consistently and efficiently. It is especially useful in collaborative editors where multiple users may be editing the same document simultaneously.
4+
5+
## How does Delta work?
6+
7+
`Delta` consists of a list of operations. Each operation describes a change in the document's content. The operations can be of three types: insertion (`insert`), deletion (`delete`), and retention (`retain`). These operations are combined to describe any change in the document's state.
8+
9+
You can import `Delta` and `Operation` class using:
10+
11+
```dart
12+
import 'package:flutter_quill/dart_quill_delta.dart';
13+
```
14+
15+
# What is a `Operation`?
16+
17+
Operations are the actions performed on the document to modify its content. Each operation can be an insertion, deletion, or retention, and is executed sequentially to transform the document's state.
18+
19+
## How Do `Operations` Work?
20+
21+
`Operations` are applied in the order they are defined in the `Delta`. Starting with the initial state of the `Document`, the operations are applied one by one, updating the document's state at each step.
22+
23+
Example of a `Operation` Code:
24+
25+
```dart
26+
[
27+
// Adds the text "Hello" to the editor's content
28+
{ "insert": "Hello" },
29+
// Retains the first 5 characters of the existing content,
30+
// and applies the "bold" attribute to those characters.
31+
{ "retain": 5, "attributes": { "bold": true } },
32+
// Deletes 2 characters starting from the current position in the editor's content.
33+
{ "delete": 2 }
34+
]
35+
```
36+
37+
# Types of Operations in Delta
38+
39+
## 1. Insertion (`Insert`)
40+
41+
An insertion adds new content to the document. The `Insert` operation contains the text or data being added.
42+
43+
Example of `Insert` operation:
44+
45+
```dart
46+
import 'package:flutter_quill/dart_quill_delta.dart';
47+
48+
void main() {
49+
// Create a Delta with a text insertion
50+
final delta = Delta()
51+
..insert('Hello, world!\n')
52+
..insert('This is an example.\n', {'bold': true})
53+
..delete(10); // Remove the first 10 characters
54+
55+
print(delta); // Output: [{insert: "Hello, world!\n"}, {insert: "This is an example.\n", attributes: {bold: true}}, {delete: 10}]
56+
}
57+
```
58+
59+
## 2. Deletion (`Delete`)
60+
61+
In Quill, operations are a way to represent changes to the editor's content. Each operation has a type and a set of properties that indicate what has changed and how.`Delete` operations are a specific type of operation that is used to remove content from the editor.
62+
63+
## Delete Operations
64+
65+
A Delete operation is used to remove a portion of the editor's content. The Delete operation has the following format:
66+
67+
```dart
68+
Delta()
69+
..retain(<number>)
70+
..delete(<number>);
71+
```
72+
73+
Where:
74+
75+
- **retain**: (Optional) The number of characters to retain before deletion is performed.
76+
- **delete**: The number of characters to delete.
77+
78+
Basic example
79+
80+
Let's say you have the following content in the editor:
81+
82+
```Arduino
83+
"Hello, world!"
84+
```
85+
86+
And you want to remove the word "world". The corresponding Delete operation could be:
87+
88+
```dart
89+
Delta()
90+
..retain(6)
91+
..delete(7);
92+
```
93+
94+
Here the first **7** characters are being retained ("Hello, ") and then 6 characters are being removed ("world!").
95+
96+
### Behavior of Delete Operations
97+
98+
**Text Deletion**: The `Delete` operation removes text in the editor document. The characters removed are those that are in the range specified by the operation.
99+
100+
**Combination with retain**: The `Delete` operation is often combined with the retain operation to specify which part of the content should remain intact and which part should be removed. For example, if you want to delete a specific section of text, you can use retain to keep the text before and after the section to be deleted.
101+
102+
**Range Calculation**: When a `Delete` operation is applied, the range of text to be deleted is calculated based on the value of retain and delete. It is important to understand how retain and delete are combined to perform correct deletion.
103+
104+
Example of `Delete` operation using `QuillController`
105+
106+
```dart
107+
import 'package:flutter_quill/flutter_quill.dart';
108+
import 'package:flutter_quill/dart_quill_delta.dart';
109+
110+
QuillController _quillController = QuillController(
111+
document: Document.fromJson([{'insert': 'Hello, world!'}]),
112+
selection: TextSelection.collapsed(offset: 0),
113+
);
114+
115+
// Create a delta with the retain and delete operations
116+
final delta = Delta()
117+
..retain(6) // Retain "Hello, "
118+
..delete(7); // Delete "world!"
119+
120+
// Apply the delta to update the content of the editor
121+
_quillController.compose(delta, ChangeSource.local);
122+
```
123+
124+
In this example, the current content of the editor is updated to reflect the removal of the word "world."
125+
126+
## 3. Retention (`Retain`)
127+
128+
`Retain` operations are particularly important because they allow you to apply attributes to specific parts of the content without modifying the content itself. A Retain operation consists of two parts:
129+
130+
- **Index**: The length of the content to retain unchanged.
131+
- **Attributes**: An optional object containing the attributes to apply.
132+
133+
Example of a `Retain` Operation
134+
135+
Suppose we have the following content in an editor:
136+
137+
```arduino
138+
"Hello world"
139+
```
140+
141+
And we want to apply bold formatting to the word "world." The `Retain` operation would be represented in a `Delta` as follows:
142+
143+
```dart
144+
[
145+
{ "insert": "Hello, " },
146+
{ "retain": 7 },
147+
{ "retain": 5, "attributes": { "bold": true } }
148+
]
149+
```
150+
151+
This Delta is interpreted as follows:
152+
153+
- `{ "retain": 7 }`: Retains the first **7** characters ("Hello, ").
154+
- `{ "retain": 5, "attributes": { "bold": true } }`: Retains the next **5** characters ("world") and applies the bold attribute.
155+
156+
### Applications of Retain
157+
158+
Retain operations are useful for various tasks in document editing, such as:
159+
160+
- **Text Formatting**: Applying styles (bold, italic, underline, etc.) to specific segments without altering the content.
161+
- **Annotations**: Adding metadata or annotations to specific sections of text.
162+
- **Content Preservation**: Ensuring that certain parts of the document remain unchanged during complex editing operations.
163+
164+
Using Directly `Delta` class:
165+
166+
```dart
167+
import 'package:flutter_quill/flutter_quill.dart';
168+
import 'package:flutter_quill/dart_quill_delta.dart';
169+
170+
void main() {
171+
// Create a Delta that retains 10 characters
172+
QuillController _quillController = QuillController(
173+
document: Document.fromJson([{'insert': 'Hello, world!'}]),
174+
selection: TextSelection.collapsed(offset: 0),
175+
);
176+
177+
// Create a delta with the retain and delete operations
178+
final delta = Delta()
179+
..retain(6) // Retain "Hello, "
180+
181+
// Apply the delta to update the content of the editor
182+
_quillController.compose(delta, ChangeSource.local);
183+
}
184+
```
185+
186+
# Transformations
187+
188+
Transformations are used to combine two Deltas and produce a third Delta that represents the combination of both operations.
189+
190+
Example 1: Transformation with Deletions
191+
192+
Deltas to combine:
193+
194+
- **Delta A**: `[{insert: "Flutter"}, {retain: 3}, {insert: "Quill"}]`
195+
- **Delta B**: `[{retain: 6}, {delete: 4}, {insert: "Editor"}]`
196+
197+
```dart
198+
199+
import 'package:flutter_quill/dart_quill_delta.dart' as quill;
200+
201+
void main() {
202+
// Defining Delta A
203+
final deltaA = quill.Delta()
204+
..insert('Flutter')
205+
..retain(3)
206+
..insert('Quill');
207+
208+
// Defining Delta B
209+
final deltaB = quill.Delta()
210+
..retain(7) // retain: Flutter
211+
..delete(5) // delete: Quill
212+
..insert('Editor');
213+
214+
// applying transformation
215+
final result = deltaA.transform(deltaB);
216+
217+
print(result.toJson()); // output: [{insert: "FlutterEditor"}]
218+
}
219+
```
220+
221+
Example 2: Complex Transformation
222+
223+
Deltas to combine:
224+
225+
- **Delta A**: `[{insert: "Hello World"}]`
226+
- **Delta B**: `[{retain: 6}, {delete: 5}, {insert: "Flutter"}]`
227+
228+
```dart
229+
import 'package:flutter_quill/dart_quill_delta.dart' as quill;
230+
231+
void main() {
232+
233+
// Defining Delta A
234+
final deltaA = quill.Delta()
235+
..insert('Hello World');
236+
237+
// Defining Delta B
238+
final deltaB = quill.Delta()
239+
..retain(6) // retain: 'Hello '
240+
..delete(5) // delete: 'World'
241+
..insert('Flutter');
242+
243+
// Applying transformations
244+
final result = deltaA.transform(deltaB);
245+
246+
print(result.toJson()); // output: [{insert: "Hello Flutter"}]
247+
}
248+
```
249+
250+
# Why Use Delta Instead of Another Format?
251+
252+
Delta offers a structured and efficient way to represent changes in text documents, especially in collaborative environments. Its operation-based design allows for easy synchronization, transformation, and conflict handling, which is essential for real-time text editing applications. Other formats may not provide the same level of granularity and control over edits and transformations.

0 commit comments

Comments
 (0)