@slate-yjs/core 0.0.1
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/index.d.ts +20 -0
- package/dist/applyToSlate/index.d.ts.map +1 -0
- package/dist/applyToSlate/index.js +32 -0
- package/dist/applyToSlate/index.js.map +1 -0
- package/dist/applyToSlate/textEvent.d.ts +4 -0
- package/dist/applyToSlate/textEvent.d.ts.map +1 -0
- package/dist/applyToSlate/textEvent.js +169 -0
- package/dist/applyToSlate/textEvent.js.map +1 -0
- package/dist/applyToYjs/index.d.ts +4 -0
- package/dist/applyToYjs/index.d.ts.map +1 -0
- package/dist/applyToYjs/index.js +17 -0
- package/dist/applyToYjs/index.js.map +1 -0
- package/dist/applyToYjs/node/index.d.ts +4 -0
- package/dist/applyToYjs/node/index.d.ts.map +1 -0
- package/dist/applyToYjs/node/index.js +15 -0
- package/dist/applyToYjs/node/index.js.map +1 -0
- package/dist/applyToYjs/node/insertNode.d.ts +4 -0
- package/dist/applyToYjs/node/insertNode.d.ts.map +1 -0
- package/dist/applyToYjs/node/insertNode.js +15 -0
- package/dist/applyToYjs/node/insertNode.js.map +1 -0
- package/dist/applyToYjs/node/mergeNode.d.ts +4 -0
- package/dist/applyToYjs/node/mergeNode.d.ts.map +1 -0
- package/dist/applyToYjs/node/mergeNode.js +35 -0
- package/dist/applyToYjs/node/mergeNode.js.map +1 -0
- package/dist/applyToYjs/node/moveNode.d.ts +4 -0
- package/dist/applyToYjs/node/moveNode.d.ts.map +1 -0
- package/dist/applyToYjs/node/moveNode.js +30 -0
- package/dist/applyToYjs/node/moveNode.js.map +1 -0
- package/dist/applyToYjs/node/removeNode.d.ts +4 -0
- package/dist/applyToYjs/node/removeNode.d.ts.map +1 -0
- package/dist/applyToYjs/node/removeNode.js +8 -0
- package/dist/applyToYjs/node/removeNode.js.map +1 -0
- package/dist/applyToYjs/node/setNode.d.ts +4 -0
- package/dist/applyToYjs/node/setNode.d.ts.map +1 -0
- package/dist/applyToYjs/node/setNode.js +23 -0
- package/dist/applyToYjs/node/setNode.js.map +1 -0
- package/dist/applyToYjs/node/splitNode.d.ts +4 -0
- package/dist/applyToYjs/node/splitNode.d.ts.map +1 -0
- package/dist/applyToYjs/node/splitNode.js +53 -0
- package/dist/applyToYjs/node/splitNode.js.map +1 -0
- package/dist/applyToYjs/text/index.d.ts +4 -0
- package/dist/applyToYjs/text/index.d.ts.map +1 -0
- package/dist/applyToYjs/text/index.js +7 -0
- package/dist/applyToYjs/text/index.js.map +1 -0
- package/dist/applyToYjs/text/insertText.d.ts +4 -0
- package/dist/applyToYjs/text/insertText.d.ts.map +1 -0
- package/dist/applyToYjs/text/insertText.js +14 -0
- package/dist/applyToYjs/text/insertText.js.map +1 -0
- package/dist/applyToYjs/text/removeText.d.ts +4 -0
- package/dist/applyToYjs/text/removeText.d.ts.map +1 -0
- package/dist/applyToYjs/text/removeText.js +8 -0
- package/dist/applyToYjs/text/removeText.js.map +1 -0
- package/dist/applyToYjs/types.d.ts +9 -0
- package/dist/applyToYjs/types.d.ts.map +1 -0
- package/dist/applyToYjs/types.js +2 -0
- package/dist/applyToYjs/types.js.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/index.modern.js +2 -0
- package/dist/index.modern.js.map +1 -0
- package/dist/index.module.js +2 -0
- package/dist/index.module.js.map +1 -0
- package/dist/index.umd.js +2 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/model/types.d.ts +38 -0
- package/dist/model/types.d.ts.map +1 -0
- package/dist/model/types.js +2 -0
- package/dist/model/types.js.map +1 -0
- package/dist/plugins/index.d.ts +4 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +4 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/withCursors.d.ts +37 -0
- package/dist/plugins/withCursors.d.ts.map +1 -0
- package/dist/plugins/withCursors.js +145 -0
- package/dist/plugins/withCursors.js.map +1 -0
- package/dist/plugins/withYHistory.d.ts +15 -0
- package/dist/plugins/withYHistory.d.ts.map +1 -0
- package/dist/plugins/withYHistory.js +66 -0
- package/dist/plugins/withYHistory.js.map +1 -0
- package/dist/plugins/withYjs.d.ts +40 -0
- package/dist/plugins/withYjs.d.ts.map +1 -0
- package/dist/plugins/withYjs.js +155 -0
- package/dist/plugins/withYjs.js.map +1 -0
- package/dist/utils/clone.d.ts +5 -0
- package/dist/utils/clone.d.ts.map +1 -0
- package/dist/utils/clone.js +23 -0
- package/dist/utils/clone.js.map +1 -0
- package/dist/utils/convert.d.ts +8 -0
- package/dist/utils/convert.d.ts.map +1 -0
- package/dist/utils/convert.js +35 -0
- package/dist/utils/convert.js.map +1 -0
- package/dist/utils/delta.d.ts +8 -0
- package/dist/utils/delta.d.ts.map +1 -0
- package/dist/utils/delta.js +52 -0
- package/dist/utils/delta.js.map +1 -0
- package/dist/utils/location.d.ts +12 -0
- package/dist/utils/location.d.ts.map +1 -0
- package/dist/utils/location.js +111 -0
- package/dist/utils/location.js.map +1 -0
- package/dist/utils/object.d.ts +7 -0
- package/dist/utils/object.d.ts.map +1 -0
- package/dist/utils/object.js +27 -0
- package/dist/utils/object.js.map +1 -0
- package/dist/utils/position.d.ts +20 -0
- package/dist/utils/position.d.ts.map +1 -0
- package/dist/utils/position.js +140 -0
- package/dist/utils/position.js.map +1 -0
- package/dist/utils/slate.d.ts +3 -0
- package/dist/utils/slate.d.ts.map +1 -0
- package/dist/utils/slate.js +6 -0
- package/dist/utils/slate.js.map +1 -0
- package/dist/utils/yjs.d.ts +5 -0
- package/dist/utils/yjs.d.ts.map +1 -0
- package/dist/utils/yjs.js +7 -0
- package/dist/utils/yjs.js.map +1 -0
- package/package.json +46 -0
- package/src/applyToSlate/index.ts +44 -0
- package/src/applyToSlate/textEvent.ts +253 -0
- package/src/applyToYjs/index.ts +28 -0
- package/src/applyToYjs/node/index.ts +17 -0
- package/src/applyToYjs/node/insertNode.ts +22 -0
- package/src/applyToYjs/node/mergeNode.ts +80 -0
- package/src/applyToYjs/node/moveNode.ts +65 -0
- package/src/applyToYjs/node/removeNode.ts +18 -0
- package/src/applyToYjs/node/setNode.ts +45 -0
- package/src/applyToYjs/node/splitNode.ts +106 -0
- package/src/applyToYjs/text/index.ts +9 -0
- package/src/applyToYjs/text/insertText.ts +30 -0
- package/src/applyToYjs/text/removeText.ts +18 -0
- package/src/applyToYjs/types.ts +12 -0
- package/src/index.ts +39 -0
- package/src/model/types.ts +50 -0
- package/src/plugins/index.ts +3 -0
- package/src/plugins/withCursors.ts +275 -0
- package/src/plugins/withYHistory.ts +139 -0
- package/src/plugins/withYjs.ts +241 -0
- package/src/utils/clone.ts +29 -0
- package/src/utils/convert.ts +48 -0
- package/src/utils/delta.ts +75 -0
- package/src/utils/location.ts +157 -0
- package/src/utils/object.ts +48 -0
- package/src/utils/position.ts +293 -0
- package/src/utils/slate.ts +11 -0
- package/src/utils/yjs.ts +10 -0
- package/test/collaboration.test.ts +395 -0
- package/test/concurrent.test.ts +233 -0
- package/test/testEditor.ts +134 -0
- package/test/types.d.ts +8 -0
- package/test/utils.ts +64 -0
- package/tsconfig.json +10 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { BaseRange, Editor, Range } from 'slate';
|
|
2
|
+
import { Awareness } from 'y-protocols/awareness';
|
|
3
|
+
import * as Y from 'yjs';
|
|
4
|
+
import {
|
|
5
|
+
relativeRangeToSlateRange,
|
|
6
|
+
slateRangeToRelativeRange,
|
|
7
|
+
} from '../utils/position';
|
|
8
|
+
import { YjsEditor } from './withYjs';
|
|
9
|
+
|
|
10
|
+
export type CursorStateChangeEvent = {
|
|
11
|
+
added: number[];
|
|
12
|
+
updated: number[];
|
|
13
|
+
removed: number[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type RemoteCursorChangeEventListener = (
|
|
17
|
+
event: CursorStateChangeEvent
|
|
18
|
+
) => void;
|
|
19
|
+
|
|
20
|
+
const CURSOR_CHANGE_EVENT_LISTENERS: WeakMap<
|
|
21
|
+
Editor,
|
|
22
|
+
Set<RemoteCursorChangeEventListener>
|
|
23
|
+
> = new WeakMap();
|
|
24
|
+
|
|
25
|
+
export type CursorState<
|
|
26
|
+
TCursorData extends Record<string, unknown> = Record<string, unknown>
|
|
27
|
+
> = {
|
|
28
|
+
selection: BaseRange | null;
|
|
29
|
+
data: TCursorData;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type CursorEditor<
|
|
33
|
+
TCursorData extends Record<string, unknown> = Record<string, unknown>
|
|
34
|
+
> = YjsEditor & {
|
|
35
|
+
awareness: Awareness;
|
|
36
|
+
|
|
37
|
+
cursorDataField: string;
|
|
38
|
+
cursorStateField: string;
|
|
39
|
+
|
|
40
|
+
sendCursorPosition: (range: Range | null) => void;
|
|
41
|
+
sendCursorData: (data: TCursorData) => void;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const CursorEditor = {
|
|
45
|
+
isCursorEditor(v: unknown): v is CursorEditor {
|
|
46
|
+
return true;
|
|
47
|
+
return (
|
|
48
|
+
YjsEditor.isYjsEditor(v) &&
|
|
49
|
+
(v as CursorEditor).awareness instanceof Awareness &&
|
|
50
|
+
typeof (v as CursorEditor).cursorDataField === 'string' &&
|
|
51
|
+
typeof (v as CursorEditor).cursorStateField === 'string' &&
|
|
52
|
+
typeof (v as CursorEditor).sendCursorPosition === 'function' &&
|
|
53
|
+
typeof (v as CursorEditor).sendCursorData === 'function'
|
|
54
|
+
);
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
sendCursorPosition<TCursorData extends Record<string, unknown>>(
|
|
58
|
+
editor: CursorEditor<TCursorData>,
|
|
59
|
+
range: Range | null = editor.selection
|
|
60
|
+
) {
|
|
61
|
+
editor.sendCursorPosition(range);
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
sendCursorData<TCursorData extends Record<string, unknown>>(
|
|
65
|
+
editor: CursorEditor<TCursorData>,
|
|
66
|
+
data: TCursorData
|
|
67
|
+
) {
|
|
68
|
+
editor.sendCursorData(data);
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
on(
|
|
72
|
+
editor: CursorEditor,
|
|
73
|
+
event: 'change',
|
|
74
|
+
listener: RemoteCursorChangeEventListener
|
|
75
|
+
) {
|
|
76
|
+
if (event !== 'change') {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const listeners = CURSOR_CHANGE_EVENT_LISTENERS.get(editor) ?? new Set();
|
|
81
|
+
listeners.add(listener);
|
|
82
|
+
CURSOR_CHANGE_EVENT_LISTENERS.set(editor, listeners);
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
off(
|
|
86
|
+
editor: CursorEditor,
|
|
87
|
+
event: 'change',
|
|
88
|
+
listener: RemoteCursorChangeEventListener
|
|
89
|
+
) {
|
|
90
|
+
if (event !== 'change') {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const listeners = CURSOR_CHANGE_EVENT_LISTENERS.get(editor);
|
|
95
|
+
if (listeners) {
|
|
96
|
+
listeners.delete(listener);
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
remoteCursor<TCursorData extends Record<string, unknown>>(
|
|
101
|
+
editor: CursorEditor,
|
|
102
|
+
clientId: number
|
|
103
|
+
): CursorState<TCursorData> | null {
|
|
104
|
+
if (
|
|
105
|
+
clientId === editor.awareness.clientID ||
|
|
106
|
+
!YjsEditor.connected(editor)
|
|
107
|
+
) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const state = editor.awareness.getStates().get(clientId);
|
|
112
|
+
if (!state) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const relativeSelection = state[editor.cursorStateField];
|
|
117
|
+
const selection = relativeSelection
|
|
118
|
+
? relativeRangeToSlateRange(editor.sharedRoot, editor, relativeSelection)
|
|
119
|
+
: null;
|
|
120
|
+
|
|
121
|
+
return { selection, data: state[editor.cursorDataField] };
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
remoteCursors<TCursorData extends Record<string, unknown>>(
|
|
125
|
+
editor: CursorEditor
|
|
126
|
+
): Record<string, CursorState<TCursorData>> {
|
|
127
|
+
if (!YjsEditor.connected(editor)) {
|
|
128
|
+
return {};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return Object.fromEntries(
|
|
132
|
+
Array.from(editor.awareness.getStates().entries(), ([id, state]) => {
|
|
133
|
+
// Ignore own state
|
|
134
|
+
if (id === editor.awareness.clientID) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const relativeSelection = state[editor.cursorStateField];
|
|
139
|
+
const selection = relativeSelection
|
|
140
|
+
? relativeRangeToSlateRange(
|
|
141
|
+
editor.sharedRoot,
|
|
142
|
+
editor,
|
|
143
|
+
relativeSelection
|
|
144
|
+
)
|
|
145
|
+
: null;
|
|
146
|
+
|
|
147
|
+
return [id, { selection, data: state[editor.cursorDataField] }];
|
|
148
|
+
}).filter(Array.isArray)
|
|
149
|
+
);
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export type WithCursorsOptions<
|
|
154
|
+
TCursorData extends Record<string, unknown> = Record<string, unknown>
|
|
155
|
+
> = {
|
|
156
|
+
// Local state field used to store the user selection
|
|
157
|
+
cursorStateField?: string;
|
|
158
|
+
|
|
159
|
+
// Local state field used to store data attached to the local client
|
|
160
|
+
cursorDataField?: string;
|
|
161
|
+
|
|
162
|
+
data?: TCursorData;
|
|
163
|
+
autoSend?: boolean;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export function withCursors<
|
|
167
|
+
TCursorData extends Record<string, unknown>,
|
|
168
|
+
TEditor extends YjsEditor
|
|
169
|
+
>(
|
|
170
|
+
editor: TEditor,
|
|
171
|
+
awareness: Awareness,
|
|
172
|
+
{
|
|
173
|
+
cursorStateField = 'selection',
|
|
174
|
+
cursorDataField = 'data',
|
|
175
|
+
autoSend = true,
|
|
176
|
+
data,
|
|
177
|
+
}: WithCursorsOptions<TCursorData> = {}
|
|
178
|
+
): TEditor & CursorEditor<TCursorData> {
|
|
179
|
+
const e = editor as TEditor & CursorEditor<TCursorData>;
|
|
180
|
+
|
|
181
|
+
e.awareness = awareness;
|
|
182
|
+
e.cursorDataField = cursorDataField;
|
|
183
|
+
e.cursorStateField = cursorStateField;
|
|
184
|
+
|
|
185
|
+
e.sendCursorData = (cursorData: TCursorData) => {
|
|
186
|
+
e.awareness.setLocalStateField(e.cursorDataField, cursorData);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
e.sendCursorPosition = (range) => {
|
|
190
|
+
const localState = awareness.getLocalState();
|
|
191
|
+
const currentRange = localState?.[cursorStateField];
|
|
192
|
+
|
|
193
|
+
if (!range) {
|
|
194
|
+
if (currentRange) {
|
|
195
|
+
awareness.setLocalStateField(e.cursorStateField, null);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const { anchor, focus } = slateRangeToRelativeRange(e.sharedRoot, e, range);
|
|
202
|
+
|
|
203
|
+
if (
|
|
204
|
+
!currentRange ||
|
|
205
|
+
!Y.compareRelativePositions(anchor, currentRange) ||
|
|
206
|
+
!Y.compareRelativePositions(focus, currentRange)
|
|
207
|
+
) {
|
|
208
|
+
awareness.setLocalStateField(e.cursorStateField, { anchor, focus });
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const awarenessChangeListener: RemoteCursorChangeEventListener = (yEvent) => {
|
|
213
|
+
const listeners = CURSOR_CHANGE_EVENT_LISTENERS.get(e);
|
|
214
|
+
if (!listeners) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const localId = e.awareness.clientID;
|
|
219
|
+
const event = {
|
|
220
|
+
added: yEvent.added.filter((id) => id !== localId),
|
|
221
|
+
removed: yEvent.removed.filter((id) => id !== localId),
|
|
222
|
+
updated: yEvent.updated.filter((id) => id !== localId),
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
if (
|
|
226
|
+
event.added.length > 0 ||
|
|
227
|
+
event.removed.length > 0 ||
|
|
228
|
+
event.updated.length > 0
|
|
229
|
+
) {
|
|
230
|
+
listeners.forEach((listener) => listener(event));
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const { connect, disconnect } = e;
|
|
235
|
+
e.connect = () => {
|
|
236
|
+
connect();
|
|
237
|
+
|
|
238
|
+
e.awareness.on('change', awarenessChangeListener);
|
|
239
|
+
|
|
240
|
+
awarenessChangeListener({
|
|
241
|
+
removed: [],
|
|
242
|
+
added: Array.from(awareness.getStates().keys()),
|
|
243
|
+
updated: [],
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
if (autoSend) {
|
|
247
|
+
if (data) {
|
|
248
|
+
CursorEditor.sendCursorData(e, data);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const { onChange } = e;
|
|
252
|
+
e.onChange = () => {
|
|
253
|
+
onChange();
|
|
254
|
+
|
|
255
|
+
if (YjsEditor.connected(e)) {
|
|
256
|
+
CursorEditor.sendCursorPosition(e);
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
e.disconnect = () => {
|
|
263
|
+
e.awareness.off('change', awarenessChangeListener);
|
|
264
|
+
|
|
265
|
+
awarenessChangeListener({
|
|
266
|
+
removed: Array.from(awareness.getStates().keys()),
|
|
267
|
+
added: [],
|
|
268
|
+
updated: [],
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
disconnect();
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
return e;
|
|
275
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Editor, Transforms } from 'slate';
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
import { HistoryStackItem, RelativeRange } from '../model/types';
|
|
4
|
+
import {
|
|
5
|
+
relativeRangeToSlateRange,
|
|
6
|
+
slateRangeToRelativeRange,
|
|
7
|
+
} from '../utils/position';
|
|
8
|
+
import { YjsEditor } from './withYjs';
|
|
9
|
+
|
|
10
|
+
const LAST_SELECTION: WeakMap<Editor, RelativeRange | null> = new WeakMap();
|
|
11
|
+
|
|
12
|
+
export type YHistoryEditor = YjsEditor & {
|
|
13
|
+
undoManager: Y.UndoManager;
|
|
14
|
+
|
|
15
|
+
undo: () => void;
|
|
16
|
+
redo: () => void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const YHistoryEditor = {
|
|
20
|
+
isYHistoryEditor(v: unknown): v is YHistoryEditor {
|
|
21
|
+
return (
|
|
22
|
+
YjsEditor.isYjsEditor(v) &&
|
|
23
|
+
(v as YHistoryEditor).undoManager instanceof Y.UndoManager &&
|
|
24
|
+
typeof (v as YHistoryEditor).undo === 'function' &&
|
|
25
|
+
typeof (v as YHistoryEditor).redo === 'function'
|
|
26
|
+
);
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
canUndo(editor: YHistoryEditor) {
|
|
30
|
+
return editor.undoManager.undoStack.length > 0;
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
canRedo(editor: YHistoryEditor) {
|
|
34
|
+
return editor.undoManager.redoStack.length > 0;
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type WithYHistoryOptions = NonNullable<
|
|
39
|
+
ConstructorParameters<typeof Y.UndoManager>[1]
|
|
40
|
+
>;
|
|
41
|
+
|
|
42
|
+
export function withYHistory<T extends YjsEditor>(
|
|
43
|
+
editor: T,
|
|
44
|
+
{
|
|
45
|
+
trackedOrigins = new Set([editor.localOrigin]),
|
|
46
|
+
...options
|
|
47
|
+
}: WithYHistoryOptions = {}
|
|
48
|
+
): T & YHistoryEditor {
|
|
49
|
+
const e = editor as T & YHistoryEditor;
|
|
50
|
+
|
|
51
|
+
const undoManager = new Y.UndoManager(e.sharedRoot, {
|
|
52
|
+
trackedOrigins,
|
|
53
|
+
...options,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const { onChange } = e;
|
|
57
|
+
e.onChange = () => {
|
|
58
|
+
onChange();
|
|
59
|
+
|
|
60
|
+
LAST_SELECTION.set(
|
|
61
|
+
e,
|
|
62
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
63
|
+
);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
undoManager.on(
|
|
67
|
+
'stack-item-added',
|
|
68
|
+
({ stackItem }: { stackItem: HistoryStackItem; type: 'redo' | 'undo' }) => {
|
|
69
|
+
stackItem.meta.set(
|
|
70
|
+
'selection',
|
|
71
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
if (!stackItem.meta.has('selectionBefore')) {
|
|
75
|
+
stackItem.meta.set('selectionBefore', LAST_SELECTION.get(e));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
undoManager.on(
|
|
81
|
+
'stack-item-popped',
|
|
82
|
+
({
|
|
83
|
+
stackItem,
|
|
84
|
+
type,
|
|
85
|
+
}: {
|
|
86
|
+
stackItem: HistoryStackItem;
|
|
87
|
+
type: 'redo' | 'undo';
|
|
88
|
+
}) => {
|
|
89
|
+
// TODO: Change once https://github.com/yjs/yjs/issues/353 is resolved
|
|
90
|
+
const inverseStack =
|
|
91
|
+
type === 'undo' ? undoManager.redoStack : undoManager.undoStack;
|
|
92
|
+
const inverseItem = inverseStack[inverseStack.length - 1];
|
|
93
|
+
if (inverseItem) {
|
|
94
|
+
inverseItem.meta.set(
|
|
95
|
+
'selection',
|
|
96
|
+
stackItem.meta.get('selectionBefore')
|
|
97
|
+
);
|
|
98
|
+
inverseItem.meta.set(
|
|
99
|
+
'selectionBefore',
|
|
100
|
+
stackItem.meta.get('selection')
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const relativeSelection = stackItem.meta.get(
|
|
105
|
+
'selectionBefore'
|
|
106
|
+
) as RelativeRange | null;
|
|
107
|
+
|
|
108
|
+
if (!relativeSelection) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const selection = relativeRangeToSlateRange(
|
|
113
|
+
e.sharedRoot,
|
|
114
|
+
e,
|
|
115
|
+
relativeSelection
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
if (!selection) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
Transforms.select(e, selection);
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
e.undoManager = undoManager;
|
|
127
|
+
|
|
128
|
+
e.undo = () => {
|
|
129
|
+
YjsEditor.flushLocalOperations(e);
|
|
130
|
+
e.undoManager.undo();
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
e.redo = () => {
|
|
134
|
+
YjsEditor.flushLocalOperations(e);
|
|
135
|
+
e.undoManager.redo();
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return e;
|
|
139
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { BaseEditor, Descendant, Editor, Operation, Point } from 'slate';
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
import { applyYjsEvents } from '../applyToSlate';
|
|
4
|
+
import { applySlateOp } from '../applyToYjs';
|
|
5
|
+
import { yTextToSlateElement } from '../utils/convert';
|
|
6
|
+
import {
|
|
7
|
+
getStoredPosition,
|
|
8
|
+
getStoredPositions,
|
|
9
|
+
relativePositionToSlatePoint,
|
|
10
|
+
removeStoredPosition,
|
|
11
|
+
setStoredPosition,
|
|
12
|
+
slatePointToRelativePosition,
|
|
13
|
+
} from '../utils/position';
|
|
14
|
+
import { assertDocumentAttachment } from '../utils/yjs';
|
|
15
|
+
|
|
16
|
+
type LocalChange = { op: Operation; doc: Descendant[] };
|
|
17
|
+
|
|
18
|
+
const DEFAULT_LOCAL_ORIGIN = Symbol('slate-yjs-operation');
|
|
19
|
+
const DEFAULT_POSITION_STORAGE_ORIGIN = Symbol('slate-yjs-position-storage');
|
|
20
|
+
|
|
21
|
+
const ORIGIN: WeakMap<Editor, unknown> = new WeakMap();
|
|
22
|
+
const LOCAL_CHANGES: WeakMap<Editor, LocalChange[]> = new WeakMap();
|
|
23
|
+
const CONNECTED: WeakSet<Editor> = new WeakSet();
|
|
24
|
+
|
|
25
|
+
export type YjsEditor = BaseEditor & {
|
|
26
|
+
sharedRoot: Y.XmlText;
|
|
27
|
+
|
|
28
|
+
localOrigin: unknown;
|
|
29
|
+
positionStorageOrigin: unknown;
|
|
30
|
+
|
|
31
|
+
applyRemoteEvents: (events: Y.YEvent[], origin: unknown) => void;
|
|
32
|
+
storeLocalOperation: (op: Operation) => void;
|
|
33
|
+
flushLocalOperations: () => void;
|
|
34
|
+
|
|
35
|
+
connect: () => void;
|
|
36
|
+
disconnect: () => void;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const YjsEditor = {
|
|
40
|
+
isYjsEditor(value: unknown): value is YjsEditor {
|
|
41
|
+
return (
|
|
42
|
+
Editor.isEditor(value) &&
|
|
43
|
+
(value as YjsEditor).sharedRoot instanceof Y.XmlText &&
|
|
44
|
+
(value as YjsEditor).localOrigin !== undefined &&
|
|
45
|
+
(value as YjsEditor).positionStorageOrigin !== undefined &&
|
|
46
|
+
typeof (value as YjsEditor).applyRemoteEvents === 'function' &&
|
|
47
|
+
typeof (value as YjsEditor).storeLocalOperation === 'function' &&
|
|
48
|
+
typeof (value as YjsEditor).flushLocalOperations === 'function' &&
|
|
49
|
+
typeof (value as YjsEditor).connect === 'function' &&
|
|
50
|
+
typeof (value as YjsEditor).disconnect === 'function'
|
|
51
|
+
);
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
localChanges(editor: YjsEditor): LocalChange[] {
|
|
55
|
+
return LOCAL_CHANGES.get(editor) ?? [];
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
applyRemoveEvents(
|
|
59
|
+
editor: YjsEditor,
|
|
60
|
+
events: Y.YEvent[],
|
|
61
|
+
origin: unknown
|
|
62
|
+
): void {
|
|
63
|
+
editor.applyRemoteEvents(events, origin);
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
storeLocalOperation(editor: YjsEditor, op: Operation): void {
|
|
67
|
+
editor.storeLocalOperation(op);
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
flushLocalOperations(editor: YjsEditor): void {
|
|
71
|
+
editor.flushLocalOperations();
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
connected(editor: YjsEditor): boolean {
|
|
75
|
+
return CONNECTED.has(editor);
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
connect(editor: YjsEditor): void {
|
|
79
|
+
editor.connect();
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
disconnect(editor: YjsEditor): void {
|
|
83
|
+
editor.disconnect();
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
remoteOrigin(editor: YjsEditor): unknown | undefined {
|
|
87
|
+
return ORIGIN.get(editor);
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
asRemote(editor: YjsEditor, origin: unknown, fn: () => void): void {
|
|
91
|
+
const prev = YjsEditor.remoteOrigin(editor);
|
|
92
|
+
ORIGIN.set(editor, origin);
|
|
93
|
+
|
|
94
|
+
fn();
|
|
95
|
+
|
|
96
|
+
if (prev === undefined) {
|
|
97
|
+
ORIGIN.delete(editor);
|
|
98
|
+
} else {
|
|
99
|
+
ORIGIN.set(editor, prev);
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
storePosition(editor: YjsEditor, key: string, point: Point): void {
|
|
104
|
+
const { sharedRoot, positionStorageOrigin: locationStorageOrigin } = editor;
|
|
105
|
+
assertDocumentAttachment(sharedRoot);
|
|
106
|
+
|
|
107
|
+
const position = slatePointToRelativePosition(sharedRoot, editor, point);
|
|
108
|
+
|
|
109
|
+
sharedRoot.doc.transact(() => {
|
|
110
|
+
setStoredPosition(sharedRoot, key, position);
|
|
111
|
+
}, locationStorageOrigin);
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
removeStoredPosition(editor: YjsEditor, key: string): void {
|
|
115
|
+
const { sharedRoot, positionStorageOrigin: locationStorageOrigin } = editor;
|
|
116
|
+
assertDocumentAttachment(sharedRoot);
|
|
117
|
+
|
|
118
|
+
sharedRoot.doc.transact(() => {
|
|
119
|
+
removeStoredPosition(sharedRoot, key);
|
|
120
|
+
}, locationStorageOrigin);
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
position(editor: YjsEditor, key: string): Point | null {
|
|
124
|
+
const position = getStoredPosition(editor.sharedRoot, key);
|
|
125
|
+
if (!position) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return relativePositionToSlatePoint(editor.sharedRoot, editor, position);
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
storedRelativePositions(
|
|
133
|
+
editor: YjsEditor
|
|
134
|
+
): Record<string, Y.RelativePosition> {
|
|
135
|
+
return getStoredPositions(editor.sharedRoot);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export type WithYjsOptions = {
|
|
140
|
+
autoConnect?: boolean;
|
|
141
|
+
|
|
142
|
+
// Origin used when applying local slate operations to yjs
|
|
143
|
+
localOrigin?: unknown;
|
|
144
|
+
|
|
145
|
+
// Origin used when storing positions
|
|
146
|
+
positionStorageOrigin?: unknown;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export function withYjs<T extends Editor>(
|
|
150
|
+
editor: T,
|
|
151
|
+
sharedRoot: Y.XmlText,
|
|
152
|
+
{
|
|
153
|
+
localOrigin,
|
|
154
|
+
positionStorageOrigin,
|
|
155
|
+
autoConnect = true,
|
|
156
|
+
}: WithYjsOptions = {}
|
|
157
|
+
): T & YjsEditor {
|
|
158
|
+
const e = editor as T & YjsEditor;
|
|
159
|
+
|
|
160
|
+
e.sharedRoot = sharedRoot;
|
|
161
|
+
|
|
162
|
+
e.localOrigin = localOrigin ?? DEFAULT_LOCAL_ORIGIN;
|
|
163
|
+
e.positionStorageOrigin =
|
|
164
|
+
positionStorageOrigin ?? DEFAULT_POSITION_STORAGE_ORIGIN;
|
|
165
|
+
|
|
166
|
+
e.applyRemoteEvents = (events, eventOrigin) => {
|
|
167
|
+
Editor.withoutNormalizing(e, () => {
|
|
168
|
+
YjsEditor.asRemote(e, eventOrigin, () => {
|
|
169
|
+
applyYjsEvents(e.sharedRoot, e, events);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const handleYEvents = (events: Y.YEvent[], transaction: Y.Transaction) => {
|
|
175
|
+
if (transaction.origin === e.localOrigin) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
YjsEditor.applyRemoveEvents(e, events, transaction.origin);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
let autoConnectTimeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
183
|
+
if (autoConnect) {
|
|
184
|
+
autoConnectTimeoutId = setTimeout(() => {
|
|
185
|
+
autoConnectTimeoutId = null;
|
|
186
|
+
YjsEditor.connect(e);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
e.connect = () => {
|
|
191
|
+
sharedRoot.observeDeep(handleYEvents);
|
|
192
|
+
const content = yTextToSlateElement(e.sharedRoot);
|
|
193
|
+
e.children = content.children;
|
|
194
|
+
CONNECTED.add(e);
|
|
195
|
+
e.onChange();
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
e.disconnect = () => {
|
|
199
|
+
if (autoConnectTimeoutId) {
|
|
200
|
+
clearTimeout(autoConnectTimeoutId);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
sharedRoot.unobserveDeep(handleYEvents);
|
|
204
|
+
CONNECTED.delete(e);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
e.storeLocalOperation = (op) => {
|
|
208
|
+
LOCAL_CHANGES.set(e, [
|
|
209
|
+
...YjsEditor.localChanges(e),
|
|
210
|
+
{ op, doc: editor.children },
|
|
211
|
+
]);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
e.flushLocalOperations = () => {
|
|
215
|
+
const localOperations = YjsEditor.localChanges(e);
|
|
216
|
+
LOCAL_CHANGES.delete(e);
|
|
217
|
+
|
|
218
|
+
assertDocumentAttachment(e.sharedRoot);
|
|
219
|
+
e.sharedRoot.doc.transact(() => {
|
|
220
|
+
localOperations.forEach((op) => {
|
|
221
|
+
applySlateOp(e.sharedRoot, { children: op.doc }, op.op);
|
|
222
|
+
});
|
|
223
|
+
}, e.localOrigin);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const { apply, onChange } = e;
|
|
227
|
+
e.apply = (op) => {
|
|
228
|
+
if (YjsEditor.remoteOrigin(e) === undefined) {
|
|
229
|
+
YjsEditor.storeLocalOperation(e, op);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
apply(op);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
e.onChange = () => {
|
|
236
|
+
YjsEditor.flushLocalOperations(e);
|
|
237
|
+
onChange();
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
return e;
|
|
241
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as Y from 'yjs';
|
|
2
|
+
import { InsertDelta } from '../model/types';
|
|
3
|
+
import { yTextToInsertDelta } from './delta';
|
|
4
|
+
|
|
5
|
+
export function cloneInsertDeltaDeep(delta: InsertDelta): InsertDelta {
|
|
6
|
+
return delta.map((element) => {
|
|
7
|
+
if (typeof element.insert === 'string') {
|
|
8
|
+
return element;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
12
|
+
return { ...element, insert: cloneDeep(element.insert) };
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function cloneDeep(yText: Y.XmlText): Y.XmlText {
|
|
17
|
+
const clone = new Y.XmlText();
|
|
18
|
+
|
|
19
|
+
const attributes = yText.getAttributes();
|
|
20
|
+
Object.entries(attributes).forEach(([key, value]) => {
|
|
21
|
+
clone.setAttribute(key, value);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
clone.applyDelta(cloneInsertDeltaDeep(yTextToInsertDelta(yText)), {
|
|
25
|
+
sanitize: false,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return clone;
|
|
29
|
+
}
|