@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.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,7 +610,9 @@ 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
|
|
@@ -579,7 +635,10 @@ function relativePositionToSlatePoint(sharedRoot, slateRoot, pos) {
|
|
|
579
635
|
if (!sharedRoot.doc) {
|
|
580
636
|
throw new Error("sharedRoot isn't attach to a yDoc");
|
|
581
637
|
}
|
|
582
|
-
const absPos = Y6.createAbsolutePositionFromRelativePosition(
|
|
638
|
+
const absPos = Y6.createAbsolutePositionFromRelativePosition(
|
|
639
|
+
pos,
|
|
640
|
+
sharedRoot.doc
|
|
641
|
+
);
|
|
583
642
|
return absPos && absolutePositionToSlatePoint(sharedRoot, slateRoot, absPos);
|
|
584
643
|
}
|
|
585
644
|
function getStoredPosition(sharedRoot, key) {
|
|
@@ -590,34 +649,48 @@ function getStoredPosition(sharedRoot, key) {
|
|
|
590
649
|
return Y6.decodeRelativePosition(rawPosition);
|
|
591
650
|
}
|
|
592
651
|
function getStoredPositions(sharedRoot) {
|
|
593
|
-
return Object.fromEntries(
|
|
594
|
-
key.
|
|
595
|
-
|
|
596
|
-
|
|
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
|
+
);
|
|
597
658
|
}
|
|
598
659
|
function getStoredPositionsAbsolute(sharedRoot) {
|
|
599
660
|
assertDocumentAttachment(sharedRoot);
|
|
600
|
-
return Object.fromEntries(
|
|
601
|
-
key.
|
|
602
|
-
|
|
603
|
-
|
|
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
|
+
);
|
|
604
672
|
}
|
|
605
673
|
function removeStoredPosition(sharedRoot, key) {
|
|
606
674
|
sharedRoot.removeAttribute(STORED_POSITION_PREFIX + key);
|
|
607
675
|
}
|
|
608
676
|
function setStoredPosition(sharedRoot, key, position) {
|
|
609
|
-
sharedRoot.setAttribute(
|
|
677
|
+
sharedRoot.setAttribute(
|
|
678
|
+
STORED_POSITION_PREFIX + key,
|
|
679
|
+
Y6.encodeRelativePosition(position)
|
|
680
|
+
);
|
|
610
681
|
}
|
|
611
682
|
function getAbsolutePositionsInTextRange(absolutePositions, yTarget, textRange) {
|
|
612
|
-
return Object.fromEntries(
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
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
|
+
);
|
|
621
694
|
}
|
|
622
695
|
function getAbsolutePositionsInYText(absolutePositions, yText, parentPath = "") {
|
|
623
696
|
const positions = {
|
|
@@ -626,7 +699,14 @@ function getAbsolutePositionsInYText(absolutePositions, yText, parentPath = "")
|
|
|
626
699
|
const insertDelta = yTextToInsertDelta(yText);
|
|
627
700
|
insertDelta.forEach(({ insert }, i) => {
|
|
628
701
|
if (insert instanceof Y6.XmlText) {
|
|
629
|
-
Object.assign(
|
|
702
|
+
Object.assign(
|
|
703
|
+
positions,
|
|
704
|
+
getAbsolutePositionsInYText(
|
|
705
|
+
absolutePositions,
|
|
706
|
+
insert,
|
|
707
|
+
parentPath ? `${parentPath}.${i}` : i.toString()
|
|
708
|
+
)
|
|
709
|
+
);
|
|
630
710
|
}
|
|
631
711
|
});
|
|
632
712
|
return positions;
|
|
@@ -641,7 +721,10 @@ function getStoredPositionsInDeltaAbsolute(sharedRoot, yText, delta, deltaOffset
|
|
|
641
721
|
};
|
|
642
722
|
delta.forEach(({ insert }, i) => {
|
|
643
723
|
if (insert instanceof Y6.XmlText) {
|
|
644
|
-
Object.assign(
|
|
724
|
+
Object.assign(
|
|
725
|
+
positions,
|
|
726
|
+
getAbsolutePositionsInYText(absolutePositions, insert, i.toString())
|
|
727
|
+
);
|
|
645
728
|
}
|
|
646
729
|
});
|
|
647
730
|
return positions;
|
|
@@ -650,12 +733,28 @@ function restoreStoredPositionsWithDeltaAbsolute(sharedRoot, yText, absolutePosi
|
|
|
650
733
|
const toRestore = absolutePositions[path];
|
|
651
734
|
if (toRestore) {
|
|
652
735
|
Object.entries(toRestore).forEach(([key, position]) => {
|
|
653
|
-
setStoredPosition(
|
|
736
|
+
setStoredPosition(
|
|
737
|
+
sharedRoot,
|
|
738
|
+
key,
|
|
739
|
+
Y6.createRelativePositionFromTypeIndex(
|
|
740
|
+
yText,
|
|
741
|
+
position.index - previousDeltaOffset + newDeltaOffset,
|
|
742
|
+
position.assoc
|
|
743
|
+
)
|
|
744
|
+
);
|
|
654
745
|
});
|
|
655
746
|
}
|
|
656
747
|
delta.forEach(({ insert }, i) => {
|
|
657
748
|
if (insert instanceof Y6.XmlText) {
|
|
658
|
-
restoreStoredPositionsWithDeltaAbsolute(
|
|
749
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
750
|
+
sharedRoot,
|
|
751
|
+
insert,
|
|
752
|
+
absolutePositions,
|
|
753
|
+
yTextToInsertDelta(insert),
|
|
754
|
+
0,
|
|
755
|
+
0,
|
|
756
|
+
path ? `${path}.${i}` : i.toString()
|
|
757
|
+
);
|
|
659
758
|
}
|
|
660
759
|
});
|
|
661
760
|
}
|
|
@@ -666,11 +765,19 @@ function slateRangeToRelativeRange(sharedRoot, slateRoot, range) {
|
|
|
666
765
|
};
|
|
667
766
|
}
|
|
668
767
|
function relativeRangeToSlateRange(sharedRoot, slateRoot, range) {
|
|
669
|
-
const anchor = relativePositionToSlatePoint(
|
|
768
|
+
const anchor = relativePositionToSlatePoint(
|
|
769
|
+
sharedRoot,
|
|
770
|
+
slateRoot,
|
|
771
|
+
range.anchor
|
|
772
|
+
);
|
|
670
773
|
if (!anchor) {
|
|
671
774
|
return null;
|
|
672
775
|
}
|
|
673
|
-
const focus = relativePositionToSlatePoint(
|
|
776
|
+
const focus = relativePositionToSlatePoint(
|
|
777
|
+
sharedRoot,
|
|
778
|
+
slateRoot,
|
|
779
|
+
range.focus
|
|
780
|
+
);
|
|
674
781
|
if (!focus) {
|
|
675
782
|
return null;
|
|
676
783
|
}
|
|
@@ -680,7 +787,11 @@ function relativeRangeToSlateRange(sharedRoot, slateRoot, range) {
|
|
|
680
787
|
// src/applyToYjs/node/mergeNode.ts
|
|
681
788
|
function mergeNode(sharedRoot, slateRoot, op) {
|
|
682
789
|
const target = getYTarget(sharedRoot, slateRoot, op.path);
|
|
683
|
-
const prev = getYTarget(
|
|
790
|
+
const prev = getYTarget(
|
|
791
|
+
target.yParent,
|
|
792
|
+
target.slateParent,
|
|
793
|
+
Path3.previous(op.path.slice(-1))
|
|
794
|
+
);
|
|
684
795
|
if (!target.yTarget !== !prev.yTarget) {
|
|
685
796
|
throw new Error("Cannot merge y text with y element");
|
|
686
797
|
}
|
|
@@ -704,11 +815,25 @@ function mergeNode(sharedRoot, slateRoot, op) {
|
|
|
704
815
|
const deltaApplyYOffset = prev.yTarget.length;
|
|
705
816
|
const targetDelta = yTextToInsertDelta(target.yTarget);
|
|
706
817
|
const clonedDelta = cloneInsertDeltaDeep(targetDelta);
|
|
707
|
-
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
818
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
819
|
+
sharedRoot,
|
|
820
|
+
target.yTarget,
|
|
821
|
+
targetDelta,
|
|
822
|
+
deltaApplyYOffset
|
|
823
|
+
);
|
|
708
824
|
const applyDelta2 = [{ retain: deltaApplyYOffset }, ...clonedDelta];
|
|
709
825
|
prev.yTarget.applyDelta(applyDelta2, PRESERVE_CONTENT);
|
|
710
|
-
target.yParent.delete(
|
|
711
|
-
|
|
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
|
+
);
|
|
712
837
|
}
|
|
713
838
|
|
|
714
839
|
// src/applyToYjs/node/moveNode.ts
|
|
@@ -729,8 +854,15 @@ function withoutNode(root, path) {
|
|
|
729
854
|
function moveNode(sharedRoot, slateRoot, op) {
|
|
730
855
|
const origin = getYTarget(sharedRoot, slateRoot, op.path);
|
|
731
856
|
const insertDelta = cloneInsertDeltaDeep(origin.targetDelta);
|
|
732
|
-
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
733
|
-
|
|
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
|
+
);
|
|
734
866
|
const rootWithoutNode = withoutNode(slateRoot, op.path);
|
|
735
867
|
const newPath = Path4.transform(op.path, op);
|
|
736
868
|
if (!newPath) {
|
|
@@ -749,18 +881,33 @@ function moveNode(sharedRoot, slateRoot, op) {
|
|
|
749
881
|
const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
|
|
750
882
|
const applyDelta2 = [{ retain: deltaApplyYOffset }, ...insertDelta];
|
|
751
883
|
target.yParent.applyDelta(applyDelta2, PRESERVE_CONTENT);
|
|
752
|
-
restoreStoredPositionsWithDeltaAbsolute(
|
|
884
|
+
restoreStoredPositionsWithDeltaAbsolute(
|
|
885
|
+
sharedRoot,
|
|
886
|
+
target.yParent,
|
|
887
|
+
storedPositions,
|
|
888
|
+
insertDelta,
|
|
889
|
+
deltaApplyYOffset,
|
|
890
|
+
origin.textRange.start
|
|
891
|
+
);
|
|
753
892
|
}
|
|
754
893
|
|
|
755
894
|
// src/applyToYjs/node/removeNode.ts
|
|
756
895
|
function removeNode(sharedRoot, slateRoot, op) {
|
|
757
|
-
const { yParent: parent, textRange } = getYTarget(
|
|
896
|
+
const { yParent: parent, textRange } = getYTarget(
|
|
897
|
+
sharedRoot,
|
|
898
|
+
slateRoot,
|
|
899
|
+
op.path
|
|
900
|
+
);
|
|
758
901
|
parent.delete(textRange.start, textRange.end - textRange.start);
|
|
759
902
|
}
|
|
760
903
|
|
|
761
904
|
// src/applyToYjs/node/setNode.ts
|
|
762
905
|
function setNode(sharedRoot, slateRoot, op) {
|
|
763
|
-
const { yTarget, textRange, yParent } = getYTarget(
|
|
906
|
+
const { yTarget, textRange, yParent } = getYTarget(
|
|
907
|
+
sharedRoot,
|
|
908
|
+
slateRoot,
|
|
909
|
+
op.path
|
|
910
|
+
);
|
|
764
911
|
if (yTarget) {
|
|
765
912
|
Object.entries(op.newProperties).forEach(([key, value]) => {
|
|
766
913
|
if (value === null) {
|
|
@@ -774,9 +921,15 @@ function setNode(sharedRoot, slateRoot, op) {
|
|
|
774
921
|
}
|
|
775
922
|
});
|
|
776
923
|
}
|
|
777
|
-
const unset = Object.fromEntries(
|
|
924
|
+
const unset = Object.fromEntries(
|
|
925
|
+
Object.keys(op.properties).map((key) => [key, null])
|
|
926
|
+
);
|
|
778
927
|
const newProperties = __spreadValues(__spreadValues({}, unset), op.newProperties);
|
|
779
|
-
yParent.format(
|
|
928
|
+
yParent.format(
|
|
929
|
+
textRange.start,
|
|
930
|
+
textRange.end - textRange.start,
|
|
931
|
+
newProperties
|
|
932
|
+
);
|
|
780
933
|
}
|
|
781
934
|
|
|
782
935
|
// src/applyToYjs/node/splitNode.ts
|
|
@@ -799,7 +952,11 @@ function splitNode(sharedRoot, slateRoot, op) {
|
|
|
799
952
|
});
|
|
800
953
|
}
|
|
801
954
|
});
|
|
802
|
-
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
|
+
);
|
|
803
960
|
}
|
|
804
961
|
if (Text9.isText(target.slateTarget)) {
|
|
805
962
|
throw new Error("Mismatch node type between y target and slate node");
|
|
@@ -808,18 +965,63 @@ function splitNode(sharedRoot, slateRoot, op) {
|
|
|
808
965
|
op.position
|
|
809
966
|
]);
|
|
810
967
|
const ySplitOffset = target.slateTarget.children.slice(0, op.position).reduce((length2, child) => length2 + getSlateNodeYLength(child), 0);
|
|
811
|
-
const length = target.slateTarget.children.reduce(
|
|
812
|
-
|
|
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
|
+
);
|
|
813
980
|
const clonedDelta = cloneInsertDeltaDeep(splitDelta);
|
|
814
|
-
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
981
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(
|
|
982
|
+
sharedRoot,
|
|
983
|
+
target.yTarget,
|
|
984
|
+
splitDelta,
|
|
985
|
+
copyStart
|
|
986
|
+
);
|
|
815
987
|
const toInsert = new Y7.XmlText();
|
|
816
988
|
toInsert.applyDelta(clonedDelta, PRESERVE_CONTENT);
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
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
|
+
);
|
|
823
1025
|
}
|
|
824
1026
|
|
|
825
1027
|
// src/applyToYjs/node/index.ts
|
|
@@ -835,17 +1037,29 @@ var NODE_MAPPER = {
|
|
|
835
1037
|
// src/applyToYjs/text/insertText.ts
|
|
836
1038
|
import { Node as Node9, Text as Text10 } from "slate";
|
|
837
1039
|
function insertText(sharedRoot, slateRoot, op) {
|
|
838
|
-
const { yParent: target, textRange } = getYTarget(
|
|
1040
|
+
const { yParent: target, textRange } = getYTarget(
|
|
1041
|
+
sharedRoot,
|
|
1042
|
+
slateRoot,
|
|
1043
|
+
op.path
|
|
1044
|
+
);
|
|
839
1045
|
const targetNode = Node9.get(slateRoot, op.path);
|
|
840
1046
|
if (!Text10.isText(targetNode)) {
|
|
841
1047
|
throw new Error("Cannot insert text into non-text node");
|
|
842
1048
|
}
|
|
843
|
-
target.insert(
|
|
1049
|
+
target.insert(
|
|
1050
|
+
textRange.start + op.offset,
|
|
1051
|
+
op.text,
|
|
1052
|
+
getProperties(targetNode)
|
|
1053
|
+
);
|
|
844
1054
|
}
|
|
845
1055
|
|
|
846
1056
|
// src/applyToYjs/text/removeText.ts
|
|
847
1057
|
function removeText(sharedRoot, slateRoot, op) {
|
|
848
|
-
const { yParent: target, textRange } = getYTarget(
|
|
1058
|
+
const { yParent: target, textRange } = getYTarget(
|
|
1059
|
+
sharedRoot,
|
|
1060
|
+
slateRoot,
|
|
1061
|
+
op.path
|
|
1062
|
+
);
|
|
849
1063
|
target.delete(textRange.start + op.offset, op.text.length);
|
|
850
1064
|
}
|
|
851
1065
|
|
|
@@ -870,8 +1084,8 @@ function applySlateOp(sharedRoot, slateRoot, op) {
|
|
|
870
1084
|
}
|
|
871
1085
|
|
|
872
1086
|
// src/plugins/withYjs.ts
|
|
873
|
-
var DEFAULT_LOCAL_ORIGIN = Symbol("slate-yjs-operation");
|
|
874
|
-
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");
|
|
875
1089
|
var ORIGIN = /* @__PURE__ */ new WeakMap();
|
|
876
1090
|
var LOCAL_CHANGES = /* @__PURE__ */ new WeakMap();
|
|
877
1091
|
var CONNECTED = /* @__PURE__ */ new WeakSet();
|
|
@@ -953,7 +1167,9 @@ function withYjs(editor, sharedRoot, {
|
|
|
953
1167
|
e.applyRemoteEvents = (events, origin) => {
|
|
954
1168
|
if (!warnedAboutQueuedChanges && YjsEditor.localChanges(e).length > 0) {
|
|
955
1169
|
warnedAboutQueuedChanges = true;
|
|
956
|
-
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
|
+
);
|
|
957
1173
|
}
|
|
958
1174
|
YjsEditor.flushLocalChanges(e);
|
|
959
1175
|
Editor3.withoutNormalizing(e, () => {
|
|
@@ -1046,7 +1262,9 @@ function withYjs(editor, sharedRoot, {
|
|
|
1046
1262
|
import { Transforms } from "slate";
|
|
1047
1263
|
import * as Y9 from "yjs";
|
|
1048
1264
|
var LAST_SELECTION = /* @__PURE__ */ new WeakMap();
|
|
1049
|
-
var DEFAULT_WITHOUT_SAVING_ORIGIN = Symbol(
|
|
1265
|
+
var DEFAULT_WITHOUT_SAVING_ORIGIN = /* @__PURE__ */ Symbol(
|
|
1266
|
+
"slate-yjs-history-without-saving"
|
|
1267
|
+
);
|
|
1050
1268
|
var YHistoryEditor = {
|
|
1051
1269
|
isYHistoryEditor(value) {
|
|
1052
1270
|
return YjsEditor.isYjsEditor(value) && value.undoManager instanceof Y9.UndoManager && typeof value.undo === "function" && typeof value.redo === "function" && "withoutSavingOrigin" in value;
|
|
@@ -1081,19 +1299,28 @@ function withYHistory(editor, _a = {}) {
|
|
|
1081
1299
|
const { onChange, isLocalOrigin } = e;
|
|
1082
1300
|
e.onChange = () => {
|
|
1083
1301
|
onChange();
|
|
1084
|
-
LAST_SELECTION.set(
|
|
1302
|
+
LAST_SELECTION.set(
|
|
1303
|
+
e,
|
|
1304
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
1305
|
+
);
|
|
1085
1306
|
};
|
|
1086
1307
|
e.isLocalOrigin = (origin) => origin === e.withoutSavingOrigin || isLocalOrigin(origin);
|
|
1087
1308
|
const handleStackItemAdded = ({
|
|
1088
1309
|
stackItem
|
|
1089
1310
|
}) => {
|
|
1090
|
-
stackItem.meta.set(
|
|
1311
|
+
stackItem.meta.set(
|
|
1312
|
+
"selection",
|
|
1313
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
1314
|
+
);
|
|
1091
1315
|
stackItem.meta.set("selectionBefore", LAST_SELECTION.get(e));
|
|
1092
1316
|
};
|
|
1093
1317
|
const handleStackItemUpdated = ({
|
|
1094
1318
|
stackItem
|
|
1095
1319
|
}) => {
|
|
1096
|
-
stackItem.meta.set(
|
|
1320
|
+
stackItem.meta.set(
|
|
1321
|
+
"selection",
|
|
1322
|
+
e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
|
|
1323
|
+
);
|
|
1097
1324
|
};
|
|
1098
1325
|
const handleStackItemPopped = ({
|
|
1099
1326
|
stackItem,
|
|
@@ -1105,11 +1332,17 @@ function withYHistory(editor, _a = {}) {
|
|
|
1105
1332
|
inverseItem.meta.set("selection", stackItem.meta.get("selectionBefore"));
|
|
1106
1333
|
inverseItem.meta.set("selectionBefore", stackItem.meta.get("selection"));
|
|
1107
1334
|
}
|
|
1108
|
-
const relativeSelection = stackItem.meta.get(
|
|
1335
|
+
const relativeSelection = stackItem.meta.get(
|
|
1336
|
+
"selectionBefore"
|
|
1337
|
+
);
|
|
1109
1338
|
if (!relativeSelection) {
|
|
1110
1339
|
return;
|
|
1111
1340
|
}
|
|
1112
|
-
const selection = relativeRangeToSlateRange(
|
|
1341
|
+
const selection = relativeRangeToSlateRange(
|
|
1342
|
+
e.sharedRoot,
|
|
1343
|
+
e,
|
|
1344
|
+
relativeSelection
|
|
1345
|
+
);
|
|
1113
1346
|
if (!selection) {
|
|
1114
1347
|
return;
|
|
1115
1348
|
}
|
|
@@ -1193,18 +1426,20 @@ var CursorEditor = {
|
|
|
1193
1426
|
if (!YjsEditor.connected(editor)) {
|
|
1194
1427
|
return {};
|
|
1195
1428
|
}
|
|
1196
|
-
return Object.fromEntries(
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
return [
|
|
1201
|
-
id,
|
|
1202
|
-
{
|
|
1203
|
-
relativeSelection: state[editor.selectionStateField],
|
|
1204
|
-
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;
|
|
1205
1433
|
}
|
|
1206
|
-
|
|
1207
|
-
|
|
1434
|
+
return [
|
|
1435
|
+
id,
|
|
1436
|
+
{
|
|
1437
|
+
relativeSelection: state[editor.selectionStateField],
|
|
1438
|
+
data: state[editor.cursorDataField]
|
|
1439
|
+
}
|
|
1440
|
+
];
|
|
1441
|
+
}).filter(Array.isArray)
|
|
1442
|
+
);
|
|
1208
1443
|
}
|
|
1209
1444
|
};
|
|
1210
1445
|
function withCursors(editor, awareness, {
|