@slate-yjs/core 0.2.0 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +3 -0
- package/dist/applyToSlate/index.d.ts +19 -19
- package/dist/applyToSlate/index.d.ts.map +1 -1
- package/dist/applyToSlate/textEvent.d.ts +3 -3
- package/dist/applyToSlate/textEvent.d.ts.map +1 -1
- package/dist/applyToYjs/index.d.ts +3 -3
- package/dist/applyToYjs/node/index.d.ts +3 -3
- package/dist/applyToYjs/node/insertNode.d.ts +3 -3
- package/dist/applyToYjs/node/mergeNode.d.ts +3 -3
- package/dist/applyToYjs/node/moveNode.d.ts +3 -3
- package/dist/applyToYjs/node/removeNode.d.ts +3 -3
- package/dist/applyToYjs/node/setNode.d.ts +3 -3
- package/dist/applyToYjs/node/splitNode.d.ts +3 -3
- package/dist/applyToYjs/text/index.d.ts +3 -3
- package/dist/applyToYjs/text/insertText.d.ts +3 -3
- package/dist/applyToYjs/text/removeText.d.ts +3 -3
- package/dist/applyToYjs/types.d.ts +8 -8
- package/dist/index.cjs +1280 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.global.js +10418 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +1257 -10
- package/dist/index.js.map +1 -1
- package/dist/model/types.d.ts +37 -37
- package/dist/plugins/index.d.ts +3 -3
- package/dist/plugins/withCursors.d.ts +38 -38
- package/dist/plugins/withYHistory.d.ts +19 -19
- package/dist/plugins/withYHistory.d.ts.map +1 -1
- package/dist/plugins/withYjs.d.ts +42 -42
- package/dist/plugins/withYjs.d.ts.map +1 -1
- package/dist/utils/clone.d.ts +4 -4
- package/dist/utils/convert.d.ts +7 -7
- package/dist/utils/delta.d.ts +7 -6
- package/dist/utils/delta.d.ts.map +1 -1
- package/dist/utils/location.d.ts +11 -11
- package/dist/utils/object.d.ts +9 -6
- package/dist/utils/object.d.ts.map +1 -1
- package/dist/utils/position.d.ts +19 -19
- package/dist/utils/slate.d.ts +2 -2
- package/dist/utils/yjs.d.ts +4 -4
- package/package.json +13 -13
- package/src/applyToSlate/index.ts +2 -2
- package/src/applyToSlate/textEvent.ts +21 -20
- package/src/plugins/withYHistory.ts +84 -61
- package/src/plugins/withYjs.ts +14 -5
- package/src/utils/delta.ts +36 -1
- package/src/utils/object.ts +65 -20
- package/test/collaboration/addMark/acrossMarks.tsx +40 -0
- package/test/collaboration/addMark/acrossMarksSame.tsx +36 -0
- package/test/collaboration/addMark/atBeginningOfDocument.tsx +27 -0
- package/test/collaboration/addMark/atEndOfDocument.tsx +30 -0
- package/test/collaboration/addMark/withOtherMarks.tsx +31 -0
- package/test/collaboration/insertNode/atBeginningOfDocument.tsx +28 -0
- package/test/collaboration/insertNode/atEndOfDocument.tsx +28 -0
- package/test/collaboration/insertNode/inTheMiddle.tsx +30 -0
- package/test/collaboration/insertText/atBeginningOfBlock.tsx +26 -0
- package/test/collaboration/insertText/atBeginningOfDocument.tsx +26 -0
- package/test/collaboration/insertText/atEndOfBlock.tsx +26 -0
- package/test/collaboration/insertText/atEndOfDocument.tsx +26 -0
- package/test/collaboration/insertText/inTheMiddle.tsx +32 -0
- package/test/collaboration/insertText/inTheMiddleOfNestedBlock.tsx +30 -0
- package/test/collaboration/insertText/insideMarks.tsx +28 -0
- package/test/collaboration/insertText/withEmptyString.tsx +25 -0
- package/test/collaboration/insertText/withEntities.tsx +28 -0
- package/test/collaboration/insertText/withMarks.tsx +25 -0
- package/test/collaboration/insertText/withUnicode.tsx +28 -0
- package/test/collaboration/mergeNode/afterADeleteBackward.tsx +27 -0
- package/test/collaboration/mergeNode/onMixedNestedNodes.tsx +29 -0
- package/test/collaboration/mergeNode/onMixedTypeNodes.tsx +27 -0
- package/test/collaboration/mergeNode/withUnicode.tsx +25 -0
- package/test/collaboration/moveNode/downward/whenBlockBecomesNested.tsx +43 -0
- package/test/collaboration/moveNode/downward/whenBlockBecomesNonNested.tsx +41 -0
- package/test/collaboration/moveNode/downward/whenBlockStaysNested.tsx +38 -0
- package/test/collaboration/moveNode/downward/whenBlockStaysNonNested.tsx +30 -0
- package/test/collaboration/moveNode/upward/whenBlockBecomesNested.tsx +43 -0
- package/test/collaboration/moveNode/upward/whenBlockBecomesNonNested.tsx +41 -0
- package/test/collaboration/moveNode/upward/whenBlockStaysNested.tsx +38 -0
- package/test/collaboration/moveNode/upward/whenBlockStaysNonNested.tsx +30 -0
- package/test/collaboration/removeMark/inTheMiddleOfText.tsx +43 -0
- package/test/collaboration/removeMark/withAddMark.tsx +31 -0
- package/test/collaboration/removeMark/withOtherMarks.tsx +47 -0
- package/test/collaboration/removeNode/atBeginningOfDocument.tsx +26 -0
- package/test/collaboration/removeNode/atEndOfDocument.tsx +26 -0
- package/test/collaboration/removeNode/nestedBlock.tsx +28 -0
- package/test/collaboration/removeNode/wrapperBlock.tsx +28 -0
- package/test/collaboration/removeText/atBeginningOfDocument.tsx +34 -0
- package/test/collaboration/removeText/atEndOfDocument.tsx +37 -0
- package/test/collaboration/removeText/withUnicode.tsx +29 -0
- package/test/collaboration/setNode/atBeginningOfDocument.tsx +27 -0
- package/test/collaboration/setNode/atEndOfDocument.tsx +27 -0
- package/test/collaboration/setNode/onDataChange.tsx +35 -0
- package/test/collaboration/setNode/onDataChangeOnInline.tsx +35 -0
- package/test/collaboration/setNode/onResetBlock.tsx +27 -0
- package/test/collaboration/setNode/withAChangeOfType.tsx +26 -0
- package/test/collaboration/splitNode/atBeginningOfDocument.tsx +28 -0
- package/test/collaboration/splitNode/atEndOfBlock.tsx +27 -0
- package/test/collaboration/splitNode/atEndOfDocument.tsx +27 -0
- package/test/collaboration/splitNode/onNonDefaultBlock.tsx +27 -0
- package/test/collaboration/splitNode/withMultipleSubNodes.tsx +32 -0
- package/test/collaboration/splitNode/withUnicode.tsx +29 -0
- package/test/index.test.ts +65 -0
- package/test/{types.d.ts → slate.d.ts} +0 -0
- package/test/withTestingElements.ts +46 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/tsup.config.ts +32 -0
- package/dist/applyToSlate/Untitled-1.d.ts +0 -101
- package/dist/applyToSlate/Untitled-1.d.ts.map +0 -1
- package/dist/applyToSlate/index.js +0 -32
- package/dist/applyToSlate/index.js.map +0 -1
- package/dist/applyToSlate/textEvent.js +0 -166
- package/dist/applyToSlate/textEvent.js.map +0 -1
- package/dist/applyToYjs/index.js +0 -17
- package/dist/applyToYjs/index.js.map +0 -1
- package/dist/applyToYjs/node/index.js +0 -15
- package/dist/applyToYjs/node/index.js.map +0 -1
- package/dist/applyToYjs/node/insertNode.js +0 -12
- package/dist/applyToYjs/node/insertNode.js.map +0 -1
- package/dist/applyToYjs/node/mergeNode.js +0 -32
- package/dist/applyToYjs/node/mergeNode.js.map +0 -1
- package/dist/applyToYjs/node/moveNode.js +0 -28
- package/dist/applyToYjs/node/moveNode.js.map +0 -1
- package/dist/applyToYjs/node/removeNode.js +0 -6
- package/dist/applyToYjs/node/removeNode.js.map +0 -1
- package/dist/applyToYjs/node/setNode.js +0 -21
- package/dist/applyToYjs/node/setNode.js.map +0 -1
- package/dist/applyToYjs/node/splitNode.js +0 -50
- package/dist/applyToYjs/node/splitNode.js.map +0 -1
- package/dist/applyToYjs/text/index.js +0 -7
- package/dist/applyToYjs/text/index.js.map +0 -1
- package/dist/applyToYjs/text/insertText.js +0 -12
- package/dist/applyToYjs/text/insertText.js.map +0 -1
- package/dist/applyToYjs/text/removeText.js +0 -6
- package/dist/applyToYjs/text/removeText.js.map +0 -1
- package/dist/applyToYjs/types.js +0 -2
- package/dist/applyToYjs/types.js.map +0 -1
- package/dist/index.modern.js +0 -2
- package/dist/index.modern.js.map +0 -1
- package/dist/index.module.js +0 -2
- package/dist/index.module.js.map +0 -1
- package/dist/index.umd.js +0 -2
- package/dist/index.umd.js.map +0 -1
- package/dist/model/types.js +0 -2
- package/dist/model/types.js.map +0 -1
- package/dist/plugins/index.js +0 -4
- package/dist/plugins/index.js.map +0 -1
- package/dist/plugins/withCursors.js +0 -145
- package/dist/plugins/withCursors.js.map +0 -1
- package/dist/plugins/withYHistory.js +0 -66
- package/dist/plugins/withYHistory.js.map +0 -1
- package/dist/plugins/withYjs.js +0 -155
- package/dist/plugins/withYjs.js.map +0 -1
- package/dist/utils/clone.js +0 -23
- package/dist/utils/clone.js.map +0 -1
- package/dist/utils/convert.js +0 -35
- package/dist/utils/convert.js.map +0 -1
- package/dist/utils/delta.js +0 -42
- package/dist/utils/delta.js.map +0 -1
- package/dist/utils/location.js +0 -111
- package/dist/utils/location.js.map +0 -1
- package/dist/utils/object.js +0 -27
- package/dist/utils/object.js.map +0 -1
- package/dist/utils/position.js +0 -140
- package/dist/utils/position.js.map +0 -1
- package/dist/utils/slate.js +0 -6
- package/dist/utils/slate.js.map +0 -1
- package/dist/utils/yjs.js +0 -7
- package/dist/utils/yjs.js.map +0 -1
- package/test/collaboration.test.ts +0 -395
- package/test/concurrent.test.ts +0 -233
- package/test/testEditor.ts +0 -134
- package/test/utils.ts +0 -64
package/test/concurrent.test.ts
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import { Node } from 'slate';
|
|
2
|
-
import { yTextToSlateElement } from '../src/utils/convert';
|
|
3
|
-
import { TestEditor, TransformFunc } from './testEditor';
|
|
4
|
-
import { createNode, createTestEditor, wait } from './utils';
|
|
5
|
-
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
7
|
-
jest.mock('y-protocols/awareness', () => {});
|
|
8
|
-
|
|
9
|
-
const initialState: Node[] = [
|
|
10
|
-
createNode('paragraph', 'alfa bravo'),
|
|
11
|
-
createNode('paragraph', 'charlie delta'),
|
|
12
|
-
createNode('paragraph', 'echo foxtrot'),
|
|
13
|
-
createNode('paragraph', 'golf hotel'),
|
|
14
|
-
];
|
|
15
|
-
|
|
16
|
-
interface Test {
|
|
17
|
-
name: string;
|
|
18
|
-
transform: TransformFunc;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const tests: Test[] = [
|
|
22
|
-
{
|
|
23
|
-
name: 'Insert text into 1st paragraph',
|
|
24
|
-
transform: TestEditor.makeInsertText('india ', { path: [0, 0], offset: 5 }),
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
name: 'Insert text into 2nd paragraph',
|
|
28
|
-
transform: TestEditor.makeInsertText('juliett ', {
|
|
29
|
-
path: [1, 0],
|
|
30
|
-
offset: 8,
|
|
31
|
-
}),
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
name: 'Insert text into 3rd paragraph',
|
|
35
|
-
transform: TestEditor.makeInsertText('kilo ', { path: [2, 0], offset: 5 }),
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
name: 'Insert text into 4th paragraph',
|
|
39
|
-
transform: TestEditor.makeInsertText('lima ', { path: [3, 0], offset: 5 }),
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
name: 'Delete text from 1st paragraph',
|
|
43
|
-
transform: TestEditor.makeRemoveCharacters(5, { path: [0, 0], offset: 2 }),
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
name: 'Delete text from 2nd paragraph',
|
|
47
|
-
transform: TestEditor.makeRemoveCharacters(6, { path: [1, 0], offset: 4 }),
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
name: 'Delete text from 3nd paragraph',
|
|
51
|
-
transform: TestEditor.makeRemoveCharacters(5, { path: [2, 0], offset: 3 }),
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
name: 'Delete text from 4th paragraph',
|
|
55
|
-
transform: TestEditor.makeRemoveCharacters(7, { path: [3, 0], offset: 1 }),
|
|
56
|
-
},
|
|
57
|
-
{
|
|
58
|
-
name: 'Insert new paragraph before 1st',
|
|
59
|
-
transform: TestEditor.makeInsertNodes(createNode('paragraph', 'mike'), [0]),
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
name: 'Insert new paragraph between 1st and 2nd',
|
|
63
|
-
transform: TestEditor.makeInsertNodes(createNode('paragraph', 'november'), [
|
|
64
|
-
1,
|
|
65
|
-
]),
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
name: 'Insert new paragraph between 2nd and 3rd',
|
|
69
|
-
transform: TestEditor.makeInsertNodes(createNode('paragraph', 'oscar'), [
|
|
70
|
-
2,
|
|
71
|
-
]),
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
name: 'Insert new paragraph between 3rd and 4th',
|
|
75
|
-
transform: TestEditor.makeInsertNodes(createNode('paragraph', 'papa'), [3]),
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
name: 'Insert new paragraph after 4th',
|
|
79
|
-
transform: TestEditor.makeInsertNodes(createNode('paragraph', 'quebec'), [
|
|
80
|
-
4,
|
|
81
|
-
]),
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
name: 'Insert new text node into 1st paragraph',
|
|
85
|
-
transform: TestEditor.makeInsertNodes({ text: 'romeo' }, [0, 0]),
|
|
86
|
-
},
|
|
87
|
-
{
|
|
88
|
-
name: 'Insert new text node into 2nd paragraph',
|
|
89
|
-
transform: TestEditor.makeInsertNodes({ text: 'sierra' }, [1, 0]),
|
|
90
|
-
},
|
|
91
|
-
{
|
|
92
|
-
name: 'Insert new text node into 3rd paragraph',
|
|
93
|
-
transform: TestEditor.makeInsertNodes({ text: 'tango' }, [2, 0]),
|
|
94
|
-
},
|
|
95
|
-
{
|
|
96
|
-
name: 'Insert new text node into 4th paragraph',
|
|
97
|
-
transform: TestEditor.makeInsertNodes({ text: 'uniform' }, [3, 0]),
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
name: 'Merge 1st and 2nd paragraphs',
|
|
101
|
-
transform: TestEditor.makeMergeNodes([1]),
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
name: 'Merge 2nd and 3rd paragraphs',
|
|
105
|
-
transform: TestEditor.makeMergeNodes([2]),
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
name: 'Merge 3nd and 4th paragraphs',
|
|
109
|
-
transform: TestEditor.makeMergeNodes([3]),
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
name: 'Remove 1st paragraph',
|
|
113
|
-
transform: TestEditor.makeRemoveNodes([0]),
|
|
114
|
-
},
|
|
115
|
-
{
|
|
116
|
-
name: 'Remove 2nd paragraph',
|
|
117
|
-
transform: TestEditor.makeRemoveNodes([1]),
|
|
118
|
-
},
|
|
119
|
-
{
|
|
120
|
-
name: 'Remove 3rd paragraph',
|
|
121
|
-
transform: TestEditor.makeRemoveNodes([2]),
|
|
122
|
-
},
|
|
123
|
-
{
|
|
124
|
-
name: 'Remove 4th paragraph',
|
|
125
|
-
transform: TestEditor.makeRemoveNodes([3]),
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
name: 'Remove text node from 1st paragraph',
|
|
129
|
-
transform: TestEditor.makeRemoveNodes([0, 0]),
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
name: 'Remove text node from 2nd paragraph',
|
|
133
|
-
transform: TestEditor.makeRemoveNodes([1, 0]),
|
|
134
|
-
},
|
|
135
|
-
{
|
|
136
|
-
name: 'Remove text node from 3nd paragraph',
|
|
137
|
-
transform: TestEditor.makeRemoveNodes([2, 0]),
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
name: 'Remove text node from 4th paragraph',
|
|
141
|
-
transform: TestEditor.makeRemoveNodes([3, 0]),
|
|
142
|
-
},
|
|
143
|
-
{
|
|
144
|
-
name: 'Split 1st paragraph',
|
|
145
|
-
transform: TestEditor.makeSplitNodes({ path: [0, 0], offset: 4 }),
|
|
146
|
-
},
|
|
147
|
-
{
|
|
148
|
-
name: 'Split 2nd paragraph',
|
|
149
|
-
transform: TestEditor.makeSplitNodes({ path: [1, 0], offset: 5 }),
|
|
150
|
-
},
|
|
151
|
-
{
|
|
152
|
-
name: 'Split 3rd paragraph',
|
|
153
|
-
transform: TestEditor.makeSplitNodes({ path: [2, 0], offset: 6 }),
|
|
154
|
-
},
|
|
155
|
-
{
|
|
156
|
-
name: 'Split 4th paragraph',
|
|
157
|
-
transform: TestEditor.makeSplitNodes({ path: [3, 0], offset: 7 }),
|
|
158
|
-
},
|
|
159
|
-
{
|
|
160
|
-
name: 'Move 1st paragraph',
|
|
161
|
-
transform: TestEditor.makeMoveNodes([0], [3]),
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
name: 'Move 2nd paragraph',
|
|
165
|
-
transform: TestEditor.makeMoveNodes([3], [2]),
|
|
166
|
-
},
|
|
167
|
-
{
|
|
168
|
-
name: 'Move 3rd paragraph',
|
|
169
|
-
transform: TestEditor.makeMoveNodes([2], [1]),
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
name: 'Move 4th paragraph',
|
|
173
|
-
transform: TestEditor.makeMoveNodes([1], [0]),
|
|
174
|
-
},
|
|
175
|
-
];
|
|
176
|
-
|
|
177
|
-
const runOneTest = async (ti: Test, tj: Test) => {
|
|
178
|
-
// Create two editors.
|
|
179
|
-
const ei = await createTestEditor();
|
|
180
|
-
const ej = await createTestEditor();
|
|
181
|
-
|
|
182
|
-
// Set initial state for 1st editor, propagate changes to 2nd.
|
|
183
|
-
TestEditor.applyTransform(
|
|
184
|
-
ei,
|
|
185
|
-
TestEditor.makeInsertNodes(initialState as Node[], [0])
|
|
186
|
-
);
|
|
187
|
-
await wait();
|
|
188
|
-
|
|
189
|
-
const updates = TestEditor.getCapturedYjsUpdates(ei);
|
|
190
|
-
TestEditor.applyYjsUpdatesToYjs(ej, updates);
|
|
191
|
-
await wait();
|
|
192
|
-
|
|
193
|
-
// Verify initial states match.
|
|
194
|
-
expect(ei.children).toEqual(ej.children);
|
|
195
|
-
expect(yTextToSlateElement(ei.sharedRoot)).toEqual(
|
|
196
|
-
yTextToSlateElement(ej.sharedRoot)
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
// Apply 1st transform to 1st editor, capture updates.
|
|
200
|
-
TestEditor.applyTransform(ei, ti.transform);
|
|
201
|
-
await wait();
|
|
202
|
-
|
|
203
|
-
const updatesFromI = TestEditor.getCapturedYjsUpdates(ei);
|
|
204
|
-
|
|
205
|
-
// Apply 2nd transform to 2nd editor, capture updates.
|
|
206
|
-
TestEditor.applyTransform(ej, tj.transform);
|
|
207
|
-
await wait();
|
|
208
|
-
|
|
209
|
-
const updatesFromJ = TestEditor.getCapturedYjsUpdates(ej);
|
|
210
|
-
|
|
211
|
-
// Cross-propagate updates between editors.
|
|
212
|
-
TestEditor.applyYjsUpdatesToYjs(ei, updatesFromJ);
|
|
213
|
-
TestEditor.applyYjsUpdatesToYjs(ej, updatesFromI);
|
|
214
|
-
await wait();
|
|
215
|
-
|
|
216
|
-
// Verify final states match.
|
|
217
|
-
expect(ei.children).toEqual(ej.children);
|
|
218
|
-
expect(yTextToSlateElement(ei.sharedRoot)).toEqual(
|
|
219
|
-
yTextToSlateElement(ej.sharedRoot)
|
|
220
|
-
);
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
describe('model concurrent edits in separate editors', () => {
|
|
224
|
-
tests.forEach((test) => {
|
|
225
|
-
describe(`Test:${test.name}`, () => {
|
|
226
|
-
tests.forEach((concurrentTest) => {
|
|
227
|
-
it(`Concurrent:${concurrentTest.name}`, async () => {
|
|
228
|
-
await runOneTest(test, concurrentTest);
|
|
229
|
-
});
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
});
|
package/test/testEditor.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { Editor, Location, Node, Path, Point, Transforms } from 'slate';
|
|
2
|
-
import * as Y from 'yjs';
|
|
3
|
-
import { YjsEditor } from '../src';
|
|
4
|
-
|
|
5
|
-
export interface TestEditor extends YjsEditor {
|
|
6
|
-
shouldCaptureYjsUpdates: boolean;
|
|
7
|
-
capturedYjsUpdates: Uint8Array[];
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export type TransformFunc = (e: Editor) => void;
|
|
11
|
-
|
|
12
|
-
export const TestEditor = {
|
|
13
|
-
/**
|
|
14
|
-
* Capture Yjs updates generated by this editor.
|
|
15
|
-
*/
|
|
16
|
-
captureYjsUpdate: (e: TestEditor, update: Uint8Array): void => {
|
|
17
|
-
if (!e.shouldCaptureYjsUpdates) return;
|
|
18
|
-
e.capturedYjsUpdates.push(update);
|
|
19
|
-
},
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Return captured Yjs updates.
|
|
23
|
-
*/
|
|
24
|
-
getCapturedYjsUpdates: (e: TestEditor): Uint8Array[] => {
|
|
25
|
-
const result = e.capturedYjsUpdates;
|
|
26
|
-
e.capturedYjsUpdates = [];
|
|
27
|
-
return result;
|
|
28
|
-
},
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Apply one Yjs update to Yjs.
|
|
32
|
-
*/
|
|
33
|
-
applyYjsUpdateToYjs: (e: TestEditor, update: Uint8Array): void => {
|
|
34
|
-
e.shouldCaptureYjsUpdates = false;
|
|
35
|
-
Y.applyUpdate(e.sharedRoot.doc, update);
|
|
36
|
-
e.shouldCaptureYjsUpdates = true;
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Apply multiple Yjs updates to Yjs.
|
|
41
|
-
*/
|
|
42
|
-
applyYjsUpdatesToYjs: (e: TestEditor, updates: Uint8Array[]): void => {
|
|
43
|
-
updates.forEach((update) => {
|
|
44
|
-
TestEditor.applyYjsUpdateToYjs(e, update);
|
|
45
|
-
});
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Apply one TransformFunc to slate.
|
|
50
|
-
*/
|
|
51
|
-
applyTransform: (e: TestEditor, transform: TransformFunc): void => {
|
|
52
|
-
transform(e);
|
|
53
|
-
},
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Apply multiple TransformFuncs to slate.
|
|
57
|
-
*/
|
|
58
|
-
applyTransforms: (e: TestEditor, transforms: TransformFunc[]): void => {
|
|
59
|
-
transforms.forEach((transform) => {
|
|
60
|
-
TestEditor.applyTransform(e, transform);
|
|
61
|
-
});
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
makeInsertText:
|
|
65
|
-
(text: string, at: Location): TransformFunc =>
|
|
66
|
-
(e: Editor) => {
|
|
67
|
-
Transforms.insertText(e, text, { at });
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
makeRemoveCharacters:
|
|
71
|
-
(count: number, at: Location): TransformFunc =>
|
|
72
|
-
(e: Editor) => {
|
|
73
|
-
Transforms.delete(e, { distance: count, at });
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
makeInsertNodes:
|
|
77
|
-
(nodes: Node | Node[], at: Location): TransformFunc =>
|
|
78
|
-
(e: Editor) => {
|
|
79
|
-
Transforms.insertNodes(e, nodes, { at });
|
|
80
|
-
},
|
|
81
|
-
|
|
82
|
-
makeMergeNodes:
|
|
83
|
-
(at: Path): TransformFunc =>
|
|
84
|
-
(e: Editor) => {
|
|
85
|
-
Transforms.mergeNodes(e, { at });
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
makeMoveNodes:
|
|
89
|
-
(from: Path, to: Path): TransformFunc =>
|
|
90
|
-
(e: Editor) => {
|
|
91
|
-
Transforms.moveNodes(e, { at: from, to });
|
|
92
|
-
},
|
|
93
|
-
|
|
94
|
-
makeRemoveNodes:
|
|
95
|
-
(at: Path): TransformFunc =>
|
|
96
|
-
(e: Editor) => {
|
|
97
|
-
Transforms.removeNodes(e, { at });
|
|
98
|
-
},
|
|
99
|
-
|
|
100
|
-
makeSetNodes:
|
|
101
|
-
(at: Location, props: Partial<Node>): TransformFunc =>
|
|
102
|
-
(e: Editor) => {
|
|
103
|
-
Transforms.setNodes(e, props, { at });
|
|
104
|
-
},
|
|
105
|
-
|
|
106
|
-
makeSplitNodes:
|
|
107
|
-
(at: Location): TransformFunc =>
|
|
108
|
-
(e: Editor) => {
|
|
109
|
-
Transforms.splitNodes(e, { at });
|
|
110
|
-
},
|
|
111
|
-
|
|
112
|
-
makeSetSelection:
|
|
113
|
-
(anchor: Point, focus: Point): TransformFunc =>
|
|
114
|
-
(e: Editor) => {
|
|
115
|
-
Transforms.setSelection(e, { anchor, focus });
|
|
116
|
-
},
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
export function withTest<T extends YjsEditor>(editor: T): T & TestEditor {
|
|
120
|
-
const e = editor as T & TestEditor;
|
|
121
|
-
|
|
122
|
-
if (!e.sharedRoot.doc) {
|
|
123
|
-
throw new Error('Shared type should be bound to a document');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
e.sharedRoot.doc.on('update', (updateMessage: Uint8Array) => {
|
|
127
|
-
TestEditor.captureYjsUpdate(e, updateMessage);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
e.shouldCaptureYjsUpdates = true;
|
|
131
|
-
e.capturedYjsUpdates = [];
|
|
132
|
-
|
|
133
|
-
return e;
|
|
134
|
-
}
|
package/test/utils.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { createEditor, Node, Text } from 'slate';
|
|
2
|
-
import * as Y from 'yjs';
|
|
3
|
-
import { TestEditor, withTest } from './testEditor';
|
|
4
|
-
import { withYjs, WithYjsOptions } from '../src';
|
|
5
|
-
import { slateNodesToInsertDelta } from '../src/utils/convert';
|
|
6
|
-
|
|
7
|
-
export function createText(text = ''): Text {
|
|
8
|
-
return {
|
|
9
|
-
text,
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function createNode(
|
|
14
|
-
type = 'paragraph',
|
|
15
|
-
text = '',
|
|
16
|
-
data?: Partial<Node>
|
|
17
|
-
): Node {
|
|
18
|
-
return {
|
|
19
|
-
type,
|
|
20
|
-
children: [createText(text)],
|
|
21
|
-
...data,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function createValue(children?: Node[]): { children: Node[] } {
|
|
26
|
-
return {
|
|
27
|
-
children: children || [createNode()],
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function createDoc(children?: Node[]): Y.Doc {
|
|
32
|
-
const doc = new Y.Doc();
|
|
33
|
-
const insertDelta = slateNodesToInsertDelta(createValue(children).children);
|
|
34
|
-
|
|
35
|
-
const sharedRoot = doc.get('content', Y.XmlText) as Y.XmlText;
|
|
36
|
-
sharedRoot.applyDelta(insertDelta, { sanitize: false });
|
|
37
|
-
|
|
38
|
-
return doc;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function wait(ms = 0): Promise<void> {
|
|
42
|
-
return new Promise<void>((resolve) => setTimeout(resolve, ms));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export async function createTestEditor(
|
|
46
|
-
value?: Node[],
|
|
47
|
-
options?: WithYjsOptions
|
|
48
|
-
): Promise<TestEditor> {
|
|
49
|
-
const doc = new Y.Doc();
|
|
50
|
-
|
|
51
|
-
const sharedRoot = doc.get('content', Y.XmlText) as Y.XmlText;
|
|
52
|
-
|
|
53
|
-
if (value) {
|
|
54
|
-
const insertDelta = slateNodesToInsertDelta(value);
|
|
55
|
-
sharedRoot.applyDelta(insertDelta, { sanitize: false });
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const editor = withTest(withYjs(createEditor(), sharedRoot, options));
|
|
59
|
-
|
|
60
|
-
// wait for value sync
|
|
61
|
-
await wait();
|
|
62
|
-
|
|
63
|
-
return editor;
|
|
64
|
-
}
|