UNPKG

@liveblocks/node-prosemirror

Version:

A server-side utility that lets you modify prosemirror and tiptap documents hosted in Liveblocks.

54 lines (51 loc) 2.3 kB
import { Liveblocks } from '@liveblocks/node'; import { TextSerializer } from '@tiptap/core'; import { Schema, Node } from '@tiptap/pm/model'; import { Transaction, EditorState } from '@tiptap/pm/state'; type LiveblocksProsemirrorOptions = { roomId: string; schema?: Schema; client: Liveblocks; field?: string; }; type LiveblocksDocumentApi = { refresh: () => Promise<void>; update: (modifyFn: (doc: Node, tr: Transaction) => Transaction) => Promise<void>; setContent: (content: null | object | string) => Promise<void>; clearContent: () => Promise<void>; getText: (options?: { blockSeparator?: string; textSerializers?: Record<string, TextSerializer>; }) => string; getEditorState: () => EditorState; toJSON: () => any; toMarkdown: () => string; }; /** * * `withProsemirrorDocument` is the main entry point to access and modify prosemirror (or TipTap) documents on your backend. * This function internally instantiates a Prosemirror state and allows you to modify and export its values asynchronously * with a simplified interface. It's important to note that the api works with Prosemirror's document state and Transactions api. * * @param options Specify the roomId, client, and optionally the prosemirror schema. If no schema is applied, a default schema of TipTap's StarterKit + Liveblocks mentions/comments is used. * @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 { withProsemirrorDocument } from "@liveblocks/node-prosemirror"; * * const client = new Liveblocks({secret: "sk_your_secret_key"}); * const text = await withProsemirrorDocument( * { client, roomId: "your-room" }, * async (docApi) => { * await docApi.update((tr) => { * return tr.insertText("Hello World, from Liveblocks!!", 0); * }); * return docApi.getTextContent(); * } * ); * */ declare function withProsemirrorDocument<T>({ roomId, schema: maybeSchema, client, field }: LiveblocksProsemirrorOptions, callback: (api: LiveblocksDocumentApi) => Promise<T> | T): Promise<T>; export { type LiveblocksDocumentApi, type LiveblocksProsemirrorOptions, withProsemirrorDocument };