@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,80 @@
|
|
|
1
|
+
import { MergeNodeOperation, Node, Path, Text } from 'slate';
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
import { Delta } from '../../model/types';
|
|
4
|
+
import { cloneInsertDeltaDeep } from '../../utils/clone';
|
|
5
|
+
import {
|
|
6
|
+
invalidateDeltaCacheForYText,
|
|
7
|
+
yTextToInsertDelta,
|
|
8
|
+
} from '../../utils/delta';
|
|
9
|
+
import { getYTarget } from '../../utils/location';
|
|
10
|
+
import {
|
|
11
|
+
getStoredPositionsInDeltaAbsolute,
|
|
12
|
+
restoreStoredPositionsWithDeltaAbsolute,
|
|
13
|
+
} from '../../utils/position';
|
|
14
|
+
import { getProperties } from '../../utils/slate';
|
|
15
|
+
|
|
16
|
+
export function mergeNode(
|
|
17
|
+
sharedRoot: Y.XmlText,
|
|
18
|
+
slateRoot: Node,
|
|
19
|
+
op: MergeNodeOperation
|
|
20
|
+
): void {
|
|
21
|
+
const target = getYTarget(sharedRoot, slateRoot, op.path);
|
|
22
|
+
const prev = getYTarget(
|
|
23
|
+
target.yParent,
|
|
24
|
+
target.slateParent,
|
|
25
|
+
Path.previous(op.path.slice(-1))
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
if (!target.yTarget !== !prev.yTarget) {
|
|
29
|
+
throw new Error('Cannot merge y text with y element');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!prev.yTarget || !target.yTarget) {
|
|
33
|
+
const { yParent: parent, textRange } = target;
|
|
34
|
+
|
|
35
|
+
const previousSibling = Node.get(slateRoot, Path.previous(op.path));
|
|
36
|
+
if (!Text.isText(previousSibling)) {
|
|
37
|
+
throw new Error('Path points to a y text but not a slate node');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
parent.format(
|
|
41
|
+
textRange.start,
|
|
42
|
+
textRange.start - textRange.end,
|
|
43
|
+
getProperties(previousSibling)
|
|
44
|
+
);
|
|
45
|
+
return invalidateDeltaCacheForYText(parent);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const deltaApplyYOffset = prev.yTarget.length;
|
|
49
|
+
const targetDelta = yTextToInsertDelta(target.yTarget);
|
|
50
|
+
const clonedDelta = cloneInsertDeltaDeep(targetDelta);
|
|
51
|
+
|
|
52
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
53
|
+
sharedRoot,
|
|
54
|
+
target.yTarget,
|
|
55
|
+
targetDelta,
|
|
56
|
+
deltaApplyYOffset
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const applyDelta: Delta = [{ retain: deltaApplyYOffset }, ...clonedDelta];
|
|
60
|
+
|
|
61
|
+
prev.yTarget.applyDelta(applyDelta, {
|
|
62
|
+
sanitize: false,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
target.yParent.delete(
|
|
66
|
+
target.textRange.start,
|
|
67
|
+
target.textRange.end - target.textRange.start
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
invalidateDeltaCacheForYText(prev.yTarget);
|
|
71
|
+
invalidateDeltaCacheForYText(target.yParent);
|
|
72
|
+
|
|
73
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
74
|
+
sharedRoot,
|
|
75
|
+
prev.yTarget,
|
|
76
|
+
storedPositions,
|
|
77
|
+
clonedDelta,
|
|
78
|
+
deltaApplyYOffset
|
|
79
|
+
);
|
|
80
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { MoveNodeOperation, Node, Path, Text } from 'slate';
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
import { Delta } from '../../model/types';
|
|
4
|
+
import { cloneInsertDeltaDeep } from '../../utils/clone';
|
|
5
|
+
import {
|
|
6
|
+
getInsertDeltaLength,
|
|
7
|
+
invalidateDeltaCacheForYText,
|
|
8
|
+
yTextToInsertDelta,
|
|
9
|
+
} from '../../utils/delta';
|
|
10
|
+
import { getYTarget } from '../../utils/location';
|
|
11
|
+
import {
|
|
12
|
+
getStoredPositionsInDeltaAbsolute,
|
|
13
|
+
restoreStoredPositionsWithDeltaAbsolute,
|
|
14
|
+
} from '../../utils/position';
|
|
15
|
+
|
|
16
|
+
export function moveNode(
|
|
17
|
+
sharedRoot: Y.XmlText,
|
|
18
|
+
slateRoot: Node,
|
|
19
|
+
op: MoveNodeOperation
|
|
20
|
+
): void {
|
|
21
|
+
const newParentPath = Path.parent(op.newPath);
|
|
22
|
+
const newPathOffset = op.newPath[op.newPath.length - 1];
|
|
23
|
+
const parent = Node.get(slateRoot, newParentPath);
|
|
24
|
+
if (Text.isText(parent)) {
|
|
25
|
+
throw new Error('Cannot move slate node into text element');
|
|
26
|
+
}
|
|
27
|
+
const normalizedNewPath = [
|
|
28
|
+
...newParentPath,
|
|
29
|
+
Math.min(newPathOffset, parent.children.length),
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
const origin = getYTarget(sharedRoot, slateRoot, op.path);
|
|
33
|
+
const target = getYTarget(sharedRoot, slateRoot, normalizedNewPath);
|
|
34
|
+
const insertDelta = cloneInsertDeltaDeep(origin.targetDelta);
|
|
35
|
+
|
|
36
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
37
|
+
sharedRoot,
|
|
38
|
+
origin.yParent,
|
|
39
|
+
origin.targetDelta
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
origin.yParent.delete(
|
|
43
|
+
origin.textRange.start,
|
|
44
|
+
origin.textRange.end - origin.textRange.start
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
invalidateDeltaCacheForYText(target.yParent);
|
|
48
|
+
|
|
49
|
+
const targetLength = getInsertDeltaLength(yTextToInsertDelta(target.yParent));
|
|
50
|
+
const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
|
|
51
|
+
const applyDelta: Delta = [{ retain: deltaApplyYOffset }, ...insertDelta];
|
|
52
|
+
|
|
53
|
+
target.yParent.applyDelta(applyDelta, { sanitize: false });
|
|
54
|
+
|
|
55
|
+
invalidateDeltaCacheForYText(origin.yParent);
|
|
56
|
+
|
|
57
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
58
|
+
sharedRoot,
|
|
59
|
+
target.yParent,
|
|
60
|
+
storedPositions,
|
|
61
|
+
insertDelta,
|
|
62
|
+
deltaApplyYOffset,
|
|
63
|
+
origin.textRange.start
|
|
64
|
+
);
|
|
65
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Node, RemoveNodeOperation } from 'slate';
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
import { invalidateDeltaCacheForYText } from '../../utils/delta';
|
|
4
|
+
import { getYTarget } from '../../utils/location';
|
|
5
|
+
|
|
6
|
+
export function removeNode(
|
|
7
|
+
sharedRoot: Y.XmlText,
|
|
8
|
+
slateRoot: Node,
|
|
9
|
+
op: RemoveNodeOperation
|
|
10
|
+
): void {
|
|
11
|
+
const { yParent: parent, textRange } = getYTarget(
|
|
12
|
+
sharedRoot,
|
|
13
|
+
slateRoot,
|
|
14
|
+
op.path
|
|
15
|
+
);
|
|
16
|
+
parent.delete(textRange.start, textRange.end - textRange.start);
|
|
17
|
+
invalidateDeltaCacheForYText(parent);
|
|
18
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Node, SetNodeOperation } from 'slate';
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
import { invalidateDeltaCacheForYText } from '../../utils/delta';
|
|
4
|
+
import { getYTarget } from '../../utils/location';
|
|
5
|
+
|
|
6
|
+
export function setNode(
|
|
7
|
+
sharedRoot: Y.XmlText,
|
|
8
|
+
slateRoot: Node,
|
|
9
|
+
op: SetNodeOperation
|
|
10
|
+
): void {
|
|
11
|
+
const { yTarget, textRange, yParent } = getYTarget(
|
|
12
|
+
sharedRoot,
|
|
13
|
+
slateRoot,
|
|
14
|
+
op.path
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
if (yTarget) {
|
|
18
|
+
Object.entries(op.newProperties).forEach(([key, value]) => {
|
|
19
|
+
if (value === null) {
|
|
20
|
+
return yTarget.removeAttribute(key);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
yTarget.setAttribute(key, value);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return Object.entries(op.properties).forEach(([key]) => {
|
|
27
|
+
if (!op.newProperties.hasOwnProperty(key)) {
|
|
28
|
+
yTarget.removeAttribute(key);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const unset = Object.fromEntries(
|
|
34
|
+
Object.keys(op.properties).map((key) => [key, null])
|
|
35
|
+
);
|
|
36
|
+
const newProperties = { ...unset, ...op.newProperties };
|
|
37
|
+
|
|
38
|
+
yParent.format(
|
|
39
|
+
textRange.start,
|
|
40
|
+
textRange.end - textRange.start,
|
|
41
|
+
newProperties
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
invalidateDeltaCacheForYText(yParent);
|
|
45
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Node, SplitNodeOperation, Text } from 'slate';
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
import { cloneInsertDeltaDeep } from '../../utils/clone';
|
|
4
|
+
import {
|
|
5
|
+
invalidateDeltaCacheForYText,
|
|
6
|
+
sliceInsertDelta,
|
|
7
|
+
yTextToInsertDelta,
|
|
8
|
+
} from '../../utils/delta';
|
|
9
|
+
import { getSlateNodeYLength, getYTarget } from '../../utils/location';
|
|
10
|
+
import {
|
|
11
|
+
getStoredPositionsInDeltaAbsolute,
|
|
12
|
+
restoreStoredPositionsWithDeltaAbsolute,
|
|
13
|
+
} from '../../utils/position';
|
|
14
|
+
|
|
15
|
+
export function splitNode(
|
|
16
|
+
sharedRoot: Y.XmlText,
|
|
17
|
+
slateRoot: Node,
|
|
18
|
+
op: SplitNodeOperation
|
|
19
|
+
): void {
|
|
20
|
+
const target = getYTarget(sharedRoot, slateRoot, op.path);
|
|
21
|
+
if (!target.slateTarget) {
|
|
22
|
+
throw new Error('Y target without corresponding slate node');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!target.yTarget) {
|
|
26
|
+
if (!Text.isText(target.slateTarget)) {
|
|
27
|
+
throw new Error('Mismatch node type between y target and slate node');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const unset: Record<string, null> = {};
|
|
31
|
+
target.targetDelta.forEach((element) => {
|
|
32
|
+
if (element.attributes) {
|
|
33
|
+
Object.keys(element.attributes).forEach((key) => {
|
|
34
|
+
unset[key] = null;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
target.yParent.format(
|
|
40
|
+
target.textRange.start,
|
|
41
|
+
target.textRange.end - target.textRange.start,
|
|
42
|
+
{ ...unset, ...op.properties }
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
return invalidateDeltaCacheForYText(target.yParent);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (Text.isText(target.slateTarget)) {
|
|
49
|
+
throw new Error('Mismatch node type between y target and slate node');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const splitTarget = getYTarget(target.yTarget, target.slateTarget, [
|
|
53
|
+
op.position,
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
const ySplitOffset = target.slateTarget.children
|
|
57
|
+
.slice(0, op.position)
|
|
58
|
+
.reduce((length, child) => length + getSlateNodeYLength(child), 0);
|
|
59
|
+
|
|
60
|
+
const length = target.slateTarget.children.reduce(
|
|
61
|
+
(current, child) => current + getSlateNodeYLength(child),
|
|
62
|
+
0
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const splitDelta = sliceInsertDelta(
|
|
66
|
+
yTextToInsertDelta(target.yTarget),
|
|
67
|
+
ySplitOffset,
|
|
68
|
+
length - ySplitOffset
|
|
69
|
+
);
|
|
70
|
+
const clonedDelta = cloneInsertDeltaDeep(splitDelta);
|
|
71
|
+
|
|
72
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
73
|
+
sharedRoot,
|
|
74
|
+
target.yTarget,
|
|
75
|
+
splitDelta,
|
|
76
|
+
ySplitOffset
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const toInsert = new Y.XmlText();
|
|
80
|
+
toInsert.applyDelta(clonedDelta, {
|
|
81
|
+
sanitize: false,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
Object.entries(op.properties).forEach(([key, value]) => {
|
|
85
|
+
toInsert.setAttribute(key, value);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
target.yTarget.delete(
|
|
89
|
+
splitTarget.textRange.start,
|
|
90
|
+
target.yTarget.length - splitTarget.textRange.start
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
target.yParent.insertEmbed(target.textRange.end, toInsert);
|
|
94
|
+
|
|
95
|
+
invalidateDeltaCacheForYText(target.yTarget);
|
|
96
|
+
invalidateDeltaCacheForYText(target.yParent);
|
|
97
|
+
|
|
98
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
99
|
+
sharedRoot,
|
|
100
|
+
toInsert,
|
|
101
|
+
storedPositions,
|
|
102
|
+
clonedDelta,
|
|
103
|
+
0,
|
|
104
|
+
ySplitOffset
|
|
105
|
+
);
|
|
106
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { TextOperation } from 'slate';
|
|
2
|
+
import { OpMapper } from '../types';
|
|
3
|
+
import { insertText } from './insertText';
|
|
4
|
+
import { removeText } from './removeText';
|
|
5
|
+
|
|
6
|
+
export const TEXT_MAPPER: OpMapper<TextOperation> = {
|
|
7
|
+
insert_text: insertText,
|
|
8
|
+
remove_text: removeText,
|
|
9
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { InsertTextOperation, Node, Text } from 'slate';
|
|
2
|
+
import type Y from 'yjs';
|
|
3
|
+
import { invalidateDeltaCacheForYText } from '../../utils/delta';
|
|
4
|
+
import { getYTarget } from '../../utils/location';
|
|
5
|
+
import { getProperties } from '../../utils/slate';
|
|
6
|
+
|
|
7
|
+
export function insertText(
|
|
8
|
+
sharedRoot: Y.XmlText,
|
|
9
|
+
slateRoot: Node,
|
|
10
|
+
op: InsertTextOperation
|
|
11
|
+
): void {
|
|
12
|
+
const { yParent: target, textRange } = getYTarget(
|
|
13
|
+
sharedRoot,
|
|
14
|
+
slateRoot,
|
|
15
|
+
op.path
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
const targetNode = Node.get(slateRoot, op.path);
|
|
19
|
+
if (!Text.isText(targetNode)) {
|
|
20
|
+
throw new Error('Cannot insert text into non-text node');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
target.insert(
|
|
24
|
+
textRange.start + op.offset,
|
|
25
|
+
op.text,
|
|
26
|
+
getProperties(targetNode)
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
invalidateDeltaCacheForYText(target);
|
|
30
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Node, RemoveTextOperation } from 'slate';
|
|
2
|
+
import type Y from 'yjs';
|
|
3
|
+
import { invalidateDeltaCacheForYText } from '../../utils/delta';
|
|
4
|
+
import { getYTarget } from '../../utils/location';
|
|
5
|
+
|
|
6
|
+
export function removeText(
|
|
7
|
+
sharedRoot: Y.XmlText,
|
|
8
|
+
slateRoot: Node,
|
|
9
|
+
op: RemoveTextOperation
|
|
10
|
+
): void {
|
|
11
|
+
const { yParent: target, textRange } = getYTarget(
|
|
12
|
+
sharedRoot,
|
|
13
|
+
slateRoot,
|
|
14
|
+
op.path
|
|
15
|
+
);
|
|
16
|
+
target.delete(textRange.start + op.offset, op.text.length);
|
|
17
|
+
invalidateDeltaCacheForYText(target);
|
|
18
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Node, Operation } from 'slate';
|
|
2
|
+
import * as Y from 'yjs';
|
|
3
|
+
|
|
4
|
+
export type ApplyFunc<O extends Operation = Operation> = (
|
|
5
|
+
sharedRoot: Y.XmlText,
|
|
6
|
+
slateRoot: Node,
|
|
7
|
+
op: O
|
|
8
|
+
) => void;
|
|
9
|
+
|
|
10
|
+
export type OpMapper<O extends Operation = Operation> = {
|
|
11
|
+
[K in O['type']]: O extends { type: K } ? ApplyFunc<O> : never;
|
|
12
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CursorEditor,
|
|
3
|
+
CursorState,
|
|
4
|
+
CursorStateChangeEvent,
|
|
5
|
+
RemoteCursorChangeEventListener,
|
|
6
|
+
withCursors,
|
|
7
|
+
WithCursorsOptions,
|
|
8
|
+
withYHistory,
|
|
9
|
+
WithYHistoryOptions,
|
|
10
|
+
withYjs,
|
|
11
|
+
WithYjsOptions,
|
|
12
|
+
YHistoryEditor,
|
|
13
|
+
YjsEditor,
|
|
14
|
+
} from './plugins';
|
|
15
|
+
import { slateNodesToInsertDelta } from './utils/convert';
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
withYjs,
|
|
19
|
+
WithYjsOptions,
|
|
20
|
+
YjsEditor,
|
|
21
|
+
// History plugin
|
|
22
|
+
withYHistory,
|
|
23
|
+
WithYHistoryOptions,
|
|
24
|
+
YHistoryEditor,
|
|
25
|
+
// Base cursor plugin
|
|
26
|
+
CursorEditor,
|
|
27
|
+
WithCursorsOptions,
|
|
28
|
+
withCursors,
|
|
29
|
+
CursorState,
|
|
30
|
+
RemoteCursorChangeEventListener,
|
|
31
|
+
CursorStateChangeEvent,
|
|
32
|
+
// TODO: Export, make the exported versions delta cache independent
|
|
33
|
+
// yTextToSlateElement,
|
|
34
|
+
slateNodesToInsertDelta,
|
|
35
|
+
// slateRangeToRelativeRange,
|
|
36
|
+
// relativeRangeToSlateRange,
|
|
37
|
+
// slatePointToRelativePosition,
|
|
38
|
+
// relativePositionToSlatePoint,
|
|
39
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Editor, Element, Node } from 'slate';
|
|
2
|
+
import type Y from 'yjs';
|
|
3
|
+
|
|
4
|
+
export type DeltaAttributes = {
|
|
5
|
+
retain: number;
|
|
6
|
+
attributes: Record<string, unknown>;
|
|
7
|
+
};
|
|
8
|
+
export type DeltaRetain = { retain: number };
|
|
9
|
+
export type DeltaDelete = { delete: number };
|
|
10
|
+
export type DeltaInsert = {
|
|
11
|
+
insert: string | Y.XmlText;
|
|
12
|
+
attributes?: Record<string, unknown>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type InsertDelta = Array<DeltaInsert>;
|
|
16
|
+
export type Delta = Array<
|
|
17
|
+
DeltaRetain | DeltaDelete | DeltaInsert | DeltaAttributes
|
|
18
|
+
>;
|
|
19
|
+
|
|
20
|
+
export type TextRange = { start: number; end: number };
|
|
21
|
+
|
|
22
|
+
export type HistoryStackItem = {
|
|
23
|
+
meta: Map<string, unknown>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type YTarget = {
|
|
27
|
+
// TextRange in the yParent mapping to the slateTarget (or position to insert)
|
|
28
|
+
textRange: TextRange;
|
|
29
|
+
|
|
30
|
+
// Y.XmlText containing the slate node
|
|
31
|
+
yParent: Y.XmlText;
|
|
32
|
+
|
|
33
|
+
// Slate element mapping to the yParent
|
|
34
|
+
slateParent: Element | Editor;
|
|
35
|
+
|
|
36
|
+
// If the target points to a slate element, Y.XmlText representing the target.
|
|
37
|
+
// If it points to a text (or position to insert), this will be undefined.
|
|
38
|
+
yTarget?: Y.XmlText;
|
|
39
|
+
|
|
40
|
+
// Slate node represented by the textRange, won't be set if position is insert.
|
|
41
|
+
slateTarget?: Node;
|
|
42
|
+
|
|
43
|
+
// InsertDelta representing the slateTarget
|
|
44
|
+
targetDelta: InsertDelta;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type RelativeRange = {
|
|
48
|
+
anchor: Y.RelativePosition;
|
|
49
|
+
focus: Y.RelativePosition;
|
|
50
|
+
};
|