@slate-yjs/core 0.2.0 → 0.2.3

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 (174) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +3 -0
  3. package/dist/applyToSlate/index.d.ts +19 -19
  4. package/dist/applyToSlate/index.d.ts.map +1 -1
  5. package/dist/applyToSlate/textEvent.d.ts +3 -3
  6. package/dist/applyToSlate/textEvent.d.ts.map +1 -1
  7. package/dist/applyToYjs/index.d.ts +3 -3
  8. package/dist/applyToYjs/node/index.d.ts +3 -3
  9. package/dist/applyToYjs/node/insertNode.d.ts +3 -3
  10. package/dist/applyToYjs/node/mergeNode.d.ts +3 -3
  11. package/dist/applyToYjs/node/moveNode.d.ts +3 -3
  12. package/dist/applyToYjs/node/removeNode.d.ts +3 -3
  13. package/dist/applyToYjs/node/setNode.d.ts +3 -3
  14. package/dist/applyToYjs/node/splitNode.d.ts +3 -3
  15. package/dist/applyToYjs/text/index.d.ts +3 -3
  16. package/dist/applyToYjs/text/insertText.d.ts +3 -3
  17. package/dist/applyToYjs/text/removeText.d.ts +3 -3
  18. package/dist/applyToYjs/types.d.ts +8 -8
  19. package/dist/index.cjs +1280 -2
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.ts +5 -5
  22. package/dist/index.global.js +10418 -0
  23. package/dist/index.global.js.map +1 -0
  24. package/dist/index.js +1257 -10
  25. package/dist/index.js.map +1 -1
  26. package/dist/model/types.d.ts +37 -37
  27. package/dist/plugins/index.d.ts +3 -3
  28. package/dist/plugins/withCursors.d.ts +38 -38
  29. package/dist/plugins/withYHistory.d.ts +19 -19
  30. package/dist/plugins/withYHistory.d.ts.map +1 -1
  31. package/dist/plugins/withYjs.d.ts +42 -42
  32. package/dist/plugins/withYjs.d.ts.map +1 -1
  33. package/dist/utils/clone.d.ts +4 -4
  34. package/dist/utils/convert.d.ts +7 -7
  35. package/dist/utils/delta.d.ts +7 -6
  36. package/dist/utils/delta.d.ts.map +1 -1
  37. package/dist/utils/location.d.ts +11 -11
  38. package/dist/utils/object.d.ts +9 -6
  39. package/dist/utils/object.d.ts.map +1 -1
  40. package/dist/utils/position.d.ts +19 -19
  41. package/dist/utils/slate.d.ts +2 -2
  42. package/dist/utils/yjs.d.ts +4 -4
  43. package/package.json +13 -13
  44. package/src/applyToSlate/index.ts +2 -2
  45. package/src/applyToSlate/textEvent.ts +21 -20
  46. package/src/plugins/withYHistory.ts +84 -61
  47. package/src/plugins/withYjs.ts +14 -5
  48. package/src/utils/delta.ts +36 -1
  49. package/src/utils/object.ts +65 -20
  50. package/test/collaboration/addMark/acrossMarks.tsx +40 -0
  51. package/test/collaboration/addMark/acrossMarksSame.tsx +36 -0
  52. package/test/collaboration/addMark/atBeginningOfDocument.tsx +27 -0
  53. package/test/collaboration/addMark/atEndOfDocument.tsx +30 -0
  54. package/test/collaboration/addMark/withOtherMarks.tsx +31 -0
  55. package/test/collaboration/insertNode/atBeginningOfDocument.tsx +28 -0
  56. package/test/collaboration/insertNode/atEndOfDocument.tsx +28 -0
  57. package/test/collaboration/insertNode/inTheMiddle.tsx +30 -0
  58. package/test/collaboration/insertText/atBeginningOfBlock.tsx +26 -0
  59. package/test/collaboration/insertText/atBeginningOfDocument.tsx +26 -0
  60. package/test/collaboration/insertText/atEndOfBlock.tsx +26 -0
  61. package/test/collaboration/insertText/atEndOfDocument.tsx +26 -0
  62. package/test/collaboration/insertText/inTheMiddle.tsx +32 -0
  63. package/test/collaboration/insertText/inTheMiddleOfNestedBlock.tsx +30 -0
  64. package/test/collaboration/insertText/insideMarks.tsx +28 -0
  65. package/test/collaboration/insertText/withEmptyString.tsx +25 -0
  66. package/test/collaboration/insertText/withEntities.tsx +28 -0
  67. package/test/collaboration/insertText/withMarks.tsx +25 -0
  68. package/test/collaboration/insertText/withUnicode.tsx +28 -0
  69. package/test/collaboration/mergeNode/afterADeleteBackward.tsx +27 -0
  70. package/test/collaboration/mergeNode/onMixedNestedNodes.tsx +29 -0
  71. package/test/collaboration/mergeNode/onMixedTypeNodes.tsx +27 -0
  72. package/test/collaboration/mergeNode/withUnicode.tsx +25 -0
  73. package/test/collaboration/moveNode/downward/whenBlockBecomesNested.tsx +43 -0
  74. package/test/collaboration/moveNode/downward/whenBlockBecomesNonNested.tsx +41 -0
  75. package/test/collaboration/moveNode/downward/whenBlockStaysNested.tsx +38 -0
  76. package/test/collaboration/moveNode/downward/whenBlockStaysNonNested.tsx +30 -0
  77. package/test/collaboration/moveNode/upward/whenBlockBecomesNested.tsx +43 -0
  78. package/test/collaboration/moveNode/upward/whenBlockBecomesNonNested.tsx +41 -0
  79. package/test/collaboration/moveNode/upward/whenBlockStaysNested.tsx +38 -0
  80. package/test/collaboration/moveNode/upward/whenBlockStaysNonNested.tsx +30 -0
  81. package/test/collaboration/removeMark/inTheMiddleOfText.tsx +43 -0
  82. package/test/collaboration/removeMark/withAddMark.tsx +31 -0
  83. package/test/collaboration/removeMark/withOtherMarks.tsx +47 -0
  84. package/test/collaboration/removeNode/atBeginningOfDocument.tsx +26 -0
  85. package/test/collaboration/removeNode/atEndOfDocument.tsx +26 -0
  86. package/test/collaboration/removeNode/nestedBlock.tsx +28 -0
  87. package/test/collaboration/removeNode/wrapperBlock.tsx +28 -0
  88. package/test/collaboration/removeText/atBeginningOfDocument.tsx +34 -0
  89. package/test/collaboration/removeText/atEndOfDocument.tsx +37 -0
  90. package/test/collaboration/removeText/withUnicode.tsx +29 -0
  91. package/test/collaboration/setNode/atBeginningOfDocument.tsx +27 -0
  92. package/test/collaboration/setNode/atEndOfDocument.tsx +27 -0
  93. package/test/collaboration/setNode/onDataChange.tsx +35 -0
  94. package/test/collaboration/setNode/onDataChangeOnInline.tsx +35 -0
  95. package/test/collaboration/setNode/onResetBlock.tsx +27 -0
  96. package/test/collaboration/setNode/withAChangeOfType.tsx +26 -0
  97. package/test/collaboration/splitNode/atBeginningOfDocument.tsx +28 -0
  98. package/test/collaboration/splitNode/atEndOfBlock.tsx +27 -0
  99. package/test/collaboration/splitNode/atEndOfDocument.tsx +27 -0
  100. package/test/collaboration/splitNode/onNonDefaultBlock.tsx +27 -0
  101. package/test/collaboration/splitNode/withMultipleSubNodes.tsx +32 -0
  102. package/test/collaboration/splitNode/withUnicode.tsx +29 -0
  103. package/test/index.test.ts +65 -0
  104. package/test/{types.d.ts → slate.d.ts} +0 -0
  105. package/test/withTestingElements.ts +46 -0
  106. package/tsconfig.json +1 -0
  107. package/tsconfig.tsbuildinfo +1 -1
  108. package/tsup.config.ts +32 -0
  109. package/dist/applyToSlate/Untitled-1.d.ts +0 -101
  110. package/dist/applyToSlate/Untitled-1.d.ts.map +0 -1
  111. package/dist/applyToSlate/index.js +0 -32
  112. package/dist/applyToSlate/index.js.map +0 -1
  113. package/dist/applyToSlate/textEvent.js +0 -166
  114. package/dist/applyToSlate/textEvent.js.map +0 -1
  115. package/dist/applyToYjs/index.js +0 -17
  116. package/dist/applyToYjs/index.js.map +0 -1
  117. package/dist/applyToYjs/node/index.js +0 -15
  118. package/dist/applyToYjs/node/index.js.map +0 -1
  119. package/dist/applyToYjs/node/insertNode.js +0 -12
  120. package/dist/applyToYjs/node/insertNode.js.map +0 -1
  121. package/dist/applyToYjs/node/mergeNode.js +0 -32
  122. package/dist/applyToYjs/node/mergeNode.js.map +0 -1
  123. package/dist/applyToYjs/node/moveNode.js +0 -28
  124. package/dist/applyToYjs/node/moveNode.js.map +0 -1
  125. package/dist/applyToYjs/node/removeNode.js +0 -6
  126. package/dist/applyToYjs/node/removeNode.js.map +0 -1
  127. package/dist/applyToYjs/node/setNode.js +0 -21
  128. package/dist/applyToYjs/node/setNode.js.map +0 -1
  129. package/dist/applyToYjs/node/splitNode.js +0 -50
  130. package/dist/applyToYjs/node/splitNode.js.map +0 -1
  131. package/dist/applyToYjs/text/index.js +0 -7
  132. package/dist/applyToYjs/text/index.js.map +0 -1
  133. package/dist/applyToYjs/text/insertText.js +0 -12
  134. package/dist/applyToYjs/text/insertText.js.map +0 -1
  135. package/dist/applyToYjs/text/removeText.js +0 -6
  136. package/dist/applyToYjs/text/removeText.js.map +0 -1
  137. package/dist/applyToYjs/types.js +0 -2
  138. package/dist/applyToYjs/types.js.map +0 -1
  139. package/dist/index.modern.js +0 -2
  140. package/dist/index.modern.js.map +0 -1
  141. package/dist/index.module.js +0 -2
  142. package/dist/index.module.js.map +0 -1
  143. package/dist/index.umd.js +0 -2
  144. package/dist/index.umd.js.map +0 -1
  145. package/dist/model/types.js +0 -2
  146. package/dist/model/types.js.map +0 -1
  147. package/dist/plugins/index.js +0 -4
  148. package/dist/plugins/index.js.map +0 -1
  149. package/dist/plugins/withCursors.js +0 -145
  150. package/dist/plugins/withCursors.js.map +0 -1
  151. package/dist/plugins/withYHistory.js +0 -66
  152. package/dist/plugins/withYHistory.js.map +0 -1
  153. package/dist/plugins/withYjs.js +0 -155
  154. package/dist/plugins/withYjs.js.map +0 -1
  155. package/dist/utils/clone.js +0 -23
  156. package/dist/utils/clone.js.map +0 -1
  157. package/dist/utils/convert.js +0 -35
  158. package/dist/utils/convert.js.map +0 -1
  159. package/dist/utils/delta.js +0 -42
  160. package/dist/utils/delta.js.map +0 -1
  161. package/dist/utils/location.js +0 -111
  162. package/dist/utils/location.js.map +0 -1
  163. package/dist/utils/object.js +0 -27
  164. package/dist/utils/object.js.map +0 -1
  165. package/dist/utils/position.js +0 -140
  166. package/dist/utils/position.js.map +0 -1
  167. package/dist/utils/slate.js +0 -6
  168. package/dist/utils/slate.js.map +0 -1
  169. package/dist/utils/yjs.js +0 -7
  170. package/dist/utils/yjs.js.map +0 -1
  171. package/test/collaboration.test.ts +0 -395
  172. package/test/concurrent.test.ts +0 -233
  173. package/test/testEditor.ts +0 -134
  174. package/test/utils.ts +0 -64
@@ -1,7 +1,10 @@
1
- declare type InspectableObject = Record<string | number | symbol, unknown>;
2
- export declare function isObject(v: unknown): v is InspectableObject;
3
- export declare function deepEquals(obj: InspectableObject, other: InspectableObject): boolean;
4
- export declare function pick<TObj extends {}, TKeys extends keyof TObj>(obj: TObj, ...keys: TKeys[]): Pick<TObj, TKeys>;
5
- export declare function omit<TObj extends {}, TKeys extends keyof TObj>(obj: TObj, ...keys: TKeys[]): Omit<TObj, TKeys>;
6
- export {};
1
+ declare type InspectableObject = Record<string | number | symbol, unknown>;
2
+ export declare function isPlainObject(o: unknown): o is InspectableObject;
3
+ export declare function deepEquals(node: InspectableObject, another: InspectableObject): boolean;
4
+ export declare function pick<TObj, TKeys extends keyof TObj>(obj: TObj, ...keys: TKeys[]): Pick<TObj, TKeys>;
5
+ export declare function omit<TObj, TKeys extends keyof TObj>(obj: TObj, ...keys: TKeys[]): Omit<TObj, TKeys>;
6
+ export declare function omitNullEntries<TObj>(obj: TObj): {
7
+ [K in keyof TObj]: TObj[K] extends null ? never : K;
8
+ };
9
+ export {};
7
10
  //# sourceMappingURL=object.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/utils/object.ts"],"names":[],"mappings":"AAAA,aAAK,iBAAiB,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,CAAC;AAEnE,wBAAgB,QAAQ,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,iBAAiB,CAE3D;AAED,wBAAgB,UAAU,CACxB,GAAG,EAAE,iBAAiB,EACtB,KAAK,EAAE,iBAAiB,GACvB,OAAO,CAkBT;AAGD,wBAAgB,IAAI,CAAC,IAAI,SAAS,EAAE,EAAE,KAAK,SAAS,MAAM,IAAI,EAC5D,GAAG,EAAE,IAAI,EACT,GAAG,IAAI,EAAE,KAAK,EAAE,GACf,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAInB;AAGD,wBAAgB,IAAI,CAAC,IAAI,SAAS,EAAE,EAAE,KAAK,SAAS,MAAM,IAAI,EAC5D,GAAG,EAAE,IAAI,EACT,GAAG,IAAI,EAAE,KAAK,EAAE,GACf,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAInB"}
1
+ {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/utils/object.ts"],"names":[],"mappings":"AAAA,aAAK,iBAAiB,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,CAAC;AAMnE,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,iBAAiB,CAwBhE;AAID,wBAAgB,UAAU,CACxB,IAAI,EAAE,iBAAiB,EACvB,OAAO,EAAE,iBAAiB,GACzB,OAAO,CA6BT;AAED,wBAAgB,IAAI,CAAC,IAAI,EAAE,KAAK,SAAS,MAAM,IAAI,EACjD,GAAG,EAAE,IAAI,EACT,GAAG,IAAI,EAAE,KAAK,EAAE,GACf,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAInB;AAED,wBAAgB,IAAI,CAAC,IAAI,EAAE,KAAK,SAAS,MAAM,IAAI,EACjD,GAAG,EAAE,IAAI,EACT,GAAG,IAAI,EAAE,KAAK,EAAE,GACf,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAInB;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,GAAG;KAC/C,CAAC,IAAI,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,KAAK,GAAG,CAAC;CACpD,CAIA"}
@@ -1,20 +1,20 @@
1
- import { BasePoint, BaseRange, Node } from 'slate';
2
- import * as Y from 'yjs';
3
- import { InsertDelta, RelativeRange } from '../model/types';
4
- export declare const STORED_POSITION_PREFIX = "__slateYjsStoredPosition_";
5
- export declare function slatePointToRelativePosition(sharedRoot: Y.XmlText, slateRoot: Node, point: BasePoint): Y.RelativePosition;
6
- export declare function absolutePositionToSlatePoint(sharedRoot: Y.XmlText, slateRoot: Node, { type, index, assoc }: Y.AbsolutePosition): BasePoint | null;
7
- export declare function relativePositionToSlatePoint(sharedRoot: Y.XmlText, slateRoot: Node, pos: Y.RelativePosition): BasePoint | null;
8
- export declare function getStoredPosition(sharedRoot: Y.XmlText, key: string): Y.RelativePosition | null;
9
- export declare function getStoredPositions(sharedRoot: Y.XmlText): Record<string, Y.RelativePosition>;
10
- export declare function removeStoredPosition(sharedRoot: Y.XmlText, key: string): void;
11
- export declare function setStoredPosition(sharedRoot: Y.XmlText, key: string, position: Y.RelativePosition): void;
12
- export declare function getStoredPositionsInDeltaAbsolute(sharedRoot: Y.XmlText, yText: Y.XmlText, delta: InsertDelta, deltaOffset?: number): {
13
- '': {
14
- [k: string]: Y.AbsolutePosition;
15
- };
16
- };
17
- export declare function restoreStoredPositionsWithDeltaAbsolute(sharedRoot: Y.XmlText, yText: Y.XmlText, absolutePositions: Record<string, Record<string, Y.AbsolutePosition>>, delta: InsertDelta, newDeltaOffset?: number, previousDeltaOffset?: number, path?: string): void;
18
- export declare function slateRangeToRelativeRange(sharedRoot: Y.XmlText, slateRoot: Node, range: BaseRange): RelativeRange;
19
- export declare function relativeRangeToSlateRange(sharedRoot: Y.XmlText, slateRoot: Node, range: RelativeRange): BaseRange | null;
1
+ import { BasePoint, BaseRange, Node } from 'slate';
2
+ import * as Y from 'yjs';
3
+ import { InsertDelta, RelativeRange } from '../model/types';
4
+ export declare const STORED_POSITION_PREFIX = "__slateYjsStoredPosition_";
5
+ export declare function slatePointToRelativePosition(sharedRoot: Y.XmlText, slateRoot: Node, point: BasePoint): Y.RelativePosition;
6
+ export declare function absolutePositionToSlatePoint(sharedRoot: Y.XmlText, slateRoot: Node, { type, index, assoc }: Y.AbsolutePosition): BasePoint | null;
7
+ export declare function relativePositionToSlatePoint(sharedRoot: Y.XmlText, slateRoot: Node, pos: Y.RelativePosition): BasePoint | null;
8
+ export declare function getStoredPosition(sharedRoot: Y.XmlText, key: string): Y.RelativePosition | null;
9
+ export declare function getStoredPositions(sharedRoot: Y.XmlText): Record<string, Y.RelativePosition>;
10
+ export declare function removeStoredPosition(sharedRoot: Y.XmlText, key: string): void;
11
+ export declare function setStoredPosition(sharedRoot: Y.XmlText, key: string, position: Y.RelativePosition): void;
12
+ export declare function getStoredPositionsInDeltaAbsolute(sharedRoot: Y.XmlText, yText: Y.XmlText, delta: InsertDelta, deltaOffset?: number): {
13
+ '': {
14
+ [k: string]: Y.AbsolutePosition;
15
+ };
16
+ };
17
+ export declare function restoreStoredPositionsWithDeltaAbsolute(sharedRoot: Y.XmlText, yText: Y.XmlText, absolutePositions: Record<string, Record<string, Y.AbsolutePosition>>, delta: InsertDelta, newDeltaOffset?: number, previousDeltaOffset?: number, path?: string): void;
18
+ export declare function slateRangeToRelativeRange(sharedRoot: Y.XmlText, slateRoot: Node, range: BaseRange): RelativeRange;
19
+ export declare function relativeRangeToSlateRange(sharedRoot: Y.XmlText, slateRoot: Node, range: RelativeRange): BaseRange | null;
20
20
  //# sourceMappingURL=position.d.ts.map
@@ -1,3 +1,3 @@
1
- import { BaseText, Descendant } from 'slate';
2
- export declare function getProperties<TNode extends Descendant>(node: TNode): Omit<TNode, TNode extends BaseText ? 'text' : 'children'>;
1
+ import { BaseText, Descendant } from 'slate';
2
+ export declare function getProperties<TNode extends Descendant>(node: TNode): Omit<TNode, TNode extends BaseText ? 'text' : 'children'>;
3
3
  //# sourceMappingURL=slate.d.ts.map
@@ -1,5 +1,5 @@
1
- import * as Y from 'yjs';
2
- export declare function assertDocumentAttachment<T extends Y.AbstractType<any>>(sharedType: T): asserts sharedType is T & {
3
- doc: NonNullable<T['doc']>;
4
- };
1
+ import * as Y from 'yjs';
2
+ export declare function assertDocumentAttachment<T extends Y.AbstractType<any>>(sharedType: T): asserts sharedType is T & {
3
+ doc: NonNullable<T['doc']>;
4
+ };
5
5
  //# sourceMappingURL=yjs.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@slate-yjs/core",
3
3
  "sideEffects": false,
4
- "version": "0.2.0",
4
+ "version": "0.2.3",
5
5
  "description": "Yjs binding for Slate.",
6
6
  "keywords": [
7
7
  "slate",
@@ -9,18 +9,17 @@
9
9
  "collaborative"
10
10
  ],
11
11
  "type": "module",
12
- "source": "src/index.ts",
13
12
  "exports": {
14
13
  "require": "./dist/index.cjs",
15
- "default": "./dist/index.modern.js"
14
+ "default": "./dist/index.js"
16
15
  },
17
16
  "main": "dist/index.cjs",
18
- "module": "dist/index.module.js",
19
- "unpkg": "dist/index.umd.js",
17
+ "module": "dist/index.js",
18
+ "unpkg": "dist/index.global.js",
20
19
  "types": "dist/index.d.ts",
21
20
  "scripts": {
22
- "build": "yarn run microbundle",
23
- "dev": "yarn run microbundle watch"
21
+ "build": "yarn run tsup && tsc --emitDeclarationOnly",
22
+ "dev": "yarn run tsup --watch"
24
23
  },
25
24
  "repository": {
26
25
  "type": "git",
@@ -28,14 +27,15 @@
28
27
  },
29
28
  "license": "MIT",
30
29
  "devDependencies": {
31
- "microbundle": "^0.14.1",
32
- "slate": ">=0.70.0",
30
+ "slate": "0.75.0",
33
31
  "slate-hyperscript": "^0.67.0",
34
- "yjs": "^13.5.22"
32
+ "tsup": "^5.12.1",
33
+ "typescript": "^4.6.3",
34
+ "yjs": "^13.5.29"
35
35
  },
36
36
  "peerDependencies": {
37
- "slate": "^0.70.0",
38
- "yjs": "^13.5.22"
37
+ "slate": ">=0.70.0",
38
+ "yjs": "^13.5.29"
39
39
  },
40
40
  "bugs": {
41
41
  "url": "https://github.com/BitPhinix/slate-yjs/issues"
@@ -44,4 +44,4 @@
44
44
  "dependencies": {
45
45
  "y-protocols": "^1.0.5"
46
46
  }
47
- }
47
+ }
@@ -12,7 +12,7 @@ import { translateYTextEvent } from './textEvent';
12
12
  export function translateYjsEvent(
13
13
  sharedRoot: Y.XmlText,
14
14
  editor: Editor,
15
- event: Y.YEvent
15
+ event: Y.YEvent<Y.XmlText>
16
16
  ): Operation[] {
17
17
  if (event instanceof Y.YTextEvent) {
18
18
  return translateYTextEvent(sharedRoot, editor, event);
@@ -32,7 +32,7 @@ export function translateYjsEvent(
32
32
  export function applyYjsEvents(
33
33
  sharedRoot: Y.XmlText,
34
34
  editor: Editor,
35
- events: Y.YEvent[]
35
+ events: Y.YEvent<Y.XmlText>[]
36
36
  ) {
37
37
  Editor.withoutNormalizing(editor, () => {
38
38
  events.forEach((event) => {
@@ -7,7 +7,7 @@ import {
7
7
  getSlatePath,
8
8
  yOffsetToSlateOffsets,
9
9
  } from '../utils/location';
10
- import { deepEquals, pick } from '../utils/object';
10
+ import { deepEquals, omitNullEntries, pick } from '../utils/object';
11
11
  import { getProperties } from '../utils/slate';
12
12
 
13
13
  function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
@@ -34,55 +34,56 @@ function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
34
34
  );
35
35
  const [endPathOffset, endTextOffset] = yOffsetToSlateOffsets(
36
36
  node,
37
- yOffset
37
+ yOffset,
38
+ { assoc: -1 }
38
39
  );
39
40
 
40
41
  for (
41
- let pathOffset =
42
- endTextOffset === 0 ? endPathOffset - 1 : endPathOffset;
42
+ let pathOffset = endPathOffset;
43
43
  pathOffset >= startPathOffset;
44
44
  pathOffset--
45
45
  ) {
46
46
  const child = node.children[pathOffset];
47
- let childPath = [...slatePath, pathOffset];
47
+ const childPath = [...slatePath, pathOffset];
48
48
 
49
49
  const newProperties = change.attributes;
50
+ const properties = pick(
51
+ node,
52
+ ...(Object.keys(change.attributes) as Array<keyof Element>)
53
+ );
50
54
 
51
55
  if (
52
56
  Text.isText(child) &&
53
57
  (pathOffset === startPathOffset || pathOffset === endPathOffset)
54
58
  ) {
55
59
  const start = pathOffset === startPathOffset ? startTextOffset : 0;
56
- let end =
60
+ const end =
57
61
  pathOffset === endPathOffset ? endTextOffset : child.text.length;
58
62
 
59
- const properties = getProperties(child);
60
- if (start !== 0) {
63
+ if (end !== child.text.length) {
61
64
  ops.push({
62
65
  type: 'split_node',
63
66
  path: childPath,
64
- position: start,
65
- properties: { ...properties, ...newProperties },
67
+ position: end,
68
+ properties: getProperties(child),
66
69
  });
67
-
68
- childPath = Path.next(childPath);
69
- end -= start;
70
70
  }
71
71
 
72
- if (end !== child.text.length) {
72
+ if (start !== 0) {
73
73
  ops.push({
74
74
  type: 'split_node',
75
75
  path: childPath,
76
- position: end,
77
- properties: { ...properties, ...newProperties },
76
+ position: start,
77
+ properties: omitNullEntries({
78
+ ...getProperties(child),
79
+ ...newProperties,
80
+ }),
78
81
  });
82
+
83
+ continue;
79
84
  }
80
85
  }
81
86
 
82
- const properties = pick(
83
- node,
84
- ...(Object.keys(change.attributes) as Array<keyof Element>)
85
- );
86
87
  ops.push({
87
88
  type: 'set_node',
88
89
  newProperties,
@@ -86,74 +86,97 @@ export function withYHistory<T extends YjsEditor>(
86
86
  e.isLocalOrigin = (origin) =>
87
87
  origin === e.withoutSavingOrigin || isLocalOrigin(origin);
88
88
 
89
- undoManager.on(
90
- 'stack-item-added',
91
- ({ stackItem }: { stackItem: HistoryStackItem; type: 'redo' | 'undo' }) => {
92
- stackItem.meta.set(
93
- 'selection',
94
- e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
95
- );
96
-
97
- if (!stackItem.meta.has('selectionBefore')) {
98
- stackItem.meta.set('selectionBefore', LAST_SELECTION.get(e));
99
- }
89
+ const handleStackItemAdded = ({
90
+ stackItem,
91
+ }: {
92
+ stackItem: HistoryStackItem;
93
+ type: 'redo' | 'undo';
94
+ }) => {
95
+ stackItem.meta.set(
96
+ 'selection',
97
+ e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
98
+ );
99
+ stackItem.meta.set('selectionBefore', LAST_SELECTION.get(e));
100
+ };
101
+
102
+ const handleStackItemUpdated = ({
103
+ stackItem,
104
+ }: {
105
+ stackItem: HistoryStackItem;
106
+ type: 'redo' | 'undo';
107
+ }) => {
108
+ stackItem.meta.set(
109
+ 'selection',
110
+ e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection)
111
+ );
112
+ };
113
+
114
+ const handleStackItemPopped = ({
115
+ stackItem,
116
+ type,
117
+ }: {
118
+ stackItem: HistoryStackItem;
119
+ type: 'redo' | 'undo';
120
+ }) => {
121
+ // TODO: Change once https://github.com/yjs/yjs/issues/353 is resolved
122
+ const inverseStack =
123
+ type === 'undo' ? undoManager.redoStack : undoManager.undoStack;
124
+ const inverseItem = inverseStack[inverseStack.length - 1];
125
+ if (inverseItem) {
126
+ inverseItem.meta.set('selection', stackItem.meta.get('selectionBefore'));
127
+ inverseItem.meta.set('selectionBefore', stackItem.meta.get('selection'));
100
128
  }
101
- );
102
-
103
- undoManager.on(
104
- 'stack-item-popped',
105
- ({
106
- stackItem,
107
- type,
108
- }: {
109
- stackItem: HistoryStackItem;
110
- type: 'redo' | 'undo';
111
- }) => {
112
- // TODO: Change once https://github.com/yjs/yjs/issues/353 is resolved
113
- const inverseStack =
114
- type === 'undo' ? undoManager.redoStack : undoManager.undoStack;
115
- const inverseItem = inverseStack[inverseStack.length - 1];
116
- if (inverseItem) {
117
- inverseItem.meta.set(
118
- 'selection',
119
- stackItem.meta.get('selectionBefore')
120
- );
121
- inverseItem.meta.set(
122
- 'selectionBefore',
123
- stackItem.meta.get('selection')
124
- );
125
- }
126
-
127
- const relativeSelection = stackItem.meta.get(
128
- 'selectionBefore'
129
- ) as RelativeRange | null;
130
-
131
- if (!relativeSelection) {
132
- return;
133
- }
134
-
135
- const selection = relativeRangeToSlateRange(
136
- e.sharedRoot,
137
- e,
138
- relativeSelection
139
- );
140
-
141
- if (!selection) {
142
- return;
143
- }
144
-
145
- Transforms.select(e, selection);
129
+
130
+ const relativeSelection = stackItem.meta.get(
131
+ 'selectionBefore'
132
+ ) as RelativeRange | null;
133
+
134
+ if (!relativeSelection) {
135
+ return;
146
136
  }
147
- );
137
+
138
+ const selection = relativeRangeToSlateRange(
139
+ e.sharedRoot,
140
+ e,
141
+ relativeSelection
142
+ );
143
+
144
+ if (!selection) {
145
+ return;
146
+ }
147
+
148
+ Transforms.select(e, selection);
149
+ };
150
+
151
+ const { connect, disconnect } = e;
152
+ e.connect = () => {
153
+ connect();
154
+
155
+ undoManager.on('stack-item-added', handleStackItemAdded);
156
+ undoManager.on('stack-item-popped', handleStackItemPopped);
157
+ undoManager.on('stack-item-updated', handleStackItemUpdated);
158
+ };
159
+
160
+ e.disconnect = () => {
161
+ undoManager.off('stack-item-added', handleStackItemAdded);
162
+ undoManager.off('stack-item-popped', handleStackItemPopped);
163
+ undoManager.off('stack-item-updated', handleStackItemUpdated);
164
+
165
+ disconnect();
166
+ };
148
167
 
149
168
  e.undo = () => {
150
- YjsEditor.flushLocalChanges(e);
151
- e.undoManager.undo();
169
+ if (YjsEditor.connected(e)) {
170
+ YjsEditor.flushLocalChanges(e);
171
+ e.undoManager.undo();
172
+ }
152
173
  };
153
174
 
154
175
  e.redo = () => {
155
- YjsEditor.flushLocalChanges(e);
156
- e.undoManager.redo();
176
+ if (YjsEditor.connected(e)) {
177
+ YjsEditor.flushLocalChanges(e);
178
+ e.undoManager.redo();
179
+ }
157
180
  };
158
181
 
159
182
  return e;
@@ -32,7 +32,7 @@ export type YjsEditor = BaseEditor & {
32
32
  localOrigin: unknown;
33
33
  positionStorageOrigin: unknown;
34
34
 
35
- applyRemoteEvents: (events: Y.YEvent[], origin: unknown) => void;
35
+ applyRemoteEvents: (events: Y.YEvent<Y.XmlText>[], origin: unknown) => void;
36
36
 
37
37
  storeLocalChange: (op: Operation) => void;
38
38
  flushLocalChanges: () => void;
@@ -65,7 +65,7 @@ export const YjsEditor = {
65
65
 
66
66
  applyRemoteEvents(
67
67
  editor: YjsEditor,
68
- events: Y.YEvent[],
68
+ events: Y.YEvent<Y.XmlText>[],
69
69
  origin: unknown
70
70
  ): void {
71
71
  editor.applyRemoteEvents(events, origin);
@@ -84,6 +84,10 @@ export const YjsEditor = {
84
84
  },
85
85
 
86
86
  connect(editor: YjsEditor): void {
87
+ if (YjsEditor.connected(editor)) {
88
+ throw new Error('already connected');
89
+ }
90
+
87
91
  editor.connect();
88
92
  },
89
93
 
@@ -96,7 +100,8 @@ export const YjsEditor = {
96
100
  },
97
101
 
98
102
  origin(editor: YjsEditor): unknown {
99
- return ORIGIN.get(editor) ?? editor.localOrigin;
103
+ const origin = ORIGIN.get(editor);
104
+ return origin !== undefined ? origin : editor.localOrigin;
100
105
  },
101
106
 
102
107
  withOrigin(editor: YjsEditor, origin: unknown, fn: () => void): void {
@@ -158,7 +163,7 @@ export function withYjs<T extends Editor>(
158
163
  {
159
164
  localOrigin,
160
165
  positionStorageOrigin,
161
- autoConnect = true,
166
+ autoConnect = false,
162
167
  }: WithYjsOptions = {}
163
168
  ): T & YjsEditor {
164
169
  const e = editor as T & YjsEditor;
@@ -181,7 +186,10 @@ export function withYjs<T extends Editor>(
181
186
 
182
187
  e.isLocalOrigin = (origin) => origin === e.localOrigin;
183
188
 
184
- const handleYEvents = (events: Y.YEvent[], transaction: Y.Transaction) => {
189
+ const handleYEvents = (
190
+ events: Y.YEvent<Y.XmlText>[],
191
+ transaction: Y.Transaction
192
+ ) => {
185
193
  if (e.isLocalOrigin(transaction.origin)) {
186
194
  return;
187
195
  }
@@ -241,6 +249,7 @@ export function withYjs<T extends Editor>(
241
249
 
242
250
  txGroups.forEach((txGroup) => {
243
251
  assertDocumentAttachment(e.sharedRoot);
252
+
244
253
  e.sharedRoot.doc.transact(() => {
245
254
  txGroup.forEach((change) => {
246
255
  assertDocumentAttachment(e.sharedRoot);
@@ -1,8 +1,43 @@
1
1
  import * as Y from 'yjs';
2
2
  import { DeltaInsert, InsertDelta } from '../model/types';
3
+ import { deepEquals } from './object';
4
+
5
+ export function normalizeInsertDelta(delta: InsertDelta): InsertDelta {
6
+ const normalized: InsertDelta = [];
7
+
8
+ for (const element of delta) {
9
+ if (typeof element.insert === 'string' && element.insert.length === 0) {
10
+ continue;
11
+ }
12
+
13
+ const prev = normalized[normalized.length - 1];
14
+ if (
15
+ !prev ||
16
+ typeof prev.insert !== 'string' ||
17
+ typeof element.insert !== 'string'
18
+ ) {
19
+ normalized.push(element);
20
+ continue;
21
+ }
22
+
23
+ const merge =
24
+ prev.attributes === element.attributes ||
25
+ (!prev.attributes === !element.attributes &&
26
+ deepEquals(prev.attributes ?? {}, element.attributes ?? {}));
27
+
28
+ if (merge) {
29
+ prev.insert += element.insert;
30
+ continue;
31
+ }
32
+
33
+ normalized.push(element);
34
+ }
35
+
36
+ return normalized;
37
+ }
3
38
 
4
39
  export function yTextToInsertDelta(yText: Y.XmlText): InsertDelta {
5
- return yText.toDelta() as InsertDelta;
40
+ return normalizeInsertDelta(yText.toDelta()) as InsertDelta;
6
41
  }
7
42
 
8
43
  export function getInsertLength({ insert }: DeltaInsert): number {
@@ -1,34 +1,72 @@
1
1
  type InspectableObject = Record<string | number | symbol, unknown>;
2
2
 
3
- export function isObject(v: unknown): v is InspectableObject {
4
- return v !== null && typeof v === 'object';
3
+ function isObject(o: unknown): o is InspectableObject {
4
+ return Object.prototype.toString.call(o) === '[object Object]';
5
5
  }
6
6
 
7
- export function deepEquals(
8
- obj: InspectableObject,
9
- other: InspectableObject
10
- ): boolean {
11
- const keys1 = Object.keys(obj);
12
- const keys2 = Object.keys(other);
7
+ export function isPlainObject(o: unknown): o is InspectableObject {
8
+ if (!isObject(o)) {
9
+ return false;
10
+ }
11
+
12
+ // If has modified constructor
13
+ const ctor = o.constructor;
14
+ if (ctor === undefined) {
15
+ return true;
16
+ }
13
17
 
14
- if (keys1.length !== keys2.length) {
18
+ // If has modified prototype
19
+ const prot = ctor.prototype;
20
+ if (isObject(prot) === false) {
15
21
  return false;
16
22
  }
17
23
 
18
- return keys1.every((key) => {
19
- const val1 = obj[key];
20
- const val2 = other[key];
24
+ // If constructor does not have an Object-specific method
25
+ if (prot.hasOwnProperty('isPrototypeOf') === false) {
26
+ return false;
27
+ }
28
+
29
+ // Most likely a plain Object
30
+ return true;
31
+ }
32
+
33
+ // Slates deep equality function: https://github.com/ianstormtaylor/slate/blob/68aff89e892fe15a16314398ff052ade6068900b/packages/slate/src/utils/deep-equal.ts#L13
34
+ // We have to match slates deepEquals behavior to merge insert deltas in the same way slate does.
35
+ export function deepEquals(
36
+ node: InspectableObject,
37
+ another: InspectableObject
38
+ ): boolean {
39
+ // eslint-disable-next-line guard-for-in
40
+ for (const key in node) {
41
+ const a = node[key];
42
+ const b = another[key];
43
+
44
+ if (isPlainObject(a) && isPlainObject(b)) {
45
+ if (!deepEquals(a, b)) {
46
+ return false;
47
+ }
48
+ } else if (Array.isArray(a) && Array.isArray(b)) {
49
+ if (a.length !== b.length) return false;
50
+ for (let i = 0; i < a.length; i++) {
51
+ if (a[i] !== b[i]) {
52
+ return false;
53
+ }
54
+ }
55
+ } else if (a !== b) {
56
+ return false;
57
+ }
58
+ }
21
59
 
22
- if (!isObject(val1)) {
23
- return val1 === val2;
60
+ for (const key in another) {
61
+ if (node[key] === undefined && another[key] !== undefined) {
62
+ return false;
24
63
  }
64
+ }
25
65
 
26
- return isObject(val2) && deepEquals(val1, val2);
27
- });
66
+ return true;
28
67
  }
29
68
 
30
- // eslint-disable-next-line @typescript-eslint/ban-types
31
- export function pick<TObj extends {}, TKeys extends keyof TObj>(
69
+ export function pick<TObj, TKeys extends keyof TObj>(
32
70
  obj: TObj,
33
71
  ...keys: TKeys[]
34
72
  ): Pick<TObj, TKeys> {
@@ -37,8 +75,7 @@ export function pick<TObj extends {}, TKeys extends keyof TObj>(
37
75
  ) as Pick<TObj, TKeys>;
38
76
  }
39
77
 
40
- // eslint-disable-next-line @typescript-eslint/ban-types
41
- export function omit<TObj extends {}, TKeys extends keyof TObj>(
78
+ export function omit<TObj, TKeys extends keyof TObj>(
42
79
  obj: TObj,
43
80
  ...keys: TKeys[]
44
81
  ): Omit<TObj, TKeys> {
@@ -46,3 +83,11 @@ export function omit<TObj extends {}, TKeys extends keyof TObj>(
46
83
  Object.entries(obj).filter(([key]) => !keys.includes(key as TKeys))
47
84
  ) as Omit<TObj, TKeys>;
48
85
  }
86
+
87
+ export function omitNullEntries<TObj>(obj: TObj): {
88
+ [K in keyof TObj]: TObj[K] extends null ? never : K;
89
+ } {
90
+ return Object.fromEntries(
91
+ Object.entries(obj).filter(([, value]) => value !== null)
92
+ ) as { [K in keyof TObj]: TObj[K] extends null ? never : K };
93
+ }
@@ -0,0 +1,40 @@
1
+ /** @jsx jsx */
2
+ import { Editor } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled>
8
+ <text>
9
+ Hel
10
+ <anchor />l
11
+ </text>
12
+ <text bold>o</text>
13
+ <text>
14
+ {' '}
15
+ w<focus />
16
+ orld!
17
+ </text>
18
+ </unstyled>
19
+ </editor>
20
+ );
21
+
22
+ export const expected = (
23
+ <editor>
24
+ <unstyled>
25
+ <text>Hel</text>
26
+ <anchor />
27
+ <text italic>l</text>
28
+ <text italic bold>
29
+ o
30
+ </text>
31
+ <text italic> w</text>
32
+ <focus />
33
+ <text>orld!</text>
34
+ </unstyled>
35
+ </editor>
36
+ );
37
+
38
+ export function run(editor: Editor) {
39
+ editor.addMark('italic', true);
40
+ }