@liveblocks/prosemirror
@liveblocks/prosemirror provides ProseMirror
plugins that sync editor documents with Liveblocks Storage and display remote
carets and selections. Text nodes are stored as
LiveText, preserving text
formatting and concurrent edits.
If you are using Tiptap, use
@liveblocks/react-tiptap with
collaborationMode: "liveblocks"
instead. It builds on this package and provides a Tiptap extension and React
components.
This package is for client-side ProseMirror editors backed by Liveblocks
Storage. For server-side editing of existing Tiptap and BlockNote documents, use
@liveblocks/node-prosemirror.
Setup
Install Liveblocks and the ProseMirror packages used by your editor:
Each Liveblocks package should use the same version.
Add the collaboration plugin to sync the document and the caret plugin to show
other users’ selections. The collaboration plugin creates its Storage document
when it first loads, so initialStorage can remain empty.
The field value identifies the editor document. Documents are stored under
root._tiptap_docs, keyed by field, so use a different value for each editor
in the same room.
Import the package stylesheet to display remote carets and selections:
createLiveblocksCollaborationPlugin
Creates a ProseMirror plugin that keeps an editor document in sync with
Liveblocks Storage. Local transactions update the Storage document, while remote
Storage changes are applied to the editor. Text leaves are represented by
LiveText.
The plugin groups local edits into the room’s undo and redo history. Connect
your editor’s undo and redo controls to room.history.undo() and
room.history.redo().
- pluginPlugin
A ProseMirror plugin to add when creating the editor state.
- roomLiveblocksProsemirrorRoomRequired
The Liveblocks room, retrieved with
useRoomorclient.enterRoom. - fieldstringRequired
The name used to store this editor under
root._tiptap_docs. Use a unique field for each editor in a room. - initialContentProseMirrorJsonNode
The initial ProseMirror JSON document. It is used only when the Storage document does not exist. If omitted, the editor’s current document is used.
- fallbackDocument() => ProseMirrorJsonNode
Returns a schema-valid document if stored content cannot be parsed or is empty.
createLiveblocksCollaborationCaretPlugin
Creates a ProseMirror plugin that broadcasts the local selection through Presence and renders other users’ carets and selection highlights.
The plugin renders elements with the .collaboration-carets__caret,
.collaboration-carets__label, and .collaboration-carets__selection class
names. The user’s color is applied with inline styles.
- pluginPlugin
A ProseMirror plugin to add when creating the editor state.
- options.roomLiveblocksProsemirrorRoomRequired
The same Liveblocks room passed to
createLiveblocksCollaborationPlugin. - options.fieldstringRequired
The same document field passed to the collaboration plugin. Cursors from other fields are ignored.
- options.userCursorUserRequired
The name and color displayed with this user’s remote caret.
- storageCollaborationCaretStorageRequired
A mutable object with a
usersarray. The plugin updates the array with the other users currently in the room.
Caret utilities
Use presencePatch when building a wrapper around the caret plugin or updating
its user data outside the plugin. It creates the Presence update expected by
other @liveblocks/prosemirror clients.
- presencePatch(presence) => JsonObject
Creates a Presence patch containing the document field, selection positions, and optional cursor user.
- getCursorUser(value: unknown) => CursorUser | undefined
Reads string
nameandcolorproperties from an unknown value.
LIVEBLOCKS_CARET_PRESENCE_KEY contains the Presence key used by the caret
plugin. In most applications, the plugin manages this Presence value directly.
Plugin state
LIVEBLOCKS_COLLABORATION_PLUGIN_KEY
The key for reading the collaboration plugin state. isReady becomes true
after Storage has loaded and the editor has received its initial document.
- isReadybooleanRequired
Whether the initial Storage document has been loaded into the editor.
LIVEBLOCKS_CARET_PLUGIN_KEY
The key for reading the collaboration caret plugin state.
- cursorsRemoteCursor[]Required
The current remote cursor positions and user data.
- decorationsDecorationSetRequired
The ProseMirror decorations rendered for remote carets and selections.
Types
ProseMirrorJsonNode
The JSON representation accepted by the collaboration and conversion APIs.
- typestringRequired
The ProseMirror node type.
- attrsJsonObject
The node’s attributes.
- contentProseMirrorJsonNode[]
The node’s children.
- textstring
The content of a text node.
- marksProseMirrorJsonMark[]
The marks applied to a text node. Marks are stored as
LiveTextattributes.
CursorUser
The user information shown with a remote caret.
- namestring
The user’s display name. Defaults to
"Anonymous"when rendered. - colorstring
A CSS color for the user’s caret, label, and selection. Defaults to
"#0f83ff".
RemoteCursor
The cursor data exposed by
LIVEBLOCKS_CARET_PLUGIN_KEY.
- anchornumberRequired
The current mapped selection anchor.
- headnumberRequired
The current mapped selection head.
- connectionIdnumberRequired
The Liveblocks connection ID for the remote user.
- rawAnchornumberRequired
The most recent anchor received through Presence.
- rawHeadnumberRequired
The most recent head received through Presence.
- userCursorUser
The remote user’s display information.
Document conversion
The collaboration plugin automatically converts between ProseMirror JSON and a Liveblocks Storage tree. Use these helpers only when you need to inspect or construct that Storage representation directly.
createLiveblocksProsemirrorNode
Converts a ProseMirror JSON node into a
LiveObject tree. Child
nodes are stored in LiveList
instances and text leaves are stored in
LiveText.
- nodeLiveblocksProsemirrorNode
The root of the converted Storage tree.
- nodeProseMirrorJsonNodeRequired
The ProseMirror JSON node to convert.
liveblocksProsemirrorNodeToJson
Converts a LiveblocksProsemirrorNode Storage tree back to one ProseMirror JSON
node.
- nodeProseMirrorJsonNode
The converted ProseMirror JSON node.
- nodeLiveblocksProsemirrorNodeRequired
The Storage node to convert.
- fallbackDocument() => ProseMirrorJsonNode
Returns a document when the converted root document is empty.
liveblocksProsemirrorNodeToJsonNodes
Converts a LiveblocksProsemirrorNode to an array of ProseMirror JSON nodes.
Formatted LiveText segments can produce multiple adjacent text nodes.
- nodesProseMirrorJsonNode[]
The converted ProseMirror JSON nodes.
- nodeLiveblocksProsemirrorNodeRequired
The Storage node to convert.
Storage node helpers
Use these helpers to read values from a LiveblocksProsemirrorNode.
- getLiveblocksNodeId(node) => string
Returns the stable ID assigned to the Storage node.
- getLiveblocksNodeContent(node) => LiveList<LiveblocksProsemirrorNode> | undefined
Returns the child-node list for a non-text node.
- getLiveblocksNodeText(node) => LiveText | undefined
Returns the
LiveTextcontent for a text node.