@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/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @slate-yjs/core
2
2
 
3
+ ## 1.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - [#455](https://github.com/slate-yjs/slate-yjs/pull/455) [`8e65a09`](https://github.com/slate-yjs/slate-yjs/commit/8e65a0939c62f4c1fb5c866b0b7dc6ab7aeb4134) Thanks [@juliankrispel](https://github.com/juliankrispel)! - With `autoSend`, a cursor hidden with `CursorEditor.sendCursorPosition(editor, null)` (as `useUnsetCursorPositionOnBlur` does) now stays hidden until the local selection moves. It was sent again on the next change, including changes from other users. An unchanged selection is also no longer re-sent on every change.
8
+
9
+ - [#454](https://github.com/slate-yjs/slate-yjs/pull/454) [`58a9c0f`](https://github.com/slate-yjs/slate-yjs/commit/58a9c0f6912e2d825c058aad47e4b23fe478e14a) Thanks [@juliankrispel](https://github.com/juliankrispel)! - Fix moving a node forward within its parent. The target position in Yjs was computed before the node was removed, so text and inline nodes landed at the wrong offset, for example inside the next text node, and other clients saw a different document.
10
+
11
+ ## 1.0.4
12
+
13
+ ### Patch Changes
14
+
15
+ - [#452](https://github.com/slate-yjs/slate-yjs/pull/452) [`968a580`](https://github.com/slate-yjs/slate-yjs/commit/968a5806b3a46977a6a559c0808032a2a2f6a6f3) Thanks [@juliankrispel](https://github.com/juliankrispel)! - Write the changes produced by normalizing a remote update to Yjs straight away. They were queued until `onChange`, so a second remote update applied first left them pointing at the wrong nodes, which corrupted the document or threw errors such as "Cannot merge y text with y element" and "yOffset out of bounds".
16
+
17
+ Also warns once per editor when a remote update is applied while local edits are still queued, which can still corrupt the document. Call `YjsEditor.flushLocalChanges(editor)` before applying remote updates in the same task as local edits.
18
+
19
+ - [#453](https://github.com/slate-yjs/slate-yjs/pull/453) [`6e012de`](https://github.com/slate-yjs/slate-yjs/commit/6e012deb8f547a82a51ff291a0c04c1e7f11429d) Thanks [@juliankrispel](https://github.com/juliankrispel)! - Keep a trailing soft line break (`\n`) when merging nodes. Yjs drops a trailing newline from `applyDelta` when `sanitize` is false.
20
+
21
+ - [#443](https://github.com/slate-yjs/slate-yjs/pull/443) [`82077b5`](https://github.com/slate-yjs/slate-yjs/commit/82077b592708c38277b1a7a1f3781e3f4fa83be6) Thanks [@juliankrispel](https://github.com/juliankrispel)! - Publish only `dist`, `src`, the changelog and the license, leaving tests and build config out of the package.
22
+
3
23
  ## 1.0.3
4
24
 
5
25
  ### Patch Changes
@@ -1 +1 @@
1
- {"version":3,"file":"mergeNode.d.ts","sourceRoot":"","sources":["../../../src/applyToYjs/node/mergeNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,IAAI,EAAc,MAAM,OAAO,CAAC;AAC7D,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAWzB,wBAAgB,SAAS,CACvB,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,EAAE,EAAE,kBAAkB,GACrB,IAAI,CAiEN"}
1
+ {"version":3,"file":"mergeNode.d.ts","sourceRoot":"","sources":["../../../src/applyToYjs/node/mergeNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,IAAI,EAAc,MAAM,OAAO,CAAC;AAC7D,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAYzB,wBAAgB,SAAS,CACvB,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,EAAE,EAAE,kBAAkB,GACrB,IAAI,CA+DN"}
@@ -1 +1 @@
1
- {"version":3,"file":"moveNode.d.ts","sourceRoot":"","sources":["../../../src/applyToYjs/node/moveNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,IAAI,EAAc,MAAM,OAAO,CAAC;AAC5D,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAUzB,wBAAgB,QAAQ,CACtB,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,EAAE,EAAE,iBAAiB,GACpB,IAAI,CAyCN"}
1
+ {"version":3,"file":"moveNode.d.ts","sourceRoot":"","sources":["../../../src/applyToYjs/node/moveNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,iBAAiB,EAAE,IAAI,EAAc,MAAM,OAAO,CAAC;AACxE,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AA6BzB,wBAAgB,QAAQ,CACtB,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,EAAE,EAAE,iBAAiB,GACpB,IAAI,CAiDN"}
@@ -1 +1 @@
1
- {"version":3,"file":"splitNode.d.ts","sourceRoot":"","sources":["../../../src/applyToYjs/node/splitNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,kBAAkB,EAAQ,MAAM,OAAO,CAAC;AACvD,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AASzB,wBAAgB,SAAS,CACvB,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,EAAE,EAAE,kBAAkB,GACrB,IAAI,CAmFN"}
1
+ {"version":3,"file":"splitNode.d.ts","sourceRoot":"","sources":["../../../src/applyToYjs/node/splitNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,kBAAkB,EAAQ,MAAM,OAAO,CAAC;AACvD,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAUzB,wBAAgB,SAAS,CACvB,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,EAAE,EAAE,kBAAkB,GACrB,IAAI,CAiFN"}
package/dist/index.cjs CHANGED
@@ -209,6 +209,14 @@ function getProperties(node) {
209
209
  return omit(node, import_slate.Text.isText(node) ? "text" : "children");
210
210
  }
211
211
 
212
+ // src/utils/yjs.ts
213
+ function assertDocumentAttachment(sharedType) {
214
+ if (!sharedType.doc) {
215
+ throw new Error("shared type isn't attached to a document");
216
+ }
217
+ }
218
+ var PRESERVE_CONTENT = { sanitize: true };
219
+
212
220
  // src/utils/convert.ts
213
221
  function yTextToSlateElement(yText) {
214
222
  const delta = yTextToInsertDelta(yText);
@@ -239,7 +247,7 @@ function slateElementToYText(_a) {
239
247
  Object.entries(attributes).forEach(([key, value]) => {
240
248
  yElement.setAttribute(key, value);
241
249
  });
242
- yElement.applyDelta(slateNodesToInsertDelta(children), { sanitize: false });
250
+ yElement.applyDelta(slateNodesToInsertDelta(children), PRESERVE_CONTENT);
243
251
  return yElement;
244
252
  }
245
253
 
@@ -561,24 +569,13 @@ function cloneDeep(yText) {
561
569
  Object.entries(attributes).forEach(([key, value]) => {
562
570
  clone.setAttribute(key, value);
563
571
  });
564
- clone.applyDelta(cloneInsertDeltaDeep(yTextToInsertDelta(yText)), {
565
- sanitize: false
566
- });
572
+ clone.applyDelta(cloneInsertDeltaDeep(yTextToInsertDelta(yText)), PRESERVE_CONTENT);
567
573
  return clone;
568
574
  }
569
575
 
570
576
  // src/utils/position.ts
571
577
  var import_slate10 = require("slate");
572
578
  var Y6 = __toESM(require("yjs"), 1);
573
-
574
- // src/utils/yjs.ts
575
- function assertDocumentAttachment(sharedType) {
576
- if (!sharedType.doc) {
577
- throw new Error("shared type isn't attached to a document");
578
- }
579
- }
580
-
581
- // src/utils/position.ts
582
579
  var STORED_POSITION_PREFIX = "__slateYjsStoredPosition_";
583
580
  function slatePointToRelativePosition(sharedRoot, slateRoot, point) {
584
581
  const { yTarget, yParent, textRange } = getYTarget(sharedRoot, slateRoot, point.path);
@@ -737,35 +734,49 @@ function mergeNode(sharedRoot, slateRoot, op) {
737
734
  const clonedDelta = cloneInsertDeltaDeep(targetDelta);
738
735
  const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, target.yTarget, targetDelta, deltaApplyYOffset);
739
736
  const applyDelta2 = [{ retain: deltaApplyYOffset }, ...clonedDelta];
740
- prev.yTarget.applyDelta(applyDelta2, {
741
- sanitize: false
742
- });
737
+ prev.yTarget.applyDelta(applyDelta2, PRESERVE_CONTENT);
743
738
  target.yParent.delete(target.textRange.start, target.textRange.end - target.textRange.start);
744
739
  restoreStoredPositionsWithDeltaAbsolute(sharedRoot, prev.yTarget, storedPositions, clonedDelta, deltaApplyYOffset);
745
740
  }
746
741
 
747
742
  // src/applyToYjs/node/moveNode.ts
748
743
  var import_slate13 = require("slate");
749
- function moveNode(sharedRoot, slateRoot, op) {
750
- const newParentPath = import_slate13.Path.parent(op.newPath);
751
- const newPathOffset = op.newPath[op.newPath.length - 1];
752
- const parent = import_slate13.Node.get(slateRoot, newParentPath);
753
- if (import_slate13.Text.isText(parent)) {
754
- throw new Error("Cannot move slate node into text element");
744
+ function withoutNode(root, path) {
745
+ if (import_slate13.Text.isText(root)) {
746
+ throw new Error("Cannot descend into slate text");
755
747
  }
756
- const normalizedNewPath = [
757
- ...newParentPath,
758
- Math.min(newPathOffset, parent.children.length)
759
- ];
748
+ const [index, ...rest] = path;
749
+ const children = [...root.children];
750
+ if (rest.length === 0) {
751
+ children.splice(index, 1);
752
+ } else {
753
+ children[index] = withoutNode(children[index], rest);
754
+ }
755
+ return __spreadProps(__spreadValues({}, root), { children });
756
+ }
757
+ function moveNode(sharedRoot, slateRoot, op) {
760
758
  const origin = getYTarget(sharedRoot, slateRoot, op.path);
761
- const target = getYTarget(sharedRoot, slateRoot, normalizedNewPath);
762
759
  const insertDelta = cloneInsertDeltaDeep(origin.targetDelta);
763
760
  const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, origin.yParent, origin.targetDelta);
764
761
  origin.yParent.delete(origin.textRange.start, origin.textRange.end - origin.textRange.start);
762
+ const rootWithoutNode = withoutNode(slateRoot, op.path);
763
+ const newPath = import_slate13.Path.transform(op.path, op);
764
+ if (!newPath) {
765
+ throw new Error("Cannot resolve the target path of a move");
766
+ }
767
+ const newParentPath = import_slate13.Path.parent(newPath);
768
+ const parent = import_slate13.Node.get(rootWithoutNode, newParentPath);
769
+ if (import_slate13.Text.isText(parent)) {
770
+ throw new Error("Cannot move slate node into text element");
771
+ }
772
+ const target = getYTarget(sharedRoot, rootWithoutNode, [
773
+ ...newParentPath,
774
+ Math.min(newPath[newPath.length - 1], parent.children.length)
775
+ ]);
765
776
  const targetLength = getInsertDeltaLength(yTextToInsertDelta(target.yParent));
766
777
  const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
767
778
  const applyDelta2 = [{ retain: deltaApplyYOffset }, ...insertDelta];
768
- target.yParent.applyDelta(applyDelta2, { sanitize: false });
779
+ target.yParent.applyDelta(applyDelta2, PRESERVE_CONTENT);
769
780
  restoreStoredPositionsWithDeltaAbsolute(sharedRoot, target.yParent, storedPositions, insertDelta, deltaApplyYOffset, origin.textRange.start);
770
781
  }
771
782
 
@@ -830,9 +841,7 @@ function splitNode(sharedRoot, slateRoot, op) {
830
841
  const clonedDelta = cloneInsertDeltaDeep(splitDelta);
831
842
  const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, target.yTarget, splitDelta, ySplitOffset);
832
843
  const toInsert = new Y7.XmlText();
833
- toInsert.applyDelta(clonedDelta, {
834
- sanitize: false
835
- });
844
+ toInsert.applyDelta(clonedDelta, PRESERVE_CONTENT);
836
845
  Object.entries(op.properties).forEach(([key, value]) => {
837
846
  toInsert.setAttribute(key, value);
838
847
  });
@@ -968,13 +977,19 @@ function withYjs(editor, sharedRoot, {
968
977
  e.sharedRoot = sharedRoot;
969
978
  e.localOrigin = localOrigin != null ? localOrigin : DEFAULT_LOCAL_ORIGIN;
970
979
  e.positionStorageOrigin = positionStorageOrigin != null ? positionStorageOrigin : DEFAULT_POSITION_STORAGE_ORIGIN;
980
+ let warnedAboutQueuedChanges = false;
971
981
  e.applyRemoteEvents = (events, origin) => {
982
+ if (!warnedAboutQueuedChanges && YjsEditor.localChanges(e).length > 0) {
983
+ warnedAboutQueuedChanges = true;
984
+ 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.");
985
+ }
972
986
  YjsEditor.flushLocalChanges(e);
973
987
  import_slate17.Editor.withoutNormalizing(e, () => {
974
988
  YjsEditor.withOrigin(e, origin, () => {
975
989
  applyYjsEvents(e.sharedRoot, e, events);
976
990
  });
977
991
  });
992
+ YjsEditor.flushLocalChanges(e);
978
993
  };
979
994
  e.isLocalOrigin = (origin) => origin === e.localOrigin;
980
995
  const handleYEvents = (events, transaction) => {
@@ -1233,18 +1248,23 @@ function withCursors(editor, awareness, {
1233
1248
  e.sendCursorData = (cursorData) => {
1234
1249
  e.awareness.setLocalStateField(e.cursorDataField, cursorData);
1235
1250
  };
1251
+ let hiddenAt;
1252
+ const relativeSelection = () => e.selection ? slateRangeToRelativeRange(e.sharedRoot, e, e.selection) : null;
1253
+ const isSameRange = (a, b) => a === b || !!a && !!b && Y10.compareRelativePositions(a.anchor, b.anchor) && Y10.compareRelativePositions(a.focus, b.focus);
1236
1254
  e.sendCursorPosition = (range) => {
1237
1255
  const localState = e.awareness.getLocalState();
1238
1256
  const currentRange = localState == null ? void 0 : localState[selectionStateField];
1239
1257
  if (!range) {
1258
+ hiddenAt = relativeSelection();
1240
1259
  if (currentRange) {
1241
1260
  e.awareness.setLocalStateField(e.selectionStateField, null);
1242
1261
  }
1243
1262
  return;
1244
1263
  }
1245
- const { anchor, focus } = slateRangeToRelativeRange(e.sharedRoot, e, range);
1246
- if (!currentRange || !Y10.compareRelativePositions(anchor, currentRange) || !Y10.compareRelativePositions(focus, currentRange)) {
1247
- e.awareness.setLocalStateField(e.selectionStateField, { anchor, focus });
1264
+ hiddenAt = void 0;
1265
+ const relativeRange = slateRangeToRelativeRange(e.sharedRoot, e, range);
1266
+ if (!currentRange || !isSameRange(relativeRange, currentRange)) {
1267
+ e.awareness.setLocalStateField(e.selectionStateField, relativeRange);
1248
1268
  }
1249
1269
  };
1250
1270
  const awarenessChangeListener = (yEvent) => {
@@ -1278,9 +1298,13 @@ function withCursors(editor, awareness, {
1278
1298
  const { onChange } = e;
1279
1299
  e.onChange = () => {
1280
1300
  onChange();
1281
- if (YjsEditor.connected(e)) {
1282
- CursorEditor.sendCursorPosition(e);
1301
+ if (!YjsEditor.connected(e)) {
1302
+ return;
1303
+ }
1304
+ if (hiddenAt !== void 0 && isSameRange(relativeSelection(), hiddenAt)) {
1305
+ return;
1283
1306
  }
1307
+ CursorEditor.sendCursorPosition(e);
1284
1308
  };
1285
1309
  }
1286
1310
  };