@slate-yjs/core 1.0.6 → 1.0.7
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 +6 -0
- package/dist/applyToYjs/node/splitNode.d.ts.map +1 -1
- package/dist/applyToYjs/text/insertText.d.ts +1 -1
- package/dist/applyToYjs/text/insertText.d.ts.map +1 -1
- package/dist/applyToYjs/text/removeText.d.ts +1 -1
- package/dist/applyToYjs/text/removeText.d.ts.map +1 -1
- package/dist/applyToYjs/types.d.ts +2 -2
- package/dist/applyToYjs/types.d.ts.map +1 -1
- package/dist/index.cjs +341 -98
- package/dist/index.cjs.map +1 -1
- package/dist/index.global.js +3157 -568
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +329 -94
- package/dist/index.js.map +1 -1
- package/dist/model/types.d.ts +11 -11
- package/dist/model/types.d.ts.map +1 -1
- package/dist/plugins/withCursors.d.ts +11 -11
- package/dist/plugins/withCursors.d.ts.map +1 -1
- package/dist/plugins/withYHistory.d.ts +2 -2
- package/dist/plugins/withYHistory.d.ts.map +1 -1
- package/dist/plugins/withYjs.d.ts +3 -3
- package/dist/plugins/withYjs.d.ts.map +1 -1
- package/dist/utils/object.d.ts +4 -4
- package/dist/utils/object.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/applyToSlate/textEvent.ts +1 -1
- package/src/applyToYjs/node/splitNode.ts +40 -12
- package/src/applyToYjs/text/insertText.ts +1 -1
- package/src/applyToYjs/text/removeText.ts +1 -1
- package/src/model/types.ts +1 -1
- package/src/utils/object.ts +5 -3
- package/src/utils/position.ts +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __defProps = Object.defineProperties;
|
|
@@ -45,12 +46,19 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
45
46
|
}
|
|
46
47
|
return to;
|
|
47
48
|
};
|
|
48
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
49
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
50
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
51
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
52
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
53
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
54
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
55
|
+
mod
|
|
56
|
+
));
|
|
49
57
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
50
58
|
|
|
51
59
|
// src/index.ts
|
|
52
|
-
var
|
|
53
|
-
__export(
|
|
60
|
+
var index_exports = {};
|
|
61
|
+
__export(index_exports, {
|
|
54
62
|
CursorEditor: () => CursorEditor,
|
|
55
63
|
YHistoryEditor: () => YHistoryEditor,
|
|
56
64
|
YjsEditor: () => YjsEditor,
|
|
@@ -64,7 +72,7 @@ __export(src_exports, {
|
|
|
64
72
|
withYjs: () => withYjs,
|
|
65
73
|
yTextToSlateElement: () => yTextToSlateElement
|
|
66
74
|
});
|
|
67
|
-
module.exports = __toCommonJS(
|
|
75
|
+
module.exports = __toCommonJS(index_exports);
|
|
68
76
|
|
|
69
77
|
// src/plugins/withYjs.ts
|
|
70
78
|
var import_slate17 = require("slate");
|
|
@@ -112,8 +120,7 @@ function deepEquals(node, another) {
|
|
|
112
120
|
return false;
|
|
113
121
|
}
|
|
114
122
|
} else if (Array.isArray(a) && Array.isArray(b)) {
|
|
115
|
-
if (a.length !== b.length)
|
|
116
|
-
return false;
|
|
123
|
+
if (a.length !== b.length) return false;
|
|
117
124
|
for (let i = 0; i < a.length; i++) {
|
|
118
125
|
if (a[i] !== b[i]) {
|
|
119
126
|
return false;
|
|
@@ -131,13 +138,19 @@ function deepEquals(node, another) {
|
|
|
131
138
|
return true;
|
|
132
139
|
}
|
|
133
140
|
function pick(obj, ...keys) {
|
|
134
|
-
return Object.fromEntries(
|
|
141
|
+
return Object.fromEntries(
|
|
142
|
+
Object.entries(obj).filter(([key]) => keys.includes(key))
|
|
143
|
+
);
|
|
135
144
|
}
|
|
136
145
|
function omit(obj, ...keys) {
|
|
137
|
-
return Object.fromEntries(
|
|
146
|
+
return Object.fromEntries(
|
|
147
|
+
Object.entries(obj).filter(([key]) => !keys.includes(key))
|
|
148
|
+
);
|
|
138
149
|
}
|
|
139
150
|
function omitNullEntries(obj) {
|
|
140
|
-
return Object.fromEntries(
|
|
151
|
+
return Object.fromEntries(
|
|
152
|
+
Object.entries(obj).filter(([, value]) => value !== null)
|
|
153
|
+
);
|
|
141
154
|
}
|
|
142
155
|
|
|
143
156
|
// src/utils/delta.ts
|
|
@@ -194,7 +207,10 @@ function sliceInsertDelta(delta, start, length) {
|
|
|
194
207
|
continue;
|
|
195
208
|
}
|
|
196
209
|
const startOffset = Math.max(0, start - currentOffset);
|
|
197
|
-
const endOffset = Math.min(
|
|
210
|
+
const endOffset = Math.min(
|
|
211
|
+
elementLength,
|
|
212
|
+
elementLength - (currentOffset + elementLength - end)
|
|
213
|
+
);
|
|
198
214
|
sliced.push(__spreadProps(__spreadValues({}, element), {
|
|
199
215
|
insert: element.insert.slice(startOffset, endOffset)
|
|
200
216
|
}));
|
|
@@ -206,7 +222,10 @@ function sliceInsertDelta(delta, start, length) {
|
|
|
206
222
|
// src/utils/slate.ts
|
|
207
223
|
var import_slate = require("slate");
|
|
208
224
|
function getProperties(node) {
|
|
209
|
-
return omit(
|
|
225
|
+
return omit(
|
|
226
|
+
node,
|
|
227
|
+
import_slate.Text.isText(node) ? "text" : "children"
|
|
228
|
+
);
|
|
210
229
|
}
|
|
211
230
|
|
|
212
231
|
// src/utils/yjs.ts
|
|
@@ -220,7 +239,10 @@ var PRESERVE_CONTENT = { sanitize: true };
|
|
|
220
239
|
// src/utils/convert.ts
|
|
221
240
|
function yTextToSlateElement(yText) {
|
|
222
241
|
const delta = yTextToInsertDelta(yText);
|
|
223
|
-
const children =
|
|
242
|
+
const children = (
|
|
243
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
244
|
+
delta.length > 0 ? delta.map(deltaInsertToSlateNode) : [{ text: "" }]
|
|
245
|
+
);
|
|
224
246
|
return __spreadProps(__spreadValues({}, yText.getAttributes()), { children });
|
|
225
247
|
}
|
|
226
248
|
function deltaInsertToSlateNode(insert) {
|
|
@@ -283,7 +305,9 @@ function getYTarget(yRoot, slateRoot, path) {
|
|
|
283
305
|
const yTarget = (_a = targetDelta[0]) == null ? void 0 : _a.insert;
|
|
284
306
|
if (childPath.length > 0) {
|
|
285
307
|
if (!(yTarget instanceof Y2.XmlText)) {
|
|
286
|
-
throw new Error(
|
|
308
|
+
throw new Error(
|
|
309
|
+
"Path doesn't match yText, cannot descent into non-yText"
|
|
310
|
+
);
|
|
287
311
|
}
|
|
288
312
|
return getYTarget(yTarget, targetNode, childPath);
|
|
289
313
|
}
|
|
@@ -375,8 +399,15 @@ function applyDelta(node, slatePath, delta) {
|
|
|
375
399
|
delta.reverse().forEach((change) => {
|
|
376
400
|
var _a;
|
|
377
401
|
if ("attributes" in change && "retain" in change) {
|
|
378
|
-
const [startPathOffset, startTextOffset] = yOffsetToSlateOffsets(
|
|
379
|
-
|
|
402
|
+
const [startPathOffset, startTextOffset] = yOffsetToSlateOffsets(
|
|
403
|
+
node,
|
|
404
|
+
yOffset - change.retain
|
|
405
|
+
);
|
|
406
|
+
const [endPathOffset, endTextOffset] = yOffsetToSlateOffsets(
|
|
407
|
+
node,
|
|
408
|
+
yOffset,
|
|
409
|
+
{ assoc: -1 }
|
|
410
|
+
);
|
|
380
411
|
for (let pathOffset = endPathOffset; pathOffset >= startPathOffset; pathOffset--) {
|
|
381
412
|
const child = node.children[pathOffset];
|
|
382
413
|
const childPath = [...slatePath, pathOffset];
|
|
@@ -384,7 +415,10 @@ function applyDelta(node, slatePath, delta) {
|
|
|
384
415
|
continue;
|
|
385
416
|
}
|
|
386
417
|
const newProperties = change.attributes;
|
|
387
|
-
const properties = pick(
|
|
418
|
+
const properties = pick(
|
|
419
|
+
node,
|
|
420
|
+
...Object.keys(change.attributes)
|
|
421
|
+
);
|
|
388
422
|
if (pathOffset === startPathOffset || pathOffset === endPathOffset) {
|
|
389
423
|
const start = pathOffset === startPathOffset ? startTextOffset : 0;
|
|
390
424
|
const end = pathOffset === endPathOffset ? endTextOffset : child.text.length;
|
|
@@ -418,8 +452,15 @@ function applyDelta(node, slatePath, delta) {
|
|
|
418
452
|
yOffset -= change.retain;
|
|
419
453
|
}
|
|
420
454
|
if ("delete" in change) {
|
|
421
|
-
const [startPathOffset, startTextOffset] = yOffsetToSlateOffsets(
|
|
422
|
-
|
|
455
|
+
const [startPathOffset, startTextOffset] = yOffsetToSlateOffsets(
|
|
456
|
+
node,
|
|
457
|
+
yOffset - change.delete
|
|
458
|
+
);
|
|
459
|
+
const [endPathOffset, endTextOffset] = yOffsetToSlateOffsets(
|
|
460
|
+
node,
|
|
461
|
+
yOffset,
|
|
462
|
+
{ assoc: -1 }
|
|
463
|
+
);
|
|
423
464
|
for (let pathOffset = endTextOffset === 0 ? endPathOffset - 1 : endPathOffset; pathOffset >= startPathOffset; pathOffset--) {
|
|
424
465
|
const child = node.children[pathOffset];
|
|
425
466
|
const childPath = [...slatePath, pathOffset];
|
|
@@ -510,11 +551,15 @@ function translateYTextEvent(sharedRoot, editor, event) {
|
|
|
510
551
|
}
|
|
511
552
|
const keyChanges = Array.from(changes.keys.entries());
|
|
512
553
|
if (slatePath.length > 0 && keyChanges.length > 0) {
|
|
513
|
-
const newProperties = Object.fromEntries(
|
|
514
|
-
key,
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
554
|
+
const newProperties = Object.fromEntries(
|
|
555
|
+
keyChanges.map(([key, info]) => [
|
|
556
|
+
key,
|
|
557
|
+
info.action === "delete" ? null : target.getAttribute(key)
|
|
558
|
+
])
|
|
559
|
+
);
|
|
560
|
+
const properties = Object.fromEntries(
|
|
561
|
+
keyChanges.map(([key]) => [key, Reflect.get(targetElement, key)])
|
|
562
|
+
);
|
|
518
563
|
ops.push({ type: "set_node", newProperties, properties, path: slatePath });
|
|
519
564
|
}
|
|
520
565
|
if (delta.length > 0) {
|
|
@@ -545,7 +590,11 @@ var import_slate8 = require("slate");
|
|
|
545
590
|
function insertNode(sharedRoot, slateRoot, op) {
|
|
546
591
|
const { yParent, textRange } = getYTarget(sharedRoot, slateRoot, op.path);
|
|
547
592
|
if (import_slate8.Text.isText(op.node)) {
|
|
548
|
-
return yParent.insert(
|
|
593
|
+
return yParent.insert(
|
|
594
|
+
textRange.start,
|
|
595
|
+
op.node.text,
|
|
596
|
+
getProperties(op.node)
|
|
597
|
+
);
|
|
549
598
|
}
|
|
550
599
|
yParent.insertEmbed(textRange.start, slateElementToYText(op.node));
|
|
551
600
|
}
|
|
@@ -569,7 +618,10 @@ function cloneDeep(yText) {
|
|
|
569
618
|
Object.entries(attributes).forEach(([key, value]) => {
|
|
570
619
|
clone.setAttribute(key, value);
|
|
571
620
|
});
|
|
572
|
-
clone.applyDelta(
|
|
621
|
+
clone.applyDelta(
|
|
622
|
+
cloneInsertDeltaDeep(yTextToInsertDelta(yText)),
|
|
623
|
+
PRESERVE_CONTENT
|
|
624
|
+
);
|
|
573
625
|
return clone;
|
|
574
626
|
}
|
|
575
627
|
|
|
@@ -578,12 +630,22 @@ var import_slate10 = require("slate");
|
|
|
578
630
|
var Y6 = __toESM(require("yjs"), 1);
|
|
579
631
|
var STORED_POSITION_PREFIX = "__slateYjsStoredPosition_";
|
|
580
632
|
function slatePointToRelativePosition(sharedRoot, slateRoot, point) {
|
|
581
|
-
const { yTarget, yParent, textRange } = getYTarget(
|
|
633
|
+
const { yTarget, yParent, textRange } = getYTarget(
|
|
634
|
+
sharedRoot,
|
|
635
|
+
slateRoot,
|
|
636
|
+
point.path
|
|
637
|
+
);
|
|
582
638
|
if (yTarget) {
|
|
583
|
-
throw new Error(
|
|
639
|
+
throw new Error(
|
|
640
|
+
"Slate point points to a non-text element inside sharedRoot"
|
|
641
|
+
);
|
|
584
642
|
}
|
|
585
643
|
const index = textRange.start + point.offset;
|
|
586
|
-
return Y6.createRelativePositionFromTypeIndex(
|
|
644
|
+
return Y6.createRelativePositionFromTypeIndex(
|
|
645
|
+
yParent,
|
|
646
|
+
index,
|
|
647
|
+
index === textRange.end ? -1 : 0
|
|
648
|
+
);
|
|
587
649
|
}
|
|
588
650
|
function absolutePositionToSlatePoint(sharedRoot, slateRoot, { type, index, assoc }) {
|
|
589
651
|
if (!(type instanceof Y6.XmlText)) {
|
|
@@ -592,7 +654,9 @@ function absolutePositionToSlatePoint(sharedRoot, slateRoot, { type, index, asso
|
|
|
592
654
|
const parentPath = getSlatePath(sharedRoot, slateRoot, type);
|
|
593
655
|
const parent = import_slate10.Node.get(slateRoot, parentPath);
|
|
594
656
|
if (import_slate10.Text.isText(parent)) {
|
|
595
|
-
throw new Error(
|
|
657
|
+
throw new Error(
|
|
658
|
+
"Absolute position doesn't match slateRoot, cannot descent into text"
|
|
659
|
+
);
|
|
596
660
|
}
|
|
597
661
|
const [pathOffset, textOffset] = yOffsetToSlateOffsets(parent, index, {
|
|
598
662
|
assoc
|
|
@@ -615,7 +679,10 @@ function relativePositionToSlatePoint(sharedRoot, slateRoot, pos) {
|
|
|
615
679
|
if (!sharedRoot.doc) {
|
|
616
680
|
throw new Error("sharedRoot isn't attach to a yDoc");
|
|
617
681
|
}
|
|
618
|
-
const absPos = Y6.createAbsolutePositionFromRelativePosition(
|
|
682
|
+
const absPos = Y6.createAbsolutePositionFromRelativePosition(
|
|
683
|
+
pos,
|
|
684
|
+
sharedRoot.doc
|
|
685
|
+
);
|
|
619
686
|
return absPos && absolutePositionToSlatePoint(sharedRoot, slateRoot, absPos);
|
|
620
687
|
}
|
|
621
688
|
function getStoredPosition(sharedRoot, key) {
|
|
@@ -626,34 +693,48 @@ function getStoredPosition(sharedRoot, key) {
|
|
|
626
693
|
return Y6.decodeRelativePosition(rawPosition);
|
|
627
694
|
}
|
|
628
695
|
function getStoredPositions(sharedRoot) {
|
|
629
|
-
return Object.fromEntries(
|
|
630
|
-
key.
|
|
631
|
-
|
|
632
|
-
|
|
696
|
+
return Object.fromEntries(
|
|
697
|
+
Object.entries(sharedRoot.getAttributes()).filter(([key]) => key.startsWith(STORED_POSITION_PREFIX)).map(([key, position]) => [
|
|
698
|
+
key.slice(STORED_POSITION_PREFIX.length),
|
|
699
|
+
Y6.decodeRelativePosition(position)
|
|
700
|
+
])
|
|
701
|
+
);
|
|
633
702
|
}
|
|
634
703
|
function getStoredPositionsAbsolute(sharedRoot) {
|
|
635
704
|
assertDocumentAttachment(sharedRoot);
|
|
636
|
-
return Object.fromEntries(
|
|
637
|
-
key.
|
|
638
|
-
|
|
639
|
-
|
|
705
|
+
return Object.fromEntries(
|
|
706
|
+
Object.entries(sharedRoot.getAttributes()).filter(([key]) => key.startsWith(STORED_POSITION_PREFIX)).map(
|
|
707
|
+
([key, position]) => [
|
|
708
|
+
key.slice(STORED_POSITION_PREFIX.length),
|
|
709
|
+
Y6.createAbsolutePositionFromRelativePosition(
|
|
710
|
+
Y6.decodeRelativePosition(position),
|
|
711
|
+
sharedRoot.doc
|
|
712
|
+
)
|
|
713
|
+
]
|
|
714
|
+
).filter(([, position]) => position)
|
|
715
|
+
);
|
|
640
716
|
}
|
|
641
717
|
function removeStoredPosition(sharedRoot, key) {
|
|
642
718
|
sharedRoot.removeAttribute(STORED_POSITION_PREFIX + key);
|
|
643
719
|
}
|
|
644
720
|
function setStoredPosition(sharedRoot, key, position) {
|
|
645
|
-
sharedRoot.setAttribute(
|
|
721
|
+
sharedRoot.setAttribute(
|
|
722
|
+
STORED_POSITION_PREFIX + key,
|
|
723
|
+
Y6.encodeRelativePosition(position)
|
|
724
|
+
);
|
|
646
725
|
}
|
|
647
726
|
function getAbsolutePositionsInTextRange(absolutePositions, yTarget, textRange) {
|
|
648
|
-
return Object.fromEntries(
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
727
|
+
return Object.fromEntries(
|
|
728
|
+
Object.entries(absolutePositions).filter(([, position]) => {
|
|
729
|
+
if (position.type !== yTarget) {
|
|
730
|
+
return false;
|
|
731
|
+
}
|
|
732
|
+
if (!textRange) {
|
|
733
|
+
return true;
|
|
734
|
+
}
|
|
735
|
+
return position.assoc >= 0 ? position.index >= textRange.start && position.index < textRange.end : position.index > textRange.start && position.index <= textRange.end;
|
|
736
|
+
})
|
|
737
|
+
);
|
|
657
738
|
}
|
|
658
739
|
function getAbsolutePositionsInYText(absolutePositions, yText, parentPath = "") {
|
|
659
740
|
const positions = {
|
|
@@ -662,7 +743,14 @@ function getAbsolutePositionsInYText(absolutePositions, yText, parentPath = "")
|
|
|
662
743
|
const insertDelta = yTextToInsertDelta(yText);
|
|
663
744
|
insertDelta.forEach(({ insert }, i) => {
|
|
664
745
|
if (insert instanceof Y6.XmlText) {
|
|
665
|
-
Object.assign(
|
|
746
|
+
Object.assign(
|
|
747
|
+
positions,
|
|
748
|
+
getAbsolutePositionsInYText(
|
|
749
|
+
absolutePositions,
|
|
750
|
+
insert,
|
|
751
|
+
parentPath ? `${parentPath}.${i}` : i.toString()
|
|
752
|
+
)
|
|
753
|
+
);
|
|
666
754
|
}
|
|
667
755
|
});
|
|
668
756
|
return positions;
|
|
@@ -677,7 +765,10 @@ function getStoredPositionsInDeltaAbsolute(sharedRoot, yText, delta, deltaOffset
|
|
|
677
765
|
};
|
|
678
766
|
delta.forEach(({ insert }, i) => {
|
|
679
767
|
if (insert instanceof Y6.XmlText) {
|
|
680
|
-
Object.assign(
|
|
768
|
+
Object.assign(
|
|
769
|
+
positions,
|
|
770
|
+
getAbsolutePositionsInYText(absolutePositions, insert, i.toString())
|
|
771
|
+
);
|
|
681
772
|
}
|
|
682
773
|
});
|
|
683
774
|
return positions;
|
|
@@ -686,12 +777,28 @@ function restoreStoredPositionsWithDeltaAbsolute(sharedRoot, yText, absolutePosi
|
|
|
686
777
|
const toRestore = absolutePositions[path];
|
|
687
778
|
if (toRestore) {
|
|
688
779
|
Object.entries(toRestore).forEach(([key, position]) => {
|
|
689
|
-
setStoredPosition(
|
|
780
|
+
setStoredPosition(
|
|
781
|
+
sharedRoot,
|
|
782
|
+
key,
|
|
783
|
+
Y6.createRelativePositionFromTypeIndex(
|
|
784
|
+
yText,
|
|
785
|
+
position.index - previousDeltaOffset + newDeltaOffset,
|
|
786
|
+
position.assoc
|
|
787
|
+
)
|
|
788
|
+
);
|
|
690
789
|
});
|
|
691
790
|
}
|
|
692
791
|
delta.forEach(({ insert }, i) => {
|
|
693
792
|
if (insert instanceof Y6.XmlText) {
|
|
694
|
-
restoreStoredPositionsWithDeltaAbsolute(
|
|
793
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
794
|
+
sharedRoot,
|
|
795
|
+
insert,
|
|
796
|
+
absolutePositions,
|
|
797
|
+
yTextToInsertDelta(insert),
|
|
798
|
+
0,
|
|
799
|
+
0,
|
|
800
|
+
path ? `${path}.${i}` : i.toString()
|
|
801
|
+
);
|
|
695
802
|
}
|
|
696
803
|
});
|
|
697
804
|
}
|
|
@@ -702,11 +809,19 @@ function slateRangeToRelativeRange(sharedRoot, slateRoot, range) {
|
|
|
702
809
|
};
|
|
703
810
|
}
|
|
704
811
|
function relativeRangeToSlateRange(sharedRoot, slateRoot, range) {
|
|
705
|
-
const anchor = relativePositionToSlatePoint(
|
|
812
|
+
const anchor = relativePositionToSlatePoint(
|
|
813
|
+
sharedRoot,
|
|
814
|
+
slateRoot,
|
|
815
|
+
range.anchor
|
|
816
|
+
);
|
|
706
817
|
if (!anchor) {
|
|
707
818
|
return null;
|
|
708
819
|
}
|
|
709
|
-
const focus = relativePositionToSlatePoint(
|
|
820
|
+
const focus = relativePositionToSlatePoint(
|
|
821
|
+
sharedRoot,
|
|
822
|
+
slateRoot,
|
|
823
|
+
range.focus
|
|
824
|
+
);
|
|
710
825
|
if (!focus) {
|
|
711
826
|
return null;
|
|
712
827
|
}
|
|
@@ -716,7 +831,11 @@ function relativeRangeToSlateRange(sharedRoot, slateRoot, range) {
|
|
|
716
831
|
// src/applyToYjs/node/mergeNode.ts
|
|
717
832
|
function mergeNode(sharedRoot, slateRoot, op) {
|
|
718
833
|
const target = getYTarget(sharedRoot, slateRoot, op.path);
|
|
719
|
-
const prev = getYTarget(
|
|
834
|
+
const prev = getYTarget(
|
|
835
|
+
target.yParent,
|
|
836
|
+
target.slateParent,
|
|
837
|
+
import_slate11.Path.previous(op.path.slice(-1))
|
|
838
|
+
);
|
|
720
839
|
if (!target.yTarget !== !prev.yTarget) {
|
|
721
840
|
throw new Error("Cannot merge y text with y element");
|
|
722
841
|
}
|
|
@@ -740,11 +859,25 @@ function mergeNode(sharedRoot, slateRoot, op) {
|
|
|
740
859
|
const deltaApplyYOffset = prev.yTarget.length;
|
|
741
860
|
const targetDelta = yTextToInsertDelta(target.yTarget);
|
|
742
861
|
const clonedDelta = cloneInsertDeltaDeep(targetDelta);
|
|
743
|
-
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
862
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
863
|
+
sharedRoot,
|
|
864
|
+
target.yTarget,
|
|
865
|
+
targetDelta,
|
|
866
|
+
deltaApplyYOffset
|
|
867
|
+
);
|
|
744
868
|
const applyDelta2 = [{ retain: deltaApplyYOffset }, ...clonedDelta];
|
|
745
869
|
prev.yTarget.applyDelta(applyDelta2, PRESERVE_CONTENT);
|
|
746
|
-
target.yParent.delete(
|
|
747
|
-
|
|
870
|
+
target.yParent.delete(
|
|
871
|
+
target.textRange.start,
|
|
872
|
+
target.textRange.end - target.textRange.start
|
|
873
|
+
);
|
|
874
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
875
|
+
sharedRoot,
|
|
876
|
+
prev.yTarget,
|
|
877
|
+
storedPositions,
|
|
878
|
+
clonedDelta,
|
|
879
|
+
deltaApplyYOffset
|
|
880
|
+
);
|
|
748
881
|
}
|
|
749
882
|
|
|
750
883
|
// src/applyToYjs/node/moveNode.ts
|
|
@@ -765,8 +898,15 @@ function withoutNode(root, path) {
|
|
|
765
898
|
function moveNode(sharedRoot, slateRoot, op) {
|
|
766
899
|
const origin = getYTarget(sharedRoot, slateRoot, op.path);
|
|
767
900
|
const insertDelta = cloneInsertDeltaDeep(origin.targetDelta);
|
|
768
|
-
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
769
|
-
|
|
901
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
902
|
+
sharedRoot,
|
|
903
|
+
origin.yParent,
|
|
904
|
+
origin.targetDelta
|
|
905
|
+
);
|
|
906
|
+
origin.yParent.delete(
|
|
907
|
+
origin.textRange.start,
|
|
908
|
+
origin.textRange.end - origin.textRange.start
|
|
909
|
+
);
|
|
770
910
|
const rootWithoutNode = withoutNode(slateRoot, op.path);
|
|
771
911
|
const newPath = import_slate13.Path.transform(op.path, op);
|
|
772
912
|
if (!newPath) {
|
|
@@ -785,18 +925,33 @@ function moveNode(sharedRoot, slateRoot, op) {
|
|
|
785
925
|
const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
|
|
786
926
|
const applyDelta2 = [{ retain: deltaApplyYOffset }, ...insertDelta];
|
|
787
927
|
target.yParent.applyDelta(applyDelta2, PRESERVE_CONTENT);
|
|
788
|
-
restoreStoredPositionsWithDeltaAbsolute(
|
|
928
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
929
|
+
sharedRoot,
|
|
930
|
+
target.yParent,
|
|
931
|
+
storedPositions,
|
|
932
|
+
insertDelta,
|
|
933
|
+
deltaApplyYOffset,
|
|
934
|
+
origin.textRange.start
|
|
935
|
+
);
|
|
789
936
|
}
|
|
790
937
|
|
|
791
938
|
// src/applyToYjs/node/removeNode.ts
|
|
792
939
|
function removeNode(sharedRoot, slateRoot, op) {
|
|
793
|
-
const { yParent: parent, textRange } = getYTarget(
|
|
940
|
+
const { yParent: parent, textRange } = getYTarget(
|
|
941
|
+
sharedRoot,
|
|
942
|
+
slateRoot,
|
|
943
|
+
op.path
|
|
944
|
+
);
|
|
794
945
|
parent.delete(textRange.start, textRange.end - textRange.start);
|
|
795
946
|
}
|
|
796
947
|
|
|
797
948
|
// src/applyToYjs/node/setNode.ts
|
|
798
949
|
function setNode(sharedRoot, slateRoot, op) {
|
|
799
|
-
const { yTarget, textRange, yParent } = getYTarget(
|
|
950
|
+
const { yTarget, textRange, yParent } = getYTarget(
|
|
951
|
+
sharedRoot,
|
|
952
|
+
slateRoot,
|
|
953
|
+
op.path
|
|
954
|
+
);
|
|
800
955
|
if (yTarget) {
|
|
801
956
|
Object.entries(op.newProperties).forEach(([key, value]) => {
|
|
802
957
|
if (value === null) {
|
|
@@ -810,9 +965,15 @@ function setNode(sharedRoot, slateRoot, op) {
|
|
|
810
965
|
}
|
|
811
966
|
});
|
|
812
967
|
}
|
|
813
|
-
const unset = Object.fromEntries(
|
|
968
|
+
const unset = Object.fromEntries(
|
|
969
|
+
Object.keys(op.properties).map((key) => [key, null])
|
|
970
|
+
);
|
|
814
971
|
const newProperties = __spreadValues(__spreadValues({}, unset), op.newProperties);
|
|
815
|
-
yParent.format(
|
|
972
|
+
yParent.format(
|
|
973
|
+
textRange.start,
|
|
974
|
+
textRange.end - textRange.start,
|
|
975
|
+
newProperties
|
|
976
|
+
);
|
|
816
977
|
}
|
|
817
978
|
|
|
818
979
|
// src/applyToYjs/node/splitNode.ts
|
|
@@ -835,7 +996,11 @@ function splitNode(sharedRoot, slateRoot, op) {
|
|
|
835
996
|
});
|
|
836
997
|
}
|
|
837
998
|
});
|
|
838
|
-
return target.yParent.format(
|
|
999
|
+
return target.yParent.format(
|
|
1000
|
+
target.textRange.start,
|
|
1001
|
+
target.textRange.end - target.textRange.start,
|
|
1002
|
+
__spreadValues(__spreadValues({}, unset), op.properties)
|
|
1003
|
+
);
|
|
839
1004
|
}
|
|
840
1005
|
if (import_slate14.Text.isText(target.slateTarget)) {
|
|
841
1006
|
throw new Error("Mismatch node type between y target and slate node");
|
|
@@ -844,18 +1009,63 @@ function splitNode(sharedRoot, slateRoot, op) {
|
|
|
844
1009
|
op.position
|
|
845
1010
|
]);
|
|
846
1011
|
const ySplitOffset = target.slateTarget.children.slice(0, op.position).reduce((length2, child) => length2 + getSlateNodeYLength(child), 0);
|
|
847
|
-
const length = target.slateTarget.children.reduce(
|
|
848
|
-
|
|
1012
|
+
const length = target.slateTarget.children.reduce(
|
|
1013
|
+
(current, child) => current + getSlateNodeYLength(child),
|
|
1014
|
+
0
|
|
1015
|
+
);
|
|
1016
|
+
const copyHead = ySplitOffset < length - ySplitOffset;
|
|
1017
|
+
const copyStart = copyHead ? 0 : ySplitOffset;
|
|
1018
|
+
const copyLength = copyHead ? ySplitOffset : length - ySplitOffset;
|
|
1019
|
+
const splitDelta = sliceInsertDelta(
|
|
1020
|
+
yTextToInsertDelta(target.yTarget),
|
|
1021
|
+
copyStart,
|
|
1022
|
+
copyLength
|
|
1023
|
+
);
|
|
849
1024
|
const clonedDelta = cloneInsertDeltaDeep(splitDelta);
|
|
850
|
-
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
1025
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
1026
|
+
sharedRoot,
|
|
1027
|
+
target.yTarget,
|
|
1028
|
+
splitDelta,
|
|
1029
|
+
copyStart
|
|
1030
|
+
);
|
|
851
1031
|
const toInsert = new Y7.XmlText();
|
|
852
1032
|
toInsert.applyDelta(clonedDelta, PRESERVE_CONTENT);
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
1033
|
+
if (copyHead) {
|
|
1034
|
+
const { yTarget } = target;
|
|
1035
|
+
const attributes = yTarget.getAttributes();
|
|
1036
|
+
Object.entries(attributes).forEach(([key, value]) => {
|
|
1037
|
+
toInsert.setAttribute(key, value);
|
|
1038
|
+
});
|
|
1039
|
+
Object.keys(attributes).forEach((key) => {
|
|
1040
|
+
if (!(key in op.properties)) {
|
|
1041
|
+
yTarget.removeAttribute(key);
|
|
1042
|
+
}
|
|
1043
|
+
});
|
|
1044
|
+
Object.entries(op.properties).forEach(([key, value]) => {
|
|
1045
|
+
if (attributes[key] !== value) {
|
|
1046
|
+
yTarget.setAttribute(key, value);
|
|
1047
|
+
}
|
|
1048
|
+
});
|
|
1049
|
+
yTarget.delete(0, splitTarget.textRange.start);
|
|
1050
|
+
target.yParent.insertEmbed(target.textRange.start, toInsert);
|
|
1051
|
+
} else {
|
|
1052
|
+
Object.entries(op.properties).forEach(([key, value]) => {
|
|
1053
|
+
toInsert.setAttribute(key, value);
|
|
1054
|
+
});
|
|
1055
|
+
target.yTarget.delete(
|
|
1056
|
+
splitTarget.textRange.start,
|
|
1057
|
+
target.yTarget.length - splitTarget.textRange.start
|
|
1058
|
+
);
|
|
1059
|
+
target.yParent.insertEmbed(target.textRange.end, toInsert);
|
|
1060
|
+
}
|
|
1061
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
1062
|
+
sharedRoot,
|
|
1063
|
+
toInsert,
|
|
1064
|
+
storedPositions,
|
|
1065
|
+
clonedDelta,
|
|
1066
|
+
0,
|
|
1067
|
+
copyStart
|
|
1068
|
+
);
|
|
859
1069
|
}
|
|
860
1070
|
|
|
861
1071
|
// src/applyToYjs/node/index.ts
|
|
@@ -871,17 +1081,29 @@ var NODE_MAPPER = {
|
|
|
871
1081
|
// src/applyToYjs/text/insertText.ts
|
|
872
1082
|
var import_slate15 = require("slate");
|
|
873
1083
|
function insertText(sharedRoot, slateRoot, op) {
|
|
874
|
-
const { yParent: target, textRange } = getYTarget(
|
|
1084
|
+
const { yParent: target, textRange } = getYTarget(
|
|
1085
|
+
sharedRoot,
|
|
1086
|
+
slateRoot,
|
|
1087
|
+
op.path
|
|
1088
|
+
);
|
|
875
1089
|
const targetNode = import_slate15.Node.get(slateRoot, op.path);
|
|
876
1090
|
if (!import_slate15.Text.isText(targetNode)) {
|
|
877
1091
|
throw new Error("Cannot insert text into non-text node");
|
|
878
1092
|
}
|
|
879
|
-
target.insert(
|
|
1093
|
+
target.insert(
|
|
1094
|
+
textRange.start + op.offset,
|
|
1095
|
+
op.text,
|
|
1096
|
+
getProperties(targetNode)
|
|
1097
|
+
);
|
|
880
1098
|
}
|
|
881
1099
|
|
|
882
1100
|
// src/applyToYjs/text/removeText.ts
|
|
883
1101
|
function removeText(sharedRoot, slateRoot, op) {
|
|
884
|
-
const { yParent: target, textRange } = getYTarget(
|
|
1102
|
+
const { yParent: target, textRange } = getYTarget(
|
|
1103
|
+
sharedRoot,
|
|
1104
|
+
slateRoot,
|
|
1105
|
+
op.path
|
|
1106
|
+
);
|
|
885
1107
|
target.delete(textRange.start + op.offset, op.text.length);
|
|
886
1108
|
}
|
|
887
1109
|
|
|
@@ -906,8 +1128,8 @@ function applySlateOp(sharedRoot, slateRoot, op) {
|
|
|
906
1128
|
}
|
|
907
1129
|
|
|
908
1130
|
// src/plugins/withYjs.ts
|
|
909
|
-
var DEFAULT_LOCAL_ORIGIN = Symbol("slate-yjs-operation");
|
|
910
|
-
var DEFAULT_POSITION_STORAGE_ORIGIN = Symbol("slate-yjs-position-storage");
|
|
1131
|
+
var DEFAULT_LOCAL_ORIGIN = /* @__PURE__ */ Symbol("slate-yjs-operation");
|
|
1132
|
+
var DEFAULT_POSITION_STORAGE_ORIGIN = /* @__PURE__ */ Symbol("slate-yjs-position-storage");
|
|
911
1133
|
var ORIGIN = /* @__PURE__ */ new WeakMap();
|
|
912
1134
|
var LOCAL_CHANGES = /* @__PURE__ */ new WeakMap();
|
|
913
1135
|
var CONNECTED = /* @__PURE__ */ new WeakSet();
|
|
@@ -989,7 +1211,9 @@ function withYjs(editor, sharedRoot, {
|
|
|
989
1211
|
e.applyRemoteEvents = (events, origin) => {
|
|
990
1212
|
if (!warnedAboutQueuedChanges && YjsEditor.localChanges(e).length > 0) {
|
|
991
1213
|
warnedAboutQueuedChanges = true;
|
|
992
|
-
console.warn(
|
|
1214
|
+
console.warn(
|
|
1215
|
+
"[slate-yjs] A remote update was applied while local changes were still queued. Call YjsEditor.flushLocalChanges(editor) before applying remote updates in the same task as local edits, or the document can be corrupted."
|
|
1216
|
+
);
|
|
993
1217
|
}
|
|
994
1218
|
YjsEditor.flushLocalChanges(e);
|
|
995
1219
|
import_slate17.Editor.withoutNormalizing(e, () => {
|
|
@@ -1082,7 +1306,9 @@ function withYjs(editor, sharedRoot, {
|
|
|
1082
1306
|
var import_slate18 = require("slate");
|
|
1083
1307
|
var Y9 = __toESM(require("yjs"), 1);
|
|
1084
1308
|
var LAST_SELECTION = /* @__PURE__ */ new WeakMap();
|
|
1085
|
-
var DEFAULT_WITHOUT_SAVING_ORIGIN = Symbol(
|
|
1309
|
+
var DEFAULT_WITHOUT_SAVING_ORIGIN = /* @__PURE__ */ Symbol(
|
|
1310
|
+
"slate-yjs-history-without-saving"
|
|
1311
|
+
);
|
|
1086
1312
|
var YHistoryEditor = {
|
|
1087
1313
|
isYHistoryEditor(value) {
|
|
1088
1314
|
return YjsEditor.isYjsEditor(value) && value.undoManager instanceof Y9.UndoManager && typeof value.undo === "function" && typeof value.redo === "function" && "withoutSavingOrigin" in value;
|
|
@@ -1117,19 +1343,28 @@ function withYHistory(editor, _a = {}) {
|
|
|
1117
1343
|
const { onChange, isLocalOrigin } = e;
|
|
1118
1344
|
e.onChange = () => {
|
|
1119
1345
|
onChange();
|
|
1120
|
-
LAST_SELECTION.set(
|
|
1346
|
+
LAST_SELECTION.set(
|
|
1347
|
+
e,
|
|
1348
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
1349
|
+
);
|
|
1121
1350
|
};
|
|
1122
1351
|
e.isLocalOrigin = (origin) => origin === e.withoutSavingOrigin || isLocalOrigin(origin);
|
|
1123
1352
|
const handleStackItemAdded = ({
|
|
1124
1353
|
stackItem
|
|
1125
1354
|
}) => {
|
|
1126
|
-
stackItem.meta.set(
|
|
1355
|
+
stackItem.meta.set(
|
|
1356
|
+
"selection",
|
|
1357
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
1358
|
+
);
|
|
1127
1359
|
stackItem.meta.set("selectionBefore", LAST_SELECTION.get(e));
|
|
1128
1360
|
};
|
|
1129
1361
|
const handleStackItemUpdated = ({
|
|
1130
1362
|
stackItem
|
|
1131
1363
|
}) => {
|
|
1132
|
-
stackItem.meta.set(
|
|
1364
|
+
stackItem.meta.set(
|
|
1365
|
+
"selection",
|
|
1366
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
1367
|
+
);
|
|
1133
1368
|
};
|
|
1134
1369
|
const handleStackItemPopped = ({
|
|
1135
1370
|
stackItem,
|
|
@@ -1141,11 +1376,17 @@ function withYHistory(editor, _a = {}) {
|
|
|
1141
1376
|
inverseItem.meta.set("selection", stackItem.meta.get("selectionBefore"));
|
|
1142
1377
|
inverseItem.meta.set("selectionBefore", stackItem.meta.get("selection"));
|
|
1143
1378
|
}
|
|
1144
|
-
const relativeSelection = stackItem.meta.get(
|
|
1379
|
+
const relativeSelection = stackItem.meta.get(
|
|
1380
|
+
"selectionBefore"
|
|
1381
|
+
);
|
|
1145
1382
|
if (!relativeSelection) {
|
|
1146
1383
|
return;
|
|
1147
1384
|
}
|
|
1148
|
-
const selection = relativeRangeToSlateRange(
|
|
1385
|
+
const selection = relativeRangeToSlateRange(
|
|
1386
|
+
e.sharedRoot,
|
|
1387
|
+
e,
|
|
1388
|
+
relativeSelection
|
|
1389
|
+
);
|
|
1149
1390
|
if (!selection) {
|
|
1150
1391
|
return;
|
|
1151
1392
|
}
|
|
@@ -1229,18 +1470,20 @@ var CursorEditor = {
|
|
|
1229
1470
|
if (!YjsEditor.connected(editor)) {
|
|
1230
1471
|
return {};
|
|
1231
1472
|
}
|
|
1232
|
-
return Object.fromEntries(
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
return [
|
|
1237
|
-
id,
|
|
1238
|
-
{
|
|
1239
|
-
relativeSelection: state[editor.selectionStateField],
|
|
1240
|
-
data: state[editor.cursorDataField]
|
|
1473
|
+
return Object.fromEntries(
|
|
1474
|
+
Array.from(editor.awareness.getStates().entries(), ([id, state]) => {
|
|
1475
|
+
if (id === editor.awareness.clientID || !state) {
|
|
1476
|
+
return null;
|
|
1241
1477
|
}
|
|
1242
|
-
|
|
1243
|
-
|
|
1478
|
+
return [
|
|
1479
|
+
id,
|
|
1480
|
+
{
|
|
1481
|
+
relativeSelection: state[editor.selectionStateField],
|
|
1482
|
+
data: state[editor.cursorDataField]
|
|
1483
|
+
}
|
|
1484
|
+
];
|
|
1485
|
+
}).filter(Array.isArray)
|
|
1486
|
+
);
|
|
1244
1487
|
}
|
|
1245
1488
|
};
|
|
1246
1489
|
function withCursors(editor, awareness, {
|