@slate-yjs/core 1.0.4 → 1.0.6
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 +14 -0
- package/dist/applyToYjs/node/moveNode.d.ts.map +1 -1
- package/dist/index.cjs +51 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.global.js +51 -18
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +51 -18
- package/dist/index.js.map +1 -1
- package/dist/plugins/withCursors.d.ts.map +1 -1
- package/dist/utils/position.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/applyToYjs/node/moveNode.ts +39 -13
- package/src/plugins/withCursors.ts +40 -15
- package/src/utils/position.ts +15 -2
|
@@ -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];
|
|
@@ -21,7 +21,7 @@ const CURSOR_CHANGE_EVENT_LISTENERS: WeakMap<
|
|
|
21
21
|
> = new WeakMap();
|
|
22
22
|
|
|
23
23
|
export type CursorState<
|
|
24
|
-
TCursorData extends Record<string, unknown> = Record<string, unknown
|
|
24
|
+
TCursorData extends Record<string, unknown> = Record<string, unknown>,
|
|
25
25
|
> = {
|
|
26
26
|
relativeSelection: RelativeRange | null;
|
|
27
27
|
data?: TCursorData;
|
|
@@ -29,7 +29,7 @@ export type CursorState<
|
|
|
29
29
|
};
|
|
30
30
|
|
|
31
31
|
export type CursorEditor<
|
|
32
|
-
TCursorData extends Record<string, unknown> = Record<string, unknown
|
|
32
|
+
TCursorData extends Record<string, unknown> = Record<string, unknown>,
|
|
33
33
|
> = YjsEditor & {
|
|
34
34
|
awareness: Awareness;
|
|
35
35
|
|
|
@@ -145,7 +145,7 @@ export const CursorEditor = {
|
|
|
145
145
|
};
|
|
146
146
|
|
|
147
147
|
export type WithCursorsOptions<
|
|
148
|
-
TCursorData extends Record<string, unknown> = Record<string, unknown
|
|
148
|
+
TCursorData extends Record<string, unknown> = Record<string, unknown>,
|
|
149
149
|
> = {
|
|
150
150
|
// Local state field used to store the user selection
|
|
151
151
|
cursorStateField?: string;
|
|
@@ -159,7 +159,7 @@ export type WithCursorsOptions<
|
|
|
159
159
|
|
|
160
160
|
export function withCursors<
|
|
161
161
|
TCursorData extends Record<string, unknown>,
|
|
162
|
-
TEditor extends YjsEditor
|
|
162
|
+
TEditor extends YjsEditor,
|
|
163
163
|
>(
|
|
164
164
|
editor: TEditor,
|
|
165
165
|
awareness: Awareness,
|
|
@@ -180,11 +180,31 @@ 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 | null | undefined;
|
|
186
205
|
|
|
187
206
|
if (!range) {
|
|
207
|
+
hiddenAt = relativeSelection();
|
|
188
208
|
if (currentRange) {
|
|
189
209
|
e.awareness.setLocalStateField(e.selectionStateField, null);
|
|
190
210
|
}
|
|
@@ -192,14 +212,10 @@ export function withCursors<
|
|
|
192
212
|
return;
|
|
193
213
|
}
|
|
194
214
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (
|
|
198
|
-
|
|
199
|
-
!Y.compareRelativePositions(anchor, currentRange) ||
|
|
200
|
-
!Y.compareRelativePositions(focus, currentRange)
|
|
201
|
-
) {
|
|
202
|
-
e.awareness.setLocalStateField(e.selectionStateField, { anchor, focus });
|
|
215
|
+
hiddenAt = undefined;
|
|
216
|
+
const relativeRange = slateRangeToRelativeRange(e.sharedRoot, e, range);
|
|
217
|
+
if (!currentRange || !isSameRange(relativeRange, currentRange)) {
|
|
218
|
+
e.awareness.setLocalStateField(e.selectionStateField, relativeRange);
|
|
203
219
|
}
|
|
204
220
|
};
|
|
205
221
|
|
|
@@ -246,9 +262,18 @@ export function withCursors<
|
|
|
246
262
|
e.onChange = () => {
|
|
247
263
|
onChange();
|
|
248
264
|
|
|
249
|
-
if (YjsEditor.connected(e)) {
|
|
250
|
-
|
|
265
|
+
if (!YjsEditor.connected(e)) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (
|
|
270
|
+
hiddenAt !== undefined &&
|
|
271
|
+
isSameRange(relativeSelection(), hiddenAt)
|
|
272
|
+
) {
|
|
273
|
+
return;
|
|
251
274
|
}
|
|
275
|
+
|
|
276
|
+
CursorEditor.sendCursorPosition(e);
|
|
252
277
|
};
|
|
253
278
|
}
|
|
254
279
|
};
|
package/src/utils/position.ts
CHANGED
|
@@ -55,11 +55,24 @@ export function absolutePositionToSlatePoint(
|
|
|
55
55
|
});
|
|
56
56
|
|
|
57
57
|
const target = parent.children[pathOffset];
|
|
58
|
-
if (
|
|
58
|
+
if (Text.isText(target)) {
|
|
59
|
+
return { path: [...parentPath, pathOffset], offset: textOffset };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// The position is next to an inline. The text node on that side can be
|
|
63
|
+
// empty, and empty text isn't stored in Yjs, so the lookup lands on the
|
|
64
|
+
// inline. Resolve to the end of the text before it or the start of the
|
|
65
|
+
// text after it.
|
|
66
|
+
const sideOffset = textOffset === 0 ? pathOffset - 1 : pathOffset + 1;
|
|
67
|
+
const side = parent.children[sideOffset];
|
|
68
|
+
if (!Text.isText(side)) {
|
|
59
69
|
return null;
|
|
60
70
|
}
|
|
61
71
|
|
|
62
|
-
return {
|
|
72
|
+
return {
|
|
73
|
+
path: [...parentPath, sideOffset],
|
|
74
|
+
offset: textOffset === 0 ? side.text.length : 0,
|
|
75
|
+
};
|
|
63
76
|
}
|
|
64
77
|
|
|
65
78
|
export function relativePositionToSlatePoint(
|