Skip to content

Commit de1f82a

Browse files
committed
fix(unique-id): do not attempt to append to y-sync plugin transactions
This is because of a bug that we've had for a while, but I was never able to trace down. - We set an initial block id for the initial content (always "initialBlockId" when collaboration is detected) - `y-sync` replaces the document with what it "sees" in the `Y.XmlFragment` we gave it - This causes a transaction to replace the initial content (replacing it with an empty document with a `blockContainer` containing an id of `null`) - The unique id plugin sees this & attempts to correct the missing id, issuing a new transaction - This means there is now a write to the `Y.XmlFragment`\ This write can happen independently to the provider actually synchronizing the content. Meaning, that the `Y.XmlFragment` and `tr.doc` are out of sync when `y-prosemirror` attempts to [restore the selection](https://github.com/yjs/y-prosemirror/blob/ef35266d660c3cd76a491fde243b0c6bee25d585/src/plugins/sync-plugin.js#L634). This is ultimately because `y-prosemirror` is listening to the Y.Doc _after_ it already is accepting the change. Which is totally valid, but ProseMirror doesn't offer a great way to keep these values in-sync. The fix here is to just stop the unique-id extension from attempting to amend any `y-prosemirror` transactions. Ultimately, the remote editor should have already written the correct ID (since to the remote editor, it was the local editor - therefore, the unique-id extension should have run).
1 parent eecbca5 commit de1f82a

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

packages/core/src/extensions/UniqueID/UniqueID.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,18 @@ const UniqueID = Extension.create({
130130
addProseMirrorPlugins() {
131131
let dragSourceElement: any = null;
132132
let transformPasted = false;
133+
let isFirstCollaborationTransaction = true;
133134
return [
134135
new Plugin({
135136
key: new PluginKey("uniqueID"),
136137
appendTransaction: (transactions, oldState, newState) => {
138+
if (
139+
isFirstCollaborationTransaction &&
140+
transactions.some((tr) => tr.getMeta("y-sync$"))
141+
) {
142+
isFirstCollaborationTransaction = false;
143+
return;
144+
}
137145
const docChanges =
138146
transactions.some((transaction) => transaction.docChanged) &&
139147
!oldState.doc.eq(newState.doc);

0 commit comments

Comments
 (0)