@slate-yjs/core 0.2.0 → 0.2.1

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 (103) hide show
  1. package/README.md +3 -0
  2. package/dist/applyToSlate/index.d.ts +2 -2
  3. package/dist/applyToSlate/index.d.ts.map +1 -1
  4. package/dist/applyToSlate/index.js +3 -1
  5. package/dist/applyToSlate/index.js.map +1 -1
  6. package/dist/applyToSlate/textEvent.d.ts.map +1 -1
  7. package/dist/applyToSlate/textEvent.js +17 -15
  8. package/dist/applyToSlate/textEvent.js.map +1 -1
  9. package/dist/applyToYjs/node/splitNode.js.map +1 -1
  10. package/dist/index.cjs +1 -1
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.js +1 -3
  13. package/dist/index.js.map +1 -1
  14. package/dist/index.modern.js +1 -1
  15. package/dist/index.modern.js.map +1 -1
  16. package/dist/index.module.js +1 -1
  17. package/dist/index.module.js.map +1 -1
  18. package/dist/index.umd.js +1 -1
  19. package/dist/index.umd.js.map +1 -1
  20. package/dist/plugins/withCursors.js +30 -29
  21. package/dist/plugins/withCursors.js.map +1 -1
  22. package/dist/plugins/withYHistory.js +20 -10
  23. package/dist/plugins/withYHistory.js.map +1 -1
  24. package/dist/plugins/withYjs.d.ts +2 -2
  25. package/dist/plugins/withYjs.d.ts.map +1 -1
  26. package/dist/plugins/withYjs.js +60 -38
  27. package/dist/plugins/withYjs.js.map +1 -1
  28. package/dist/utils/delta.d.ts +1 -0
  29. package/dist/utils/delta.d.ts.map +1 -1
  30. package/dist/utils/delta.js +26 -1
  31. package/dist/utils/delta.js.map +1 -1
  32. package/dist/utils/object.d.ts +7 -4
  33. package/dist/utils/object.d.ts.map +1 -1
  34. package/dist/utils/object.js +54 -15
  35. package/dist/utils/object.js.map +1 -1
  36. package/dist/utils/slate.js.map +1 -1
  37. package/package.json +5 -5
  38. package/src/applyToSlate/index.ts +2 -2
  39. package/src/applyToSlate/textEvent.ts +21 -20
  40. package/src/plugins/withYjs.ts +13 -4
  41. package/src/utils/delta.ts +36 -1
  42. package/src/utils/object.ts +65 -20
  43. package/test/collaboration/addMark/acrossMarks.tsx +40 -0
  44. package/test/collaboration/addMark/acrossMarksSame.tsx +36 -0
  45. package/test/collaboration/addMark/atBeginningOfDocument.tsx +27 -0
  46. package/test/collaboration/addMark/atEndOfDocument.tsx +30 -0
  47. package/test/collaboration/addMark/withOtherMarks.tsx +31 -0
  48. package/test/collaboration/insertNode/atBeginningOfDocument.tsx +28 -0
  49. package/test/collaboration/insertNode/atEndOfDocument.tsx +28 -0
  50. package/test/collaboration/insertNode/inTheMiddle.tsx +30 -0
  51. package/test/collaboration/insertText/atBeginningOfBlock.tsx +26 -0
  52. package/test/collaboration/insertText/atBeginningOfDocument.tsx +26 -0
  53. package/test/collaboration/insertText/atEndOfBlock.tsx +26 -0
  54. package/test/collaboration/insertText/atEndOfDocument.tsx +26 -0
  55. package/test/collaboration/insertText/inTheMiddle.tsx +32 -0
  56. package/test/collaboration/insertText/inTheMiddleOfNestedBlock.tsx +30 -0
  57. package/test/collaboration/insertText/insideMarks.tsx +28 -0
  58. package/test/collaboration/insertText/withEmptyString.tsx +25 -0
  59. package/test/collaboration/insertText/withEntities.tsx +28 -0
  60. package/test/collaboration/insertText/withMarks.tsx +25 -0
  61. package/test/collaboration/insertText/withUnicode.tsx +28 -0
  62. package/test/collaboration/mergeNode/afterADeleteBackward.tsx +27 -0
  63. package/test/collaboration/mergeNode/onMixedNestedNodes.tsx +29 -0
  64. package/test/collaboration/mergeNode/onMixedTypeNodes.tsx +27 -0
  65. package/test/collaboration/mergeNode/withUnicode.tsx +25 -0
  66. package/test/collaboration/moveNode/downward/whenBlockBecomesNested.tsx +43 -0
  67. package/test/collaboration/moveNode/downward/whenBlockBecomesNonNested.tsx +41 -0
  68. package/test/collaboration/moveNode/downward/whenBlockStaysNested.tsx +38 -0
  69. package/test/collaboration/moveNode/downward/whenBlockStaysNonNested.tsx +30 -0
  70. package/test/collaboration/moveNode/upward/whenBlockBecomesNested.tsx +43 -0
  71. package/test/collaboration/moveNode/upward/whenBlockBecomesNonNested.tsx +41 -0
  72. package/test/collaboration/moveNode/upward/whenBlockStaysNested.tsx +38 -0
  73. package/test/collaboration/moveNode/upward/whenBlockStaysNonNested.tsx +30 -0
  74. package/test/collaboration/removeMark/inTheMiddleOfText.tsx +43 -0
  75. package/test/collaboration/removeMark/withAddMark.tsx +31 -0
  76. package/test/collaboration/removeMark/withOtherMarks.tsx +47 -0
  77. package/test/collaboration/removeNode/atBeginningOfDocument.tsx +26 -0
  78. package/test/collaboration/removeNode/atEndOfDocument.tsx +26 -0
  79. package/test/collaboration/removeNode/nestedBlock.tsx +28 -0
  80. package/test/collaboration/removeNode/wrapperBlock.tsx +28 -0
  81. package/test/collaboration/removeText/atBeginningOfDocument.tsx +34 -0
  82. package/test/collaboration/removeText/atEndOfDocument.tsx +37 -0
  83. package/test/collaboration/removeText/withUnicode.tsx +29 -0
  84. package/test/collaboration/setNode/atBeginningOfDocument.tsx +27 -0
  85. package/test/collaboration/setNode/atEndOfDocument.tsx +27 -0
  86. package/test/collaboration/setNode/onDataChange.tsx +35 -0
  87. package/test/collaboration/setNode/onDataChangeOnInline.tsx +35 -0
  88. package/test/collaboration/setNode/onResetBlock.tsx +27 -0
  89. package/test/collaboration/setNode/withAChangeOfType.tsx +26 -0
  90. package/test/collaboration/splitNode/atBeginningOfDocument.tsx +28 -0
  91. package/test/collaboration/splitNode/atEndOfBlock.tsx +27 -0
  92. package/test/collaboration/splitNode/atEndOfDocument.tsx +27 -0
  93. package/test/collaboration/splitNode/onNonDefaultBlock.tsx +27 -0
  94. package/test/collaboration/splitNode/withMultipleSubNodes.tsx +32 -0
  95. package/test/collaboration/splitNode/withUnicode.tsx +29 -0
  96. package/test/index.test.ts +66 -0
  97. package/test/{types.d.ts → slate.d.ts} +0 -0
  98. package/test/withTestingElements.ts +46 -0
  99. package/tsconfig.tsbuildinfo +1 -1
  100. package/test/collaboration.test.ts +0 -395
  101. package/test/concurrent.test.ts +0 -233
  102. package/test/testEditor.ts +0 -134
  103. package/test/utils.ts +0 -64
@@ -0,0 +1,26 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled>
8
+ <cursor />
9
+ Hello world!
10
+ </unstyled>
11
+ <unstyled id="myBlockId">Welcome to slate-yjs!</unstyled>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <unstyled>
18
+ <cursor />
19
+ Hello world!
20
+ </unstyled>
21
+ </editor>
22
+ );
23
+
24
+ export function run(editor: Editor) {
25
+ Transforms.removeNodes(editor, { at: [1] });
26
+ }
@@ -0,0 +1,28 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled id="myBlockId">
8
+ <cursor />
9
+ Hello world!
10
+ </unstyled>
11
+ <ul>
12
+ <ul-li>Welcome to slate-yjs!</ul-li>
13
+ </ul>
14
+ </editor>
15
+ );
16
+
17
+ export const expected = (
18
+ <editor>
19
+ <unstyled id="myBlockId">
20
+ <cursor />
21
+ Hello world!
22
+ </unstyled>
23
+ </editor>
24
+ );
25
+
26
+ export function run(editor: Editor) {
27
+ Transforms.removeNodes(editor, { at: [1] });
28
+ }
@@ -0,0 +1,28 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled id="myBlockId">
8
+ <cursor />
9
+ Hello world!
10
+ </unstyled>
11
+ <ul>
12
+ <ul-li>Welcome to slate-yjs!</ul-li>
13
+ </ul>
14
+ </editor>
15
+ );
16
+
17
+ export const expected = (
18
+ <editor>
19
+ <unstyled id="myBlockId">
20
+ <cursor />
21
+ Hello world!
22
+ </unstyled>
23
+ </editor>
24
+ );
25
+
26
+ export function run(editor: Editor) {
27
+ Transforms.removeNodes(editor, { at: [1] });
28
+ }
@@ -0,0 +1,34 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled>
8
+ <anchor />
9
+ Hello world!
10
+ <focus />
11
+ </unstyled>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <unstyled>
18
+ <cursor />
19
+ </unstyled>
20
+ </editor>
21
+ );
22
+
23
+ export const inputRemoteEditor = (
24
+ <editor>
25
+ <unstyled>
26
+ Hello world!
27
+ <cursor />
28
+ </unstyled>
29
+ </editor>
30
+ );
31
+
32
+ export function run(editor: Editor) {
33
+ Transforms.delete(editor);
34
+ }
@@ -0,0 +1,37 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled>Hello world!</unstyled>
8
+ <unstyled>
9
+ <anchor />
10
+ Welcome to slate-yjs!
11
+ <focus />
12
+ </unstyled>
13
+ </editor>
14
+ );
15
+
16
+ export const expected = (
17
+ <editor>
18
+ <unstyled>Hello world!</unstyled>
19
+ <unstyled>
20
+ <cursor />
21
+ </unstyled>
22
+ </editor>
23
+ );
24
+
25
+ export const inputRemoteEditor = (
26
+ <editor>
27
+ <unstyled>Hello world!</unstyled>
28
+ <unstyled>
29
+ <cursor />
30
+ Welcome to slate-yjs!
31
+ </unstyled>
32
+ </editor>
33
+ );
34
+
35
+ export function run(editor: Editor) {
36
+ Transforms.delete(editor);
37
+ }
@@ -0,0 +1,29 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled>
8
+ {'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}
9
+ <anchor />
10
+ {'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}
11
+ <focus />
12
+ {'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}
13
+ </unstyled>
14
+ </editor>
15
+ );
16
+
17
+ export const expected = (
18
+ <editor>
19
+ <unstyled>
20
+ {'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}
21
+ <cursor />
22
+ {'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}
23
+ </unstyled>
24
+ </editor>
25
+ );
26
+
27
+ export function run(editor: Editor) {
28
+ Transforms.delete(editor);
29
+ }
@@ -0,0 +1,27 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled id="block1">
8
+ <cursor />
9
+ Hello world!
10
+ </unstyled>
11
+ <unstyled>Welcome to slate-yjs!</unstyled>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <h1 id="block1">
18
+ <cursor />
19
+ Hello world!
20
+ </h1>
21
+ <unstyled>Welcome to slate-yjs!</unstyled>
22
+ </editor>
23
+ );
24
+
25
+ export function run(editor: Editor) {
26
+ Transforms.setNodes(editor, { type: 'header-one' });
27
+ }
@@ -0,0 +1,27 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled id="block1">Hello world!</unstyled>
8
+ <h1 id="block2">
9
+ <cursor />
10
+ Welcome to slate-yjs!
11
+ </h1>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <unstyled id="block1">Hello world!</unstyled>
18
+ <unstyled id="block2">
19
+ <cursor />
20
+ Welcome to slate-yjs!
21
+ </unstyled>
22
+ </editor>
23
+ );
24
+
25
+ export function run(editor: Editor) {
26
+ Transforms.setNodes(editor, { type: 'unstyled', id: 'block2' });
27
+ }
@@ -0,0 +1,35 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <ul>
8
+ <ul-li id="block1">
9
+ <cursor />
10
+ Hello World!
11
+ </ul-li>
12
+ </ul>
13
+ </editor>
14
+ );
15
+
16
+ export const expected = (
17
+ <editor>
18
+ <ul>
19
+ <ul-li id="block1" checked>
20
+ <cursor />
21
+ Hello World!
22
+ </ul-li>
23
+ </ul>
24
+ </editor>
25
+ );
26
+
27
+ export function run(editor: Editor) {
28
+ Transforms.setNodes(
29
+ editor,
30
+ { checked: true },
31
+ {
32
+ mode: 'lowest',
33
+ }
34
+ );
35
+ }
@@ -0,0 +1,35 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled>
8
+ <note-link noteId="note1">
9
+ Meeting notes
10
+ <cursor />
11
+ </note-link>
12
+ </unstyled>
13
+ </editor>
14
+ );
15
+
16
+ export const expected = (
17
+ <editor>
18
+ <unstyled>
19
+ <note-link noteId="note2">
20
+ Meeting notes
21
+ <cursor />
22
+ </note-link>
23
+ </unstyled>
24
+ </editor>
25
+ );
26
+
27
+ export function run(editor: Editor) {
28
+ Transforms.setNodes(
29
+ editor,
30
+ { noteId: 'note2' },
31
+ {
32
+ match: (node) => Editor.isInline(editor, node),
33
+ }
34
+ );
35
+ }
@@ -0,0 +1,27 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <h1 id="block1">
8
+ <cursor />
9
+ Hello world!
10
+ </h1>
11
+ <unstyled>Welcome to slate-yjs!</unstyled>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <unstyled id="block2">
18
+ <cursor />
19
+ Hello world!
20
+ </unstyled>
21
+ <unstyled>Welcome to slate-yjs!</unstyled>
22
+ </editor>
23
+ );
24
+
25
+ export function run(editor: Editor) {
26
+ Transforms.setNodes(editor, { type: 'unstyled', id: 'block2' });
27
+ }
@@ -0,0 +1,26 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled id="block1">
8
+ Hello world!
9
+ <cursor />
10
+ </unstyled>
11
+ </editor>
12
+ );
13
+
14
+ export const expected = (
15
+ <editor>
16
+ <unstyled id="block1">Hello world!</unstyled>
17
+ <h1>
18
+ <cursor />
19
+ </h1>
20
+ </editor>
21
+ );
22
+
23
+ export function run(editor: Editor) {
24
+ editor.insertNode({ type: 'unstyled', children: [{ text: '' }] });
25
+ Transforms.setNodes(editor, { type: 'header-one' });
26
+ }
@@ -0,0 +1,28 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled id="block1">
8
+ <cursor />
9
+ Hello world!
10
+ </unstyled>
11
+ <unstyled>Welcome to slate-yjs!</unstyled>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <unstyled id="block1" />
18
+ <unstyled id="block1">
19
+ <cursor />
20
+ Hello world!
21
+ </unstyled>
22
+ <unstyled>Welcome to slate-yjs!</unstyled>
23
+ </editor>
24
+ );
25
+
26
+ export function run(editor: Editor) {
27
+ Transforms.splitNodes(editor, { always: true });
28
+ }
@@ -0,0 +1,27 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled id="block1">
8
+ Hello world!
9
+ <cursor />
10
+ </unstyled>
11
+ <unstyled>Welcome to slate-yjs!</unstyled>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <unstyled id="block1">Hello world!</unstyled>
18
+ <unstyled id="block1">
19
+ <cursor />
20
+ </unstyled>
21
+ <unstyled>Welcome to slate-yjs!</unstyled>
22
+ </editor>
23
+ );
24
+
25
+ export function run(editor: Editor) {
26
+ Transforms.splitNodes(editor, { always: true });
27
+ }
@@ -0,0 +1,27 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled>Hello world!</unstyled>
8
+ <unstyled id="block1">
9
+ Welcome to slate-yjs!
10
+ <cursor />
11
+ </unstyled>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <unstyled>Hello world!</unstyled>
18
+ <unstyled id="block1">Welcome to slate-yjs!</unstyled>
19
+ <unstyled id="block1">
20
+ <cursor />
21
+ </unstyled>
22
+ </editor>
23
+ );
24
+
25
+ export function run(editor: Editor) {
26
+ Transforms.splitNodes(editor, { always: true });
27
+ }
@@ -0,0 +1,27 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <h1 id="block1">
8
+ Hello world!
9
+ <cursor />
10
+ </h1>
11
+ <unstyled>Welcome to slate-yjs!</unstyled>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <h1 id="block1">Hello world!</h1>
18
+ <h1 id="block1">
19
+ <cursor />
20
+ </h1>
21
+ <unstyled>Welcome to slate-yjs!</unstyled>
22
+ </editor>
23
+ );
24
+
25
+ export function run(editor: Editor) {
26
+ Transforms.splitNodes(editor, { always: true });
27
+ }
@@ -0,0 +1,32 @@
1
+ /* eslint-disable react/void-dom-elements-no-children */
2
+ /** @jsx jsx */
3
+ import { Editor, Transforms } from 'slate';
4
+ import { jsx } from '../../../../../support/jsx';
5
+
6
+ export const input = (
7
+ <editor>
8
+ <unstyled id="block1">
9
+ slate-yjs
10
+ <cursor />
11
+ slate-yjs
12
+ <link url="https://slate-yjs.dev">slate-yjs</link>
13
+ <link url="https://slate-yjs.dev">slate-yjs</link>
14
+ </unstyled>
15
+ </editor>
16
+ );
17
+
18
+ export const expected = (
19
+ <editor>
20
+ <unstyled id="block1">slate-yjs</unstyled>
21
+ <unstyled id="block1">
22
+ <cursor />
23
+ slate-yjs
24
+ <link url="https://slate-yjs.dev">slate-yjs</link>
25
+ <link url="https://slate-yjs.dev">slate-yjs</link>
26
+ </unstyled>
27
+ </editor>
28
+ );
29
+
30
+ export function run(editor: Editor) {
31
+ Transforms.splitNodes(editor, { always: true });
32
+ }
@@ -0,0 +1,29 @@
1
+ /** @jsx jsx */
2
+ import { Editor, Transforms } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled id="block1">
8
+ H{'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}
9
+ <cursor />
10
+ {'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}
11
+ </unstyled>
12
+ <unstyled>{'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}</unstyled>
13
+ </editor>
14
+ );
15
+
16
+ export const expected = (
17
+ <editor>
18
+ <unstyled id="block1">H{'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}</unstyled>
19
+ <unstyled id="block1">
20
+ <cursor />
21
+ {'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}
22
+ </unstyled>
23
+ <unstyled>{'Iñtërnâtiônàlizætiøn☃💩\uFEFF'}</unstyled>
24
+ </editor>
25
+ );
26
+
27
+ export function run(editor: Editor) {
28
+ Transforms.splitNodes(editor, { always: true });
29
+ }
@@ -0,0 +1,66 @@
1
+ import { createEditor, Editor } from 'slate';
2
+ import * as Y from 'yjs';
3
+ import { fixtures } from '../../../support/fixtures';
4
+ import { yTextToSlateElement } from '../src';
5
+ import { withTestingElements } from './withTestingElements';
6
+
7
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
8
+ jest.mock('y-protocols/awareness', () => {});
9
+
10
+ export type FixtureModule = {
11
+ module: {
12
+ input: Editor;
13
+ expected: Editor;
14
+ run: (e: Editor) => void;
15
+ };
16
+ };
17
+
18
+ async function normalizedSlateDoc(sharedRoot: Y.XmlText) {
19
+ const editor = createEditor();
20
+ editor.children = yTextToSlateElement(sharedRoot).children;
21
+ const e = await withTestingElements(editor);
22
+ Editor.normalize(e, { force: true });
23
+ return e.children;
24
+ }
25
+
26
+ async function runCollaborationTest({ module }: FixtureModule) {
27
+ // Setup 'local' editor
28
+ const { input, run, expected } = module;
29
+ const editor = await withTestingElements(input);
30
+
31
+ // Keep the 'local' editor state before applying run.
32
+ const baseState = Y.encodeStateAsUpdateV2(editor.sharedRoot.doc);
33
+
34
+ Editor.normalize(editor, { force: true });
35
+
36
+ // The normalized editor state should match the shared root.
37
+ expect(await normalizedSlateDoc(editor.sharedRoot)).toEqual(editor.children);
38
+
39
+ run(editor);
40
+ editor.onChange();
41
+
42
+ // Editor state after run should match shared root.
43
+ expect(await normalizedSlateDoc(editor.sharedRoot)).toEqual(editor.children);
44
+
45
+ // Setup remote editor with input base state
46
+ const remoteDoc = new Y.Doc();
47
+ Y.applyUpdateV2(remoteDoc, baseState);
48
+ const remote = await withTestingElements(createEditor(), remoteDoc);
49
+
50
+ // Apply changes from 'run'
51
+ Y.applyUpdateV2(remoteDoc, Y.encodeStateAsUpdateV2(editor.sharedRoot.doc));
52
+
53
+ // Verify remote and editor state are equal
54
+ expect(await normalizedSlateDoc(remote.sharedRoot)).toEqual(remote.children);
55
+ expect(editor.children).toEqual(remote.children);
56
+ expect(await normalizedSlateDoc(editor.sharedRoot)).toEqual(editor.children);
57
+
58
+ // Verify editor is in expected state
59
+ const expectedEditor = await withTestingElements(expected);
60
+ Editor.normalize(expectedEditor, { force: true });
61
+ expect(editor.children).toEqual(expectedEditor.children);
62
+ }
63
+
64
+ describe('adapter', () => {
65
+ fixtures(__dirname, 'collaboration', runCollaborationTest);
66
+ });
File without changes
@@ -0,0 +1,46 @@
1
+ import { Editor, Element, Transforms } from 'slate';
2
+ import * as Y from 'yjs';
3
+ import { wait } from '../../../support/utils';
4
+ import { slateNodesToInsertDelta, withYjs } from '../src';
5
+
6
+ const INLINE_ELEMENTS = ['note-link', 'link'];
7
+
8
+ export async function withTestingElements(
9
+ editor: Editor,
10
+ doc: Y.Doc = new Y.Doc()
11
+ ) {
12
+ const { normalizeNode, isInline } = editor;
13
+
14
+ // normalizations needed for nested tests
15
+ editor.normalizeNode = (entry) => {
16
+ const [node, path] = entry;
17
+
18
+ // remove empty list
19
+ if (
20
+ Element.isElement(node) &&
21
+ !Editor.isEditor(node) &&
22
+ node.type === 'unordered-list'
23
+ ) {
24
+ if (!node.children.length) {
25
+ return Transforms.removeNodes(editor, { at: path });
26
+ }
27
+ }
28
+
29
+ normalizeNode(entry);
30
+ };
31
+
32
+ editor.isInline = (element) =>
33
+ INLINE_ELEMENTS.includes(element.type as string) || isInline(element);
34
+
35
+ const sharedType = doc.get('sharedRoot', Y.XmlText) as Y.XmlText;
36
+ if (sharedType.toDelta().length === 0) {
37
+ sharedType.applyDelta(slateNodesToInsertDelta(editor.children));
38
+ }
39
+
40
+ const e = withYjs(editor, sharedType);
41
+
42
+ // Wait for editor to be initialized
43
+ await wait();
44
+
45
+ return e;
46
+ }