# Comments

- Human documentation: [https://docs.univer.ai/guides/docs/features/comments](https://docs.univer.ai/guides/docs/features/comments)

- Agent Markdown: [https://docs.univer.ai/guides/docs/features/comments.md](https://docs.univer.ai/guides/docs/features/comments.md)

- Requested language: `en-US`

- Content language: `en-US`

- Documentation version: `1.0.0-rc.0`

- Source: [docs/features/comments.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/docs/features/comments.mdx)

---

#### Package metadata

```json
{
  "preset": [
    {
      "client": "@univerjs/preset-docs-thread-comment",
      "locale": "@univerjs/preset-docs-thread-comment/locales/en-US",
      "style": "@univerjs/preset-docs-thread-comment/lib/index.css"
    }
  ],
  "plugins": [
    {
      "client": "@univerjs/thread-comment"
    },
    {
      "client": "@univerjs/thread-comment-ui",
      "locale": "@univerjs/thread-comment-ui/locale/en-US",
      "style": "@univerjs/thread-comment-ui/lib/index.css"
    },
    {
      "client": "@univerjs/docs-thread-comment",
      "facade": "@univerjs/docs-thread-comment/facade"
    },
    {
      "client": "@univerjs/docs-thread-comment-ui",
      "locale": "@univerjs/docs-thread-comment-ui/locale/en-US",
      "style": "@univerjs/docs-thread-comment-ui/lib/index.css"
    }
  ],
  "server": "optional"
}
```

Comments allow users to add comments and replies within documents, facilitating communication and collaboration among team members.

## Preset Mode

### Installation

#### npm

```bash
npm install @univerjs/preset-docs-thread-comment
```

#### pnpm

```bash
pnpm add @univerjs/preset-docs-thread-comment
```

#### yarn

```bash
yarn add @univerjs/preset-docs-thread-comment
```

#### bun

```bash
bun add @univerjs/preset-docs-thread-comment
```

### Usage

```typescript
import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'
import UniverPresetDocsCoreEnUS from '@univerjs/preset-docs-core/locales/en-US'
import { UniverDocsThreadCommentPreset } from '@univerjs/preset-docs-thread-comment' // [!code ++]
import UniverPresetDocsThreadCommentEnUS from '@univerjs/preset-docs-thread-comment/locales/en-US' // [!code ++]
import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'

import '@univerjs/preset-docs-core/lib/index.css'
import '@univerjs/preset-docs-thread-comment/lib/index.css' // [!code ++]

const { univerAPI } = createUniver({
  locale: LocaleType.EN_US,
  locales: {
    [LocaleType.EN_US]: mergeLocales(
      UniverPresetDocsCoreEnUS,
      UniverPresetDocsThreadCommentEnUS, // [!code ++]
    ),
  },
  presets: [
    UniverDocsCorePreset(),
    UniverDocsThreadCommentPreset(), // [!code ++]
  ],
})
```

## Plugin Mode

### Installation

#### npm

```bash
npm install @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/docs-thread-comment @univerjs/docs-thread-comment-ui
```

#### pnpm

```bash
pnpm add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/docs-thread-comment @univerjs/docs-thread-comment-ui
```

#### yarn

```bash
yarn add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/docs-thread-comment @univerjs/docs-thread-comment-ui
```

#### bun

```bash
bun add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/docs-thread-comment @univerjs/docs-thread-comment-ui
```

### Usage

```typescript
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'
import { UniverDocsThreadCommentPlugin } from '@univerjs/docs-thread-comment' // [!code ++]
import { UniverDocsThreadCommentUIPlugin } from '@univerjs/docs-thread-comment-ui' // [!code ++]
import { UniverThreadCommentPlugin } from '@univerjs/thread-comment' // [!code ++]
import { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui' // [!code ++]
import DocsCommentLocale from '@univerjs/docs-thread-comment-ui/locale/en-US'
import ThreadCommentUIEnUS from '@univerjs/thread-comment-ui/locale/en-US' // [!code ++]

import '@univerjs/docs-thread-comment/facade' // [!code ++]
import '@univerjs/docs-thread-comment-ui/lib/index.css'
import '@univerjs/thread-comment-ui/lib/index.css' // [!code ++]

const univer = new Univer({
  locale: LocaleType.EN_US,
  locales: {
    [LocaleType.EN_US]: mergeLocales(
      ThreadCommentUIEnUS,
      DocsCommentLocale, // [!code ++]
    ),
  },
})

univer.registerPlugin(UniverThreadCommentPlugin) // [!code ++]
univer.registerPlugin(UniverThreadCommentUIPlugin) // [!code ++]
univer.registerPlugin(UniverDocsThreadCommentPlugin) // [!code ++]
univer.registerPlugin(UniverDocsThreadCommentUIPlugin) // [!code ++]
```

#### Using with Collaboration Feature

If you are using the [Collaboration](https://docs.univer.ai/guides/docs/features/collaboration.md) feature, configure it as follows:

#### npm

```bash
npm install @univerjs-pro/thread-comment-datasource
```

#### pnpm

```bash
pnpm add @univerjs-pro/thread-comment-datasource
```

#### yarn

```bash
yarn add @univerjs-pro/thread-comment-datasource
```

#### bun

```bash
bun add @univerjs-pro/thread-comment-datasource
```

```typescript
import { UniverThreadCommentDataSourcePlugin } from '@univerjs-pro/thread-comment-datasource' // [!code ++]

univer.registerPlugin(UniverThreadCommentDataSourcePlugin) // [!code ++]
```

## Comment Facade

Import both the shared comment Facade and the product Facade before calling these methods. Product helpers create stable anchors; the shared API handles replies, updates, resolution, and deletion. `getComments()` reads local state. `listCommentsAsync()` synchronizes already known threads through the configured data source; it does not discover every thread on the server.

```ts
import '@univerjs/thread-comment/facade'
import '@univerjs/docs-thread-comment/facade'

const range = univerAPI.getActiveDocument()?.getTextRange(0, 1)
if (range) {
  await range.createCommentAsync('Please review this text.')
  console.log(await range.listCommentsAsync())
}
```

```ts
import '@univerjs/thread-comment/facade'

const [thread] = univerAPI.getComments({ resolved: false })
if (thread) {
  await univerAPI.replyCommentAsync({
    unitId: thread.unitId,
    subUnitId: thread.subUnitId,
    threadId: thread.threadId,
    content: 'Reviewed.',
  })
}
```

## Shared discussions

For shared discussions, integrate the Comment Service and Endpoint with [Server SDK](https://docs.univer.ai/server/collaboration/extensions.md), then register `UniverThreadCommentDataSourcePlugin` from `@univerjs-pro/thread-comment-datasource` after collaboration and license setup. The supplied data source uses `/universer-api/comment/unit/{unitId}/...` and `/universer-api/user/list`; expose those routes through your application. Server permissions must cover comment reads and writes independently of document editing.
