@slate-yjs/core 0.1.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 (111) 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.d.ts +7 -2
  23. package/dist/plugins/withYHistory.d.ts.map +1 -1
  24. package/dist/plugins/withYHistory.js +20 -10
  25. package/dist/plugins/withYHistory.js.map +1 -1
  26. package/dist/plugins/withYjs.d.ts +9 -6
  27. package/dist/plugins/withYjs.d.ts.map +1 -1
  28. package/dist/plugins/withYjs.js +60 -38
  29. package/dist/plugins/withYjs.js.map +1 -1
  30. package/dist/utils/delta.d.ts +1 -0
  31. package/dist/utils/delta.d.ts.map +1 -1
  32. package/dist/utils/delta.js +26 -1
  33. package/dist/utils/delta.js.map +1 -1
  34. package/dist/utils/object.d.ts +7 -4
  35. package/dist/utils/object.d.ts.map +1 -1
  36. package/dist/utils/object.js +54 -15
  37. package/dist/utils/object.js.map +1 -1
  38. package/dist/utils/position.d.ts.map +1 -1
  39. package/dist/utils/slate.d.ts +2 -2
  40. package/dist/utils/slate.d.ts.map +1 -1
  41. package/dist/utils/slate.js.map +1 -1
  42. package/package.json +5 -5
  43. package/src/applyToSlate/index.ts +2 -2
  44. package/src/applyToSlate/textEvent.ts +21 -20
  45. package/src/plugins/withYHistory.ts +26 -5
  46. package/src/plugins/withYjs.ts +68 -33
  47. package/src/utils/delta.ts +36 -1
  48. package/src/utils/object.ts +65 -20
  49. package/src/utils/position.ts +1 -1
  50. package/src/utils/slate.ts +3 -3
  51. package/test/collaboration/addMark/acrossMarks.tsx +40 -0
  52. package/test/collaboration/addMark/acrossMarksSame.tsx +36 -0
  53. package/test/collaboration/addMark/atBeginningOfDocument.tsx +27 -0
  54. package/test/collaboration/addMark/atEndOfDocument.tsx +30 -0
  55. package/test/collaboration/addMark/withOtherMarks.tsx +31 -0
  56. package/test/collaboration/insertNode/atBeginningOfDocument.tsx +28 -0
  57. package/test/collaboration/insertNode/atEndOfDocument.tsx +28 -0
  58. package/test/collaboration/insertNode/inTheMiddle.tsx +30 -0
  59. package/test/collaboration/insertText/atBeginningOfBlock.tsx +26 -0
  60. package/test/collaboration/insertText/atBeginningOfDocument.tsx +26 -0
  61. package/test/collaboration/insertText/atEndOfBlock.tsx +26 -0
  62. package/test/collaboration/insertText/atEndOfDocument.tsx +26 -0
  63. package/test/collaboration/insertText/inTheMiddle.tsx +32 -0
  64. package/test/collaboration/insertText/inTheMiddleOfNestedBlock.tsx +30 -0
  65. package/test/collaboration/insertText/insideMarks.tsx +28 -0
  66. package/test/collaboration/insertText/withEmptyString.tsx +25 -0
  67. package/test/collaboration/insertText/withEntities.tsx +28 -0
  68. package/test/collaboration/insertText/withMarks.tsx +25 -0
  69. package/test/collaboration/insertText/withUnicode.tsx +28 -0
  70. package/test/collaboration/mergeNode/afterADeleteBackward.tsx +27 -0
  71. package/test/collaboration/mergeNode/onMixedNestedNodes.tsx +29 -0
  72. package/test/collaboration/mergeNode/onMixedTypeNodes.tsx +27 -0
  73. package/test/collaboration/mergeNode/withUnicode.tsx +25 -0
  74. package/test/collaboration/moveNode/downward/whenBlockBecomesNested.tsx +43 -0
  75. package/test/collaboration/moveNode/downward/whenBlockBecomesNonNested.tsx +41 -0
  76. package/test/collaboration/moveNode/downward/whenBlockStaysNested.tsx +38 -0
  77. package/test/collaboration/moveNode/downward/whenBlockStaysNonNested.tsx +30 -0
  78. package/test/collaboration/moveNode/upward/whenBlockBecomesNested.tsx +43 -0
  79. package/test/collaboration/moveNode/upward/whenBlockBecomesNonNested.tsx +41 -0
  80. package/test/collaboration/moveNode/upward/whenBlockStaysNested.tsx +38 -0
  81. package/test/collaboration/moveNode/upward/whenBlockStaysNonNested.tsx +30 -0
  82. package/test/collaboration/removeMark/inTheMiddleOfText.tsx +43 -0
  83. package/test/collaboration/removeMark/withAddMark.tsx +31 -0
  84. package/test/collaboration/removeMark/withOtherMarks.tsx +47 -0
  85. package/test/collaboration/removeNode/atBeginningOfDocument.tsx +26 -0
  86. package/test/collaboration/removeNode/atEndOfDocument.tsx +26 -0
  87. package/test/collaboration/removeNode/nestedBlock.tsx +28 -0
  88. package/test/collaboration/removeNode/wrapperBlock.tsx +28 -0
  89. package/test/collaboration/removeText/atBeginningOfDocument.tsx +34 -0
  90. package/test/collaboration/removeText/atEndOfDocument.tsx +37 -0
  91. package/test/collaboration/removeText/withUnicode.tsx +29 -0
  92. package/test/collaboration/setNode/atBeginningOfDocument.tsx +27 -0
  93. package/test/collaboration/setNode/atEndOfDocument.tsx +27 -0
  94. package/test/collaboration/setNode/onDataChange.tsx +35 -0
  95. package/test/collaboration/setNode/onDataChangeOnInline.tsx +35 -0
  96. package/test/collaboration/setNode/onResetBlock.tsx +27 -0
  97. package/test/collaboration/setNode/withAChangeOfType.tsx +26 -0
  98. package/test/collaboration/splitNode/atBeginningOfDocument.tsx +28 -0
  99. package/test/collaboration/splitNode/atEndOfBlock.tsx +27 -0
  100. package/test/collaboration/splitNode/atEndOfDocument.tsx +27 -0
  101. package/test/collaboration/splitNode/onNonDefaultBlock.tsx +27 -0
  102. package/test/collaboration/splitNode/withMultipleSubNodes.tsx +32 -0
  103. package/test/collaboration/splitNode/withUnicode.tsx +29 -0
  104. package/test/index.test.ts +66 -0
  105. package/test/{types.d.ts → slate.d.ts} +0 -0
  106. package/test/withTestingElements.ts +46 -0
  107. package/tsconfig.tsbuildinfo +1 -1
  108. package/test/collaboration.test.ts +0 -395
  109. package/test/concurrent.test.ts +0 -233
  110. package/test/testEditor.ts +0 -134
  111. package/test/utils.ts +0 -64
@@ -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,
@@ -8,10 +8,15 @@ import {
8
8
  import { YjsEditor } from './withYjs';
9
9
 
10
10
  const LAST_SELECTION: WeakMap<Editor, RelativeRange | null> = new WeakMap();
11
+ const DEFAULT_WITHOUT_SAVING_ORIGIN = Symbol(
12
+ 'slate-yjs-history-without-saving'
13
+ );
11
14
 
12
15
  export type YHistoryEditor = YjsEditor & {
13
16
  undoManager: Y.UndoManager;
14
17
 
18
+ withoutSavingOrigin: unknown;
19
+
15
20
  undo: () => void;
16
21
  redo: () => void;
17
22
  };
@@ -22,7 +27,8 @@ export const YHistoryEditor = {
22
27
  YjsEditor.isYjsEditor(value) &&
23
28
  (value as YHistoryEditor).undoManager instanceof Y.UndoManager &&
24
29
  typeof (value as YHistoryEditor).undo === 'function' &&
25
- typeof (value as YHistoryEditor).redo === 'function'
30
+ typeof (value as YHistoryEditor).redo === 'function' &&
31
+ 'withoutSavingOrigin' in value
26
32
  );
27
33
  },
28
34
 
@@ -33,15 +39,26 @@ export const YHistoryEditor = {
33
39
  canRedo(editor: YHistoryEditor) {
34
40
  return editor.undoManager.redoStack.length > 0;
35
41
  },
42
+
43
+ isSaving(editor: YHistoryEditor): boolean {
44
+ return editor.undoManager.trackedOrigins.has(YjsEditor.origin(editor));
45
+ },
46
+
47
+ withoutSaving(editor: YHistoryEditor, fn: () => void) {
48
+ YjsEditor.withOrigin(editor, editor.withoutSavingOrigin, fn);
49
+ },
36
50
  };
37
51
 
38
52
  export type WithYHistoryOptions = NonNullable<
39
53
  ConstructorParameters<typeof Y.UndoManager>[1]
40
- >;
54
+ > & {
55
+ withoutSavingOrigin?: unknown;
56
+ };
41
57
 
42
58
  export function withYHistory<T extends YjsEditor>(
43
59
  editor: T,
44
60
  {
61
+ withoutSavingOrigin = DEFAULT_WITHOUT_SAVING_ORIGIN,
45
62
  trackedOrigins = new Set([editor.localOrigin]),
46
63
  ...options
47
64
  }: WithYHistoryOptions = {}
@@ -53,7 +70,10 @@ export function withYHistory<T extends YjsEditor>(
53
70
  ...options,
54
71
  });
55
72
 
56
- const { onChange } = e;
73
+ e.undoManager = undoManager;
74
+ e.withoutSavingOrigin = withoutSavingOrigin;
75
+
76
+ const { onChange, isLocalOrigin } = e;
57
77
  e.onChange = () => {
58
78
  onChange();
59
79
 
@@ -63,6 +83,9 @@ export function withYHistory<T extends YjsEditor>(
63
83
  );
64
84
  };
65
85
 
86
+ e.isLocalOrigin = (origin) =>
87
+ origin === e.withoutSavingOrigin || isLocalOrigin(origin);
88
+
66
89
  undoManager.on(
67
90
  'stack-item-added',
68
91
  ({ stackItem }: { stackItem: HistoryStackItem; type: 'redo' | 'undo' }) => {
@@ -123,8 +146,6 @@ export function withYHistory<T extends YjsEditor>(
123
146
  }
124
147
  );
125
148
 
126
- e.undoManager = undoManager;
127
-
128
149
  e.undo = () => {
129
150
  YjsEditor.flushLocalChanges(e);
130
151
  e.undoManager.undo();
@@ -13,7 +13,11 @@ import {
13
13
  } from '../utils/position';
14
14
  import { assertDocumentAttachment } from '../utils/yjs';
15
15
 
16
- type LocalChange = { op: Operation; doc: Descendant[] };
16
+ type LocalChange = {
17
+ op: Operation;
18
+ doc: Descendant[];
19
+ origin: unknown;
20
+ };
17
21
 
18
22
  const DEFAULT_LOCAL_ORIGIN = Symbol('slate-yjs-operation');
19
23
  const DEFAULT_POSITION_STORAGE_ORIGIN = Symbol('slate-yjs-position-storage');
@@ -28,10 +32,13 @@ export type YjsEditor = BaseEditor & {
28
32
  localOrigin: unknown;
29
33
  positionStorageOrigin: unknown;
30
34
 
31
- applyRemoteEvents: (events: Y.YEvent[], origin: unknown) => void;
32
- storeLocalOperation: (op: Operation) => void;
35
+ applyRemoteEvents: (events: Y.YEvent<Y.XmlText>[], origin: unknown) => void;
36
+
37
+ storeLocalChange: (op: Operation) => void;
33
38
  flushLocalChanges: () => void;
34
39
 
40
+ isLocalOrigin: (origin: unknown) => boolean;
41
+
35
42
  connect: () => void;
36
43
  disconnect: () => void;
37
44
  };
@@ -41,11 +48,12 @@ export const YjsEditor = {
41
48
  return (
42
49
  Editor.isEditor(value) &&
43
50
  (value as YjsEditor).sharedRoot instanceof Y.XmlText &&
44
- (value as YjsEditor).localOrigin !== undefined &&
45
- (value as YjsEditor).positionStorageOrigin !== undefined &&
51
+ 'localOrigin' in value &&
52
+ 'positionStorageOrigin' in value &&
46
53
  typeof (value as YjsEditor).applyRemoteEvents === 'function' &&
47
- typeof (value as YjsEditor).storeLocalOperation === 'function' &&
54
+ typeof (value as YjsEditor).storeLocalChange === 'function' &&
48
55
  typeof (value as YjsEditor).flushLocalChanges === 'function' &&
56
+ typeof (value as YjsEditor).isLocalOrigin === 'function' &&
49
57
  typeof (value as YjsEditor).connect === 'function' &&
50
58
  typeof (value as YjsEditor).disconnect === 'function'
51
59
  );
@@ -57,14 +65,14 @@ export const YjsEditor = {
57
65
 
58
66
  applyRemoteEvents(
59
67
  editor: YjsEditor,
60
- events: Y.YEvent[],
68
+ events: Y.YEvent<Y.XmlText>[],
61
69
  origin: unknown
62
70
  ): void {
63
71
  editor.applyRemoteEvents(events, origin);
64
72
  },
65
73
 
66
- storeLocalOperation(editor: YjsEditor, op: Operation): void {
67
- editor.storeLocalOperation(op);
74
+ storeLocalChange(editor: YjsEditor, op: Operation): void {
75
+ editor.storeLocalChange(op);
68
76
  },
69
77
 
70
78
  flushLocalChanges(editor: YjsEditor): void {
@@ -76,6 +84,10 @@ export const YjsEditor = {
76
84
  },
77
85
 
78
86
  connect(editor: YjsEditor): void {
87
+ if (YjsEditor.connected(editor)) {
88
+ throw new Error('already connected');
89
+ }
90
+
79
91
  editor.connect();
80
92
  },
81
93
 
@@ -83,21 +95,20 @@ export const YjsEditor = {
83
95
  editor.disconnect();
84
96
  },
85
97
 
86
- remoteOrigin(editor: YjsEditor): unknown | undefined {
87
- return ORIGIN.get(editor);
98
+ isLocal(editor: YjsEditor): boolean {
99
+ return editor.isLocalOrigin(YjsEditor.origin(editor));
88
100
  },
89
101
 
90
- asRemote(editor: YjsEditor, origin: unknown, fn: () => void): void {
91
- const prev = YjsEditor.remoteOrigin(editor);
92
- ORIGIN.set(editor, origin);
102
+ origin(editor: YjsEditor): unknown {
103
+ const origin = ORIGIN.get(editor);
104
+ return origin !== undefined ? origin : editor.localOrigin;
105
+ },
93
106
 
107
+ withOrigin(editor: YjsEditor, origin: unknown, fn: () => void): void {
108
+ const prev = YjsEditor.origin(editor);
109
+ ORIGIN.set(editor, origin);
94
110
  fn();
95
-
96
- if (prev === undefined) {
97
- ORIGIN.delete(editor);
98
- } else {
99
- ORIGIN.set(editor, prev);
100
- }
111
+ ORIGIN.set(editor, prev);
101
112
  },
102
113
 
103
114
  storePosition(editor: YjsEditor, key: string, point: Point): void {
@@ -163,16 +174,23 @@ export function withYjs<T extends Editor>(
163
174
  e.positionStorageOrigin =
164
175
  positionStorageOrigin ?? DEFAULT_POSITION_STORAGE_ORIGIN;
165
176
 
166
- e.applyRemoteEvents = (events, eventOrigin) => {
177
+ e.applyRemoteEvents = (events, origin) => {
178
+ YjsEditor.flushLocalChanges(e);
179
+
167
180
  Editor.withoutNormalizing(e, () => {
168
- YjsEditor.asRemote(e, eventOrigin, () => {
181
+ YjsEditor.withOrigin(e, origin, () => {
169
182
  applyYjsEvents(e.sharedRoot, e, events);
170
183
  });
171
184
  });
172
185
  };
173
186
 
174
- const handleYEvents = (events: Y.YEvent[], transaction: Y.Transaction) => {
175
- if (transaction.origin === e.localOrigin) {
187
+ e.isLocalOrigin = (origin) => origin === e.localOrigin;
188
+
189
+ const handleYEvents = (
190
+ events: Y.YEvent<Y.XmlText>[],
191
+ transaction: Y.Transaction
192
+ ) => {
193
+ if (e.isLocalOrigin(transaction.origin)) {
176
194
  return;
177
195
  }
178
196
 
@@ -205,10 +223,10 @@ export function withYjs<T extends Editor>(
205
223
  CONNECTED.delete(e);
206
224
  };
207
225
 
208
- e.storeLocalOperation = (op) => {
226
+ e.storeLocalChange = (op) => {
209
227
  LOCAL_CHANGES.set(e, [
210
228
  ...YjsEditor.localChanges(e),
211
- { op, doc: editor.children },
229
+ { op, doc: editor.children, origin: YjsEditor.origin(e) },
212
230
  ]);
213
231
  };
214
232
 
@@ -217,17 +235,34 @@ export function withYjs<T extends Editor>(
217
235
  const localChanges = YjsEditor.localChanges(e);
218
236
  LOCAL_CHANGES.delete(e);
219
237
 
220
- e.sharedRoot.doc.transact(() => {
221
- localChanges.forEach((change) => {
222
- applySlateOp(e.sharedRoot, { children: change.doc }, change.op);
223
- });
224
- }, e.localOrigin);
238
+ // Group local changes by origin so we can apply them in the correct order
239
+ // with the correct origin with a minimal amount of transactions.
240
+ const txGroups: LocalChange[][] = [];
241
+ localChanges.forEach((change) => {
242
+ const currentGroup = txGroups[txGroups.length - 1];
243
+ if (currentGroup && currentGroup[0].origin === change.origin) {
244
+ return currentGroup.push(change);
245
+ }
246
+
247
+ txGroups.push([change]);
248
+ });
249
+
250
+ txGroups.forEach((txGroup) => {
251
+ assertDocumentAttachment(e.sharedRoot);
252
+
253
+ e.sharedRoot.doc.transact(() => {
254
+ txGroup.forEach((change) => {
255
+ assertDocumentAttachment(e.sharedRoot);
256
+ applySlateOp(e.sharedRoot, { children: change.doc }, change.op);
257
+ });
258
+ }, txGroup[0].origin);
259
+ });
225
260
  };
226
261
 
227
262
  const { apply, onChange } = e;
228
263
  e.apply = (op) => {
229
- if (YjsEditor.connected(e) && YjsEditor.remoteOrigin(e) === undefined) {
230
- YjsEditor.storeLocalOperation(e, op);
264
+ if (YjsEditor.connected(e) && YjsEditor.isLocal(e)) {
265
+ YjsEditor.storeLocalChange(e, op);
231
266
  }
232
267
 
233
268
  apply(op);
@@ -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
+ }
@@ -1,4 +1,4 @@
1
- import { BasePoint, BaseRange, Node, Point, Text } from 'slate';
1
+ import { BasePoint, BaseRange, Node, Text } from 'slate';
2
2
  import * as Y from 'yjs';
3
3
  import { InsertDelta, RelativeRange, TextRange } from '../model/types';
4
4
  import { getInsertDeltaLength, yTextToInsertDelta } from './delta';
@@ -1,11 +1,11 @@
1
- import { Descendant, Text } from 'slate';
1
+ import { BaseText, Descendant, Text } from 'slate';
2
2
  import { omit } from './object';
3
3
 
4
4
  export function getProperties<TNode extends Descendant>(
5
5
  node: TNode
6
- ): Omit<TNode, TNode extends Text ? 'text' : 'children'> {
6
+ ): Omit<TNode, TNode extends BaseText ? 'text' : 'children'> {
7
7
  return omit(
8
8
  node,
9
9
  (Text.isText(node) ? 'text' : 'children') as keyof TNode
10
- ) as unknown as Omit<TNode, TNode extends Text ? 'text' : 'children'>;
10
+ ) as Omit<TNode, TNode extends BaseText ? 'text' : 'children'>;
11
11
  }
@@ -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
+ }
@@ -0,0 +1,36 @@
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 bold>lo w</text>
28
+ <focus />
29
+ <text>orld!</text>
30
+ </unstyled>
31
+ </editor>
32
+ );
33
+
34
+ export function run(editor: Editor) {
35
+ editor.addMark('bold', true);
36
+ }
@@ -0,0 +1,27 @@
1
+ /** @jsx jsx */
2
+ import { Editor } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled>
8
+ <anchor />
9
+ Hello
10
+ <focus /> world!
11
+ </unstyled>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <unstyled>
18
+ <anchor />
19
+ <text bold>Hello</text>
20
+ <focus /> world!
21
+ </unstyled>
22
+ </editor>
23
+ );
24
+
25
+ export function run(editor: Editor) {
26
+ editor.addMark('bold', true);
27
+ }
@@ -0,0 +1,30 @@
1
+ /** @jsx jsx */
2
+ import { Editor } from 'slate';
3
+ import { jsx } from '../../../../../support/jsx';
4
+
5
+ export const input = (
6
+ <editor>
7
+ <unstyled>
8
+ Hello <anchor />
9
+ world!
10
+ <focus />
11
+ </unstyled>
12
+ </editor>
13
+ );
14
+
15
+ export const expected = (
16
+ <editor>
17
+ <unstyled>
18
+ Hello{' '}
19
+ <text bold>
20
+ <anchor />
21
+ world!
22
+ </text>
23
+ <focus />
24
+ </unstyled>
25
+ </editor>
26
+ );
27
+
28
+ export function run(editor: Editor) {
29
+ editor.addMark('bold', true);
30
+ }