UNPKG

@liveblocks/node-lexical

Version:

A server-side utility that lets you modify lexical documents hosted in Liveblocks.

52 lines (49 loc) 2.09 kB
import { Liveblocks } from '@liveblocks/node'; import { Klass, LexicalNode, LexicalNodeReplacement, EditorState, LexicalEditor, SerializedEditorState, SerializedLexicalNode } from 'lexical'; export { $createParagraphNode, $createTextNode, $getRoot } from 'lexical'; type LiveblocksLexicalOptions = { roomId: string; nodes?: ReadonlyArray<Klass<LexicalNode> | LexicalNodeReplacement>; client: Liveblocks; }; type LiveblocksDocumentApi = { refresh: () => Promise<void>; update: (modifyFn: () => void) => Promise<void>; getTextContent: () => string; getEditorState: () => EditorState; getLexicalEditor: () => LexicalEditor; toJSON: () => SerializedEditorState<SerializedLexicalNode>; toMarkdown: () => string; }; /** * * `withLexicalDocument` is the main entry point to access and modify Lexical documents on your backend. * This function internally instantiates a Lexical headless editor and allows you to modify and export its values asynchronously * with a simplified interface. * * @param options Specify the roomId, client, and nodes. * @param callback The call back function is optionally async and receives the document API as its only argument. * * @example * * import { Liveblocks } from "@liveblocks/node"; * import { withLexicalDocument } from "@liveblocks/node-lexical"; * * const client = new Liveblocks({secret: "sk_your_secret_key"}); * const text = await withLexicalDocument( * { client, roomId: "your-room" }, * async (doc) => { * await doc.update(() => { * const root = $getRoot(); * const paragraphNode = $createParagraphNode(); * const textNode = $createTextNode("Hello from node"); * paragraphNode.append(textNode); * root.append(paragraphNode); * }); * return doc.getTextContent(); * } * ); * */ declare function withLexicalDocument<T>({ roomId, nodes, client }: LiveblocksLexicalOptions, callback: (api: LiveblocksDocumentApi) => Promise<T> | T): Promise<T>; export { type LiveblocksDocumentApi, type LiveblocksLexicalOptions, withLexicalDocument };