@liveblocks/node-lexical
provides a Node.js package to export and modifyLexical documents on the server.
withLexicalDocument
is the main entry point to modifying a document on theserver. It takes a room ID and aLiveblocks Node client,and returns a callback used to work with Lexical documents stored in Liveblocks.
import{ Liveblocks}from"@liveblocks/node";import{ withLexicalDocument}from"@liveblocks/node-lexical";
const liveblocks=newLiveblocks({ secret:"",});
awaitwithLexicalDocument({ roomId:"your-room-id", client: liveblocks},async(doc)=>{// Modify your Lexical `doc`// ...});
Returns the value you return from thedoc
callback.
The ID of the room to use.
TheLiveblocksclient to use.
Optional. The Lexical nodes used in the document. Will extend the defaultschema which uses Liveblocks mentions and Liveblocks comments.
Get your editor’s text content by returningdoc.getTextContent
inside thecallback.
const textContent=awaitwithLexicalDocument({ roomId:"my-room-id", client: liveblocks},async(doc)=>{return doc.getTextContent();});
// "My content"console.log(TextContent);
If your Lexical document has custom nodes, they must be passed into thewithLexicalDocument
, similarly to with a front end Lexical client.
import{ CodeNode}from"@lexical/code";
awaitwithLexicalDocument({ roomId:"my-room-id", client: liveblocks, nodes:[CodeNode]},async(doc)=>{// Modify your Lexical `doc`// ...});
You can easily modify your document with the Lexical document API.
Liveblocks providesdoc.update
which is a callback function similar toLexical’seditor.update
. This makes it easy to use Lexical’s editor functions.Any edits will be persisted and appear in realtime to connected users as soon astheupdate
promise resolves. Unlike Lexical’seditor.update
, this change isalways discrete. The callback can also be anasync
function.
awaitwithLexicalDocument({ roomId:"my-room-id", client: liveblocks},async(doc)=>{await doc.update(()=>{// Make your modifications// ...});});
Callback function where you should handle your modifications.
Here’s an example of some modifications to a Lexical document.
import{ $getRoot}from"lexical";import{ $createParagraphNode, $createTextNode}from"lexical/nodes";
awaitwithLexicalDocument({ roomId:"my-room-id", client: liveblocks},async(doc)=>{await doc.update(()=>{// Adding a paragraph node with contained text nodeconst root=$getRoot();const paragraphNode=$createParagraphNode();const textNode=$createTextNode("Hello world"); paragraphNode.append(textNode); root.append(paragraphNode);});});
Returns the text content from the root node as astring
.
const textContent=awaitwithLexicalDocument({ roomId:"my-room-id", client: liveblocks},async(doc)=>{return doc.getTextContent();});
Returns the text retrieved from the document.
Returns Lexical’seditorState.
const editorState=awaitwithLexicalDocument({ roomId:"my-room-id", client: liveblocks},async(doc)=>{return doc.getEditorState();});
Your editor’s Lexical state.
Returns a headless Lexical editor.@lexical/headless.
const headlessEditor=awaitwithLexicalDocument({ roomId:"my-room-id", client: liveblocks},async(doc)=>{return doc.getLexicalEditor();});
Your headless Lexical editor.
Returns a serialized JSON object representation of your document. See Lexical’sSerialization & Deserializationpage for more information.
const docAsJSON=awaitwithLexicalDocument({ roomId:"my-room-id", client: liveblocks},async(doc)=>{return doc.toJson();});
A serialized JSON object representation of your document.
Returns a markdownstring
of your document. See Lexical’s@lexical/markdown page formore information.
const markdown=awaitwithLexicalDocument({ roomId:"my-room-id", client: liveblocks},async(doc)=>{return doc.toMarkdown();});
Returns the markdown string.
We use cookies to collect data to improve your experience on our site. Read ourPrivacy Policy to learn more.