@slate-yjs/core 0.0.1 → 0.2.0
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/dist/applyToSlate/Untitled-1.d.ts +101 -0
- package/dist/applyToSlate/Untitled-1.d.ts.map +1 -0
- package/dist/applyToSlate/textEvent.d.ts.map +1 -1
- package/dist/applyToSlate/textEvent.js +0 -3
- package/dist/applyToSlate/textEvent.js.map +1 -1
- package/dist/applyToYjs/node/insertNode.d.ts.map +1 -1
- package/dist/applyToYjs/node/insertNode.js +1 -4
- package/dist/applyToYjs/node/insertNode.js.map +1 -1
- package/dist/applyToYjs/node/mergeNode.d.ts.map +1 -1
- package/dist/applyToYjs/node/mergeNode.js +2 -5
- package/dist/applyToYjs/node/mergeNode.js.map +1 -1
- package/dist/applyToYjs/node/moveNode.d.ts.map +1 -1
- package/dist/applyToYjs/node/moveNode.js +1 -3
- package/dist/applyToYjs/node/moveNode.js.map +1 -1
- package/dist/applyToYjs/node/removeNode.d.ts.map +1 -1
- package/dist/applyToYjs/node/removeNode.js +0 -2
- package/dist/applyToYjs/node/removeNode.js.map +1 -1
- package/dist/applyToYjs/node/setNode.d.ts.map +1 -1
- package/dist/applyToYjs/node/setNode.js +0 -2
- package/dist/applyToYjs/node/setNode.js.map +1 -1
- package/dist/applyToYjs/node/splitNode.d.ts.map +1 -1
- package/dist/applyToYjs/node/splitNode.js +2 -5
- package/dist/applyToYjs/node/splitNode.js.map +1 -1
- package/dist/applyToYjs/text/insertText.d.ts.map +1 -1
- package/dist/applyToYjs/text/insertText.js +0 -2
- package/dist/applyToYjs/text/insertText.js.map +1 -1
- package/dist/applyToYjs/text/removeText.d.ts.map +1 -1
- package/dist/applyToYjs/text/removeText.js +0 -2
- package/dist/applyToYjs/text/removeText.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -9
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +1 -1
- package/dist/index.modern.js.map +1 -1
- package/dist/index.module.js +1 -1
- package/dist/index.module.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/plugins/withCursors.d.ts +12 -10
- package/dist/plugins/withCursors.d.ts.map +1 -1
- package/dist/plugins/withYHistory.d.ts +8 -3
- package/dist/plugins/withYHistory.d.ts.map +1 -1
- package/dist/plugins/withYjs.d.ts +12 -9
- package/dist/plugins/withYjs.d.ts.map +1 -1
- package/dist/utils/delta.d.ts +0 -1
- package/dist/utils/delta.d.ts.map +1 -1
- package/dist/utils/delta.js +1 -11
- package/dist/utils/delta.js.map +1 -1
- package/dist/utils/position.d.ts.map +1 -1
- package/dist/utils/slate.d.ts +2 -2
- package/dist/utils/slate.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/applyToSlate/index.ts +3 -3
- package/src/applyToSlate/textEvent.ts +9 -12
- package/src/applyToYjs/node/insertNode.ts +5 -4
- package/src/applyToYjs/node/mergeNode.ts +2 -9
- package/src/applyToYjs/node/moveNode.ts +1 -9
- package/src/applyToYjs/node/removeNode.ts +0 -2
- package/src/applyToYjs/node/setNode.ts +0 -3
- package/src/applyToYjs/node/splitNode.ts +3 -11
- package/src/applyToYjs/text/insertText.ts +0 -3
- package/src/applyToYjs/text/removeText.ts +0 -2
- package/src/index.ts +15 -7
- package/src/plugins/withCursors.ts +42 -48
- package/src/plugins/withYHistory.ts +32 -11
- package/src/plugins/withYjs.ts +73 -43
- package/src/utils/delta.ts +1 -14
- package/src/utils/position.ts +2 -2
- package/src/utils/slate.ts +3 -3
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -8,21 +8,27 @@ import {
|
|
|
8
8
|
import { YjsEditor } from './withYjs';
|
|
9
9
|
|
|
10
10
|
const LAST_SELECTION: WeakMap<Editor, RelativeRange | null> = new WeakMap();
|
|
11
|
+
const DEFAULT_WITHOUT_SAVING_ORIGIN = Symbol(
|
|
12
|
+
'slate-yjs-history-without-saving'
|
|
13
|
+
);
|
|
11
14
|
|
|
12
15
|
export type YHistoryEditor = YjsEditor & {
|
|
13
16
|
undoManager: Y.UndoManager;
|
|
14
17
|
|
|
18
|
+
withoutSavingOrigin: unknown;
|
|
19
|
+
|
|
15
20
|
undo: () => void;
|
|
16
21
|
redo: () => void;
|
|
17
22
|
};
|
|
18
23
|
|
|
19
24
|
export const YHistoryEditor = {
|
|
20
|
-
isYHistoryEditor(
|
|
25
|
+
isYHistoryEditor(value: unknown): value is YHistoryEditor {
|
|
21
26
|
return (
|
|
22
|
-
YjsEditor.isYjsEditor(
|
|
23
|
-
(
|
|
24
|
-
typeof (
|
|
25
|
-
typeof (
|
|
27
|
+
YjsEditor.isYjsEditor(value) &&
|
|
28
|
+
(value as YHistoryEditor).undoManager instanceof Y.UndoManager &&
|
|
29
|
+
typeof (value as YHistoryEditor).undo === 'function' &&
|
|
30
|
+
typeof (value as YHistoryEditor).redo === 'function' &&
|
|
31
|
+
'withoutSavingOrigin' in value
|
|
26
32
|
);
|
|
27
33
|
},
|
|
28
34
|
|
|
@@ -33,15 +39,26 @@ export const YHistoryEditor = {
|
|
|
33
39
|
canRedo(editor: YHistoryEditor) {
|
|
34
40
|
return editor.undoManager.redoStack.length > 0;
|
|
35
41
|
},
|
|
42
|
+
|
|
43
|
+
isSaving(editor: YHistoryEditor): boolean {
|
|
44
|
+
return editor.undoManager.trackedOrigins.has(YjsEditor.origin(editor));
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
withoutSaving(editor: YHistoryEditor, fn: () => void) {
|
|
48
|
+
YjsEditor.withOrigin(editor, editor.withoutSavingOrigin, fn);
|
|
49
|
+
},
|
|
36
50
|
};
|
|
37
51
|
|
|
38
52
|
export type WithYHistoryOptions = NonNullable<
|
|
39
53
|
ConstructorParameters<typeof Y.UndoManager>[1]
|
|
40
|
-
|
|
54
|
+
> & {
|
|
55
|
+
withoutSavingOrigin?: unknown;
|
|
56
|
+
};
|
|
41
57
|
|
|
42
58
|
export function withYHistory<T extends YjsEditor>(
|
|
43
59
|
editor: T,
|
|
44
60
|
{
|
|
61
|
+
withoutSavingOrigin = DEFAULT_WITHOUT_SAVING_ORIGIN,
|
|
45
62
|
trackedOrigins = new Set([editor.localOrigin]),
|
|
46
63
|
...options
|
|
47
64
|
}: WithYHistoryOptions = {}
|
|
@@ -53,7 +70,10 @@ export function withYHistory<T extends YjsEditor>(
|
|
|
53
70
|
...options,
|
|
54
71
|
});
|
|
55
72
|
|
|
56
|
-
|
|
73
|
+
e.undoManager = undoManager;
|
|
74
|
+
e.withoutSavingOrigin = withoutSavingOrigin;
|
|
75
|
+
|
|
76
|
+
const { onChange, isLocalOrigin } = e;
|
|
57
77
|
e.onChange = () => {
|
|
58
78
|
onChange();
|
|
59
79
|
|
|
@@ -63,6 +83,9 @@ export function withYHistory<T extends YjsEditor>(
|
|
|
63
83
|
);
|
|
64
84
|
};
|
|
65
85
|
|
|
86
|
+
e.isLocalOrigin = (origin) =>
|
|
87
|
+
origin === e.withoutSavingOrigin || isLocalOrigin(origin);
|
|
88
|
+
|
|
66
89
|
undoManager.on(
|
|
67
90
|
'stack-item-added',
|
|
68
91
|
({ stackItem }: { stackItem: HistoryStackItem; type: 'redo' | 'undo' }) => {
|
|
@@ -123,15 +146,13 @@ export function withYHistory<T extends YjsEditor>(
|
|
|
123
146
|
}
|
|
124
147
|
);
|
|
125
148
|
|
|
126
|
-
e.undoManager = undoManager;
|
|
127
|
-
|
|
128
149
|
e.undo = () => {
|
|
129
|
-
YjsEditor.
|
|
150
|
+
YjsEditor.flushLocalChanges(e);
|
|
130
151
|
e.undoManager.undo();
|
|
131
152
|
};
|
|
132
153
|
|
|
133
154
|
e.redo = () => {
|
|
134
|
-
YjsEditor.
|
|
155
|
+
YjsEditor.flushLocalChanges(e);
|
|
135
156
|
e.undoManager.redo();
|
|
136
157
|
};
|
|
137
158
|
|
package/src/plugins/withYjs.ts
CHANGED
|
@@ -13,7 +13,11 @@ import {
|
|
|
13
13
|
} from '../utils/position';
|
|
14
14
|
import { assertDocumentAttachment } from '../utils/yjs';
|
|
15
15
|
|
|
16
|
-
type LocalChange = {
|
|
16
|
+
type LocalChange = {
|
|
17
|
+
op: Operation;
|
|
18
|
+
doc: Descendant[];
|
|
19
|
+
origin: unknown;
|
|
20
|
+
};
|
|
17
21
|
|
|
18
22
|
const DEFAULT_LOCAL_ORIGIN = Symbol('slate-yjs-operation');
|
|
19
23
|
const DEFAULT_POSITION_STORAGE_ORIGIN = Symbol('slate-yjs-position-storage');
|
|
@@ -29,8 +33,11 @@ export type YjsEditor = BaseEditor & {
|
|
|
29
33
|
positionStorageOrigin: unknown;
|
|
30
34
|
|
|
31
35
|
applyRemoteEvents: (events: Y.YEvent[], origin: unknown) => void;
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
|
|
37
|
+
storeLocalChange: (op: Operation) => void;
|
|
38
|
+
flushLocalChanges: () => void;
|
|
39
|
+
|
|
40
|
+
isLocalOrigin: (origin: unknown) => boolean;
|
|
34
41
|
|
|
35
42
|
connect: () => void;
|
|
36
43
|
disconnect: () => void;
|
|
@@ -41,11 +48,12 @@ export const YjsEditor = {
|
|
|
41
48
|
return (
|
|
42
49
|
Editor.isEditor(value) &&
|
|
43
50
|
(value as YjsEditor).sharedRoot instanceof Y.XmlText &&
|
|
44
|
-
|
|
45
|
-
|
|
51
|
+
'localOrigin' in value &&
|
|
52
|
+
'positionStorageOrigin' in value &&
|
|
46
53
|
typeof (value as YjsEditor).applyRemoteEvents === 'function' &&
|
|
47
|
-
typeof (value as YjsEditor).
|
|
48
|
-
typeof (value as YjsEditor).
|
|
54
|
+
typeof (value as YjsEditor).storeLocalChange === 'function' &&
|
|
55
|
+
typeof (value as YjsEditor).flushLocalChanges === 'function' &&
|
|
56
|
+
typeof (value as YjsEditor).isLocalOrigin === 'function' &&
|
|
49
57
|
typeof (value as YjsEditor).connect === 'function' &&
|
|
50
58
|
typeof (value as YjsEditor).disconnect === 'function'
|
|
51
59
|
);
|
|
@@ -55,7 +63,7 @@ export const YjsEditor = {
|
|
|
55
63
|
return LOCAL_CHANGES.get(editor) ?? [];
|
|
56
64
|
},
|
|
57
65
|
|
|
58
|
-
|
|
66
|
+
applyRemoteEvents(
|
|
59
67
|
editor: YjsEditor,
|
|
60
68
|
events: Y.YEvent[],
|
|
61
69
|
origin: unknown
|
|
@@ -63,12 +71,12 @@ export const YjsEditor = {
|
|
|
63
71
|
editor.applyRemoteEvents(events, origin);
|
|
64
72
|
},
|
|
65
73
|
|
|
66
|
-
|
|
67
|
-
editor.
|
|
74
|
+
storeLocalChange(editor: YjsEditor, op: Operation): void {
|
|
75
|
+
editor.storeLocalChange(op);
|
|
68
76
|
},
|
|
69
77
|
|
|
70
|
-
|
|
71
|
-
editor.
|
|
78
|
+
flushLocalChanges(editor: YjsEditor): void {
|
|
79
|
+
editor.flushLocalChanges();
|
|
72
80
|
},
|
|
73
81
|
|
|
74
82
|
connected(editor: YjsEditor): boolean {
|
|
@@ -83,21 +91,19 @@ export const YjsEditor = {
|
|
|
83
91
|
editor.disconnect();
|
|
84
92
|
},
|
|
85
93
|
|
|
86
|
-
|
|
87
|
-
return
|
|
94
|
+
isLocal(editor: YjsEditor): boolean {
|
|
95
|
+
return editor.isLocalOrigin(YjsEditor.origin(editor));
|
|
88
96
|
},
|
|
89
97
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
98
|
+
origin(editor: YjsEditor): unknown {
|
|
99
|
+
return ORIGIN.get(editor) ?? editor.localOrigin;
|
|
100
|
+
},
|
|
93
101
|
|
|
102
|
+
withOrigin(editor: YjsEditor, origin: unknown, fn: () => void): void {
|
|
103
|
+
const prev = YjsEditor.origin(editor);
|
|
104
|
+
ORIGIN.set(editor, origin);
|
|
94
105
|
fn();
|
|
95
|
-
|
|
96
|
-
if (prev === undefined) {
|
|
97
|
-
ORIGIN.delete(editor);
|
|
98
|
-
} else {
|
|
99
|
-
ORIGIN.set(editor, prev);
|
|
100
|
-
}
|
|
106
|
+
ORIGIN.set(editor, prev);
|
|
101
107
|
},
|
|
102
108
|
|
|
103
109
|
storePosition(editor: YjsEditor, key: string, point: Point): void {
|
|
@@ -120,16 +126,16 @@ export const YjsEditor = {
|
|
|
120
126
|
}, locationStorageOrigin);
|
|
121
127
|
},
|
|
122
128
|
|
|
123
|
-
position(editor: YjsEditor, key: string): Point | null {
|
|
129
|
+
position(editor: YjsEditor, key: string): Point | null | undefined {
|
|
124
130
|
const position = getStoredPosition(editor.sharedRoot, key);
|
|
125
131
|
if (!position) {
|
|
126
|
-
return
|
|
132
|
+
return undefined;
|
|
127
133
|
}
|
|
128
134
|
|
|
129
135
|
return relativePositionToSlatePoint(editor.sharedRoot, editor, position);
|
|
130
136
|
},
|
|
131
137
|
|
|
132
|
-
|
|
138
|
+
storedPositionsRelative(
|
|
133
139
|
editor: YjsEditor
|
|
134
140
|
): Record<string, Y.RelativePosition> {
|
|
135
141
|
return getStoredPositions(editor.sharedRoot);
|
|
@@ -163,20 +169,24 @@ export function withYjs<T extends Editor>(
|
|
|
163
169
|
e.positionStorageOrigin =
|
|
164
170
|
positionStorageOrigin ?? DEFAULT_POSITION_STORAGE_ORIGIN;
|
|
165
171
|
|
|
166
|
-
e.applyRemoteEvents = (events,
|
|
172
|
+
e.applyRemoteEvents = (events, origin) => {
|
|
173
|
+
YjsEditor.flushLocalChanges(e);
|
|
174
|
+
|
|
167
175
|
Editor.withoutNormalizing(e, () => {
|
|
168
|
-
YjsEditor.
|
|
176
|
+
YjsEditor.withOrigin(e, origin, () => {
|
|
169
177
|
applyYjsEvents(e.sharedRoot, e, events);
|
|
170
178
|
});
|
|
171
179
|
});
|
|
172
180
|
};
|
|
173
181
|
|
|
182
|
+
e.isLocalOrigin = (origin) => origin === e.localOrigin;
|
|
183
|
+
|
|
174
184
|
const handleYEvents = (events: Y.YEvent[], transaction: Y.Transaction) => {
|
|
175
|
-
if (transaction.origin
|
|
185
|
+
if (e.isLocalOrigin(transaction.origin)) {
|
|
176
186
|
return;
|
|
177
187
|
}
|
|
178
188
|
|
|
179
|
-
YjsEditor.
|
|
189
|
+
YjsEditor.applyRemoteEvents(e, events, transaction.origin);
|
|
180
190
|
};
|
|
181
191
|
|
|
182
192
|
let autoConnectTimeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
@@ -200,40 +210,60 @@ export function withYjs<T extends Editor>(
|
|
|
200
210
|
clearTimeout(autoConnectTimeoutId);
|
|
201
211
|
}
|
|
202
212
|
|
|
213
|
+
YjsEditor.flushLocalChanges(e);
|
|
203
214
|
sharedRoot.unobserveDeep(handleYEvents);
|
|
204
215
|
CONNECTED.delete(e);
|
|
205
216
|
};
|
|
206
217
|
|
|
207
|
-
e.
|
|
218
|
+
e.storeLocalChange = (op) => {
|
|
208
219
|
LOCAL_CHANGES.set(e, [
|
|
209
220
|
...YjsEditor.localChanges(e),
|
|
210
|
-
{ op, doc: editor.children },
|
|
221
|
+
{ op, doc: editor.children, origin: YjsEditor.origin(e) },
|
|
211
222
|
]);
|
|
212
223
|
};
|
|
213
224
|
|
|
214
|
-
e.
|
|
215
|
-
|
|
225
|
+
e.flushLocalChanges = () => {
|
|
226
|
+
assertDocumentAttachment(e.sharedRoot);
|
|
227
|
+
const localChanges = YjsEditor.localChanges(e);
|
|
216
228
|
LOCAL_CHANGES.delete(e);
|
|
217
229
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
230
|
+
// Group local changes by origin so we can apply them in the correct order
|
|
231
|
+
// with the correct origin with a minimal amount of transactions.
|
|
232
|
+
const txGroups: LocalChange[][] = [];
|
|
233
|
+
localChanges.forEach((change) => {
|
|
234
|
+
const currentGroup = txGroups[txGroups.length - 1];
|
|
235
|
+
if (currentGroup && currentGroup[0].origin === change.origin) {
|
|
236
|
+
return currentGroup.push(change);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
txGroups.push([change]);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
txGroups.forEach((txGroup) => {
|
|
243
|
+
assertDocumentAttachment(e.sharedRoot);
|
|
244
|
+
e.sharedRoot.doc.transact(() => {
|
|
245
|
+
txGroup.forEach((change) => {
|
|
246
|
+
assertDocumentAttachment(e.sharedRoot);
|
|
247
|
+
applySlateOp(e.sharedRoot, { children: change.doc }, change.op);
|
|
248
|
+
});
|
|
249
|
+
}, txGroup[0].origin);
|
|
250
|
+
});
|
|
224
251
|
};
|
|
225
252
|
|
|
226
253
|
const { apply, onChange } = e;
|
|
227
254
|
e.apply = (op) => {
|
|
228
|
-
if (YjsEditor.
|
|
229
|
-
YjsEditor.
|
|
255
|
+
if (YjsEditor.connected(e) && YjsEditor.isLocal(e)) {
|
|
256
|
+
YjsEditor.storeLocalChange(e, op);
|
|
230
257
|
}
|
|
231
258
|
|
|
232
259
|
apply(op);
|
|
233
260
|
};
|
|
234
261
|
|
|
235
262
|
e.onChange = () => {
|
|
236
|
-
YjsEditor.
|
|
263
|
+
if (YjsEditor.connected(e)) {
|
|
264
|
+
YjsEditor.flushLocalChanges(e);
|
|
265
|
+
}
|
|
266
|
+
|
|
237
267
|
onChange();
|
|
238
268
|
};
|
|
239
269
|
|
package/src/utils/delta.ts
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
1
|
import * as Y from 'yjs';
|
|
2
2
|
import { DeltaInsert, InsertDelta } from '../model/types';
|
|
3
3
|
|
|
4
|
-
const INSERT_DELTA_CACHE: WeakMap<Y.XmlText, InsertDelta> = new WeakMap();
|
|
5
|
-
|
|
6
|
-
export function invalidateDeltaCacheForYText(yText: Y.XmlText) {
|
|
7
|
-
INSERT_DELTA_CACHE.delete(yText);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
4
|
export function yTextToInsertDelta(yText: Y.XmlText): InsertDelta {
|
|
11
|
-
|
|
12
|
-
if (cached) {
|
|
13
|
-
return cached;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const delta = yText.toDelta() as InsertDelta;
|
|
17
|
-
INSERT_DELTA_CACHE.set(yText, delta);
|
|
18
|
-
return delta;
|
|
5
|
+
return yText.toDelta() as InsertDelta;
|
|
19
6
|
}
|
|
20
7
|
|
|
21
8
|
export function getInsertLength({ insert }: DeltaInsert): number {
|
package/src/utils/position.ts
CHANGED
|
@@ -11,7 +11,7 @@ export function slatePointToRelativePosition(
|
|
|
11
11
|
sharedRoot: Y.XmlText,
|
|
12
12
|
slateRoot: Node,
|
|
13
13
|
point: BasePoint
|
|
14
|
-
) {
|
|
14
|
+
): Y.RelativePosition {
|
|
15
15
|
const { yTarget, yParent, textRange } = getYTarget(
|
|
16
16
|
sharedRoot,
|
|
17
17
|
slateRoot,
|
|
@@ -65,7 +65,7 @@ export function relativePositionToSlatePoint(
|
|
|
65
65
|
sharedRoot: Y.XmlText,
|
|
66
66
|
slateRoot: Node,
|
|
67
67
|
pos: Y.RelativePosition
|
|
68
|
-
) {
|
|
68
|
+
): BasePoint | null {
|
|
69
69
|
if (!sharedRoot.doc) {
|
|
70
70
|
throw new Error("sharedRoot isn't attach to a yDoc");
|
|
71
71
|
}
|
package/src/utils/slate.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Descendant, Text } from 'slate';
|
|
1
|
+
import { BaseText, Descendant, Text } from 'slate';
|
|
2
2
|
import { omit } from './object';
|
|
3
3
|
|
|
4
4
|
export function getProperties<TNode extends Descendant>(
|
|
5
5
|
node: TNode
|
|
6
|
-
): Omit<TNode, TNode extends
|
|
6
|
+
): Omit<TNode, TNode extends BaseText ? 'text' : 'children'> {
|
|
7
7
|
return omit(
|
|
8
8
|
node,
|
|
9
9
|
(Text.isText(node) ? 'text' : 'children') as keyof TNode
|
|
10
|
-
) as
|
|
10
|
+
) as Omit<TNode, TNode extends BaseText ? 'text' : 'children'>;
|
|
11
11
|
}
|