# 评论

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

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

- Requested language: `zh-CN`

- Content language: `zh-CN`

- Documentation version: `1.0.0-rc.0`

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

---

#### Package metadata

```json
{
  "preset": [
    {
      "client": "@univerjs/preset-docs-thread-comment",
      "locale": "@univerjs/preset-docs-thread-comment/locales/zh-CN",
      "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/zh-CN",
      "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/zh-CN",
      "style": "@univerjs/docs-thread-comment-ui/lib/index.css"
    }
  ],
  "server": "optional"
}
```

评论功能允许用户在文档中添加评论和回复，便于团队成员之间的交流和协作。

## 预设模式

### 安装

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

### 使用

```typescript
import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'
import UniverPresetDocsCoreZhCN from '@univerjs/preset-docs-core/locales/zh-CN'
import { UniverDocsThreadCommentPreset } from '@univerjs/preset-docs-thread-comment' // [!code ++]
import UniverPresetDocsThreadCommentZhCN from '@univerjs/preset-docs-thread-comment/locales/zh-CN' // [!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.ZH_CN,
  locales: {
    [LocaleType.ZH_CN]: mergeLocales(
      UniverPresetDocsCoreZhCN,
      UniverPresetDocsThreadCommentZhCN, // [!code ++]
    ),
  },
  presets: [
    UniverDocsCorePreset(),
    UniverDocsThreadCommentPreset(), // [!code ++]
  ],
})
```

## 插件模式

### 安装

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

### 使用

```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/zh-CN'
import ThreadCommentUIZhCN from '@univerjs/thread-comment-ui/locale/zh-CN' // [!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.ZH_CN,
  locales: {
    [LocaleType.ZH_CN]: mergeLocales(
      ThreadCommentUIZhCN,
      DocsCommentLocale, // [!code ++]
    ),
  },
})

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

#### 配合协同编辑使用

如使用[协同编辑](https://docs.univer.ai/zh-CN/guides/docs/features/collaboration.md)功能，需参照以下方式配置：

#### 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 ++]
```

## 评论 Facade

调用前同时导入通用评论 Facade 和品类 Facade。品类方法负责建立稳定锚点，通用 API 用于回复、修改、解决和删除评论。`getComments()` 读取本地状态；`listCommentsAsync()` 通过已配置的数据源同步已知线程，不会发现服务端的全部线程。

```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.',
  })
}
```

## 共享讨论

共享讨论需要先通过 [Server SDK](https://docs.univer.ai/zh-CN/server/collaboration/extensions.md) 集成 Comment Service 和 Endpoint，再在协同与许可证配置之后注册 `@univerjs-pro/thread-comment-datasource` 的 `UniverThreadCommentDataSourcePlugin`。内置数据源访问 `/universer-api/comment/unit/{unitId}/...` 和 `/universer-api/user/list`，请通过应用暴露这些路由。服务端需要独立验证评论读写权限，不能仅依赖文档编辑权限。
