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

# Ace Editor Setup

<img src="https://mintcdn.com/velt/gAz_vLsG-ukKamYM/gifs/Add-Text-Comments.gif?s=1da499b73486c2acd4667b517baab389" alt="" width="1280" height="720" data-path="gifs/Add-Text-Comments.gif" />

## 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.

<Tabs>
  <Tab title="React / Next.js">
    ```js theme={null}
    <VeltProvider apiKey="API_KEY">
      <VeltComments textMode={false} />
    </VeltProvider>
    ```
  </Tab>

  <Tab title="Other Frameworks">
    ```html theme={null}
    <body>
      <velt-comments text-mode="false"></velt-comments>
    </body>
    ```
  </Tab>
</Tabs>

#### 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

<Tabs>
  <Tab title="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>

  <Tab title="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();
    ```
  </Tab>
</Tabs>

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

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

<Tabs>
  <Tab title="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>

  <Tab title="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 });
    });
    ```
  </Tab>
</Tabs>

#### 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`](/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](/async-collaboration/comments/customize-behavior#metadata). (optional)

<Tabs>
  <Tab title="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>

  <Tab title="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);
    ```
  </Tab>
</Tabs>

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

* Get the comment data from Velt SDK and render it in the Ace Editor.
* Params: [`RenderCommentsRequest`](/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.

<Tabs>
  <Tab title="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>

  <Tab title="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);
      }
    });
    ```
  </Tab>
</Tabs>

#### 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()](/api-reference/sdk/api/api-methods#aceveltcomments)

Initializes the Velt Comments extension for an Ace Editor instance.

* Params: `editor: Ace.Editor`, `config?:` [AceVeltCommentsConfig](/api-reference/sdk/models/data-models#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

<Tabs>
  <Tab title="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>

  <Tab title="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();
    ```
  </Tab>
</Tabs>

#### [addComment()](/api-reference/sdk/api/api-methods#addcomment-5)

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

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

<Tabs>
  <Tab title="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>

  <Tab title="Other Frameworks">
    ```tsx theme={null}
    import { addComment } from '@veltdev/ace-velt-comments';

    addComment({
      editor,
      editorId: 'my-editor',
      context: { customData: 'value' },
    });
    ```
  </Tab>
</Tabs>

#### [renderComments()](/api-reference/sdk/api/api-methods#rendercomments-5)

Renders and highlights comment annotations in the editor.

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

<Tabs>
  <Tab title="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>

  <Tab title="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,
        });
      }
    });
    ```
  </Tab>
</Tabs>
