
Setup
Step 1: Add Comment components
- Add the
Velt Comments
component to the root of your app. - This component is required to render comments in your app.
- Set the
text mode
prop tofalse
to hide the default text comment tool.
- React / Next.js
Step 2: Install the Velt Lexical extension
Step 3: Configure the Lexical editor
- Register the
CommentNode
and configure your Lexical editor.
- React / Next.js
Step 4: Add a comment button to your Lexical editor
- Add a button that appears in the context menu of your Lexical editor when the user selects text.
- This button will be used to add a comment to the selected text.
- React / Next.js
Step 5: Call addComment
to add a comment
- Call this method to add a comment to selected text in the Lexical editor. You can use this when the user clicks on the comment button in context menu or presses a keyboard shortcut.
- Params:
AddCommentRequest
editorId
: string, the id of the editor. Use this if you have multiple editors in your app.editor
: instance of the Lexical editor.context
: optional object to set the Comment Annotation’s custom metadata.
- React / Next.js
Step 6: Render Comments in Lexical editor
- Get the comment data.
- Render the comments in the Lexical editor.
- Params:
RenderCommentsRequest
editorId
: string, the id of the editor. Use this if you have multiple editors in your app.editor
: instance of the Lexical editor.commentAnnotations
: CommentAnnotation[]
- React / Next.js
Step 7: Remove comments from editor JSON
- Use this utility to export editor JSON without Velt comment nodes.
- React / Next.js
Step 8: Style the commented text
- You can style the commented text by adding a CSS class to the
velt-comment-text
element. - By using the
comment-available
attribute, you can apply styles only when the comment data has loaded.
Complete Example
APIs
addComment()
Creates a comment annotation for the currently selected text in the editor.- Signature:
addComment: async (request:
AddCommentRequest)=> Promise<void>
- Params:
AddCommentRequest
- Returns:
Promise<void>
- React / Next.js
renderComments()
Renders and highlights comment annotations in the editor.- Signature:
renderComments: (request:
RenderCommentsRequest) => void
- Params:
RenderCommentsRequest
- Returns:
void
- React / Next.js
CommentNode
A custom LexicalElementNode
that wraps text content with comment annotation metadata, including Velt annotation IDs.
-
Extends:
ElementNode
- Usage: Register the node with your Lexical editor so comment elements render correctly.
- React / Next.js
exportJSONWithoutComments()
Exports the editor state as serialized JSON while stripping out all comment nodes and normalizing adjacent text nodes.- Signature:
exportJSONWithoutComments(editor: LexicalEditor): SerializedEditorState<SerializedLexicalNode>
- Params:
editor:
LexicalEditor
- Returns:
SerializedEditorState<SerializedLexicalNode>