@slate-yjs/core 1.0.4 → 1.0.5
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 +8 -0
- package/dist/applyToYjs/node/moveNode.d.ts.map +1 -1
- package/dist/index.cjs +41 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.global.js +41 -16
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +41 -16
- package/dist/index.js.map +1 -1
- package/dist/plugins/withCursors.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/applyToYjs/node/moveNode.ts +39 -13
- package/src/plugins/withCursors.ts +38 -11
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MoveNodeOperation, Node, Path, Text } from 'slate';
|
|
1
|
+
import { Descendant, MoveNodeOperation, Node, Path, Text } from 'slate';
|
|
2
2
|
import * as Y from 'yjs';
|
|
3
3
|
import { Delta } from '../../model/types';
|
|
4
4
|
import { cloneInsertDeltaDeep } from '../../utils/clone';
|
|
@@ -10,24 +10,30 @@ import {
|
|
|
10
10
|
} from '../../utils/position';
|
|
11
11
|
import { PRESERVE_CONTENT } from '../../utils/yjs';
|
|
12
12
|
|
|
13
|
+
// Returns a copy of root without the node at path. Only the ancestors of the
|
|
14
|
+
// removed node are copied.
|
|
15
|
+
function withoutNode(root: Node, path: Path): Node {
|
|
16
|
+
if (Text.isText(root)) {
|
|
17
|
+
throw new Error('Cannot descend into slate text');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const [index, ...rest] = path;
|
|
21
|
+
const children = [...root.children];
|
|
22
|
+
if (rest.length === 0) {
|
|
23
|
+
children.splice(index, 1);
|
|
24
|
+
} else {
|
|
25
|
+
children[index] = withoutNode(children[index], rest) as Descendant;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return { ...root, children } as Node;
|
|
29
|
+
}
|
|
30
|
+
|
|
13
31
|
export function moveNode(
|
|
14
32
|
sharedRoot: Y.XmlText,
|
|
15
33
|
slateRoot: Node,
|
|
16
34
|
op: MoveNodeOperation
|
|
17
35
|
): void {
|
|
18
|
-
const newParentPath = Path.parent(op.newPath);
|
|
19
|
-
const newPathOffset = op.newPath[op.newPath.length - 1];
|
|
20
|
-
const parent = Node.get(slateRoot, newParentPath);
|
|
21
|
-
if (Text.isText(parent)) {
|
|
22
|
-
throw new Error('Cannot move slate node into text element');
|
|
23
|
-
}
|
|
24
|
-
const normalizedNewPath = [
|
|
25
|
-
...newParentPath,
|
|
26
|
-
Math.min(newPathOffset, parent.children.length),
|
|
27
|
-
];
|
|
28
|
-
|
|
29
36
|
const origin = getYTarget(sharedRoot, slateRoot, op.path);
|
|
30
|
-
const target = getYTarget(sharedRoot, slateRoot, normalizedNewPath);
|
|
31
37
|
const insertDelta = cloneInsertDeltaDeep(origin.targetDelta);
|
|
32
38
|
|
|
33
39
|
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
@@ -41,6 +47,26 @@ export function moveNode(
|
|
|
41
47
|
origin.textRange.end - origin.textRange.start
|
|
42
48
|
);
|
|
43
49
|
|
|
50
|
+
// Slate removes the node first and inserts it at Path.transform(path, op)
|
|
51
|
+
// in the remaining tree. Resolve the target the same way, so it matches
|
|
52
|
+
// Yjs after the delete above.
|
|
53
|
+
const rootWithoutNode = withoutNode(slateRoot, op.path);
|
|
54
|
+
const newPath = Path.transform(op.path, op);
|
|
55
|
+
if (!newPath) {
|
|
56
|
+
throw new Error('Cannot resolve the target path of a move');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const newParentPath = Path.parent(newPath);
|
|
60
|
+
const parent = Node.get(rootWithoutNode, newParentPath);
|
|
61
|
+
if (Text.isText(parent)) {
|
|
62
|
+
throw new Error('Cannot move slate node into text element');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const target = getYTarget(sharedRoot, rootWithoutNode, [
|
|
66
|
+
...newParentPath,
|
|
67
|
+
Math.min(newPath[newPath.length - 1], parent.children.length),
|
|
68
|
+
]);
|
|
69
|
+
|
|
44
70
|
const targetLength = getInsertDeltaLength(yTextToInsertDelta(target.yParent));
|
|
45
71
|
const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
|
|
46
72
|
const applyDelta: Delta = [{ retain: deltaApplyYOffset }, ...insertDelta];
|
|
@@ -180,11 +180,33 @@ export function withCursors<
|
|
|
180
180
|
e.awareness.setLocalStateField(e.cursorDataField, cursorData);
|
|
181
181
|
};
|
|
182
182
|
|
|
183
|
+
// Where the selection was when the cursor was last hidden, or undefined
|
|
184
|
+
// while it's shown. autoSend leaves a hidden cursor hidden until the
|
|
185
|
+
// selection moves away from this point. Relative positions don't change
|
|
186
|
+
// when other clients edit the document.
|
|
187
|
+
let hiddenAt: RelativeRange | null | undefined;
|
|
188
|
+
|
|
189
|
+
const relativeSelection = () =>
|
|
190
|
+
e.selection
|
|
191
|
+
? slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
192
|
+
: null;
|
|
193
|
+
|
|
194
|
+
const isSameRange = (a: RelativeRange | null, b: RelativeRange | null) =>
|
|
195
|
+
a === b ||
|
|
196
|
+
(!!a &&
|
|
197
|
+
!!b &&
|
|
198
|
+
Y.compareRelativePositions(a.anchor, b.anchor) &&
|
|
199
|
+
Y.compareRelativePositions(a.focus, b.focus));
|
|
200
|
+
|
|
183
201
|
e.sendCursorPosition = (range) => {
|
|
184
202
|
const localState = e.awareness.getLocalState();
|
|
185
|
-
const currentRange = localState?.[selectionStateField]
|
|
203
|
+
const currentRange = localState?.[selectionStateField] as
|
|
204
|
+
| RelativeRange
|
|
205
|
+
| null
|
|
206
|
+
| undefined;
|
|
186
207
|
|
|
187
208
|
if (!range) {
|
|
209
|
+
hiddenAt = relativeSelection();
|
|
188
210
|
if (currentRange) {
|
|
189
211
|
e.awareness.setLocalStateField(e.selectionStateField, null);
|
|
190
212
|
}
|
|
@@ -192,14 +214,10 @@ export function withCursors<
|
|
|
192
214
|
return;
|
|
193
215
|
}
|
|
194
216
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (
|
|
198
|
-
|
|
199
|
-
!Y.compareRelativePositions(anchor, currentRange) ||
|
|
200
|
-
!Y.compareRelativePositions(focus, currentRange)
|
|
201
|
-
) {
|
|
202
|
-
e.awareness.setLocalStateField(e.selectionStateField, { anchor, focus });
|
|
217
|
+
hiddenAt = undefined;
|
|
218
|
+
const relativeRange = slateRangeToRelativeRange(e.sharedRoot, e, range);
|
|
219
|
+
if (!currentRange || !isSameRange(relativeRange, currentRange)) {
|
|
220
|
+
e.awareness.setLocalStateField(e.selectionStateField, relativeRange);
|
|
203
221
|
}
|
|
204
222
|
};
|
|
205
223
|
|
|
@@ -246,9 +264,18 @@ export function withCursors<
|
|
|
246
264
|
e.onChange = () => {
|
|
247
265
|
onChange();
|
|
248
266
|
|
|
249
|
-
if (YjsEditor.connected(e)) {
|
|
250
|
-
|
|
267
|
+
if (!YjsEditor.connected(e)) {
|
|
268
|
+
return;
|
|
251
269
|
}
|
|
270
|
+
|
|
271
|
+
if (
|
|
272
|
+
hiddenAt !== undefined &&
|
|
273
|
+
isSameRange(relativeSelection(), hiddenAt)
|
|
274
|
+
) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
CursorEditor.sendCursorPosition(e);
|
|
252
279
|
};
|
|
253
280
|
}
|
|
254
281
|
};
|