@slate-yjs/core 1.0.2 → 1.0.4

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 (85) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE.md +7 -0
  3. package/dist/applyToYjs/node/mergeNode.d.ts.map +1 -1
  4. package/dist/applyToYjs/node/moveNode.d.ts.map +1 -1
  5. package/dist/applyToYjs/node/splitNode.d.ts.map +1 -1
  6. package/dist/index.cjs +20 -21
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.global.js +3908 -5463
  9. package/dist/index.global.js.map +1 -1
  10. package/dist/index.js +20 -21
  11. package/dist/index.js.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 +18 -13
  18. package/src/applyToYjs/node/mergeNode.ts +2 -3
  19. package/src/applyToYjs/node/moveNode.ts +2 -1
  20. package/src/applyToYjs/node/splitNode.ts +2 -3
  21. package/src/plugins/withYjs.ts +17 -0
  22. package/src/utils/clone.ts +5 -3
  23. package/src/utils/convert.ts +2 -1
  24. package/src/utils/position.ts +1 -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/tsconfig.tsbuildinfo +0 -1
  85. package/tsup.config.ts +0 -32
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @slate-yjs/core
2
2
 
3
+ ## 1.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#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".
8
+
9
+ 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.
10
+
11
+ - [#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.
12
+
13
+ - [#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.
14
+
15
+ ## 1.0.3
16
+
17
+ ### Patch Changes
18
+
19
+ - [#421](https://github.com/slate-yjs/slate-yjs/pull/421) [`ea9b642`](https://github.com/slate-yjs/slate-yjs/commit/ea9b642b3ae1781833de57c16c146bb6cad848be) Thanks [@jul13579](https://github.com/jul13579)! - Fix `getStoredPositions` decoding
20
+
3
21
  ## 1.0.2
4
22
 
5
23
  ### Patch Changes
package/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2021 Eric Meier <eric@meier.sh>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -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,EAAE,iBAAiB,EAAE,IAAI,EAAc,MAAM,OAAO,CAAC;AAC5D,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAWzB,wBAAgB,QAAQ,CACtB,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,EAAE,EAAE,iBAAiB,GACpB,IAAI,CAyCN"}
@@ -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);
@@ -623,7 +620,7 @@ function getStoredPosition(sharedRoot, key) {
623
620
  function getStoredPositions(sharedRoot) {
624
621
  return Object.fromEntries(Object.entries(sharedRoot.getAttributes()).filter(([key]) => key.startsWith(STORED_POSITION_PREFIX)).map(([key, position]) => [
625
622
  key.slice(STORED_POSITION_PREFIX.length),
626
- Y6.createRelativePositionFromJSON(position)
623
+ Y6.decodeRelativePosition(position)
627
624
  ]));
628
625
  }
629
626
  function getStoredPositionsAbsolute(sharedRoot) {
@@ -737,9 +734,7 @@ 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
  }
@@ -765,7 +760,7 @@ function moveNode(sharedRoot, slateRoot, op) {
765
760
  const targetLength = getInsertDeltaLength(yTextToInsertDelta(target.yParent));
766
761
  const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
767
762
  const applyDelta2 = [{ retain: deltaApplyYOffset }, ...insertDelta];
768
- target.yParent.applyDelta(applyDelta2, { sanitize: false });
763
+ target.yParent.applyDelta(applyDelta2, PRESERVE_CONTENT);
769
764
  restoreStoredPositionsWithDeltaAbsolute(sharedRoot, target.yParent, storedPositions, insertDelta, deltaApplyYOffset, origin.textRange.start);
770
765
  }
771
766
 
@@ -830,9 +825,7 @@ function splitNode(sharedRoot, slateRoot, op) {
830
825
  const clonedDelta = cloneInsertDeltaDeep(splitDelta);
831
826
  const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, target.yTarget, splitDelta, ySplitOffset);
832
827
  const toInsert = new Y7.XmlText();
833
- toInsert.applyDelta(clonedDelta, {
834
- sanitize: false
835
- });
828
+ toInsert.applyDelta(clonedDelta, PRESERVE_CONTENT);
836
829
  Object.entries(op.properties).forEach(([key, value]) => {
837
830
  toInsert.setAttribute(key, value);
838
831
  });
@@ -968,13 +961,19 @@ function withYjs(editor, sharedRoot, {
968
961
  e.sharedRoot = sharedRoot;
969
962
  e.localOrigin = localOrigin != null ? localOrigin : DEFAULT_LOCAL_ORIGIN;
970
963
  e.positionStorageOrigin = positionStorageOrigin != null ? positionStorageOrigin : DEFAULT_POSITION_STORAGE_ORIGIN;
964
+ let warnedAboutQueuedChanges = false;
971
965
  e.applyRemoteEvents = (events, origin) => {
966
+ if (!warnedAboutQueuedChanges && YjsEditor.localChanges(e).length > 0) {
967
+ warnedAboutQueuedChanges = true;
968
+ 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.");
969
+ }
972
970
  YjsEditor.flushLocalChanges(e);
973
971
  import_slate17.Editor.withoutNormalizing(e, () => {
974
972
  YjsEditor.withOrigin(e, origin, () => {
975
973
  applyYjsEvents(e.sharedRoot, e, events);
976
974
  });
977
975
  });
976
+ YjsEditor.flushLocalChanges(e);
978
977
  };
979
978
  e.isLocalOrigin = (origin) => origin === e.localOrigin;
980
979
  const handleYEvents = (events, transaction) => {