Skip to main content
Add text comments in an editor The Monaco integration renders comment highlights as view-only overlay elements positioned over the commented text. It does not modify your Monaco model, undo history, or saved source code.

Setup

Step 1: Add Comment components

  • Add the Velt Comments component to the root of your app. This component is required to create and render comments in your app.
  • Authenticate the user with authProvider and set the Velt document before users add comments.
  • Set the textMode prop to false to hide the default Velt text comment tool. Monaco selections are handled by @veltdev/monaco-velt-comments.
  • Add VeltCommentsSidebar if you want a Google Docs-style comment sidebar.

Step 2: Install the Velt Monaco extension

monaco-editor is a peer dependency and must be provided by your Monaco app. The package uses the Monaco editor instance your app creates and does not bundle Monaco at runtime.

Step 3: Register the Monaco editor with Velt Comments

Register the integration on the Monaco editor instance returned by monaco.editor.create or by @monaco-editor/react’s onMount. Keep the returned handle so you can dispose it on teardown.

Step 4: Add a comment button to your Monaco editor

Add a button that users can click to add comments after selecting text in the Monaco editor. Important: Use onMouseDown with preventDefault() so the browser does not move focus away from Monaco before addComment reads the current selection. Keep the actual addComment call in onClick.
Registering the integration also adds an Add Comment Monaco editor action in the command palette and right-click menu. The action is enabled only when there is a non-empty editor selection.

Step 5: Call addComment to add a comment

  • Call this method to add a comment to selected text in the Monaco editor.
  • Params: AddCommentRequest. It has the following properties:
    • editor: Monaco IStandaloneCodeEditor instance.
    • editorId: Id of the editor. Use this if you have multiple Monaco editors on the same page. (optional)
    • context: Add custom metadata to the Comment Annotation. Learn more. (optional)
The library automatically writes context.textEditorConfig with the selected text, its 1-based occurrence index in the document, and the editor ID when one is provided.

Step 6: Render comments in Monaco editor

  • Get the comment data from Velt SDK and render it in the Monaco editor.
  • Params: RenderCommentsRequest. It has the following properties:
    • editor: Monaco IStandaloneCodeEditor instance.
    • editorId: Id of the editor. Use this if you have multiple Monaco editors on the same page. (optional)
    • commentAnnotations: Array of Comment Annotation objects.

Step 7: Re-apply Monaco comment highlights (optional)

  • Monaco renders comment highlights as view-only overlay elements.
  • The integration does not write Velt comment marks into your Monaco model or saved source code.
  • Save your Monaco model value normally. After you call model.setValue(...), editor.setModel(...), or load saved content, call renderComments again.

Step 8: Style the commented text

  • You can style the commented text by adding CSS for the velt-comment-text element.
  • Monaco highlights are rendered as overlay elements in the outer document, so broad velt-comment-text styles are appropriate for this integration.

Multiple Monaco editors

When using multiple Monaco editors on the same page, provide unique editorId values. The library stores editorId in context.textEditorConfig.editorId and filters annotations by that value when rendering.

TypeScript support

The package includes full TypeScript definitions.

Complete Example

APIs

registerVeltComments()

Registers the Velt comments integration on a Monaco editor instance. The call is idempotent per editor and returns a handle that tears down editor listeners, tracking decorations, overlay elements, renderer state, and local selected-comment subscriber cleanup.

addComment()

Creates a Velt comment annotation for the currently selected Monaco text. The selected text and its occurrence index are stored in annotation.context.textEditorConfig; the document content is not modified.

renderComments()

Resolves Velt comment annotations to Monaco ranges and renders them as stable <velt-comment-text> overlay elements positioned over the commented text.

isVeltAvailable()

Checks whether the Velt SDK is loaded on window.Velt.
  • Params: none
  • Returns: boolean