Using@liveblocks/node-prosemirror
,it’s possible to retrieve the state of your ProseMirror document on the server.
To get your document state, you can usewithProsemirrorDocument
andapi.getText
.
import{ Liveblocks}from"@liveblocks/node";import{ withProsemirrorDocument}from"@liveblocks/node-prosemirror";
const liveblocks=newLiveblocks({ secret:"",});
const textContent=awaitwithProsemirrorDocument({ roomId:"your-room-id", client: liveblocks, field:"prosemirror"},async(api)=>{return api.getText();});
// "My content"console.log(textContent);
To modify document state with transactions, useapi.update
. Onthe ProseMirror website you can find a full list oftransformsandtransactions functions.
import{ Liveblocks}from"@liveblocks/node";import{ withProsemirrorDocument}from"@liveblocks/node-prosemirror";
const liveblocks=newLiveblocks({ secret:"",});
awaitwithProsemirrorDocument({ roomId:"your-room-id", client: liveblocks, field:"prosemirror"},async(api)=>{await api.update((_, tr)=>{// Transaction examplereturn tr.insertText("Hello world");});});
We don’t generally recommend it, but it’s also possible to use@liveblocks/node
to retrieve the stateof your ProseMirror document, and itsY.Doc
,on the server. This may give you more control in some cases.
UsingLiveblocks.getYjsDocumentAsBinaryUpdate
you can fetch your Yjs data, and place it inside aY.Doc
. We can then callyDocToProseMirror
fromy-prosemirror
to retrieve the ProseMirror editor’s state.
import*asYfrom"yjs";import{ Liveblocks}from"@liveblocks/node";import{ yDocToProsemirrorJSON}from"y-prosemirror";
const liveblocks=newLiveblocks({ secret:"",});
exportasyncfunctionPOST(){// Get your Yjs data as a binary updateconst update=await liveblocks.getYjsDocumentAsBinaryUpdate("my-room-name");
// Create a Yjs documentconst yDoc=newY.Doc();
// Apply the binary update to `yDoc`Y.applyUpdate(yDoc,newUint8Array(update));
// Get ProseMirror state from the default Yjs property it uses, "prosemirror"const prosemirrorState=yDocToProsemirrorJSON(yDoc,"prosemirror");
// { type: "doc", content: [{ type: "paragraph", content: [...] }] }console.log(prosemirrorState);}
If you’d like to edit yourY.Doc
, make sure to readhow to use yourY.Doc
on the server.
We use cookies to collect data to improve your experience on our site. Read ourPrivacy Policy to learn more.