> ## Documentation Index
> Fetch the complete documentation index at: https://velt.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Ace Editor Setup

![](https://mintcdn.com/velt/gAz_vLsG-ukKamYM/gifs/Add-Text-Comments.gif?s=1da499b73486c2acd4667b517baab389)

## 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 to `false` to hide the default text comment tool.

> [tab] React / Next.js
>
> ```js theme={null}
> <VeltProvider apiKey="API_KEY">
>   <VeltComments textMode={false} />
> </VeltProvider>
> ```

> [tab] Other Frameworks
>
> ```html theme={null}
> <body>
>   <velt-comments text-mode="false"></velt-comments>
> </body>
> ```

#### Step 2: Install the Velt Ace extension

```bash theme={null}
npm i @veltdev/ace-velt-comments
```

#### Step 3: Configure the Ace editor with the Velt Comments extension

> [tab] React / Next.js
>
> ```js theme={null}
> import { useEffect, useRef, useCallback } from 'react';
> import AceEditor from 'react-ace';
> import 'ace-builds/src-noconflict/mode-markdown';
> import 'ace-builds/src-noconflict/theme-github';
> import { AceVeltComments } from '@veltdev/ace-velt-comments';
>
> function AceEditorComponent() {
>   const editorRef = useRef(null);
>   const cleanupRef = useRef(null);
>
>   const handleLoad = useCallback((editor) => {
>     editorRef.current = editor;
>     // Initialize Velt comments - returns a cleanup function
>     cleanupRef.current = AceVeltComments(editor);
>   }, []);
>
>   useEffect(() => {
>     return () => {
>       if (cleanupRef.current) {
>         cleanupRef.current();
>       }
>     };
>   }, []);
>
>   return (
>     <AceEditor
>       mode="markdown"
>       theme="github"
>       name="ace-editor"
>       defaultValue="Your initial content here"
>       onLoad={handleLoad}
>       width="100%"
>       height="100%"
>     />
>   );
> }
> ```

> [tab] Other Frameworks
>
> ```js theme={null}
> import * as ace from 'ace-builds';
> import 'ace-builds/src-noconflict/mode-markdown';
> import 'ace-builds/src-noconflict/theme-github';
> import { AceVeltComments } from '@veltdev/ace-velt-comments';
>
> const editor = ace.edit(document.querySelector('#editor'));
> editor.setTheme('ace/theme/github');
> editor.session.setMode('ace/mode/markdown');
> editor.setValue('Your initial content here', -1);
>
> // Initialize Velt comments - returns a cleanup function
> const cleanup = AceVeltComments(editor);
>
> // Call cleanup() when done to remove event listeners
> // cleanup();
> ```

#### Step 4: Add a comment button to your Ace editor

* Add a button that users can click to add comments after selecting text.

> [tab] React / Next.js
>
> ```js theme={null}
> import { addComment } from '@veltdev/ace-velt-comments';
>
> // Add this button before <AceEditor> in your JSX
> <button
>   onMouseDown={(e) => e.preventDefault()}
>   onClick={() => {
>     if (editorRef.current) {
>       addComment({ editor: editorRef.current });
>     }
>   }}
> >
>   Add Comment
> </button>
> ```

> [tab] Other Frameworks
>
> ```html theme={null}
> <button id="add-comment-btn">Add Comment</button>
> ```
>
> ```js theme={null}
> import { addComment } from '@veltdev/ace-velt-comments';
>
> const addCommentBtn = document.getElementById('add-comment-btn');
> addCommentBtn.addEventListener('mousedown', (e) => e.preventDefault());
> addCommentBtn.addEventListener('click', () => {
>   addComment({ editor });
> });
> ```

#### Step 5: Call `addComment` to add a comment

* Call this method to add a comment to selected text in the Ace editor. You can use this when the user clicks on the comment button or presses a keyboard shortcut.
* Params: [`AddCommentRequest`](https://velt.dev/docs/api-reference/sdk/models/data-models#addcommentrequest-5). It has the following properties:
  * `editor`: instance of the Ace Editor.
  * `editorId`: Id of the Ace editor. Use this if you have multiple Ace editors on the same page in your app. (optional)
  * `context`: Add any custom metadata to the Comment Annotation. [Learn more](https://velt.dev/docs/async-collaboration/comments/customize-behavior.md#metadata). (optional)

> [tab] React / Next.js
>
> ```js theme={null}
> import { addComment } from '@veltdev/ace-velt-comments';
>
> const addCommentRequest = {
>   editor: editorRef.current,
>   editorId: 'EDITOR_ID',
>   context: {
>     storyId: 'story-id',
>     storyName: 'story-name',
>   },
> };
>
> addComment(addCommentRequest);
> ```

> [tab] Other Frameworks
>
> ```js theme={null}
> import { addComment } from '@veltdev/ace-velt-comments';
>
> const addCommentRequest = {
>   editor,
>   editorId: 'EDITOR_ID',
>   context: {
>     storyId: 'story-id',
>     storyName: 'story-name',
>   },
> };
>
> addComment(addCommentRequest);
> ```

#### Step 6: Render comments in Ace editor

* Get the comment data from Velt SDK and render it in the Ace Editor.
* Params: [`RenderCommentsRequest`](https://velt.dev/docs/api-reference/sdk/models/data-models#rendercommentsrequest-5). It has the following properties:
  * `editor`: Instance of the Ace Editor.
  * `editorId`: Id of the Ace editor. Use this if you have multiple Ace editors on the same page in your app. (optional)
  * `commentAnnotations`: Array of Comment Annotation objects.

> [tab] React / Next.js
>
> ```js theme={null}
> import { renderComments } from '@veltdev/ace-velt-comments';
> import { useCommentAnnotations } from '@veltdev/react';
>
> const annotations = useCommentAnnotations();
>
> useEffect(() => {
>   if (editorRef.current && annotations?.length) {
>     const renderCommentsRequest = {
>       editor: editorRef.current,
>       editorId: 'EDITOR_ID',
>       commentAnnotations: annotations,
>     };
>     renderComments(renderCommentsRequest);
>   }
> }, [annotations]);
> ```

> [tab] Other Frameworks
>
> ```js theme={null}
> import { renderComments } from '@veltdev/ace-velt-comments';
>
> const commentElement = Velt.getCommentElement();
> commentElement.getAllCommentAnnotations().subscribe((annotations) => {
>   if (editor && annotations?.length) {
>     const renderCommentsRequest = {
>       editor,
>       editorId: 'EDITOR_ID',
>       commentAnnotations: annotations,
>     };
>     renderComments(renderCommentsRequest);
>   }
> });
> ```

#### Step 7: Persist Velt Comment Marks (optional)

* By default, Velt comment marks (`<velt-comment-text>`) are persisted in the Ace editor by Velt SDK. When the editor loads and the Velt SDK initializes, the marks will be automatically added to the editor.
* If you plan to store the contents of the Ace editor on your end with the comment marks already included, you can disable this feature.
* Default: `true`

```js theme={null}
const cleanup = AceVeltComments(editor, {
  persistVeltMarks: false,
});
```

#### Step 8: Style the commented text

* You can style the commented text by adding CSS for the `velt-comment-text` element.

```css theme={null}
velt-comment-text {
  background-color: rgba(255, 255, 0, 0.3);
  border-bottom: 2px solid #ffcc00;
  cursor: pointer;
}

velt-comment-text:hover {
  background-color: rgba(255, 255, 0, 0.5);
}

velt-comment-text.velt-comment-selected {
  background-color: rgba(255, 255, 0, 0.5);
}
```

## APIs

#### [AceVeltComments()](https://velt.dev/docs/api-reference/sdk/api/api-methods.md#aceveltcomments)

Initializes the Velt Comments extension for an Ace Editor instance.

* Params: `editor: Ace.Editor`, `config?:` [AceVeltCommentsConfig](https://velt.dev/docs/api-reference/sdk/models/data-models.md#aceveltcommentsconfig)
  * `editorId?: string` - Unique identifier for this editor instance (for multi-editor scenarios)
  * `persistVeltMarks?: boolean` - Whether to persist Velt marks. Default: `true`
* Returns: `() => void` - Cleanup function to remove event listeners

> [tab] React / Next.js
>
> ```tsx theme={null}
> import { useRef, useCallback } from 'react';
> import AceEditor from 'react-ace';
> import { AceVeltComments } from '@veltdev/ace-velt-comments';
>
> function AceEditorComponent() {
>   const editorRef = useRef(null);
>   const cleanupRef = useRef(null);
>
>   const handleLoad = useCallback((editor) => {
>     editorRef.current = editor;
>     cleanupRef.current = AceVeltComments(editor, {
>       editorId: 'my-editor',
>       persistVeltMarks: true,
>     });
>   }, []);
>
>   return (
>     <AceEditor
>       onLoad={handleLoad}
>       mode="markdown"
>       theme="github"
>       name="ace-editor"
>     />
>   );
> }
> ```

> [tab] Other Frameworks
>
> ```tsx theme={null}
> import * as ace from 'ace-builds';
> import { AceVeltComments } from '@veltdev/ace-velt-comments';
>
> const editor = ace.edit(document.querySelector('#editor'));
>
> const cleanup = AceVeltComments(editor, {
>   editorId: 'my-editor',
>   persistVeltMarks: true,
> });
>
> // Later, when done:
> cleanup();
> ```

#### [addComment()](https://velt.dev/docs/api-reference/sdk/api/api-methods.md#addcomment-5)

Creates a comment annotation for the currently selected text in the editor.

* Params:
  * `request:` [AddCommentRequest](https://velt.dev/docs/api-reference/sdk/models/data-models.md#addcommentrequest-5)
    * `editor: Ace.Editor`
    * `editorId?: string`
    * `context?: object`
* Returns: `Promise<void>`

> [tab] React / Next.js
>
> ```tsx theme={null}
> import { addComment } from '@veltdev/ace-velt-comments';
>
> <button
>   onMouseDown={(e) => {
>     e.preventDefault();
>     addComment({
>       editor: editorRef.current,
>       editorId: 'my-editor',
>       context: { customData: 'value' }
>     });
>   }}
> >
>   Comment
> </button>
> ```

> [tab] Other Frameworks
>
> ```tsx theme={null}
> import { addComment } from '@veltdev/ace-velt-comments';
>
> addComment({
>   editor,
>   editorId: 'my-editor',
>   context: { customData: 'value' },
> });
> ```

#### [renderComments()](https://velt.dev/docs/api-reference/sdk/api/api-methods.md#rendercomments-5)

Renders and highlights comment annotations in the editor.

* Params:
  * `request:` [RenderCommentsRequest](https://velt.dev/docs/api-reference/sdk/models/data-models.md#rendercommentsrequest-5)
    * `editor: Ace.Editor`
    * `editorId?: string`
    * `commentAnnotations?:` [CommentAnnotation\[\]](https://velt.dev/docs/api-reference/sdk/models/data-models#commentannotation)
* Returns: `void`

> [tab] React / Next.js
>
> ```tsx theme={null}
> import { useEffect } from 'react';
> import { renderComments } from '@veltdev/ace-velt-comments';
> import { useCommentAnnotations } from '@veltdev/react';
>
> const annotations = useCommentAnnotations();
>
> useEffect(() => {
>   if (editorRef.current && annotations) {
>     renderComments({
>       editor: editorRef.current,
>       editorId: 'my-editor',
>       commentAnnotations: annotations,
>     });
>   }
> }, [annotations]);
> ```

> [tab] Other Frameworks
>
> ```tsx theme={null}
> import { renderComments } from '@veltdev/ace-velt-comments';
>
> const commentElement = Velt.getCommentElement();
> commentElement.getAllCommentAnnotations().subscribe((annotations) => {
>   if (editor && annotations) {
>     renderComments({
>       editor,
>       editorId: 'my-editor',
>       commentAnnotations: annotations,
>     });
>   }
> });
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.