@slate-yjs/core 1.0.3 → 1.0.5

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/applyToYjs/node/mergeNode.d.ts.map +1 -1
  3. package/dist/applyToYjs/node/moveNode.d.ts.map +1 -1
  4. package/dist/applyToYjs/node/splitNode.d.ts.map +1 -1
  5. package/dist/index.cjs +60 -36
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.global.js +3851 -5380
  8. package/dist/index.global.js.map +1 -1
  9. package/dist/index.js +60 -36
  10. package/dist/index.js.map +1 -1
  11. package/dist/plugins/withCursors.d.ts.map +1 -1
  12. package/dist/plugins/withYjs.d.ts.map +1 -1
  13. package/dist/utils/clone.d.ts.map +1 -1
  14. package/dist/utils/convert.d.ts.map +1 -1
  15. package/dist/utils/yjs.d.ts +3 -0
  16. package/dist/utils/yjs.d.ts.map +1 -1
  17. package/package.json +10 -5
  18. package/src/applyToYjs/node/mergeNode.ts +2 -3
  19. package/src/applyToYjs/node/moveNode.ts +41 -14
  20. package/src/applyToYjs/node/splitNode.ts +2 -3
  21. package/src/plugins/withCursors.ts +38 -11
  22. package/src/plugins/withYjs.ts +17 -0
  23. package/src/utils/clone.ts +5 -3
  24. package/src/utils/convert.ts +2 -1
  25. package/src/utils/yjs.ts +6 -0
  26. package/test/collaboration/addMark/acrossMarks.tsx +0 -40
  27. package/test/collaboration/addMark/acrossMarksSame.tsx +0 -36
  28. package/test/collaboration/addMark/atBeginningOfDocument.tsx +0 -27
  29. package/test/collaboration/addMark/atEndOfDocument.tsx +0 -30
  30. package/test/collaboration/addMark/withOtherMarks.tsx +0 -31
  31. package/test/collaboration/insertNode/atBeginningOfDocument.tsx +0 -28
  32. package/test/collaboration/insertNode/atEndOfDocument.tsx +0 -28
  33. package/test/collaboration/insertNode/inTheMiddle.tsx +0 -30
  34. package/test/collaboration/insertText/atBeginningOfBlock.tsx +0 -26
  35. package/test/collaboration/insertText/atBeginningOfDocument.tsx +0 -26
  36. package/test/collaboration/insertText/atEndOfBlock.tsx +0 -26
  37. package/test/collaboration/insertText/atEndOfDocument.tsx +0 -26
  38. package/test/collaboration/insertText/inTheMiddle.tsx +0 -32
  39. package/test/collaboration/insertText/inTheMiddleOfNestedBlock.tsx +0 -30
  40. package/test/collaboration/insertText/insideMarks.tsx +0 -28
  41. package/test/collaboration/insertText/withEmptyString.tsx +0 -25
  42. package/test/collaboration/insertText/withEntities.tsx +0 -28
  43. package/test/collaboration/insertText/withMarks.tsx +0 -25
  44. package/test/collaboration/insertText/withUnicode.tsx +0 -28
  45. package/test/collaboration/mergeNode/afterADeleteBackward.tsx +0 -27
  46. package/test/collaboration/mergeNode/inSameParent.tsx +0 -34
  47. package/test/collaboration/mergeNode/onMixedNestedNodes.tsx +0 -29
  48. package/test/collaboration/mergeNode/onMixedTypeNodes.tsx +0 -27
  49. package/test/collaboration/mergeNode/withUnicode.tsx +0 -25
  50. package/test/collaboration/moveNode/downward/whenBlockBecomesNested.tsx +0 -43
  51. package/test/collaboration/moveNode/downward/whenBlockBecomesNonNested.tsx +0 -41
  52. package/test/collaboration/moveNode/downward/whenBlockStaysNested.tsx +0 -38
  53. package/test/collaboration/moveNode/downward/whenBlockStaysNonNested.tsx +0 -30
  54. package/test/collaboration/moveNode/upward/whenBlockBecomesNested.tsx +0 -43
  55. package/test/collaboration/moveNode/upward/whenBlockBecomesNonNested.tsx +0 -41
  56. package/test/collaboration/moveNode/upward/whenBlockStaysNested.tsx +0 -38
  57. package/test/collaboration/moveNode/upward/whenBlockStaysNonNested.tsx +0 -30
  58. package/test/collaboration/removeMark/inTheMiddleOfText.tsx +0 -43
  59. package/test/collaboration/removeMark/withAddMark.tsx +0 -31
  60. package/test/collaboration/removeMark/withOtherMarks.tsx +0 -47
  61. package/test/collaboration/removeNode/atBeginningOfDocument.tsx +0 -26
  62. package/test/collaboration/removeNode/atEndOfDocument.tsx +0 -26
  63. package/test/collaboration/removeNode/nestedBlock.tsx +0 -28
  64. package/test/collaboration/removeNode/wrapperBlock.tsx +0 -28
  65. package/test/collaboration/removeText/atBeginningOfDocument.tsx +0 -34
  66. package/test/collaboration/removeText/atEndOfDocument.tsx +0 -37
  67. package/test/collaboration/removeText/withUnicode.tsx +0 -29
  68. package/test/collaboration/setNode/atBeginningOfDocument.tsx +0 -27
  69. package/test/collaboration/setNode/atEndOfDocument.tsx +0 -27
  70. package/test/collaboration/setNode/onDataChange.tsx +0 -35
  71. package/test/collaboration/setNode/onDataChangeOnInline.tsx +0 -35
  72. package/test/collaboration/setNode/onResetBlock.tsx +0 -27
  73. package/test/collaboration/setNode/withAChangeOfType.tsx +0 -26
  74. package/test/collaboration/splitNode/atBeginningOfDocument.tsx +0 -28
  75. package/test/collaboration/splitNode/atEndOfBlock.tsx +0 -27
  76. package/test/collaboration/splitNode/atEndOfDocument.tsx +0 -27
  77. package/test/collaboration/splitNode/onNonDefaultBlock.tsx +0 -27
  78. package/test/collaboration/splitNode/withMultipleSubNodes.tsx +0 -32
  79. package/test/collaboration/splitNode/withUnicode.tsx +0 -29
  80. package/test/index.test.ts +0 -65
  81. package/test/slate.d.ts +0 -8
  82. package/test/withTestingElements.ts +0 -46
  83. package/tsconfig.json +0 -11
  84. package/tsup.config.ts +0 -32
package/dist/index.js CHANGED
@@ -173,6 +173,14 @@ function getProperties(node) {
173
173
  return omit(node, Text.isText(node) ? "text" : "children");
174
174
  }
175
175
 
176
+ // src/utils/yjs.ts
177
+ function assertDocumentAttachment(sharedType) {
178
+ if (!sharedType.doc) {
179
+ throw new Error("shared type isn't attached to a document");
180
+ }
181
+ }
182
+ var PRESERVE_CONTENT = { sanitize: true };
183
+
176
184
  // src/utils/convert.ts
177
185
  function yTextToSlateElement(yText) {
178
186
  const delta = yTextToInsertDelta(yText);
@@ -203,7 +211,7 @@ function slateElementToYText(_a) {
203
211
  Object.entries(attributes).forEach(([key, value]) => {
204
212
  yElement.setAttribute(key, value);
205
213
  });
206
- yElement.applyDelta(slateNodesToInsertDelta(children), { sanitize: false });
214
+ yElement.applyDelta(slateNodesToInsertDelta(children), PRESERVE_CONTENT);
207
215
  return yElement;
208
216
  }
209
217
 
@@ -525,24 +533,13 @@ function cloneDeep(yText) {
525
533
  Object.entries(attributes).forEach(([key, value]) => {
526
534
  clone.setAttribute(key, value);
527
535
  });
528
- clone.applyDelta(cloneInsertDeltaDeep(yTextToInsertDelta(yText)), {
529
- sanitize: false
530
- });
536
+ clone.applyDelta(cloneInsertDeltaDeep(yTextToInsertDelta(yText)), PRESERVE_CONTENT);
531
537
  return clone;
532
538
  }
533
539
 
534
540
  // src/utils/position.ts
535
541
  import { Node as Node5, Text as Text6 } from "slate";
536
542
  import * as Y6 from "yjs";
537
-
538
- // src/utils/yjs.ts
539
- function assertDocumentAttachment(sharedType) {
540
- if (!sharedType.doc) {
541
- throw new Error("shared type isn't attached to a document");
542
- }
543
- }
544
-
545
- // src/utils/position.ts
546
543
  var STORED_POSITION_PREFIX = "__slateYjsStoredPosition_";
547
544
  function slatePointToRelativePosition(sharedRoot, slateRoot, point) {
548
545
  const { yTarget, yParent, textRange } = getYTarget(sharedRoot, slateRoot, point.path);
@@ -701,35 +698,49 @@ function mergeNode(sharedRoot, slateRoot, op) {
701
698
  const clonedDelta = cloneInsertDeltaDeep(targetDelta);
702
699
  const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, target.yTarget, targetDelta, deltaApplyYOffset);
703
700
  const applyDelta2 = [{ retain: deltaApplyYOffset }, ...clonedDelta];
704
- prev.yTarget.applyDelta(applyDelta2, {
705
- sanitize: false
706
- });
701
+ prev.yTarget.applyDelta(applyDelta2, PRESERVE_CONTENT);
707
702
  target.yParent.delete(target.textRange.start, target.textRange.end - target.textRange.start);
708
703
  restoreStoredPositionsWithDeltaAbsolute(sharedRoot, prev.yTarget, storedPositions, clonedDelta, deltaApplyYOffset);
709
704
  }
710
705
 
711
706
  // src/applyToYjs/node/moveNode.ts
712
707
  import { Node as Node7, Path as Path4, Text as Text8 } from "slate";
713
- function moveNode(sharedRoot, slateRoot, op) {
714
- const newParentPath = Path4.parent(op.newPath);
715
- const newPathOffset = op.newPath[op.newPath.length - 1];
716
- const parent = Node7.get(slateRoot, newParentPath);
717
- if (Text8.isText(parent)) {
718
- throw new Error("Cannot move slate node into text element");
708
+ function withoutNode(root, path) {
709
+ if (Text8.isText(root)) {
710
+ throw new Error("Cannot descend into slate text");
719
711
  }
720
- const normalizedNewPath = [
721
- ...newParentPath,
722
- Math.min(newPathOffset, parent.children.length)
723
- ];
712
+ const [index, ...rest] = path;
713
+ const children = [...root.children];
714
+ if (rest.length === 0) {
715
+ children.splice(index, 1);
716
+ } else {
717
+ children[index] = withoutNode(children[index], rest);
718
+ }
719
+ return __spreadProps(__spreadValues({}, root), { children });
720
+ }
721
+ function moveNode(sharedRoot, slateRoot, op) {
724
722
  const origin = getYTarget(sharedRoot, slateRoot, op.path);
725
- const target = getYTarget(sharedRoot, slateRoot, normalizedNewPath);
726
723
  const insertDelta = cloneInsertDeltaDeep(origin.targetDelta);
727
724
  const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, origin.yParent, origin.targetDelta);
728
725
  origin.yParent.delete(origin.textRange.start, origin.textRange.end - origin.textRange.start);
726
+ const rootWithoutNode = withoutNode(slateRoot, op.path);
727
+ const newPath = Path4.transform(op.path, op);
728
+ if (!newPath) {
729
+ throw new Error("Cannot resolve the target path of a move");
730
+ }
731
+ const newParentPath = Path4.parent(newPath);
732
+ const parent = Node7.get(rootWithoutNode, newParentPath);
733
+ if (Text8.isText(parent)) {
734
+ throw new Error("Cannot move slate node into text element");
735
+ }
736
+ const target = getYTarget(sharedRoot, rootWithoutNode, [
737
+ ...newParentPath,
738
+ Math.min(newPath[newPath.length - 1], parent.children.length)
739
+ ]);
729
740
  const targetLength = getInsertDeltaLength(yTextToInsertDelta(target.yParent));
730
741
  const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
731
742
  const applyDelta2 = [{ retain: deltaApplyYOffset }, ...insertDelta];
732
- target.yParent.applyDelta(applyDelta2, { sanitize: false });
743
+ target.yParent.applyDelta(applyDelta2, PRESERVE_CONTENT);
733
744
  restoreStoredPositionsWithDeltaAbsolute(sharedRoot, target.yParent, storedPositions, insertDelta, deltaApplyYOffset, origin.textRange.start);
734
745
  }
735
746
 
@@ -794,9 +805,7 @@ function splitNode(sharedRoot, slateRoot, op) {
794
805
  const clonedDelta = cloneInsertDeltaDeep(splitDelta);
795
806
  const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, target.yTarget, splitDelta, ySplitOffset);
796
807
  const toInsert = new Y7.XmlText();
797
- toInsert.applyDelta(clonedDelta, {
798
- sanitize: false
799
- });
808
+ toInsert.applyDelta(clonedDelta, PRESERVE_CONTENT);
800
809
  Object.entries(op.properties).forEach(([key, value]) => {
801
810
  toInsert.setAttribute(key, value);
802
811
  });
@@ -932,13 +941,19 @@ function withYjs(editor, sharedRoot, {
932
941
  e.sharedRoot = sharedRoot;
933
942
  e.localOrigin = localOrigin != null ? localOrigin : DEFAULT_LOCAL_ORIGIN;
934
943
  e.positionStorageOrigin = positionStorageOrigin != null ? positionStorageOrigin : DEFAULT_POSITION_STORAGE_ORIGIN;
944
+ let warnedAboutQueuedChanges = false;
935
945
  e.applyRemoteEvents = (events, origin) => {
946
+ if (!warnedAboutQueuedChanges && YjsEditor.localChanges(e).length > 0) {
947
+ warnedAboutQueuedChanges = true;
948
+ console.warn("[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.");
949
+ }
936
950
  YjsEditor.flushLocalChanges(e);
937
951
  Editor3.withoutNormalizing(e, () => {
938
952
  YjsEditor.withOrigin(e, origin, () => {
939
953
  applyYjsEvents(e.sharedRoot, e, events);
940
954
  });
941
955
  });
956
+ YjsEditor.flushLocalChanges(e);
942
957
  };
943
958
  e.isLocalOrigin = (origin) => origin === e.localOrigin;
944
959
  const handleYEvents = (events, transaction) => {
@@ -1197,18 +1212,23 @@ function withCursors(editor, awareness, {
1197
1212
  e.sendCursorData = (cursorData) => {
1198
1213
  e.awareness.setLocalStateField(e.cursorDataField, cursorData);
1199
1214
  };
1215
+ let hiddenAt;
1216
+ const relativeSelection = () => e.selection ? slateRangeToRelativeRange(e.sharedRoot, e, e.selection) : null;
1217
+ const isSameRange = (a, b) => a === b || !!a && !!b && Y10.compareRelativePositions(a.anchor, b.anchor) && Y10.compareRelativePositions(a.focus, b.focus);
1200
1218
  e.sendCursorPosition = (range) => {
1201
1219
  const localState = e.awareness.getLocalState();
1202
1220
  const currentRange = localState == null ? void 0 : localState[selectionStateField];
1203
1221
  if (!range) {
1222
+ hiddenAt = relativeSelection();
1204
1223
  if (currentRange) {
1205
1224
  e.awareness.setLocalStateField(e.selectionStateField, null);
1206
1225
  }
1207
1226
  return;
1208
1227
  }
1209
- const { anchor, focus } = slateRangeToRelativeRange(e.sharedRoot, e, range);
1210
- if (!currentRange || !Y10.compareRelativePositions(anchor, currentRange) || !Y10.compareRelativePositions(focus, currentRange)) {
1211
- e.awareness.setLocalStateField(e.selectionStateField, { anchor, focus });
1228
+ hiddenAt = void 0;
1229
+ const relativeRange = slateRangeToRelativeRange(e.sharedRoot, e, range);
1230
+ if (!currentRange || !isSameRange(relativeRange, currentRange)) {
1231
+ e.awareness.setLocalStateField(e.selectionStateField, relativeRange);
1212
1232
  }
1213
1233
  };
1214
1234
  const awarenessChangeListener = (yEvent) => {
@@ -1242,9 +1262,13 @@ function withCursors(editor, awareness, {
1242
1262
  const { onChange } = e;
1243
1263
  e.onChange = () => {
1244
1264
  onChange();
1245
- if (YjsEditor.connected(e)) {
1246
- CursorEditor.sendCursorPosition(e);
1265
+ if (!YjsEditor.connected(e)) {
1266
+ return;
1267
+ }
1268
+ if (hiddenAt !== void 0 && isSameRange(relativeSelection(), hiddenAt)) {
1269
+ return;
1247
1270
  }
1271
+ CursorEditor.sendCursorPosition(e);
1248
1272
  };
1249
1273
  }
1250
1274
  };