@slate-yjs/core 0.0.2 → 0.1.0

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.
@@ -47,10 +47,6 @@ function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
47
47
  let 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
- );
54
50
 
55
51
  if (
56
52
  Text.isText(child) &&
@@ -60,12 +56,13 @@ function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
60
56
  let end =
61
57
  pathOffset === endPathOffset ? endTextOffset : child.text.length;
62
58
 
59
+ const properties = getProperties(child);
63
60
  if (start !== 0) {
64
61
  ops.push({
65
62
  type: 'split_node',
66
63
  path: childPath,
67
64
  position: start,
68
- properties: newProperties,
65
+ properties: { ...properties, ...newProperties },
69
66
  });
70
67
 
71
68
  childPath = Path.next(childPath);
@@ -77,11 +74,15 @@ function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
77
74
  type: 'split_node',
78
75
  path: childPath,
79
76
  position: end,
80
- properties,
77
+ properties: { ...properties, ...newProperties },
81
78
  });
82
79
  }
83
80
  }
84
81
 
82
+ const properties = pick(
83
+ node,
84
+ ...(Object.keys(change.attributes) as Array<keyof Element>)
85
+ );
85
86
  ops.push({
86
87
  type: 'set_node',
87
88
  newProperties,
@@ -174,7 +175,7 @@ function applyDelta(node: Element, slatePath: Path, delta: Delta): Operation[] {
174
175
  });
175
176
  }
176
177
 
177
- if (textOffset < delta.length) {
178
+ if (textOffset < child.text.length) {
178
179
  ops.push({
179
180
  type: 'split_node',
180
181
  path: childPath,
@@ -238,7 +239,7 @@ export function translateYTextEvent(
238
239
  }
239
240
 
240
241
  if (delta.length > 0) {
241
- if (Text.isText(target)) {
242
+ if (Text.isText(targetElement)) {
242
243
  throw new Error('Cannot apply delta to slate text');
243
244
  }
244
245
 
@@ -14,6 +14,7 @@ export function splitNode(
14
14
  op: SplitNodeOperation
15
15
  ): void {
16
16
  const target = getYTarget(sharedRoot, slateRoot, op.path);
17
+
17
18
  if (!target.slateTarget) {
18
19
  throw new Error('Y target without corresponding slate node');
19
20
  }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { RelativeRange } from './model/types';
1
2
  import {
2
3
  CursorEditor,
3
4
  CursorState,
@@ -36,6 +37,7 @@ export {
36
37
  RemoteCursorChangeEventListener,
37
38
  CursorStateChangeEvent,
38
39
  // Utils
40
+ RelativeRange,
39
41
  yTextToSlateElement,
40
42
  slateNodesToInsertDelta,
41
43
  slateRangeToRelativeRange,
@@ -1,10 +1,8 @@
1
- import { BaseRange, Editor, Range } from 'slate';
1
+ import { Editor, Range } from 'slate';
2
2
  import { Awareness } from 'y-protocols/awareness';
3
3
  import * as Y from 'yjs';
4
- import {
5
- relativeRangeToSlateRange,
6
- slateRangeToRelativeRange,
7
- } from '../utils/position';
4
+ import { RelativeRange } from '../model/types';
5
+ import { slateRangeToRelativeRange } from '../utils/position';
8
6
  import { YjsEditor } from './withYjs';
9
7
 
10
8
  export type CursorStateChangeEvent = {
@@ -25,8 +23,9 @@ const CURSOR_CHANGE_EVENT_LISTENERS: WeakMap<
25
23
  export type CursorState<
26
24
  TCursorData extends Record<string, unknown> = Record<string, unknown>
27
25
  > = {
28
- selection: BaseRange | null;
29
- data: TCursorData;
26
+ relativeSelection: RelativeRange | null;
27
+ data?: TCursorData;
28
+ clientId: number;
30
29
  };
31
30
 
32
31
  export type CursorEditor<
@@ -35,22 +34,21 @@ export type CursorEditor<
35
34
  awareness: Awareness;
36
35
 
37
36
  cursorDataField: string;
38
- cursorStateField: string;
37
+ selectionStateField: string;
39
38
 
40
39
  sendCursorPosition: (range: Range | null) => void;
41
40
  sendCursorData: (data: TCursorData) => void;
42
41
  };
43
42
 
44
43
  export const CursorEditor = {
45
- isCursorEditor(v: unknown): v is CursorEditor {
46
- return true;
44
+ isCursorEditor(value: unknown): value is CursorEditor {
47
45
  return (
48
- YjsEditor.isYjsEditor(v) &&
49
- (v as CursorEditor).awareness instanceof Awareness &&
50
- typeof (v as CursorEditor).cursorDataField === 'string' &&
51
- typeof (v as CursorEditor).cursorStateField === 'string' &&
52
- typeof (v as CursorEditor).sendCursorPosition === 'function' &&
53
- typeof (v as CursorEditor).sendCursorData === 'function'
46
+ YjsEditor.isYjsEditor(value) &&
47
+ (value as CursorEditor).awareness instanceof Awareness &&
48
+ typeof (value as CursorEditor).cursorDataField === 'string' &&
49
+ typeof (value as CursorEditor).selectionStateField === 'string' &&
50
+ typeof (value as CursorEditor).sendCursorPosition === 'function' &&
51
+ typeof (value as CursorEditor).sendCursorData === 'function'
54
52
  );
55
53
  },
56
54
 
@@ -68,22 +66,22 @@ export const CursorEditor = {
68
66
  editor.sendCursorData(data);
69
67
  },
70
68
 
71
- on(
72
- editor: CursorEditor,
69
+ on<TCursorData extends Record<string, unknown>>(
70
+ editor: CursorEditor<TCursorData>,
73
71
  event: 'change',
74
- listener: RemoteCursorChangeEventListener
72
+ handler: RemoteCursorChangeEventListener
75
73
  ) {
76
74
  if (event !== 'change') {
77
75
  return;
78
76
  }
79
77
 
80
78
  const listeners = CURSOR_CHANGE_EVENT_LISTENERS.get(editor) ?? new Set();
81
- listeners.add(listener);
79
+ listeners.add(handler);
82
80
  CURSOR_CHANGE_EVENT_LISTENERS.set(editor, listeners);
83
81
  },
84
82
 
85
- off(
86
- editor: CursorEditor,
83
+ off<TCursorData extends Record<string, unknown>>(
84
+ editor: CursorEditor<TCursorData>,
87
85
  event: 'change',
88
86
  listener: RemoteCursorChangeEventListener
89
87
  ) {
@@ -97,8 +95,8 @@ export const CursorEditor = {
97
95
  }
98
96
  },
99
97
 
100
- remoteCursor<TCursorData extends Record<string, unknown>>(
101
- editor: CursorEditor,
98
+ cursorState<TCursorData extends Record<string, unknown>>(
99
+ editor: CursorEditor<TCursorData>,
102
100
  clientId: number
103
101
  ): CursorState<TCursorData> | null {
104
102
  if (
@@ -113,16 +111,15 @@ export const CursorEditor = {
113
111
  return null;
114
112
  }
115
113
 
116
- const relativeSelection = state[editor.cursorStateField];
117
- const selection = relativeSelection
118
- ? relativeRangeToSlateRange(editor.sharedRoot, editor, relativeSelection)
119
- : null;
120
-
121
- return { selection, data: state[editor.cursorDataField] };
114
+ return {
115
+ relativeSelection: state[editor.selectionStateField] ?? null,
116
+ data: state[editor.cursorDataField],
117
+ clientId,
118
+ };
122
119
  },
123
120
 
124
- remoteCursors<TCursorData extends Record<string, unknown>>(
125
- editor: CursorEditor
121
+ cursorStates<TCursorData extends Record<string, unknown>>(
122
+ editor: CursorEditor<TCursorData>
126
123
  ): Record<string, CursorState<TCursorData>> {
127
124
  if (!YjsEditor.connected(editor)) {
128
125
  return {};
@@ -131,20 +128,17 @@ export const CursorEditor = {
131
128
  return Object.fromEntries(
132
129
  Array.from(editor.awareness.getStates().entries(), ([id, state]) => {
133
130
  // Ignore own state
134
- if (id === editor.awareness.clientID) {
131
+ if (id === editor.awareness.clientID || !state) {
135
132
  return null;
136
133
  }
137
134
 
138
- const relativeSelection = state[editor.cursorStateField];
139
- const selection = relativeSelection
140
- ? relativeRangeToSlateRange(
141
- editor.sharedRoot,
142
- editor,
143
- relativeSelection
144
- )
145
- : null;
146
-
147
- return [id, { selection, data: state[editor.cursorDataField] }];
135
+ return [
136
+ id,
137
+ {
138
+ relativeSelection: state[editor.selectionStateField],
139
+ data: state[editor.cursorDataField],
140
+ },
141
+ ];
148
142
  }).filter(Array.isArray)
149
143
  );
150
144
  },
@@ -170,7 +164,7 @@ export function withCursors<
170
164
  editor: TEditor,
171
165
  awareness: Awareness,
172
166
  {
173
- cursorStateField = 'selection',
167
+ cursorStateField: selectionStateField = 'selection',
174
168
  cursorDataField = 'data',
175
169
  autoSend = true,
176
170
  data,
@@ -180,7 +174,7 @@ export function withCursors<
180
174
 
181
175
  e.awareness = awareness;
182
176
  e.cursorDataField = cursorDataField;
183
- e.cursorStateField = cursorStateField;
177
+ e.selectionStateField = selectionStateField;
184
178
 
185
179
  e.sendCursorData = (cursorData: TCursorData) => {
186
180
  e.awareness.setLocalStateField(e.cursorDataField, cursorData);
@@ -188,11 +182,11 @@ export function withCursors<
188
182
 
189
183
  e.sendCursorPosition = (range) => {
190
184
  const localState = awareness.getLocalState();
191
- const currentRange = localState?.[cursorStateField];
185
+ const currentRange = localState?.[selectionStateField];
192
186
 
193
187
  if (!range) {
194
188
  if (currentRange) {
195
- awareness.setLocalStateField(e.cursorStateField, null);
189
+ awareness.setLocalStateField(e.selectionStateField, null);
196
190
  }
197
191
 
198
192
  return;
@@ -205,7 +199,7 @@ export function withCursors<
205
199
  !Y.compareRelativePositions(anchor, currentRange) ||
206
200
  !Y.compareRelativePositions(focus, currentRange)
207
201
  ) {
208
- awareness.setLocalStateField(e.cursorStateField, { anchor, focus });
202
+ awareness.setLocalStateField(e.selectionStateField, { anchor, focus });
209
203
  }
210
204
  };
211
205
 
@@ -17,12 +17,12 @@ export type YHistoryEditor = YjsEditor & {
17
17
  };
18
18
 
19
19
  export const YHistoryEditor = {
20
- isYHistoryEditor(v: unknown): v is YHistoryEditor {
20
+ isYHistoryEditor(value: unknown): value is YHistoryEditor {
21
21
  return (
22
- YjsEditor.isYjsEditor(v) &&
23
- (v as YHistoryEditor).undoManager instanceof Y.UndoManager &&
24
- typeof (v as YHistoryEditor).undo === 'function' &&
25
- typeof (v as YHistoryEditor).redo === 'function'
22
+ YjsEditor.isYjsEditor(value) &&
23
+ (value as YHistoryEditor).undoManager instanceof Y.UndoManager &&
24
+ typeof (value as YHistoryEditor).undo === 'function' &&
25
+ typeof (value as YHistoryEditor).redo === 'function'
26
26
  );
27
27
  },
28
28
 
@@ -126,12 +126,12 @@ export function withYHistory<T extends YjsEditor>(
126
126
  e.undoManager = undoManager;
127
127
 
128
128
  e.undo = () => {
129
- YjsEditor.flushLocalOperations(e);
129
+ YjsEditor.flushLocalChanges(e);
130
130
  e.undoManager.undo();
131
131
  };
132
132
 
133
133
  e.redo = () => {
134
- YjsEditor.flushLocalOperations(e);
134
+ YjsEditor.flushLocalChanges(e);
135
135
  e.undoManager.redo();
136
136
  };
137
137
 
@@ -30,7 +30,7 @@ export type YjsEditor = BaseEditor & {
30
30
 
31
31
  applyRemoteEvents: (events: Y.YEvent[], origin: unknown) => void;
32
32
  storeLocalOperation: (op: Operation) => void;
33
- flushLocalOperations: () => void;
33
+ flushLocalChanges: () => void;
34
34
 
35
35
  connect: () => void;
36
36
  disconnect: () => void;
@@ -45,7 +45,7 @@ export const YjsEditor = {
45
45
  (value as YjsEditor).positionStorageOrigin !== undefined &&
46
46
  typeof (value as YjsEditor).applyRemoteEvents === 'function' &&
47
47
  typeof (value as YjsEditor).storeLocalOperation === 'function' &&
48
- typeof (value as YjsEditor).flushLocalOperations === 'function' &&
48
+ typeof (value as YjsEditor).flushLocalChanges === 'function' &&
49
49
  typeof (value as YjsEditor).connect === 'function' &&
50
50
  typeof (value as YjsEditor).disconnect === 'function'
51
51
  );
@@ -55,7 +55,7 @@ export const YjsEditor = {
55
55
  return LOCAL_CHANGES.get(editor) ?? [];
56
56
  },
57
57
 
58
- applyRemoveEvents(
58
+ applyRemoteEvents(
59
59
  editor: YjsEditor,
60
60
  events: Y.YEvent[],
61
61
  origin: unknown
@@ -67,8 +67,8 @@ export const YjsEditor = {
67
67
  editor.storeLocalOperation(op);
68
68
  },
69
69
 
70
- flushLocalOperations(editor: YjsEditor): void {
71
- editor.flushLocalOperations();
70
+ flushLocalChanges(editor: YjsEditor): void {
71
+ editor.flushLocalChanges();
72
72
  },
73
73
 
74
74
  connected(editor: YjsEditor): boolean {
@@ -120,16 +120,16 @@ export const YjsEditor = {
120
120
  }, locationStorageOrigin);
121
121
  },
122
122
 
123
- position(editor: YjsEditor, key: string): Point | null {
123
+ position(editor: YjsEditor, key: string): Point | null | undefined {
124
124
  const position = getStoredPosition(editor.sharedRoot, key);
125
125
  if (!position) {
126
- return null;
126
+ return undefined;
127
127
  }
128
128
 
129
129
  return relativePositionToSlatePoint(editor.sharedRoot, editor, position);
130
130
  },
131
131
 
132
- storedRelativePositions(
132
+ storedPositionsRelative(
133
133
  editor: YjsEditor
134
134
  ): Record<string, Y.RelativePosition> {
135
135
  return getStoredPositions(editor.sharedRoot);
@@ -176,7 +176,7 @@ export function withYjs<T extends Editor>(
176
176
  return;
177
177
  }
178
178
 
179
- YjsEditor.applyRemoveEvents(e, events, transaction.origin);
179
+ YjsEditor.applyRemoteEvents(e, events, transaction.origin);
180
180
  };
181
181
 
182
182
  let autoConnectTimeoutId: ReturnType<typeof setTimeout> | null = null;
@@ -200,6 +200,7 @@ export function withYjs<T extends Editor>(
200
200
  clearTimeout(autoConnectTimeoutId);
201
201
  }
202
202
 
203
+ YjsEditor.flushLocalChanges(e);
203
204
  sharedRoot.unobserveDeep(handleYEvents);
204
205
  CONNECTED.delete(e);
205
206
  };
@@ -211,21 +212,21 @@ export function withYjs<T extends Editor>(
211
212
  ]);
212
213
  };
213
214
 
214
- e.flushLocalOperations = () => {
215
- const localOperations = YjsEditor.localChanges(e);
215
+ e.flushLocalChanges = () => {
216
+ assertDocumentAttachment(e.sharedRoot);
217
+ const localChanges = YjsEditor.localChanges(e);
216
218
  LOCAL_CHANGES.delete(e);
217
219
 
218
- assertDocumentAttachment(e.sharedRoot);
219
220
  e.sharedRoot.doc.transact(() => {
220
- localOperations.forEach((op) => {
221
- applySlateOp(e.sharedRoot, { children: op.doc }, op.op);
221
+ localChanges.forEach((change) => {
222
+ applySlateOp(e.sharedRoot, { children: change.doc }, change.op);
222
223
  });
223
224
  }, e.localOrigin);
224
225
  };
225
226
 
226
227
  const { apply, onChange } = e;
227
228
  e.apply = (op) => {
228
- if (YjsEditor.remoteOrigin(e) === undefined) {
229
+ if (YjsEditor.connected(e) && YjsEditor.remoteOrigin(e) === undefined) {
229
230
  YjsEditor.storeLocalOperation(e, op);
230
231
  }
231
232
 
@@ -233,7 +234,10 @@ export function withYjs<T extends Editor>(
233
234
  };
234
235
 
235
236
  e.onChange = () => {
236
- YjsEditor.flushLocalOperations(e);
237
+ if (YjsEditor.connected(e)) {
238
+ YjsEditor.flushLocalChanges(e);
239
+ }
240
+
237
241
  onChange();
238
242
  };
239
243
 
@@ -1,4 +1,4 @@
1
- import { BasePoint, BaseRange, Node, Text } from 'slate';
1
+ import { BasePoint, BaseRange, Node, Point, 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';
@@ -11,7 +11,7 @@ export function slatePointToRelativePosition(
11
11
  sharedRoot: Y.XmlText,
12
12
  slateRoot: Node,
13
13
  point: BasePoint
14
- ) {
14
+ ): Y.RelativePosition {
15
15
  const { yTarget, yParent, textRange } = getYTarget(
16
16
  sharedRoot,
17
17
  slateRoot,
@@ -65,7 +65,7 @@ export function relativePositionToSlatePoint(
65
65
  sharedRoot: Y.XmlText,
66
66
  slateRoot: Node,
67
67
  pos: Y.RelativePosition
68
- ) {
68
+ ): BasePoint | null {
69
69
  if (!sharedRoot.doc) {
70
70
  throw new Error("sharedRoot isn't attach to a yDoc");
71
71
  }