@slate-yjs/core 1.0.5 → 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 +12 -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 +351 -100
- package/dist/index.cjs.map +1 -1
- package/dist/index.global.js +3167 -570
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +339 -96
- 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/dist/utils/position.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/plugins/withCursors.ts +5 -7
- package/src/utils/object.ts +5 -3
- package/src/utils/position.ts +16 -3
package/dist/index.js
CHANGED
|
@@ -76,8 +76,7 @@ function deepEquals(node, another) {
|
|
|
76
76
|
return false;
|
|
77
77
|
}
|
|
78
78
|
} else if (Array.isArray(a) && Array.isArray(b)) {
|
|
79
|
-
if (a.length !== b.length)
|
|
80
|
-
return false;
|
|
79
|
+
if (a.length !== b.length) return false;
|
|
81
80
|
for (let i = 0; i < a.length; i++) {
|
|
82
81
|
if (a[i] !== b[i]) {
|
|
83
82
|
return false;
|
|
@@ -95,13 +94,19 @@ function deepEquals(node, another) {
|
|
|
95
94
|
return true;
|
|
96
95
|
}
|
|
97
96
|
function pick(obj, ...keys) {
|
|
98
|
-
return Object.fromEntries(
|
|
97
|
+
return Object.fromEntries(
|
|
98
|
+
Object.entries(obj).filter(([key]) => keys.includes(key))
|
|
99
|
+
);
|
|
99
100
|
}
|
|
100
101
|
function omit(obj, ...keys) {
|
|
101
|
-
return Object.fromEntries(
|
|
102
|
+
return Object.fromEntries(
|
|
103
|
+
Object.entries(obj).filter(([key]) => !keys.includes(key))
|
|
104
|
+
);
|
|
102
105
|
}
|
|
103
106
|
function omitNullEntries(obj) {
|
|
104
|
-
return Object.fromEntries(
|
|
107
|
+
return Object.fromEntries(
|
|
108
|
+
Object.entries(obj).filter(([, value]) => value !== null)
|
|
109
|
+
);
|
|
105
110
|
}
|
|
106
111
|
|
|
107
112
|
// src/utils/delta.ts
|
|
@@ -158,7 +163,10 @@ function sliceInsertDelta(delta, start, length) {
|
|
|
158
163
|
continue;
|
|
159
164
|
}
|
|
160
165
|
const startOffset = Math.max(0, start - currentOffset);
|
|
161
|
-
const endOffset = Math.min(
|
|
166
|
+
const endOffset = Math.min(
|
|
167
|
+
elementLength,
|
|
168
|
+
elementLength - (currentOffset + elementLength - end)
|
|
169
|
+
);
|
|
162
170
|
sliced.push(__spreadProps(__spreadValues({}, element), {
|
|
163
171
|
insert: element.insert.slice(startOffset, endOffset)
|
|
164
172
|
}));
|
|
@@ -170,7 +178,10 @@ function sliceInsertDelta(delta, start, length) {
|
|
|
170
178
|
// src/utils/slate.ts
|
|
171
179
|
import { Text } from "slate";
|
|
172
180
|
function getProperties(node) {
|
|
173
|
-
return omit(
|
|
181
|
+
return omit(
|
|
182
|
+
node,
|
|
183
|
+
Text.isText(node) ? "text" : "children"
|
|
184
|
+
);
|
|
174
185
|
}
|
|
175
186
|
|
|
176
187
|
// src/utils/yjs.ts
|
|
@@ -184,7 +195,10 @@ var PRESERVE_CONTENT = { sanitize: true };
|
|
|
184
195
|
// src/utils/convert.ts
|
|
185
196
|
function yTextToSlateElement(yText) {
|
|
186
197
|
const delta = yTextToInsertDelta(yText);
|
|
187
|
-
const children =
|
|
198
|
+
const children = (
|
|
199
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
200
|
+
delta.length > 0 ? delta.map(deltaInsertToSlateNode) : [{ text: "" }]
|
|
201
|
+
);
|
|
188
202
|
return __spreadProps(__spreadValues({}, yText.getAttributes()), { children });
|
|
189
203
|
}
|
|
190
204
|
function deltaInsertToSlateNode(insert) {
|
|
@@ -247,7 +261,9 @@ function getYTarget(yRoot, slateRoot, path) {
|
|
|
247
261
|
const yTarget = (_a = targetDelta[0]) == null ? void 0 : _a.insert;
|
|
248
262
|
if (childPath.length > 0) {
|
|
249
263
|
if (!(yTarget instanceof Y2.XmlText)) {
|
|
250
|
-
throw new Error(
|
|
264
|
+
throw new Error(
|
|
265
|
+
"Path doesn't match yText, cannot descent into non-yText"
|
|
266
|
+
);
|
|
251
267
|
}
|
|
252
268
|
return getYTarget(yTarget, targetNode, childPath);
|
|
253
269
|
}
|
|
@@ -339,8 +355,15 @@ function applyDelta(node, slatePath, delta) {
|
|
|
339
355
|
delta.reverse().forEach((change) => {
|
|
340
356
|
var _a;
|
|
341
357
|
if ("attributes" in change && "retain" in change) {
|
|
342
|
-
const [startPathOffset, startTextOffset] = yOffsetToSlateOffsets(
|
|
343
|
-
|
|
358
|
+
const [startPathOffset, startTextOffset] = yOffsetToSlateOffsets(
|
|
359
|
+
node,
|
|
360
|
+
yOffset - change.retain
|
|
361
|
+
);
|
|
362
|
+
const [endPathOffset, endTextOffset] = yOffsetToSlateOffsets(
|
|
363
|
+
node,
|
|
364
|
+
yOffset,
|
|
365
|
+
{ assoc: -1 }
|
|
366
|
+
);
|
|
344
367
|
for (let pathOffset = endPathOffset; pathOffset >= startPathOffset; pathOffset--) {
|
|
345
368
|
const child = node.children[pathOffset];
|
|
346
369
|
const childPath = [...slatePath, pathOffset];
|
|
@@ -348,7 +371,10 @@ function applyDelta(node, slatePath, delta) {
|
|
|
348
371
|
continue;
|
|
349
372
|
}
|
|
350
373
|
const newProperties = change.attributes;
|
|
351
|
-
const properties = pick(
|
|
374
|
+
const properties = pick(
|
|
375
|
+
node,
|
|
376
|
+
...Object.keys(change.attributes)
|
|
377
|
+
);
|
|
352
378
|
if (pathOffset === startPathOffset || pathOffset === endPathOffset) {
|
|
353
379
|
const start = pathOffset === startPathOffset ? startTextOffset : 0;
|
|
354
380
|
const end = pathOffset === endPathOffset ? endTextOffset : child.text.length;
|
|
@@ -382,8 +408,15 @@ function applyDelta(node, slatePath, delta) {
|
|
|
382
408
|
yOffset -= change.retain;
|
|
383
409
|
}
|
|
384
410
|
if ("delete" in change) {
|
|
385
|
-
const [startPathOffset, startTextOffset] = yOffsetToSlateOffsets(
|
|
386
|
-
|
|
411
|
+
const [startPathOffset, startTextOffset] = yOffsetToSlateOffsets(
|
|
412
|
+
node,
|
|
413
|
+
yOffset - change.delete
|
|
414
|
+
);
|
|
415
|
+
const [endPathOffset, endTextOffset] = yOffsetToSlateOffsets(
|
|
416
|
+
node,
|
|
417
|
+
yOffset,
|
|
418
|
+
{ assoc: -1 }
|
|
419
|
+
);
|
|
387
420
|
for (let pathOffset = endTextOffset === 0 ? endPathOffset - 1 : endPathOffset; pathOffset >= startPathOffset; pathOffset--) {
|
|
388
421
|
const child = node.children[pathOffset];
|
|
389
422
|
const childPath = [...slatePath, pathOffset];
|
|
@@ -474,11 +507,15 @@ function translateYTextEvent(sharedRoot, editor, event) {
|
|
|
474
507
|
}
|
|
475
508
|
const keyChanges = Array.from(changes.keys.entries());
|
|
476
509
|
if (slatePath.length > 0 && keyChanges.length > 0) {
|
|
477
|
-
const newProperties = Object.fromEntries(
|
|
478
|
-
key,
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
510
|
+
const newProperties = Object.fromEntries(
|
|
511
|
+
keyChanges.map(([key, info]) => [
|
|
512
|
+
key,
|
|
513
|
+
info.action === "delete" ? null : target.getAttribute(key)
|
|
514
|
+
])
|
|
515
|
+
);
|
|
516
|
+
const properties = Object.fromEntries(
|
|
517
|
+
keyChanges.map(([key]) => [key, Reflect.get(targetElement, key)])
|
|
518
|
+
);
|
|
482
519
|
ops.push({ type: "set_node", newProperties, properties, path: slatePath });
|
|
483
520
|
}
|
|
484
521
|
if (delta.length > 0) {
|
|
@@ -509,7 +546,11 @@ import { Text as Text5 } from "slate";
|
|
|
509
546
|
function insertNode(sharedRoot, slateRoot, op) {
|
|
510
547
|
const { yParent, textRange } = getYTarget(sharedRoot, slateRoot, op.path);
|
|
511
548
|
if (Text5.isText(op.node)) {
|
|
512
|
-
return yParent.insert(
|
|
549
|
+
return yParent.insert(
|
|
550
|
+
textRange.start,
|
|
551
|
+
op.node.text,
|
|
552
|
+
getProperties(op.node)
|
|
553
|
+
);
|
|
513
554
|
}
|
|
514
555
|
yParent.insertEmbed(textRange.start, slateElementToYText(op.node));
|
|
515
556
|
}
|
|
@@ -533,7 +574,10 @@ function cloneDeep(yText) {
|
|
|
533
574
|
Object.entries(attributes).forEach(([key, value]) => {
|
|
534
575
|
clone.setAttribute(key, value);
|
|
535
576
|
});
|
|
536
|
-
clone.applyDelta(
|
|
577
|
+
clone.applyDelta(
|
|
578
|
+
cloneInsertDeltaDeep(yTextToInsertDelta(yText)),
|
|
579
|
+
PRESERVE_CONTENT
|
|
580
|
+
);
|
|
537
581
|
return clone;
|
|
538
582
|
}
|
|
539
583
|
|
|
@@ -542,12 +586,22 @@ import { Node as Node5, Text as Text6 } from "slate";
|
|
|
542
586
|
import * as Y6 from "yjs";
|
|
543
587
|
var STORED_POSITION_PREFIX = "__slateYjsStoredPosition_";
|
|
544
588
|
function slatePointToRelativePosition(sharedRoot, slateRoot, point) {
|
|
545
|
-
const { yTarget, yParent, textRange } = getYTarget(
|
|
589
|
+
const { yTarget, yParent, textRange } = getYTarget(
|
|
590
|
+
sharedRoot,
|
|
591
|
+
slateRoot,
|
|
592
|
+
point.path
|
|
593
|
+
);
|
|
546
594
|
if (yTarget) {
|
|
547
|
-
throw new Error(
|
|
595
|
+
throw new Error(
|
|
596
|
+
"Slate point points to a non-text element inside sharedRoot"
|
|
597
|
+
);
|
|
548
598
|
}
|
|
549
599
|
const index = textRange.start + point.offset;
|
|
550
|
-
return Y6.createRelativePositionFromTypeIndex(
|
|
600
|
+
return Y6.createRelativePositionFromTypeIndex(
|
|
601
|
+
yParent,
|
|
602
|
+
index,
|
|
603
|
+
index === textRange.end ? -1 : 0
|
|
604
|
+
);
|
|
551
605
|
}
|
|
552
606
|
function absolutePositionToSlatePoint(sharedRoot, slateRoot, { type, index, assoc }) {
|
|
553
607
|
if (!(type instanceof Y6.XmlText)) {
|
|
@@ -556,22 +610,35 @@ function absolutePositionToSlatePoint(sharedRoot, slateRoot, { type, index, asso
|
|
|
556
610
|
const parentPath = getSlatePath(sharedRoot, slateRoot, type);
|
|
557
611
|
const parent = Node5.get(slateRoot, parentPath);
|
|
558
612
|
if (Text6.isText(parent)) {
|
|
559
|
-
throw new Error(
|
|
613
|
+
throw new Error(
|
|
614
|
+
"Absolute position doesn't match slateRoot, cannot descent into text"
|
|
615
|
+
);
|
|
560
616
|
}
|
|
561
617
|
const [pathOffset, textOffset] = yOffsetToSlateOffsets(parent, index, {
|
|
562
618
|
assoc
|
|
563
619
|
});
|
|
564
620
|
const target = parent.children[pathOffset];
|
|
565
|
-
if (
|
|
621
|
+
if (Text6.isText(target)) {
|
|
622
|
+
return { path: [...parentPath, pathOffset], offset: textOffset };
|
|
623
|
+
}
|
|
624
|
+
const sideOffset = textOffset === 0 ? pathOffset - 1 : pathOffset + 1;
|
|
625
|
+
const side = parent.children[sideOffset];
|
|
626
|
+
if (!Text6.isText(side)) {
|
|
566
627
|
return null;
|
|
567
628
|
}
|
|
568
|
-
return {
|
|
629
|
+
return {
|
|
630
|
+
path: [...parentPath, sideOffset],
|
|
631
|
+
offset: textOffset === 0 ? side.text.length : 0
|
|
632
|
+
};
|
|
569
633
|
}
|
|
570
634
|
function relativePositionToSlatePoint(sharedRoot, slateRoot, pos) {
|
|
571
635
|
if (!sharedRoot.doc) {
|
|
572
636
|
throw new Error("sharedRoot isn't attach to a yDoc");
|
|
573
637
|
}
|
|
574
|
-
const absPos = Y6.createAbsolutePositionFromRelativePosition(
|
|
638
|
+
const absPos = Y6.createAbsolutePositionFromRelativePosition(
|
|
639
|
+
pos,
|
|
640
|
+
sharedRoot.doc
|
|
641
|
+
);
|
|
575
642
|
return absPos && absolutePositionToSlatePoint(sharedRoot, slateRoot, absPos);
|
|
576
643
|
}
|
|
577
644
|
function getStoredPosition(sharedRoot, key) {
|
|
@@ -582,34 +649,48 @@ function getStoredPosition(sharedRoot, key) {
|
|
|
582
649
|
return Y6.decodeRelativePosition(rawPosition);
|
|
583
650
|
}
|
|
584
651
|
function getStoredPositions(sharedRoot) {
|
|
585
|
-
return Object.fromEntries(
|
|
586
|
-
key.
|
|
587
|
-
|
|
588
|
-
|
|
652
|
+
return Object.fromEntries(
|
|
653
|
+
Object.entries(sharedRoot.getAttributes()).filter(([key]) => key.startsWith(STORED_POSITION_PREFIX)).map(([key, position]) => [
|
|
654
|
+
key.slice(STORED_POSITION_PREFIX.length),
|
|
655
|
+
Y6.decodeRelativePosition(position)
|
|
656
|
+
])
|
|
657
|
+
);
|
|
589
658
|
}
|
|
590
659
|
function getStoredPositionsAbsolute(sharedRoot) {
|
|
591
660
|
assertDocumentAttachment(sharedRoot);
|
|
592
|
-
return Object.fromEntries(
|
|
593
|
-
key.
|
|
594
|
-
|
|
595
|
-
|
|
661
|
+
return Object.fromEntries(
|
|
662
|
+
Object.entries(sharedRoot.getAttributes()).filter(([key]) => key.startsWith(STORED_POSITION_PREFIX)).map(
|
|
663
|
+
([key, position]) => [
|
|
664
|
+
key.slice(STORED_POSITION_PREFIX.length),
|
|
665
|
+
Y6.createAbsolutePositionFromRelativePosition(
|
|
666
|
+
Y6.decodeRelativePosition(position),
|
|
667
|
+
sharedRoot.doc
|
|
668
|
+
)
|
|
669
|
+
]
|
|
670
|
+
).filter(([, position]) => position)
|
|
671
|
+
);
|
|
596
672
|
}
|
|
597
673
|
function removeStoredPosition(sharedRoot, key) {
|
|
598
674
|
sharedRoot.removeAttribute(STORED_POSITION_PREFIX + key);
|
|
599
675
|
}
|
|
600
676
|
function setStoredPosition(sharedRoot, key, position) {
|
|
601
|
-
sharedRoot.setAttribute(
|
|
677
|
+
sharedRoot.setAttribute(
|
|
678
|
+
STORED_POSITION_PREFIX + key,
|
|
679
|
+
Y6.encodeRelativePosition(position)
|
|
680
|
+
);
|
|
602
681
|
}
|
|
603
682
|
function getAbsolutePositionsInTextRange(absolutePositions, yTarget, textRange) {
|
|
604
|
-
return Object.fromEntries(
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
683
|
+
return Object.fromEntries(
|
|
684
|
+
Object.entries(absolutePositions).filter(([, position]) => {
|
|
685
|
+
if (position.type !== yTarget) {
|
|
686
|
+
return false;
|
|
687
|
+
}
|
|
688
|
+
if (!textRange) {
|
|
689
|
+
return true;
|
|
690
|
+
}
|
|
691
|
+
return position.assoc >= 0 ? position.index >= textRange.start && position.index < textRange.end : position.index > textRange.start && position.index <= textRange.end;
|
|
692
|
+
})
|
|
693
|
+
);
|
|
613
694
|
}
|
|
614
695
|
function getAbsolutePositionsInYText(absolutePositions, yText, parentPath = "") {
|
|
615
696
|
const positions = {
|
|
@@ -618,7 +699,14 @@ function getAbsolutePositionsInYText(absolutePositions, yText, parentPath = "")
|
|
|
618
699
|
const insertDelta = yTextToInsertDelta(yText);
|
|
619
700
|
insertDelta.forEach(({ insert }, i) => {
|
|
620
701
|
if (insert instanceof Y6.XmlText) {
|
|
621
|
-
Object.assign(
|
|
702
|
+
Object.assign(
|
|
703
|
+
positions,
|
|
704
|
+
getAbsolutePositionsInYText(
|
|
705
|
+
absolutePositions,
|
|
706
|
+
insert,
|
|
707
|
+
parentPath ? `${parentPath}.${i}` : i.toString()
|
|
708
|
+
)
|
|
709
|
+
);
|
|
622
710
|
}
|
|
623
711
|
});
|
|
624
712
|
return positions;
|
|
@@ -633,7 +721,10 @@ function getStoredPositionsInDeltaAbsolute(sharedRoot, yText, delta, deltaOffset
|
|
|
633
721
|
};
|
|
634
722
|
delta.forEach(({ insert }, i) => {
|
|
635
723
|
if (insert instanceof Y6.XmlText) {
|
|
636
|
-
Object.assign(
|
|
724
|
+
Object.assign(
|
|
725
|
+
positions,
|
|
726
|
+
getAbsolutePositionsInYText(absolutePositions, insert, i.toString())
|
|
727
|
+
);
|
|
637
728
|
}
|
|
638
729
|
});
|
|
639
730
|
return positions;
|
|
@@ -642,12 +733,28 @@ function restoreStoredPositionsWithDeltaAbsolute(sharedRoot, yText, absolutePosi
|
|
|
642
733
|
const toRestore = absolutePositions[path];
|
|
643
734
|
if (toRestore) {
|
|
644
735
|
Object.entries(toRestore).forEach(([key, position]) => {
|
|
645
|
-
setStoredPosition(
|
|
736
|
+
setStoredPosition(
|
|
737
|
+
sharedRoot,
|
|
738
|
+
key,
|
|
739
|
+
Y6.createRelativePositionFromTypeIndex(
|
|
740
|
+
yText,
|
|
741
|
+
position.index - previousDeltaOffset + newDeltaOffset,
|
|
742
|
+
position.assoc
|
|
743
|
+
)
|
|
744
|
+
);
|
|
646
745
|
});
|
|
647
746
|
}
|
|
648
747
|
delta.forEach(({ insert }, i) => {
|
|
649
748
|
if (insert instanceof Y6.XmlText) {
|
|
650
|
-
restoreStoredPositionsWithDeltaAbsolute(
|
|
749
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
750
|
+
sharedRoot,
|
|
751
|
+
insert,
|
|
752
|
+
absolutePositions,
|
|
753
|
+
yTextToInsertDelta(insert),
|
|
754
|
+
0,
|
|
755
|
+
0,
|
|
756
|
+
path ? `${path}.${i}` : i.toString()
|
|
757
|
+
);
|
|
651
758
|
}
|
|
652
759
|
});
|
|
653
760
|
}
|
|
@@ -658,11 +765,19 @@ function slateRangeToRelativeRange(sharedRoot, slateRoot, range) {
|
|
|
658
765
|
};
|
|
659
766
|
}
|
|
660
767
|
function relativeRangeToSlateRange(sharedRoot, slateRoot, range) {
|
|
661
|
-
const anchor = relativePositionToSlatePoint(
|
|
768
|
+
const anchor = relativePositionToSlatePoint(
|
|
769
|
+
sharedRoot,
|
|
770
|
+
slateRoot,
|
|
771
|
+
range.anchor
|
|
772
|
+
);
|
|
662
773
|
if (!anchor) {
|
|
663
774
|
return null;
|
|
664
775
|
}
|
|
665
|
-
const focus = relativePositionToSlatePoint(
|
|
776
|
+
const focus = relativePositionToSlatePoint(
|
|
777
|
+
sharedRoot,
|
|
778
|
+
slateRoot,
|
|
779
|
+
range.focus
|
|
780
|
+
);
|
|
666
781
|
if (!focus) {
|
|
667
782
|
return null;
|
|
668
783
|
}
|
|
@@ -672,7 +787,11 @@ function relativeRangeToSlateRange(sharedRoot, slateRoot, range) {
|
|
|
672
787
|
// src/applyToYjs/node/mergeNode.ts
|
|
673
788
|
function mergeNode(sharedRoot, slateRoot, op) {
|
|
674
789
|
const target = getYTarget(sharedRoot, slateRoot, op.path);
|
|
675
|
-
const prev = getYTarget(
|
|
790
|
+
const prev = getYTarget(
|
|
791
|
+
target.yParent,
|
|
792
|
+
target.slateParent,
|
|
793
|
+
Path3.previous(op.path.slice(-1))
|
|
794
|
+
);
|
|
676
795
|
if (!target.yTarget !== !prev.yTarget) {
|
|
677
796
|
throw new Error("Cannot merge y text with y element");
|
|
678
797
|
}
|
|
@@ -696,11 +815,25 @@ function mergeNode(sharedRoot, slateRoot, op) {
|
|
|
696
815
|
const deltaApplyYOffset = prev.yTarget.length;
|
|
697
816
|
const targetDelta = yTextToInsertDelta(target.yTarget);
|
|
698
817
|
const clonedDelta = cloneInsertDeltaDeep(targetDelta);
|
|
699
|
-
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
818
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
819
|
+
sharedRoot,
|
|
820
|
+
target.yTarget,
|
|
821
|
+
targetDelta,
|
|
822
|
+
deltaApplyYOffset
|
|
823
|
+
);
|
|
700
824
|
const applyDelta2 = [{ retain: deltaApplyYOffset }, ...clonedDelta];
|
|
701
825
|
prev.yTarget.applyDelta(applyDelta2, PRESERVE_CONTENT);
|
|
702
|
-
target.yParent.delete(
|
|
703
|
-
|
|
826
|
+
target.yParent.delete(
|
|
827
|
+
target.textRange.start,
|
|
828
|
+
target.textRange.end - target.textRange.start
|
|
829
|
+
);
|
|
830
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
831
|
+
sharedRoot,
|
|
832
|
+
prev.yTarget,
|
|
833
|
+
storedPositions,
|
|
834
|
+
clonedDelta,
|
|
835
|
+
deltaApplyYOffset
|
|
836
|
+
);
|
|
704
837
|
}
|
|
705
838
|
|
|
706
839
|
// src/applyToYjs/node/moveNode.ts
|
|
@@ -721,8 +854,15 @@ function withoutNode(root, path) {
|
|
|
721
854
|
function moveNode(sharedRoot, slateRoot, op) {
|
|
722
855
|
const origin = getYTarget(sharedRoot, slateRoot, op.path);
|
|
723
856
|
const insertDelta = cloneInsertDeltaDeep(origin.targetDelta);
|
|
724
|
-
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
725
|
-
|
|
857
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
858
|
+
sharedRoot,
|
|
859
|
+
origin.yParent,
|
|
860
|
+
origin.targetDelta
|
|
861
|
+
);
|
|
862
|
+
origin.yParent.delete(
|
|
863
|
+
origin.textRange.start,
|
|
864
|
+
origin.textRange.end - origin.textRange.start
|
|
865
|
+
);
|
|
726
866
|
const rootWithoutNode = withoutNode(slateRoot, op.path);
|
|
727
867
|
const newPath = Path4.transform(op.path, op);
|
|
728
868
|
if (!newPath) {
|
|
@@ -741,18 +881,33 @@ function moveNode(sharedRoot, slateRoot, op) {
|
|
|
741
881
|
const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
|
|
742
882
|
const applyDelta2 = [{ retain: deltaApplyYOffset }, ...insertDelta];
|
|
743
883
|
target.yParent.applyDelta(applyDelta2, PRESERVE_CONTENT);
|
|
744
|
-
restoreStoredPositionsWithDeltaAbsolute(
|
|
884
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
885
|
+
sharedRoot,
|
|
886
|
+
target.yParent,
|
|
887
|
+
storedPositions,
|
|
888
|
+
insertDelta,
|
|
889
|
+
deltaApplyYOffset,
|
|
890
|
+
origin.textRange.start
|
|
891
|
+
);
|
|
745
892
|
}
|
|
746
893
|
|
|
747
894
|
// src/applyToYjs/node/removeNode.ts
|
|
748
895
|
function removeNode(sharedRoot, slateRoot, op) {
|
|
749
|
-
const { yParent: parent, textRange } = getYTarget(
|
|
896
|
+
const { yParent: parent, textRange } = getYTarget(
|
|
897
|
+
sharedRoot,
|
|
898
|
+
slateRoot,
|
|
899
|
+
op.path
|
|
900
|
+
);
|
|
750
901
|
parent.delete(textRange.start, textRange.end - textRange.start);
|
|
751
902
|
}
|
|
752
903
|
|
|
753
904
|
// src/applyToYjs/node/setNode.ts
|
|
754
905
|
function setNode(sharedRoot, slateRoot, op) {
|
|
755
|
-
const { yTarget, textRange, yParent } = getYTarget(
|
|
906
|
+
const { yTarget, textRange, yParent } = getYTarget(
|
|
907
|
+
sharedRoot,
|
|
908
|
+
slateRoot,
|
|
909
|
+
op.path
|
|
910
|
+
);
|
|
756
911
|
if (yTarget) {
|
|
757
912
|
Object.entries(op.newProperties).forEach(([key, value]) => {
|
|
758
913
|
if (value === null) {
|
|
@@ -766,9 +921,15 @@ function setNode(sharedRoot, slateRoot, op) {
|
|
|
766
921
|
}
|
|
767
922
|
});
|
|
768
923
|
}
|
|
769
|
-
const unset = Object.fromEntries(
|
|
924
|
+
const unset = Object.fromEntries(
|
|
925
|
+
Object.keys(op.properties).map((key) => [key, null])
|
|
926
|
+
);
|
|
770
927
|
const newProperties = __spreadValues(__spreadValues({}, unset), op.newProperties);
|
|
771
|
-
yParent.format(
|
|
928
|
+
yParent.format(
|
|
929
|
+
textRange.start,
|
|
930
|
+
textRange.end - textRange.start,
|
|
931
|
+
newProperties
|
|
932
|
+
);
|
|
772
933
|
}
|
|
773
934
|
|
|
774
935
|
// src/applyToYjs/node/splitNode.ts
|
|
@@ -791,7 +952,11 @@ function splitNode(sharedRoot, slateRoot, op) {
|
|
|
791
952
|
});
|
|
792
953
|
}
|
|
793
954
|
});
|
|
794
|
-
return target.yParent.format(
|
|
955
|
+
return target.yParent.format(
|
|
956
|
+
target.textRange.start,
|
|
957
|
+
target.textRange.end - target.textRange.start,
|
|
958
|
+
__spreadValues(__spreadValues({}, unset), op.properties)
|
|
959
|
+
);
|
|
795
960
|
}
|
|
796
961
|
if (Text9.isText(target.slateTarget)) {
|
|
797
962
|
throw new Error("Mismatch node type between y target and slate node");
|
|
@@ -800,18 +965,63 @@ function splitNode(sharedRoot, slateRoot, op) {
|
|
|
800
965
|
op.position
|
|
801
966
|
]);
|
|
802
967
|
const ySplitOffset = target.slateTarget.children.slice(0, op.position).reduce((length2, child) => length2 + getSlateNodeYLength(child), 0);
|
|
803
|
-
const length = target.slateTarget.children.reduce(
|
|
804
|
-
|
|
968
|
+
const length = target.slateTarget.children.reduce(
|
|
969
|
+
(current, child) => current + getSlateNodeYLength(child),
|
|
970
|
+
0
|
|
971
|
+
);
|
|
972
|
+
const copyHead = ySplitOffset < length - ySplitOffset;
|
|
973
|
+
const copyStart = copyHead ? 0 : ySplitOffset;
|
|
974
|
+
const copyLength = copyHead ? ySplitOffset : length - ySplitOffset;
|
|
975
|
+
const splitDelta = sliceInsertDelta(
|
|
976
|
+
yTextToInsertDelta(target.yTarget),
|
|
977
|
+
copyStart,
|
|
978
|
+
copyLength
|
|
979
|
+
);
|
|
805
980
|
const clonedDelta = cloneInsertDeltaDeep(splitDelta);
|
|
806
|
-
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
981
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
982
|
+
sharedRoot,
|
|
983
|
+
target.yTarget,
|
|
984
|
+
splitDelta,
|
|
985
|
+
copyStart
|
|
986
|
+
);
|
|
807
987
|
const toInsert = new Y7.XmlText();
|
|
808
988
|
toInsert.applyDelta(clonedDelta, PRESERVE_CONTENT);
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
989
|
+
if (copyHead) {
|
|
990
|
+
const { yTarget } = target;
|
|
991
|
+
const attributes = yTarget.getAttributes();
|
|
992
|
+
Object.entries(attributes).forEach(([key, value]) => {
|
|
993
|
+
toInsert.setAttribute(key, value);
|
|
994
|
+
});
|
|
995
|
+
Object.keys(attributes).forEach((key) => {
|
|
996
|
+
if (!(key in op.properties)) {
|
|
997
|
+
yTarget.removeAttribute(key);
|
|
998
|
+
}
|
|
999
|
+
});
|
|
1000
|
+
Object.entries(op.properties).forEach(([key, value]) => {
|
|
1001
|
+
if (attributes[key] !== value) {
|
|
1002
|
+
yTarget.setAttribute(key, value);
|
|
1003
|
+
}
|
|
1004
|
+
});
|
|
1005
|
+
yTarget.delete(0, splitTarget.textRange.start);
|
|
1006
|
+
target.yParent.insertEmbed(target.textRange.start, toInsert);
|
|
1007
|
+
} else {
|
|
1008
|
+
Object.entries(op.properties).forEach(([key, value]) => {
|
|
1009
|
+
toInsert.setAttribute(key, value);
|
|
1010
|
+
});
|
|
1011
|
+
target.yTarget.delete(
|
|
1012
|
+
splitTarget.textRange.start,
|
|
1013
|
+
target.yTarget.length - splitTarget.textRange.start
|
|
1014
|
+
);
|
|
1015
|
+
target.yParent.insertEmbed(target.textRange.end, toInsert);
|
|
1016
|
+
}
|
|
1017
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
1018
|
+
sharedRoot,
|
|
1019
|
+
toInsert,
|
|
1020
|
+
storedPositions,
|
|
1021
|
+
clonedDelta,
|
|
1022
|
+
0,
|
|
1023
|
+
copyStart
|
|
1024
|
+
);
|
|
815
1025
|
}
|
|
816
1026
|
|
|
817
1027
|
// src/applyToYjs/node/index.ts
|
|
@@ -827,17 +1037,29 @@ var NODE_MAPPER = {
|
|
|
827
1037
|
// src/applyToYjs/text/insertText.ts
|
|
828
1038
|
import { Node as Node9, Text as Text10 } from "slate";
|
|
829
1039
|
function insertText(sharedRoot, slateRoot, op) {
|
|
830
|
-
const { yParent: target, textRange } = getYTarget(
|
|
1040
|
+
const { yParent: target, textRange } = getYTarget(
|
|
1041
|
+
sharedRoot,
|
|
1042
|
+
slateRoot,
|
|
1043
|
+
op.path
|
|
1044
|
+
);
|
|
831
1045
|
const targetNode = Node9.get(slateRoot, op.path);
|
|
832
1046
|
if (!Text10.isText(targetNode)) {
|
|
833
1047
|
throw new Error("Cannot insert text into non-text node");
|
|
834
1048
|
}
|
|
835
|
-
target.insert(
|
|
1049
|
+
target.insert(
|
|
1050
|
+
textRange.start + op.offset,
|
|
1051
|
+
op.text,
|
|
1052
|
+
getProperties(targetNode)
|
|
1053
|
+
);
|
|
836
1054
|
}
|
|
837
1055
|
|
|
838
1056
|
// src/applyToYjs/text/removeText.ts
|
|
839
1057
|
function removeText(sharedRoot, slateRoot, op) {
|
|
840
|
-
const { yParent: target, textRange } = getYTarget(
|
|
1058
|
+
const { yParent: target, textRange } = getYTarget(
|
|
1059
|
+
sharedRoot,
|
|
1060
|
+
slateRoot,
|
|
1061
|
+
op.path
|
|
1062
|
+
);
|
|
841
1063
|
target.delete(textRange.start + op.offset, op.text.length);
|
|
842
1064
|
}
|
|
843
1065
|
|
|
@@ -862,8 +1084,8 @@ function applySlateOp(sharedRoot, slateRoot, op) {
|
|
|
862
1084
|
}
|
|
863
1085
|
|
|
864
1086
|
// src/plugins/withYjs.ts
|
|
865
|
-
var DEFAULT_LOCAL_ORIGIN = Symbol("slate-yjs-operation");
|
|
866
|
-
var DEFAULT_POSITION_STORAGE_ORIGIN = Symbol("slate-yjs-position-storage");
|
|
1087
|
+
var DEFAULT_LOCAL_ORIGIN = /* @__PURE__ */ Symbol("slate-yjs-operation");
|
|
1088
|
+
var DEFAULT_POSITION_STORAGE_ORIGIN = /* @__PURE__ */ Symbol("slate-yjs-position-storage");
|
|
867
1089
|
var ORIGIN = /* @__PURE__ */ new WeakMap();
|
|
868
1090
|
var LOCAL_CHANGES = /* @__PURE__ */ new WeakMap();
|
|
869
1091
|
var CONNECTED = /* @__PURE__ */ new WeakSet();
|
|
@@ -945,7 +1167,9 @@ function withYjs(editor, sharedRoot, {
|
|
|
945
1167
|
e.applyRemoteEvents = (events, origin) => {
|
|
946
1168
|
if (!warnedAboutQueuedChanges && YjsEditor.localChanges(e).length > 0) {
|
|
947
1169
|
warnedAboutQueuedChanges = true;
|
|
948
|
-
console.warn(
|
|
1170
|
+
console.warn(
|
|
1171
|
+
"[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."
|
|
1172
|
+
);
|
|
949
1173
|
}
|
|
950
1174
|
YjsEditor.flushLocalChanges(e);
|
|
951
1175
|
Editor3.withoutNormalizing(e, () => {
|
|
@@ -1038,7 +1262,9 @@ function withYjs(editor, sharedRoot, {
|
|
|
1038
1262
|
import { Transforms } from "slate";
|
|
1039
1263
|
import * as Y9 from "yjs";
|
|
1040
1264
|
var LAST_SELECTION = /* @__PURE__ */ new WeakMap();
|
|
1041
|
-
var DEFAULT_WITHOUT_SAVING_ORIGIN = Symbol(
|
|
1265
|
+
var DEFAULT_WITHOUT_SAVING_ORIGIN = /* @__PURE__ */ Symbol(
|
|
1266
|
+
"slate-yjs-history-without-saving"
|
|
1267
|
+
);
|
|
1042
1268
|
var YHistoryEditor = {
|
|
1043
1269
|
isYHistoryEditor(value) {
|
|
1044
1270
|
return YjsEditor.isYjsEditor(value) && value.undoManager instanceof Y9.UndoManager && typeof value.undo === "function" && typeof value.redo === "function" && "withoutSavingOrigin" in value;
|
|
@@ -1073,19 +1299,28 @@ function withYHistory(editor, _a = {}) {
|
|
|
1073
1299
|
const { onChange, isLocalOrigin } = e;
|
|
1074
1300
|
e.onChange = () => {
|
|
1075
1301
|
onChange();
|
|
1076
|
-
LAST_SELECTION.set(
|
|
1302
|
+
LAST_SELECTION.set(
|
|
1303
|
+
e,
|
|
1304
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
1305
|
+
);
|
|
1077
1306
|
};
|
|
1078
1307
|
e.isLocalOrigin = (origin) => origin === e.withoutSavingOrigin || isLocalOrigin(origin);
|
|
1079
1308
|
const handleStackItemAdded = ({
|
|
1080
1309
|
stackItem
|
|
1081
1310
|
}) => {
|
|
1082
|
-
stackItem.meta.set(
|
|
1311
|
+
stackItem.meta.set(
|
|
1312
|
+
"selection",
|
|
1313
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
1314
|
+
);
|
|
1083
1315
|
stackItem.meta.set("selectionBefore", LAST_SELECTION.get(e));
|
|
1084
1316
|
};
|
|
1085
1317
|
const handleStackItemUpdated = ({
|
|
1086
1318
|
stackItem
|
|
1087
1319
|
}) => {
|
|
1088
|
-
stackItem.meta.set(
|
|
1320
|
+
stackItem.meta.set(
|
|
1321
|
+
"selection",
|
|
1322
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
1323
|
+
);
|
|
1089
1324
|
};
|
|
1090
1325
|
const handleStackItemPopped = ({
|
|
1091
1326
|
stackItem,
|
|
@@ -1097,11 +1332,17 @@ function withYHistory(editor, _a = {}) {
|
|
|
1097
1332
|
inverseItem.meta.set("selection", stackItem.meta.get("selectionBefore"));
|
|
1098
1333
|
inverseItem.meta.set("selectionBefore", stackItem.meta.get("selection"));
|
|
1099
1334
|
}
|
|
1100
|
-
const relativeSelection = stackItem.meta.get(
|
|
1335
|
+
const relativeSelection = stackItem.meta.get(
|
|
1336
|
+
"selectionBefore"
|
|
1337
|
+
);
|
|
1101
1338
|
if (!relativeSelection) {
|
|
1102
1339
|
return;
|
|
1103
1340
|
}
|
|
1104
|
-
const selection = relativeRangeToSlateRange(
|
|
1341
|
+
const selection = relativeRangeToSlateRange(
|
|
1342
|
+
e.sharedRoot,
|
|
1343
|
+
e,
|
|
1344
|
+
relativeSelection
|
|
1345
|
+
);
|
|
1105
1346
|
if (!selection) {
|
|
1106
1347
|
return;
|
|
1107
1348
|
}
|
|
@@ -1185,18 +1426,20 @@ var CursorEditor = {
|
|
|
1185
1426
|
if (!YjsEditor.connected(editor)) {
|
|
1186
1427
|
return {};
|
|
1187
1428
|
}
|
|
1188
|
-
return Object.fromEntries(
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
return [
|
|
1193
|
-
id,
|
|
1194
|
-
{
|
|
1195
|
-
relativeSelection: state[editor.selectionStateField],
|
|
1196
|
-
data: state[editor.cursorDataField]
|
|
1429
|
+
return Object.fromEntries(
|
|
1430
|
+
Array.from(editor.awareness.getStates().entries(), ([id, state]) => {
|
|
1431
|
+
if (id === editor.awareness.clientID || !state) {
|
|
1432
|
+
return null;
|
|
1197
1433
|
}
|
|
1198
|
-
|
|
1199
|
-
|
|
1434
|
+
return [
|
|
1435
|
+
id,
|
|
1436
|
+
{
|
|
1437
|
+
relativeSelection: state[editor.selectionStateField],
|
|
1438
|
+
data: state[editor.cursorDataField]
|
|
1439
|
+
}
|
|
1440
|
+
];
|
|
1441
|
+
}).filter(Array.isArray)
|
|
1442
|
+
);
|
|
1200
1443
|
}
|
|
1201
1444
|
};
|
|
1202
1445
|
function withCursors(editor, awareness, {
|