Hello hello,
Firstly thanks for this wonderful library.
I am trying to use prosemirror in this manner:
- I have a UI where a user selects some items, let’s assume they’re images.
- I have a prosemirror (actually blocknote which is based on tiptap which is based on
pm
) doc open with collaboration on. - I have custom blocks set up for displaying the above charts. Charts created from within the prosemirror docs work and display fine with no problems. Collaboration works too.
I am trying to do this:
- When the user selects a chart on the UI, I connect to the yjs provider for the doc.
- I get the current state of the doc after yjs’s sync event is called. I get the latest state which appears to be correct upon checking.
- I add an xmlelement to the yjs doc containing the necessary info to render the custom chart: Basically an image path as a prop which I can later fetch from a db.
My expectation is that this change should show up immediately on the prosemirror doc which is listening(?) to changes on the yjs doc.
But something weird seems to happen. On the page where the prosemirror doc is displayed, I tried logging from inside the the yjs doc’s update event and got this:
- Initially I get the correct updated state of the doc with the new image at the end.
- But then, a few logs later and before
pm
can even flush that new state to the actual doc, I get more logs showing the state going back to the initial state of the doc without that image. - I am just using the
doc.toJSON
method for checking yjs state.
I’m wondering what I’m doing wrong? Is it even possible to update the prosemirror doc in this manner? Or if there’s something in the transaction I can add which makes it not revert?
Here’s some snippets:
- This is the function I’m using to add an element to the doc:
export function appendImageToYjsDoc(yjsDoc, imagePath) {
const newBlock = createNewYjsXmlElement("blockcontainer", {
id: v4(),
backgroundColor: "default",
textColor: "default",
});
const newImage = createNewYjsXmlElement("chart-image", {
imagePath: imagePath,
});
newBlock.insert(0, [newImage]);
yjsDoc.transact((tr) => {
const blockGroup = yjsDoc.getXmlFragment("document-store").firstChild;
blockGroup.insert(blockGroup.length, [newBlock]);
});
}
- Here’s my YPartykitProvider setup on the blocknote end:
const yjsDoc = new Y.Doc();
const yjsProvider = new YPartyKitProvider(partyEndpoint, docId, yjsDoc, {
params: {
doc_id: docId,
},
protocol: "ws",
});
- Here’s my click handler for that button:
const newDoc = new Doc();
const yjsProvider = new YPartyKitProvider(
partyEndpoint,
docId,
newDoc,
{
params: {
doc_id: docId,
},
protocol: "ws",
}
);
yjsProvider.on("sync", () => {
appendimageToYjsDoc(yjsProvider.doc, imagePath);
});