Skip to content

Commit

Permalink
fix: avoid throwing an exception if the current selection no longer m…
Browse files Browse the repository at this point in the history
…akes sense when initializing ySyncPlugin
  • Loading branch information
gvergnaud committed Nov 21, 2024
1 parent cf57a76 commit a501e0b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/plugins/sync-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,15 @@ export class ProsemirrorBinding {
new PModel.Slice(PModel.Fragment.from(fragmentContent), 0, 0)
)
if (sel) {
tr.setSelection(TextSelection.create(tr.doc, sel.anchor, sel.head))
/**
* If the Prosemirror document we just created from this.type is
* smaller than the previous document, the selection might be
* out of bound, which would make Prosemirror throw an error.
*/
const clampedAnchor = math.min(math.max(sel.anchor, 0), tr.doc.content.size)
const clampedHead = math.min(math.max(sel.head, 0), tr.doc.content.size)

tr.setSelection(TextSelection.create(tr.doc, clampedAnchor, clampedHead))
}
this.prosemirrorView.dispatch(
tr.setMeta(ySyncPluginKey, { isChangeOrigin: true, binding: this })
Expand Down

0 comments on commit a501e0b

Please sign in to comment.