@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
package/dist/utils/delta.js
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
const INSERT_DELTA_CACHE = new WeakMap();
|
|
2
|
-
export function invalidateDeltaCacheForYText(yText) {
|
|
3
|
-
INSERT_DELTA_CACHE.delete(yText);
|
|
4
|
-
}
|
|
5
1
|
export function yTextToInsertDelta(yText) {
|
|
6
|
-
|
|
7
|
-
if (cached) {
|
|
8
|
-
return cached;
|
|
9
|
-
}
|
|
10
|
-
const delta = yText.toDelta();
|
|
11
|
-
INSERT_DELTA_CACHE.set(yText, delta);
|
|
12
|
-
return delta;
|
|
2
|
+
return yText.toDelta();
|
|
13
3
|
}
|
|
14
4
|
export function getInsertLength({ insert }) {
|
|
15
5
|
return typeof insert === 'string' ? insert.length : 1;
|
package/dist/utils/delta.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delta.js","sourceRoot":"","sources":["../../src/utils/delta.ts"],"names":[],"mappings":"AAGA,MAAM,
|
|
1
|
+
{"version":3,"file":"delta.js","sourceRoot":"","sources":["../../src/utils/delta.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,kBAAkB,CAAC,KAAgB;IACjD,OAAO,KAAK,CAAC,OAAO,EAAiB,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,EAAE,MAAM,EAAe;IACrD,OAAO,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AACxD,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,KAAkB;IACrD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,IAAI,GAAG,eAAe,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;AAC7E,CAAC;AAED,MAAM,UAAU,gBAAgB,CAC9B,KAAkB,EAClB,KAAa,EACb,MAAc;IAEd,IAAI,MAAM,GAAG,CAAC,EAAE;QACd,OAAO,EAAE,CAAC;KACX;IAED,IAAI,aAAa,GAAG,CAAC,CAAC;IACtB,MAAM,MAAM,GAAgB,EAAE,CAAC;IAC/B,MAAM,GAAG,GAAG,KAAK,GAAG,MAAM,CAAC;IAE3B,KAAK,IAAI,CAAC,GAAG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BAAiB,CAC/B,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,CAAC,CAAC,gBAAgB,QAM7B;AAkDD,wBAAgB,iCAAiC,CAC/C,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,KAAK,EAAE,CAAC,CAAC,OAAO,EAChB,KAAK,EAAE,WAAW,EAClB,WAAW,SAAI;;;;EAqBhB;AAED,wBAAgB,uCAAuC,CACrD,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,KAAK,EAAE,CAAC,CAAC,OAAO,EAChB,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,gBAAgB,CAAC,CAAC,EACrE,KAAK,EAAE,WAAW,EAClB,cAAc,SAAI,EAClB,mBAAmB,SAAI,EACvB,IAAI,SAAK,QA+BV;AAED,wBAAgB,yBAAyB,CACvC,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,KAAK,EAAE,SAAS,GACf,aAAa,CAKf;AAED,wBAAgB,yBAAyB,CACvC,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,KAAK,EAAE,aAAa,GACnB,SAAS,GAAG,IAAI,CAsBlB"}
|
package/dist/utils/slate.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function getProperties<TNode extends Descendant>(node: TNode): Omit<TNode, TNode extends
|
|
1
|
+
import { BaseText, Descendant } from 'slate';
|
|
2
|
+
export declare function getProperties<TNode extends Descendant>(node: TNode): Omit<TNode, TNode extends BaseText ? 'text' : 'children'>;
|
|
3
3
|
//# sourceMappingURL=slate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slate.d.ts","sourceRoot":"","sources":["../../src/utils/slate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"slate.d.ts","sourceRoot":"","sources":["../../src/utils/slate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAQ,MAAM,OAAO,CAAC;AAGnD,wBAAgB,aAAa,CAAC,KAAK,SAAS,UAAU,EACpD,IAAI,EAAE,KAAK,GACV,IAAI,CAAC,KAAK,EAAE,KAAK,SAAS,QAAQ,GAAG,MAAM,GAAG,UAAU,CAAC,CAK3D"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slate-yjs/core",
|
|
3
3
|
"sideEffects": false,
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"description": "Yjs binding for Slate.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"slate",
|
|
@@ -30,11 +30,12 @@
|
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"microbundle": "^0.14.1",
|
|
32
32
|
"slate": ">=0.70.0",
|
|
33
|
-
"
|
|
33
|
+
"slate-hyperscript": "^0.67.0",
|
|
34
|
+
"yjs": "^13.5.22"
|
|
34
35
|
},
|
|
35
36
|
"peerDependencies": {
|
|
36
|
-
"slate": "^0.
|
|
37
|
-
"yjs": "^13.5.
|
|
37
|
+
"slate": "^0.70.0",
|
|
38
|
+
"yjs": "^13.5.22"
|
|
38
39
|
},
|
|
39
40
|
"bugs": {
|
|
40
41
|
"url": "https://github.com/BitPhinix/slate-yjs/issues"
|
|
@@ -36,9 +36,9 @@ export function applyYjsEvents(
|
|
|
36
36
|
) {
|
|
37
37
|
Editor.withoutNormalizing(editor, () => {
|
|
38
38
|
events.forEach((event) => {
|
|
39
|
-
translateYjsEvent(sharedRoot, editor, event).forEach((op) =>
|
|
40
|
-
editor.apply(op)
|
|
41
|
-
);
|
|
39
|
+
translateYjsEvent(sharedRoot, editor, event).forEach((op) => {
|
|
40
|
+
editor.apply(op);
|
|
41
|
+
});
|
|
42
42
|
});
|
|
43
43
|
});
|
|
44
44
|
}
|
|
@@ -2,7 +2,6 @@ import { Editor, Element, Node, Operation, Path, Text } from 'slate';
|
|
|
2
2
|
import * as Y from 'yjs';
|
|
3
3
|
import { Delta } from '../model/types';
|
|
4
4
|
import { deltaInsertToSlateNode } from '../utils/convert';
|
|
5
|
-
import { invalidateDeltaCacheForYText } from '../utils/delta';
|
|
6
5
|
import {
|
|
7
6
|
getSlateNodeYLength,
|
|
8
7
|
getSlatePath,
|
|
@@ -48,10 +47,6 @@ function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
|
|
|
48
47
|
let childPath = [...slatePath, pathOffset];
|
|
49
48
|
|
|
50
49
|
const newProperties = change.attributes;
|
|
51
|
-
const properties = pick(
|
|
52
|
-
node,
|
|
53
|
-
...(Object.keys(change.attributes) as Array<keyof Element>)
|
|
54
|
-
);
|
|
55
50
|
|
|
56
51
|
if (
|
|
57
52
|
Text.isText(child) &&
|
|
@@ -61,12 +56,13 @@ function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
|
|
|
61
56
|
let end =
|
|
62
57
|
pathOffset === endPathOffset ? endTextOffset : child.text.length;
|
|
63
58
|
|
|
59
|
+
const properties = getProperties(child);
|
|
64
60
|
if (start !== 0) {
|
|
65
61
|
ops.push({
|
|
66
62
|
type: 'split_node',
|
|
67
63
|
path: childPath,
|
|
68
64
|
position: start,
|
|
69
|
-
properties: newProperties,
|
|
65
|
+
properties: { ...properties, ...newProperties },
|
|
70
66
|
});
|
|
71
67
|
|
|
72
68
|
childPath = Path.next(childPath);
|
|
@@ -78,11 +74,15 @@ function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
|
|
|
78
74
|
type: 'split_node',
|
|
79
75
|
path: childPath,
|
|
80
76
|
position: end,
|
|
81
|
-
properties,
|
|
77
|
+
properties: { ...properties, ...newProperties },
|
|
82
78
|
});
|
|
83
79
|
}
|
|
84
80
|
}
|
|
85
81
|
|
|
82
|
+
const properties = pick(
|
|
83
|
+
node,
|
|
84
|
+
...(Object.keys(change.attributes) as Array<keyof Element>)
|
|
85
|
+
);
|
|
86
86
|
ops.push({
|
|
87
87
|
type: 'set_node',
|
|
88
88
|
newProperties,
|
|
@@ -175,7 +175,7 @@ function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
|
|
|
175
175
|
});
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
-
if (textOffset <
|
|
178
|
+
if (textOffset < child.text.length) {
|
|
179
179
|
ops.push({
|
|
180
180
|
type: 'split_node',
|
|
181
181
|
path: childPath,
|
|
@@ -214,9 +214,6 @@ export function translateYTextEvent(
|
|
|
214
214
|
throw new Error('Unexpected target node type');
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
-
// Invalidate delta cache entry on change
|
|
218
|
-
invalidateDeltaCacheForYText(target);
|
|
219
|
-
|
|
220
217
|
const ops: Operation[] = [];
|
|
221
218
|
const slatePath = getSlatePath(sharedRoot, editor, target);
|
|
222
219
|
const targetElement = Node.get(editor, slatePath);
|
|
@@ -242,7 +239,7 @@ export function translateYTextEvent(
|
|
|
242
239
|
}
|
|
243
240
|
|
|
244
241
|
if (delta.length > 0) {
|
|
245
|
-
if (Text.isText(
|
|
242
|
+
if (Text.isText(targetElement)) {
|
|
246
243
|
throw new Error('Cannot apply delta to slate text');
|
|
247
244
|
}
|
|
248
245
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { InsertNodeOperation, Node, Text } from 'slate';
|
|
2
2
|
import * as Y from 'yjs';
|
|
3
3
|
import { slateElementToYText } from '../../utils/convert';
|
|
4
|
-
import { invalidateDeltaCacheForYText } from '../../utils/delta';
|
|
5
4
|
import { getYTarget } from '../../utils/location';
|
|
6
5
|
import { getProperties } from '../../utils/slate';
|
|
7
6
|
|
|
@@ -13,10 +12,12 @@ export function insertNode(
|
|
|
13
12
|
const { yParent, textRange } = getYTarget(sharedRoot, slateRoot, op.path);
|
|
14
13
|
|
|
15
14
|
if (Text.isText(op.node)) {
|
|
16
|
-
yParent.insert(
|
|
17
|
-
|
|
15
|
+
return yParent.insert(
|
|
16
|
+
textRange.start,
|
|
17
|
+
op.node.text,
|
|
18
|
+
getProperties(op.node)
|
|
19
|
+
);
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
yParent.insertEmbed(textRange.start, slateElementToYText(op.node));
|
|
21
|
-
invalidateDeltaCacheForYText(yParent);
|
|
22
23
|
}
|
|
@@ -2,10 +2,7 @@ import { MergeNodeOperation, 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';
|
|
5
|
-
import {
|
|
6
|
-
invalidateDeltaCacheForYText,
|
|
7
|
-
yTextToInsertDelta,
|
|
8
|
-
} from '../../utils/delta';
|
|
5
|
+
import { yTextToInsertDelta } from '../../utils/delta';
|
|
9
6
|
import { getYTarget } from '../../utils/location';
|
|
10
7
|
import {
|
|
11
8
|
getStoredPositionsInDeltaAbsolute,
|
|
@@ -37,12 +34,11 @@ export function mergeNode(
|
|
|
37
34
|
throw new Error('Path points to a y text but not a slate node');
|
|
38
35
|
}
|
|
39
36
|
|
|
40
|
-
parent.format(
|
|
37
|
+
return parent.format(
|
|
41
38
|
textRange.start,
|
|
42
39
|
textRange.start - textRange.end,
|
|
43
40
|
getProperties(previousSibling)
|
|
44
41
|
);
|
|
45
|
-
return invalidateDeltaCacheForYText(parent);
|
|
46
42
|
}
|
|
47
43
|
|
|
48
44
|
const deltaApplyYOffset = prev.yTarget.length;
|
|
@@ -67,9 +63,6 @@ export function mergeNode(
|
|
|
67
63
|
target.textRange.end - target.textRange.start
|
|
68
64
|
);
|
|
69
65
|
|
|
70
|
-
invalidateDeltaCacheForYText(prev.yTarget);
|
|
71
|
-
invalidateDeltaCacheForYText(target.yParent);
|
|
72
|
-
|
|
73
66
|
restoreStoredPositionsWithDeltaAbsolute(
|
|
74
67
|
sharedRoot,
|
|
75
68
|
prev.yTarget,
|
|
@@ -2,11 +2,7 @@ import { 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';
|
|
5
|
-
import {
|
|
6
|
-
getInsertDeltaLength,
|
|
7
|
-
invalidateDeltaCacheForYText,
|
|
8
|
-
yTextToInsertDelta,
|
|
9
|
-
} from '../../utils/delta';
|
|
5
|
+
import { getInsertDeltaLength, yTextToInsertDelta } from '../../utils/delta';
|
|
10
6
|
import { getYTarget } from '../../utils/location';
|
|
11
7
|
import {
|
|
12
8
|
getStoredPositionsInDeltaAbsolute,
|
|
@@ -44,16 +40,12 @@ export function moveNode(
|
|
|
44
40
|
origin.textRange.end - origin.textRange.start
|
|
45
41
|
);
|
|
46
42
|
|
|
47
|
-
invalidateDeltaCacheForYText(target.yParent);
|
|
48
|
-
|
|
49
43
|
const targetLength = getInsertDeltaLength(yTextToInsertDelta(target.yParent));
|
|
50
44
|
const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
|
|
51
45
|
const applyDelta: Delta = [{ retain: deltaApplyYOffset }, ...insertDelta];
|
|
52
46
|
|
|
53
47
|
target.yParent.applyDelta(applyDelta, { sanitize: false });
|
|
54
48
|
|
|
55
|
-
invalidateDeltaCacheForYText(origin.yParent);
|
|
56
|
-
|
|
57
49
|
restoreStoredPositionsWithDeltaAbsolute(
|
|
58
50
|
sharedRoot,
|
|
59
51
|
target.yParent,
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Node, RemoveNodeOperation } from 'slate';
|
|
2
2
|
import * as Y from 'yjs';
|
|
3
|
-
import { invalidateDeltaCacheForYText } from '../../utils/delta';
|
|
4
3
|
import { getYTarget } from '../../utils/location';
|
|
5
4
|
|
|
6
5
|
export function removeNode(
|
|
@@ -14,5 +13,4 @@ export function removeNode(
|
|
|
14
13
|
op.path
|
|
15
14
|
);
|
|
16
15
|
parent.delete(textRange.start, textRange.end - textRange.start);
|
|
17
|
-
invalidateDeltaCacheForYText(parent);
|
|
18
16
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Node, SetNodeOperation } from 'slate';
|
|
2
2
|
import * as Y from 'yjs';
|
|
3
|
-
import { invalidateDeltaCacheForYText } from '../../utils/delta';
|
|
4
3
|
import { getYTarget } from '../../utils/location';
|
|
5
4
|
|
|
6
5
|
export function setNode(
|
|
@@ -40,6 +39,4 @@ export function setNode(
|
|
|
40
39
|
textRange.end - textRange.start,
|
|
41
40
|
newProperties
|
|
42
41
|
);
|
|
43
|
-
|
|
44
|
-
invalidateDeltaCacheForYText(yParent);
|
|
45
42
|
}
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { Node, SplitNodeOperation, Text } from 'slate';
|
|
2
2
|
import * as Y from 'yjs';
|
|
3
3
|
import { cloneInsertDeltaDeep } from '../../utils/clone';
|
|
4
|
-
import {
|
|
5
|
-
invalidateDeltaCacheForYText,
|
|
6
|
-
sliceInsertDelta,
|
|
7
|
-
yTextToInsertDelta,
|
|
8
|
-
} from '../../utils/delta';
|
|
4
|
+
import { sliceInsertDelta, yTextToInsertDelta } from '../../utils/delta';
|
|
9
5
|
import { getSlateNodeYLength, getYTarget } from '../../utils/location';
|
|
10
6
|
import {
|
|
11
7
|
getStoredPositionsInDeltaAbsolute,
|
|
@@ -18,6 +14,7 @@ export function splitNode(
|
|
|
18
14
|
op: SplitNodeOperation
|
|
19
15
|
): void {
|
|
20
16
|
const target = getYTarget(sharedRoot, slateRoot, op.path);
|
|
17
|
+
|
|
21
18
|
if (!target.slateTarget) {
|
|
22
19
|
throw new Error('Y target without corresponding slate node');
|
|
23
20
|
}
|
|
@@ -36,13 +33,11 @@ export function splitNode(
|
|
|
36
33
|
}
|
|
37
34
|
});
|
|
38
35
|
|
|
39
|
-
target.yParent.format(
|
|
36
|
+
return target.yParent.format(
|
|
40
37
|
target.textRange.start,
|
|
41
38
|
target.textRange.end - target.textRange.start,
|
|
42
39
|
{ ...unset, ...op.properties }
|
|
43
40
|
);
|
|
44
|
-
|
|
45
|
-
return invalidateDeltaCacheForYText(target.yParent);
|
|
46
41
|
}
|
|
47
42
|
|
|
48
43
|
if (Text.isText(target.slateTarget)) {
|
|
@@ -92,9 +87,6 @@ export function splitNode(
|
|
|
92
87
|
|
|
93
88
|
target.yParent.insertEmbed(target.textRange.end, toInsert);
|
|
94
89
|
|
|
95
|
-
invalidateDeltaCacheForYText(target.yTarget);
|
|
96
|
-
invalidateDeltaCacheForYText(target.yParent);
|
|
97
|
-
|
|
98
90
|
restoreStoredPositionsWithDeltaAbsolute(
|
|
99
91
|
sharedRoot,
|
|
100
92
|
toInsert,
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { InsertTextOperation, Node, Text } from 'slate';
|
|
2
2
|
import type Y from 'yjs';
|
|
3
|
-
import { invalidateDeltaCacheForYText } from '../../utils/delta';
|
|
4
3
|
import { getYTarget } from '../../utils/location';
|
|
5
4
|
import { getProperties } from '../../utils/slate';
|
|
6
5
|
|
|
@@ -25,6 +24,4 @@ export function insertText(
|
|
|
25
24
|
op.text,
|
|
26
25
|
getProperties(targetNode)
|
|
27
26
|
);
|
|
28
|
-
|
|
29
|
-
invalidateDeltaCacheForYText(target);
|
|
30
27
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Node, RemoveTextOperation } from 'slate';
|
|
2
2
|
import type Y from 'yjs';
|
|
3
|
-
import { invalidateDeltaCacheForYText } from '../../utils/delta';
|
|
4
3
|
import { getYTarget } from '../../utils/location';
|
|
5
4
|
|
|
6
5
|
export function removeText(
|
|
@@ -14,5 +13,4 @@ export function removeText(
|
|
|
14
13
|
op.path
|
|
15
14
|
);
|
|
16
15
|
target.delete(textRange.start + op.offset, op.text.length);
|
|
17
|
-
invalidateDeltaCacheForYText(target);
|
|
18
16
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RelativeRange } from './model/types';
|
|
1
2
|
import {
|
|
2
3
|
CursorEditor,
|
|
3
4
|
CursorState,
|
|
@@ -12,7 +13,13 @@ import {
|
|
|
12
13
|
YHistoryEditor,
|
|
13
14
|
YjsEditor,
|
|
14
15
|
} from './plugins';
|
|
15
|
-
import { slateNodesToInsertDelta } from './utils/convert';
|
|
16
|
+
import { slateNodesToInsertDelta, yTextToSlateElement } from './utils/convert';
|
|
17
|
+
import {
|
|
18
|
+
relativePositionToSlatePoint,
|
|
19
|
+
relativeRangeToSlateRange,
|
|
20
|
+
slatePointToRelativePosition,
|
|
21
|
+
slateRangeToRelativeRange,
|
|
22
|
+
} from './utils/position';
|
|
16
23
|
|
|
17
24
|
export {
|
|
18
25
|
withYjs,
|
|
@@ -29,11 +36,12 @@ export {
|
|
|
29
36
|
CursorState,
|
|
30
37
|
RemoteCursorChangeEventListener,
|
|
31
38
|
CursorStateChangeEvent,
|
|
32
|
-
//
|
|
33
|
-
|
|
39
|
+
// Utils
|
|
40
|
+
RelativeRange,
|
|
41
|
+
yTextToSlateElement,
|
|
34
42
|
slateNodesToInsertDelta,
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
43
|
+
slateRangeToRelativeRange,
|
|
44
|
+
relativeRangeToSlateRange,
|
|
45
|
+
slatePointToRelativePosition,
|
|
46
|
+
relativePositionToSlatePoint,
|
|
39
47
|
};
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Editor, Range } from 'slate';
|
|
2
2
|
import { Awareness } from 'y-protocols/awareness';
|
|
3
3
|
import * as Y from 'yjs';
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
slateRangeToRelativeRange,
|
|
7
|
-
} from '../utils/position';
|
|
4
|
+
import { RelativeRange } from '../model/types';
|
|
5
|
+
import { slateRangeToRelativeRange } from '../utils/position';
|
|
8
6
|
import { YjsEditor } from './withYjs';
|
|
9
7
|
|
|
10
8
|
export type CursorStateChangeEvent = {
|
|
@@ -25,8 +23,9 @@ const CURSOR_CHANGE_EVENT_LISTENERS: WeakMap<
|
|
|
25
23
|
export type CursorState<
|
|
26
24
|
TCursorData extends Record<string, unknown> = Record<string, unknown>
|
|
27
25
|
> = {
|
|
28
|
-
|
|
29
|
-
data
|
|
26
|
+
relativeSelection: RelativeRange | null;
|
|
27
|
+
data?: TCursorData;
|
|
28
|
+
clientId: number;
|
|
30
29
|
};
|
|
31
30
|
|
|
32
31
|
export type CursorEditor<
|
|
@@ -35,22 +34,21 @@ export type CursorEditor<
|
|
|
35
34
|
awareness: Awareness;
|
|
36
35
|
|
|
37
36
|
cursorDataField: string;
|
|
38
|
-
|
|
37
|
+
selectionStateField: string;
|
|
39
38
|
|
|
40
39
|
sendCursorPosition: (range: Range | null) => void;
|
|
41
40
|
sendCursorData: (data: TCursorData) => void;
|
|
42
41
|
};
|
|
43
42
|
|
|
44
43
|
export const CursorEditor = {
|
|
45
|
-
isCursorEditor(
|
|
46
|
-
return true;
|
|
44
|
+
isCursorEditor(value: unknown): value is CursorEditor {
|
|
47
45
|
return (
|
|
48
|
-
YjsEditor.isYjsEditor(
|
|
49
|
-
(
|
|
50
|
-
typeof (
|
|
51
|
-
typeof (
|
|
52
|
-
typeof (
|
|
53
|
-
typeof (
|
|
46
|
+
YjsEditor.isYjsEditor(value) &&
|
|
47
|
+
(value as CursorEditor).awareness instanceof Awareness &&
|
|
48
|
+
typeof (value as CursorEditor).cursorDataField === 'string' &&
|
|
49
|
+
typeof (value as CursorEditor).selectionStateField === 'string' &&
|
|
50
|
+
typeof (value as CursorEditor).sendCursorPosition === 'function' &&
|
|
51
|
+
typeof (value as CursorEditor).sendCursorData === 'function'
|
|
54
52
|
);
|
|
55
53
|
},
|
|
56
54
|
|
|
@@ -68,22 +66,22 @@ export const CursorEditor = {
|
|
|
68
66
|
editor.sendCursorData(data);
|
|
69
67
|
},
|
|
70
68
|
|
|
71
|
-
on(
|
|
72
|
-
editor: CursorEditor
|
|
69
|
+
on<TCursorData extends Record<string, unknown>>(
|
|
70
|
+
editor: CursorEditor<TCursorData>,
|
|
73
71
|
event: 'change',
|
|
74
|
-
|
|
72
|
+
handler: RemoteCursorChangeEventListener
|
|
75
73
|
) {
|
|
76
74
|
if (event !== 'change') {
|
|
77
75
|
return;
|
|
78
76
|
}
|
|
79
77
|
|
|
80
78
|
const listeners = CURSOR_CHANGE_EVENT_LISTENERS.get(editor) ?? new Set();
|
|
81
|
-
listeners.add(
|
|
79
|
+
listeners.add(handler);
|
|
82
80
|
CURSOR_CHANGE_EVENT_LISTENERS.set(editor, listeners);
|
|
83
81
|
},
|
|
84
82
|
|
|
85
|
-
off(
|
|
86
|
-
editor: CursorEditor
|
|
83
|
+
off<TCursorData extends Record<string, unknown>>(
|
|
84
|
+
editor: CursorEditor<TCursorData>,
|
|
87
85
|
event: 'change',
|
|
88
86
|
listener: RemoteCursorChangeEventListener
|
|
89
87
|
) {
|
|
@@ -97,8 +95,8 @@ export const CursorEditor = {
|
|
|
97
95
|
}
|
|
98
96
|
},
|
|
99
97
|
|
|
100
|
-
|
|
101
|
-
editor: CursorEditor
|
|
98
|
+
cursorState<TCursorData extends Record<string, unknown>>(
|
|
99
|
+
editor: CursorEditor<TCursorData>,
|
|
102
100
|
clientId: number
|
|
103
101
|
): CursorState<TCursorData> | null {
|
|
104
102
|
if (
|
|
@@ -113,16 +111,15 @@ export const CursorEditor = {
|
|
|
113
111
|
return null;
|
|
114
112
|
}
|
|
115
113
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return { selection, data: state[editor.cursorDataField] };
|
|
114
|
+
return {
|
|
115
|
+
relativeSelection: state[editor.selectionStateField] ?? null,
|
|
116
|
+
data: state[editor.cursorDataField],
|
|
117
|
+
clientId,
|
|
118
|
+
};
|
|
122
119
|
},
|
|
123
120
|
|
|
124
|
-
|
|
125
|
-
editor: CursorEditor
|
|
121
|
+
cursorStates<TCursorData extends Record<string, unknown>>(
|
|
122
|
+
editor: CursorEditor<TCursorData>
|
|
126
123
|
): Record<string, CursorState<TCursorData>> {
|
|
127
124
|
if (!YjsEditor.connected(editor)) {
|
|
128
125
|
return {};
|
|
@@ -131,20 +128,17 @@ export const CursorEditor = {
|
|
|
131
128
|
return Object.fromEntries(
|
|
132
129
|
Array.from(editor.awareness.getStates().entries(), ([id, state]) => {
|
|
133
130
|
// Ignore own state
|
|
134
|
-
if (id === editor.awareness.clientID) {
|
|
131
|
+
if (id === editor.awareness.clientID || !state) {
|
|
135
132
|
return null;
|
|
136
133
|
}
|
|
137
134
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
: null;
|
|
146
|
-
|
|
147
|
-
return [id, { selection, data: state[editor.cursorDataField] }];
|
|
135
|
+
return [
|
|
136
|
+
id,
|
|
137
|
+
{
|
|
138
|
+
relativeSelection: state[editor.selectionStateField],
|
|
139
|
+
data: state[editor.cursorDataField],
|
|
140
|
+
},
|
|
141
|
+
];
|
|
148
142
|
}).filter(Array.isArray)
|
|
149
143
|
);
|
|
150
144
|
},
|
|
@@ -170,7 +164,7 @@ export function withCursors<
|
|
|
170
164
|
editor: TEditor,
|
|
171
165
|
awareness: Awareness,
|
|
172
166
|
{
|
|
173
|
-
cursorStateField = 'selection',
|
|
167
|
+
cursorStateField: selectionStateField = 'selection',
|
|
174
168
|
cursorDataField = 'data',
|
|
175
169
|
autoSend = true,
|
|
176
170
|
data,
|
|
@@ -180,7 +174,7 @@ export function withCursors<
|
|
|
180
174
|
|
|
181
175
|
e.awareness = awareness;
|
|
182
176
|
e.cursorDataField = cursorDataField;
|
|
183
|
-
e.
|
|
177
|
+
e.selectionStateField = selectionStateField;
|
|
184
178
|
|
|
185
179
|
e.sendCursorData = (cursorData: TCursorData) => {
|
|
186
180
|
e.awareness.setLocalStateField(e.cursorDataField, cursorData);
|
|
@@ -188,11 +182,11 @@ export function withCursors<
|
|
|
188
182
|
|
|
189
183
|
e.sendCursorPosition = (range) => {
|
|
190
184
|
const localState = awareness.getLocalState();
|
|
191
|
-
const currentRange = localState?.[
|
|
185
|
+
const currentRange = localState?.[selectionStateField];
|
|
192
186
|
|
|
193
187
|
if (!range) {
|
|
194
188
|
if (currentRange) {
|
|
195
|
-
awareness.setLocalStateField(e.
|
|
189
|
+
awareness.setLocalStateField(e.selectionStateField, null);
|
|
196
190
|
}
|
|
197
191
|
|
|
198
192
|
return;
|
|
@@ -205,7 +199,7 @@ export function withCursors<
|
|
|
205
199
|
!Y.compareRelativePositions(anchor, currentRange) ||
|
|
206
200
|
!Y.compareRelativePositions(focus, currentRange)
|
|
207
201
|
) {
|
|
208
|
-
awareness.setLocalStateField(e.
|
|
202
|
+
awareness.setLocalStateField(e.selectionStateField, { anchor, focus });
|
|
209
203
|
}
|
|
210
204
|
};
|
|
211
205
|
|