Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid throwing an exception if the current selection no longer makes sense when initializing ySyncPlugin #171

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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