Movatterモバイル変換


[0]ホーム

URL:


Getting ProseMirror state on the server

Using@liveblocks/node-prosemirror,it’s possible to retrieve the state of your ProseMirror document on the server.

Getting document state

To get your document state, you can usewithProsemirrorDocumentandapi.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);

Modifying document state

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");});});

Using Yjs APIs instead

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.getYjsDocumentAsBinaryUpdateyou can fetch your Yjs data, and place it inside aY.Doc. We can then callyDocToProseMirror fromy-prosemirrorto 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.


    [8]ページ先頭

    ©2009-2025 Movatter.jp