@kerebron/extension-yjs 0.3.0 → 0.3.2

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.
@@ -15,10 +15,6 @@ import * as map from 'lib0/map';
15
15
  import { ySyncPluginKey, yUndoPluginKey } from './keys.js';
16
16
  import * as utils from './utils.js';
17
17
  import { absolutePositionToRelativePosition, relativePositionToAbsolutePosition, } from './lib.js';
18
- export const createEmptyMeta = () => ({
19
- mapping: new Map(),
20
- isOMark: new Map(),
21
- });
22
18
  export const isVisible = (item, snapshot) => snapshot === undefined
23
19
  ? !item.deleted
24
20
  : (snapshot.sv.has(item.id.client) && /** @type {number} */
@@ -38,7 +34,7 @@ const getUserColor = (colorMapping, colors, user) => {
38
34
  }
39
35
  colorMapping.set(user, random.oneOf(colors));
40
36
  }
41
- return /** @type {ColorDef} */ (colorMapping.get(user));
37
+ return colorMapping.get(user);
42
38
  };
43
39
  /**
44
40
  * This plugin listens to changes in prosemirror view and keeps yXmlState and view in sync.
@@ -94,7 +90,7 @@ export const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping
94
90
  if (change !== undefined &&
95
91
  (change.snapshot != null || change.prevSnapshot != null)) {
96
92
  // snapshot changed, rerender next
97
- eventloop.timeout(0, () => {
93
+ setTimeout(() => {
98
94
  if (binding.prosemirrorView == null) {
99
95
  return;
100
96
  }
@@ -111,7 +107,7 @@ export const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping
111
107
  binding._prosemirrorChanged(binding.prosemirrorView.state.doc);
112
108
  });
113
109
  }
114
- });
110
+ }, 0);
115
111
  }
116
112
  }
117
113
  return pluginState;
@@ -138,14 +134,12 @@ export const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping
138
134
  if (pluginState.addToHistory === false &&
139
135
  !pluginState.isChangeOrigin) {
140
136
  const yUndoPluginState = yUndoPluginKey.getState(view.state);
141
- const um = yUndoPluginState &&
142
- yUndoPluginState.undoManager;
143
- if (um) {
144
- um.stopCapturing();
137
+ if (yUndoPluginState?.undoManager) {
138
+ yUndoPluginState.undoManager.stopCapturing();
145
139
  }
146
140
  }
147
141
  binding.mux(() => {
148
- /** @type {Y.Doc} */ (pluginState.doc).transact((tr) => {
142
+ pluginState.doc.transact((tr) => {
149
143
  tr.meta.set('addToHistory', pluginState.addToHistory);
150
144
  binding._prosemirrorChanged(view.state.doc);
151
145
  }, ySyncPluginKey);
@@ -167,12 +161,12 @@ const restoreRelativeSelection = (tr, relSel, binding) => {
167
161
  tr.setSelection(new AllSelection(tr.doc));
168
162
  }
169
163
  else if (relSel.type === 'node') {
170
- const anchor = relativePositionToAbsolutePosition(binding.doc, binding.type, relSel.anchor, binding.mapping);
164
+ const anchor = relativePositionToAbsolutePosition(binding.ydoc, binding.type, relSel.anchor, binding.mapping);
171
165
  tr.setSelection(NodeSelection.create(tr.doc, anchor));
172
166
  }
173
167
  else {
174
- const anchor = relativePositionToAbsolutePosition(binding.doc, binding.type, relSel.anchor, binding.mapping);
175
- const head = relativePositionToAbsolutePosition(binding.doc, binding.type, relSel.head, binding.mapping);
168
+ const anchor = relativePositionToAbsolutePosition(binding.ydoc, binding.type, relSel.anchor, binding.mapping);
169
+ const head = relativePositionToAbsolutePosition(binding.ydoc, binding.type, relSel.head, binding.mapping);
176
170
  if (anchor !== null && head !== null) {
177
171
  const sel = TextSelection.between(tr.doc.resolve(anchor), tr.doc.resolve(head));
178
172
  tr.setSelection(sel);
@@ -180,8 +174,20 @@ const restoreRelativeSelection = (tr, relSel, binding) => {
180
174
  }
181
175
  }
182
176
  };
177
+ function getSelectionType(selection) {
178
+ if (selection instanceof TextSelection) {
179
+ return 'text';
180
+ }
181
+ if (selection instanceof AllSelection) {
182
+ return 'all';
183
+ }
184
+ if (selection instanceof NodeSelection) {
185
+ return 'node';
186
+ }
187
+ return 'other_selection';
188
+ }
183
189
  export const getRelativeSelection = (pmbinding, state) => ({
184
- type: /** @type {any} */ (state.selection).jsonID,
190
+ type: getSelectionType(state.selection),
185
191
  anchor: absolutePositionToRelativePosition(state.selection.anchor, pmbinding.type, pmbinding.mapping),
186
192
  head: absolutePositionToRelativePosition(state.selection.head, pmbinding.type, pmbinding.mapping),
187
193
  });
@@ -198,61 +204,61 @@ export class ProsemirrorBinding {
198
204
  this._beforeTransactionSelection = value;
199
205
  }
200
206
  constructor(yXmlFragment, mapping = new Map()) {
201
- Object.defineProperty(this, "doc", {
207
+ Object.defineProperty(this, "mapping", {
202
208
  enumerable: true,
203
209
  configurable: true,
204
210
  writable: true,
205
- value: void 0
211
+ value: mapping
206
212
  });
207
- Object.defineProperty(this, "type", {
213
+ Object.defineProperty(this, "ydoc", {
208
214
  enumerable: true,
209
215
  configurable: true,
210
216
  writable: true,
211
217
  value: void 0
212
218
  });
213
- Object.defineProperty(this, "mux", {
219
+ Object.defineProperty(this, "isOMark", {
214
220
  enumerable: true,
215
221
  configurable: true,
216
222
  writable: true,
217
223
  value: void 0
218
224
  });
219
- Object.defineProperty(this, "prosemirrorView", {
225
+ Object.defineProperty(this, "type", {
220
226
  enumerable: true,
221
227
  configurable: true,
222
228
  writable: true,
223
229
  value: void 0
224
230
  });
225
- Object.defineProperty(this, "isOMark", {
231
+ Object.defineProperty(this, "mux", {
226
232
  enumerable: true,
227
233
  configurable: true,
228
234
  writable: true,
229
235
  value: void 0
230
236
  });
231
- Object.defineProperty(this, "_observeFunction", {
237
+ Object.defineProperty(this, "prosemirrorView", {
232
238
  enumerable: true,
233
239
  configurable: true,
234
240
  writable: true,
235
241
  value: void 0
236
242
  });
237
- Object.defineProperty(this, "mapping", {
243
+ Object.defineProperty(this, "_beforeTransactionSelection", {
238
244
  enumerable: true,
239
245
  configurable: true,
240
246
  writable: true,
241
247
  value: void 0
242
248
  });
243
- Object.defineProperty(this, "_beforeTransactionSelection", {
249
+ Object.defineProperty(this, "beforeAllTransactions", {
244
250
  enumerable: true,
245
251
  configurable: true,
246
252
  writable: true,
247
253
  value: void 0
248
254
  });
249
- Object.defineProperty(this, "beforeAllTransactions", {
255
+ Object.defineProperty(this, "afterAllTransactions", {
250
256
  enumerable: true,
251
257
  configurable: true,
252
258
  writable: true,
253
259
  value: void 0
254
260
  });
255
- Object.defineProperty(this, "afterAllTransactions", {
261
+ Object.defineProperty(this, "_observeFunction", {
256
262
  enumerable: true,
257
263
  configurable: true,
258
264
  writable: true,
@@ -262,18 +268,17 @@ export class ProsemirrorBinding {
262
268
  enumerable: true,
263
269
  configurable: true,
264
270
  writable: true,
265
- value: void 0
271
+ value: false
266
272
  });
267
273
  this.type = yXmlFragment;
268
274
  this.prosemirrorView = null;
269
275
  this.mux = createMutex();
270
- this.mapping = mapping;
271
276
  /**
272
277
  * Is overlapping mark - i.e. mark does not exclude itself.
273
278
  */
274
279
  this.isOMark = new Map();
275
- this._observeFunction = this._typeChanged.bind(this);
276
- this.doc = yXmlFragment.doc;
280
+ this._observeFunction = (event, transaction) => this.yXmlChanged(event, transaction);
281
+ this.ydoc = yXmlFragment.doc;
277
282
  /**
278
283
  * current selection as relative positions in the Yjs model
279
284
  */
@@ -287,31 +292,26 @@ export class ProsemirrorBinding {
287
292
  this.afterAllTransactions = () => {
288
293
  this._beforeTransactionSelection = null;
289
294
  };
290
- this._domSelectionInView = null;
295
+ this._domSelectionInView = false;
291
296
  }
292
- /**
293
- * Create a transaction for changing the prosemirror state.
294
- *
295
- * @returns
296
- */
297
- get _tr() {
298
- return this.prosemirrorView.state.tr.setMeta('addToHistory', false);
297
+ debug(msg = 'ydoc.prosemirror') {
298
+ console.log(msg, this.type.toString());
299
299
  }
300
300
  _isLocalCursorInView() {
301
- if (!this.prosemirrorView.hasFocus())
301
+ if (!this.prosemirrorView?.hasFocus())
302
302
  return false;
303
- if (environment.isBrowser && this._domSelectionInView === null) {
303
+ if (environment.isBrowser && this._domSelectionInView === false) {
304
304
  // Calculate the domSelectionInView and clear by next tick after all events are finished
305
305
  eventloop.timeout(0, () => {
306
- this._domSelectionInView = null;
306
+ this._domSelectionInView = false;
307
307
  });
308
308
  this._domSelectionInView = this._isDomSelectionInView();
309
309
  }
310
310
  return this._domSelectionInView;
311
311
  }
312
312
  _isDomSelectionInView() {
313
- const selection = this.prosemirrorView.root.getSelection(); // https://stackoverflow.com/questions/62054839/shadowroot-getselection
314
- if (selection == null || selection.anchorNode == null)
313
+ const selection = this.prosemirrorView?.root?.getSelection(); // https://stackoverflow.com/questions/62054839/shadowroot-getselection
314
+ if (!selection || selection.anchorNode == null)
315
315
  return false;
316
316
  const range = dom.doc.createRange(); // https://github.com/yjs/y-prosemirror/pull/193
317
317
  range.setStart(selection.anchorNode, selection.anchorOffset);
@@ -337,15 +337,22 @@ export class ProsemirrorBinding {
337
337
  if (!prevSnapshot) {
338
338
  prevSnapshot = Y.createSnapshot(Y.createDeleteSet(), new Map());
339
339
  }
340
- this.prosemirrorView.dispatch(this._tr.setMeta(ySyncPluginKey, { snapshot, prevSnapshot }));
340
+ if (this.prosemirrorView) {
341
+ const _tr = this.prosemirrorView.state.tr.setMeta('addToHistory', false);
342
+ this.prosemirrorView.dispatch(_tr.setMeta(ySyncPluginKey, { snapshot, prevSnapshot }));
343
+ }
341
344
  }
342
345
  unrenderSnapshot() {
343
346
  this.mapping.clear();
344
347
  this.mux(() => {
345
- const fragmentContent = this.type.toArray().map((t) => createNodeFromYElement(
346
- /** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this)).filter((n) => n !== null);
348
+ if (!this.prosemirrorView) {
349
+ return;
350
+ }
351
+ const state = this.prosemirrorView.state;
352
+ const fragmentContent = this.type.toArray().map((t) => createNodeFromYElement(t, state.schema, this)).filter((n) => n !== null);
347
353
  // @ts-ignore
348
- const tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(PModel.Fragment.from(fragmentContent), 0, 0));
354
+ const _tr = state.tr.setMeta('addToHistory', false);
355
+ const tr = _tr.replace(0, state.doc.content.size, new PModel.Slice(PModel.Fragment.from(fragmentContent), 0, 0));
349
356
  tr.setMeta(ySyncPluginKey, { snapshot: null, prevSnapshot: null });
350
357
  this.prosemirrorView.dispatch(tr);
351
358
  });
@@ -353,16 +360,21 @@ export class ProsemirrorBinding {
353
360
  _forceRerender() {
354
361
  this.mapping.clear();
355
362
  this.mux(() => {
363
+ if (!this.prosemirrorView) {
364
+ return;
365
+ }
366
+ const state = this.prosemirrorView.state;
356
367
  // If this is a forced rerender, this might neither happen as a pm change nor within a Yjs
357
368
  // transaction. Then the "before selection" doesn't exist. In this case, we need to create a
358
369
  // relative position before replacing content. Fixes #126
359
370
  const sel = this._beforeTransactionSelection !== null
360
371
  ? null
361
- : this.prosemirrorView.state.selection;
372
+ : state.selection;
362
373
  const fragmentContent = this.type.toArray().map((t) => createNodeFromYElement(
363
- /** @type {Y.XmlElement} */ (t), this.prosemirrorView.state.schema, this)).filter((n) => n !== null);
374
+ /** @type {Y.XmlElement} */ (t), state.schema, this)).filter((n) => n !== null);
364
375
  // @ts-ignore
365
- const tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(PModel.Fragment.from(fragmentContent), 0, 0));
376
+ const _tr = state.tr.setMeta('addToHistory', false);
377
+ const tr = _tr.replace(0, state.doc.content.size, new PModel.Slice(PModel.Fragment.from(fragmentContent), 0, 0));
366
378
  if (sel) {
367
379
  /**
368
380
  * If the Prosemirror document we just created from this.type is
@@ -380,10 +392,10 @@ export class ProsemirrorBinding {
380
392
  /**
381
393
  * The document that contains the full history of this document.
382
394
  */
383
- let historyDoc = this.doc;
395
+ let historyDoc = this.ydoc;
384
396
  let historyType = this.type;
385
397
  if (!snapshot) {
386
- snapshot = Y.snapshot(this.doc);
398
+ snapshot = Y.snapshot(this.ydoc);
387
399
  }
388
400
  if (snapshot instanceof Uint8Array || prevSnapshot instanceof Uint8Array) {
389
401
  if (!(snapshot instanceof Uint8Array) ||
@@ -401,7 +413,7 @@ export class ProsemirrorBinding {
401
413
  * If is a root type, we need to find the root key in the initial document
402
414
  * and use it to get the history type.
403
415
  */
404
- const rootKey = Array.from(this.doc.share.keys()).find((key) => this.doc.share.get(key) === this.type);
416
+ const rootKey = Array.from(this.ydoc.share.keys()).find((key) => this.ydoc.share.get(key) === this.type);
405
417
  historyType = historyDoc.getXmlFragment(rootKey);
406
418
  }
407
419
  else {
@@ -452,13 +464,13 @@ export class ProsemirrorBinding {
452
464
  return null;
453
465
  }
454
466
  }).filter((n) => n !== null);
455
- // @ts-ignore
456
- const tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(PModel.Fragment.from(fragmentContent), 0, 0));
467
+ const _tr = this.prosemirrorView.state.tr.setMeta('addToHistory', false);
468
+ const tr = _tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(PModel.Fragment.from(fragmentContent), 0, 0));
457
469
  this.prosemirrorView.dispatch(tr.setMeta(ySyncPluginKey, { isChangeOrigin: true }));
458
470
  }, ySyncPluginKey);
459
471
  });
460
472
  }
461
- _typeChanged(events, transaction) {
473
+ yXmlChanged(events, transaction) {
462
474
  if (this.prosemirrorView == null)
463
475
  return;
464
476
  const syncState = ySyncPluginKey.getState(this.prosemirrorView.state);
@@ -480,11 +492,20 @@ export class ProsemirrorBinding {
480
492
  });
481
493
  transaction.changed.forEach(delType);
482
494
  transaction.changedParentTypes.forEach(delType);
495
+ if (!this.prosemirrorView) {
496
+ return;
497
+ }
498
+ const state = this.prosemirrorView.state;
483
499
  const fragmentContent = this.type.toArray().map((t) => createNodeIfNotExists(
484
- /** @type {Y.XmlElement | Y.XmlHook} */ (t), this.prosemirrorView.state.schema, this)).filter((n) => n !== null);
485
- // @ts-ignore
486
- let tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new PModel.Slice(PModel.Fragment.from(fragmentContent), 0, 0));
487
- restoreRelativeSelection(tr, this._beforeTransactionSelection, this);
500
+ /** @type {Y.XmlElement | Y.XmlHook} */ (t), state.schema, this)).filter((n) => n !== null);
501
+ const _tr = state.tr.setMeta('addToHistory', false);
502
+ let tr = _tr.replace(0, state.doc.content.size, new PModel.Slice(PModel.Fragment.from(fragmentContent), 0, 0));
503
+ try {
504
+ restoreRelativeSelection(tr, this._beforeTransactionSelection, this);
505
+ }
506
+ catch (err) {
507
+ console.warn(err);
508
+ }
488
509
  tr = tr.setMeta(ySyncPluginKey, {
489
510
  isChangeOrigin: true,
490
511
  isUndoRedoOperation: transaction.origin instanceof Y.UndoManager,
@@ -496,8 +517,8 @@ export class ProsemirrorBinding {
496
517
  });
497
518
  }
498
519
  _prosemirrorChanged(doc) {
499
- this.doc.transact(() => {
500
- updateYFragment(this.doc, this.type, doc, this);
520
+ this.ydoc.transact(() => {
521
+ updateYFragment(this.ydoc, this.type, doc, this);
501
522
  this._beforeTransactionSelection = getRelativeSelection(this, this.prosemirrorView.state);
502
523
  }, ySyncPluginKey);
503
524
  }
@@ -508,8 +529,8 @@ export class ProsemirrorBinding {
508
529
  if (this.prosemirrorView != null)
509
530
  this.destroy();
510
531
  this.prosemirrorView = prosemirrorView;
511
- this.doc.on('beforeAllTransactions', this.beforeAllTransactions);
512
- this.doc.on('afterAllTransactions', this.afterAllTransactions);
532
+ this.ydoc.on('beforeAllTransactions', this.beforeAllTransactions);
533
+ this.ydoc.on('afterAllTransactions', this.afterAllTransactions);
513
534
  this.type.observeDeep(this._observeFunction);
514
535
  }
515
536
  destroy() {
@@ -517,12 +538,12 @@ export class ProsemirrorBinding {
517
538
  return;
518
539
  this.prosemirrorView = null;
519
540
  this.type.unobserveDeep(this._observeFunction);
520
- this.doc.off('beforeAllTransactions', this.beforeAllTransactions);
521
- this.doc.off('afterAllTransactions', this.afterAllTransactions);
541
+ this.ydoc.off('beforeAllTransactions', this.beforeAllTransactions);
542
+ this.ydoc.off('afterAllTransactions', this.afterAllTransactions);
522
543
  }
523
544
  }
524
545
  const createNodeIfNotExists = (el, schema, meta, snapshot, prevSnapshot, computeYChange) => {
525
- const node = /** @type {PModel.Node} */ (meta.mapping.get(el));
546
+ const node = meta.mapping.get(el);
526
547
  if (node === undefined) {
527
548
  if (el instanceof Y.XmlElement) {
528
549
  return createNodeFromYElement(el, schema, meta, snapshot, prevSnapshot, computeYChange);
@@ -757,9 +778,6 @@ const computeChildEqualityFactor = (ytype, pnode, meta) => {
757
778
  };
758
779
  const ytextTrans = (ytext) => {
759
780
  let str = '';
760
- /**
761
- * @type {Y.Item|null}
762
- */
763
781
  let n = ytext._start;
764
782
  const nAttrs = {};
765
783
  while (n !== null) {
@@ -1 +1 @@
1
- {"version":3,"file":"yUndoPlugin.d.ts","sourceRoot":"","sources":["../../../src/extension-yjs/src/yUndoPlugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACjE,OAAO,EAA2B,WAAW,EAAc,MAAM,KAAK,CAAC;AAEvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAGxD,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,WAAW,CAAC;IACzB,OAAO,EAAE,UAAU,CAAC,OAAO,oBAAoB,CAAC,GAAG,IAAI,CAAC;IACxD,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,eAAO,MAAM,IAAI,GAAI,OAAO,WAAW,KAAG,OACmB,CAAC;AAE9D,eAAO,MAAM,IAAI,GAAI,OAAO,WAAW,KAAG,OACmB,CAAC;AAE9D,eAAO,MAAM,WAAW,EAAE,OAGT,CAAC;AAElB,eAAO,MAAM,WAAW,EAAE,OAGT,CAAC;AAElB,eAAO,MAAM,qBAAqB,aAAyB,CAAC;AAE5D,eAAO,MAAM,mBAAmB,GAC9B,MAAM,OAAO,KAAK,EAAE,IAAI,EACxB,gBAAgB,GAAG,CAAC,MAAM,CAAC,KAC1B,OAM8B,CAAC;AAElC,eAAO,MAAM,WAAW,GAAI,mDAIzB;IACD,cAAc,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAC7B,cAAc,CAAC,EAAE,GAAG,EAAE,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;CAC3C,gBAoEF,CAAC"}
1
+ {"version":3,"file":"yUndoPlugin.d.ts","sourceRoot":"","sources":["../../../src/extension-yjs/src/yUndoPlugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACjE,OAAO,EAA2B,WAAW,EAAc,MAAM,KAAK,CAAC;AAEvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAGxD,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,WAAW,CAAC;IACzB,OAAO,EAAE,UAAU,CAAC,OAAO,oBAAoB,CAAC,GAAG,IAAI,CAAC;IACxD,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,eAAO,MAAM,IAAI,GAAI,OAAO,WAAW,KAAG,OACmB,CAAC;AAE9D,eAAO,MAAM,IAAI,GAAI,OAAO,WAAW,KAAG,OACmB,CAAC;AAE9D,eAAO,MAAM,WAAW,EAAE,OAGT,CAAC;AAElB,eAAO,MAAM,WAAW,EAAE,OAGT,CAAC;AAElB,eAAO,MAAM,qBAAqB,aAAyB,CAAC;AAE5D,eAAO,MAAM,mBAAmB,GAC9B,MAAM,OAAO,KAAK,EAAE,IAAI,EACxB,gBAAgB,GAAG,CAAC,MAAM,CAAC,KAC1B,OAM8B,CAAC;AAElC,eAAO,MAAM,WAAW,GAAI,mDAIzB;IACD,cAAc,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAC7B,cAAc,CAAC,EAAE,GAAG,EAAE,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;CAC3C,gBAuEF,CAAC"}
@@ -63,23 +63,26 @@ export const yUndoPlugin = ({ protectedNodes = defaultProtectedNodes, trackedOri
63
63
  },
64
64
  view: (view) => {
65
65
  const ystate = ySyncPluginKey.getState(view.state);
66
- const undoManager = yUndoPluginKey.getState(view.state).undoManager;
67
- undoManager.on('stack-item-added', ({ stackItem }) => {
68
- const binding = ystate.binding;
69
- if (binding) {
70
- stackItem.meta.set(binding, yUndoPluginKey.getState(view.state).prevSel);
71
- }
72
- });
73
- undoManager.on('stack-item-popped', ({ stackItem }) => {
74
- const binding = ystate.binding;
75
- if (binding) {
76
- binding.beforeTransactionSelection = stackItem.meta.get(binding) ||
77
- binding.beforeTransactionSelection;
78
- }
79
- });
66
+ const yUndoPlugin = yUndoPluginKey.getState(view.state);
67
+ const undoManager = yUndoPlugin?.undoManager;
68
+ if (undoManager) {
69
+ undoManager.on('stack-item-added', ({ stackItem }) => {
70
+ const binding = ystate.binding;
71
+ if (binding) {
72
+ stackItem.meta.set(binding, yUndoPlugin.prevSel);
73
+ }
74
+ });
75
+ undoManager.on('stack-item-popped', ({ stackItem }) => {
76
+ const binding = ystate.binding;
77
+ if (binding) {
78
+ binding.beforeTransactionSelection = stackItem.meta.get(binding) ||
79
+ binding.beforeTransactionSelection;
80
+ }
81
+ });
82
+ }
80
83
  return {
81
84
  destroy: () => {
82
- undoManager.destroy();
85
+ undoManager?.destroy();
83
86
  },
84
87
  };
85
88
  },
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kerebron/extension-yjs",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "license": "MIT",
5
5
  "module": "./esm/extension-yjs/src/ExtensionYjs.js",
6
6
  "exports": {
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "scripts": {},
15
15
  "dependencies": {
16
- "prosemirror-model": "1.25.2",
16
+ "prosemirror-model": "1.25.3",
17
17
  "prosemirror-state": "1.4.3",
18
18
  "prosemirror-transform": "1.10.4",
19
19
  "prosemirror-view": "1.40.0",