-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_text.test.ts
125 lines (119 loc) · 3.26 KB
/
02_text.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { Delta, LoroDoc } from "npm:[email protected]";
import { expect } from "npm:[email protected]";
Deno.test("Long text", () => {
/**
* Loro supports text manipulation.
*/
const doc = new LoroDoc();
const text = doc.getText("text");
for (let i = 0; i < 1_000_000; i += 1) {
text.insert(i, i.toString());
}
doc.export({ mode: "update" });
doc.export({ mode: "snapshot" });
});
Deno.test("Text", () => {
/**
* Loro supports text manipulation.
*/
const doc = new LoroDoc();
const text = doc.getText("text");
text.insert(0, "Hello world!");
text.delete(0, 6);
expect(text.toString()).toBe("world!");
});
Deno.test("Rich text", () => {
/**
* Loro supports rich text CRDTs
*/
const doc = new LoroDoc();
const text = doc.getText("text");
text.insert(0, "Hello world!");
text.mark({ start: 0, end: 5 }, "bold", true);
expect(text.toDelta()).toStrictEqual([
{
insert: "Hello",
attributes: { bold: true },
},
{
insert: " world!",
},
] as Delta<string>[]);
});
Deno.test("Rich text custom expand behavior - Bold", () => {
/**
* For different styles on rich text, you may want different expand behavior
* when users inserting new text at the boundary of the style.
*
* - Bold: expand the style to cover the new text inserted after the boundary.
* - Link: will not expand the style when inserting new text at the boundary.
*/
const doc = new LoroDoc();
doc.configTextStyle({ bold: { expand: "after" } });
const text = doc.getText("text");
text.insert(0, "Hello world!");
text.mark({ start: 0, end: 5 }, "bold", true);
text.insert(5, "!");
expect(text.toDelta()).toStrictEqual([
{
insert: "Hello!",
attributes: { bold: true },
},
{
insert: " world!",
},
] as Delta<string>[]);
});
Deno.test("Rich text custom expand behavior - Link", () => {
/**
* For different styles on rich text, you may want different expand behavior
* when users inserting new text at the boundary of the style.
*
* - Bold: expand the style to cover the new text inserted after the boundary.
* - Link: will not expand the style when inserting new text at the boundary.
*/
const doc = new LoroDoc();
doc.configTextStyle({
link: { expand: "none" },
});
const text = doc.getText("text");
text.insert(0, "Hello world!");
text.mark({ start: 0, end: 5 }, "link", true);
text.insert(5, "!");
expect(text.toDelta()).toStrictEqual([
{
insert: "Hello",
attributes: { link: true },
},
{
insert: "! world!",
},
] as Delta<string>[]);
});
Deno.test("Rich text event", async () => {
/**
* Loro text will receive rich text event in Quill Delta format
*/
const doc = new LoroDoc();
const text = doc.getText("text");
text.insert(0, "Hello world!");
doc.commit();
let ran = false;
text.subscribe((events) => {
for (const event of events.events) {
if (event.diff.type === "text") {
expect(event.diff.diff).toStrictEqual([
{
retain: 5,
attributes: { bold: true },
},
]);
ran = true;
}
}
});
text.mark({ start: 0, end: 5 }, "bold", true);
doc.commit();
await new Promise((resolve) => setTimeout(resolve, 0));
expect(ran).toBeTruthy();
});