@tiptap/y-tiptap 3.0.0 → 3.0.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.
package/dist/y-tiptap.js CHANGED
@@ -296,7 +296,7 @@ const restoreRelativeSelection = (tr, relSel, binding) => {
296
296
  relSel.anchor,
297
297
  binding.mapping
298
298
  );
299
- tr.setSelection(NodeSelection.create(tr.doc, anchor));
299
+ tr.setSelection(createSafeNodeSelection(tr, anchor));
300
300
  } else {
301
301
  const anchor = relativePositionToAbsolutePosition(
302
302
  binding.doc,
@@ -317,6 +317,23 @@ const restoreRelativeSelection = (tr, relSel, binding) => {
317
317
  }
318
318
  };
319
319
 
320
+ /**
321
+ * @param {import('prosemirror-state').Transaction} tr
322
+ * @param {number} pos
323
+ * @returns {import('prosemirror-state').Selection}
324
+ *
325
+ * Creates a NodeSelection if the position points to a valid node, otherwise
326
+ * creates a TextSelection near the position.
327
+ */
328
+ const createSafeNodeSelection = (tr, pos) => {
329
+ const $pos = tr.doc.resolve(pos);
330
+ if ($pos.nodeAfter) {
331
+ return NodeSelection.create(tr.doc, pos)
332
+ } else {
333
+ return TextSelection.near($pos)
334
+ }
335
+ };
336
+
320
337
  /**
321
338
  * @param {ProsemirrorBinding} pmbinding
322
339
  * @param {import('prosemirror-state').EditorState} state
@@ -999,9 +1016,18 @@ const equalYTextPText = (ytext, ptexts) => {
999
1016
  delta.every(/** @type {(d:any,i:number) => boolean} */ (d, i) =>
1000
1017
  d.insert === /** @type {any} */ (ptexts[i]).text &&
1001
1018
  object.keys(d.attributes || {}).length === ptexts[i].marks.length &&
1002
- object.every(d.attributes, (attr, yattrname) => {
1019
+ object.every(d.attributes, (attr, /** @type {string} */ yattrname) => {
1003
1020
  const markname = yattr2markname(yattrname);
1004
1021
  const pmarks = ptexts[i].marks;
1022
+
1023
+ const pmark = pmarks.find(/** @param {any} mark */ mark => mark.type.name === markname);
1024
+
1025
+ // Ensure the pmark is present in the ptexts before checking equality. When replacing a mark with another mark
1026
+ // the pmark will be missing from the ptexts and the equalAttrs will throw an error.
1027
+ if (!pmark) {
1028
+ return false
1029
+ }
1030
+
1005
1031
  return equalAttrs(attr, pmarks.find(/** @param {any} mark */ mark => mark.type.name === markname)?.attrs)
1006
1032
  })
1007
1033
  )