# 留言

- Human documentation: [https://docs.univer.ai/zh-TW/guides/bases/features/thread-comment](https://docs.univer.ai/zh-TW/guides/bases/features/thread-comment)

- Agent Markdown: [https://docs.univer.ai/zh-TW/guides/bases/features/thread-comment.md](https://docs.univer.ai/zh-TW/guides/bases/features/thread-comment.md)

- Requested language: `zh-TW`

- Content language: `zh-TW`

- Documentation version: `1.2.0`

- Source: [bases/features/thread-comment.zh-TW.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/bases/features/thread-comment.zh-TW.mdx)

---

#### Package metadata

```json
{
  "preset": [],
  "plugins": [
    {
      "client": "@univerjs/thread-comment",
      "facade": "@univerjs/thread-comment/facade"
    },
    {
      "client": "@univerjs/thread-comment-ui",
      "locale": "@univerjs/thread-comment-ui/locale/zh-TW",
      "style": "@univerjs/thread-comment-ui/lib/index.css"
    },
    {
      "client": "@univerjs-pro/bases-thread-comment",
      "facade": "@univerjs-pro/bases-thread-comment/facade"
    },
    {
      "client": "@univerjs-pro/bases-thread-comment-ui",
      "locale": "@univerjs-pro/bases-thread-comment-ui/locale/zh-TW",
      "style": "@univerjs-pro/bases-thread-comment-ui/lib/index.css"
    }
  ],
  "license": true,
  "server": "optional"
}
```

針對任務、訂單及其他記錄進行討論。留言會持續關聯到該記錄，無論使用格線、看板、圖庫、行事曆或甘特圖檢視；切換檢視不會建立另一組討論。

## 註冊外掛

在 Bases 核心與 UI 外掛之後加入：

#### npm

```bash
npm install @univerjs-pro/bases-thread-comment @univerjs-pro/bases-thread-comment-ui
```

#### pnpm

```bash
pnpm add @univerjs-pro/bases-thread-comment @univerjs-pro/bases-thread-comment-ui
```

#### yarn

```bash
yarn add @univerjs-pro/bases-thread-comment @univerjs-pro/bases-thread-comment-ui
```

#### bun

```bash
bun add @univerjs-pro/bases-thread-comment @univerjs-pro/bases-thread-comment-ui
```

```ts
import { UniverBasesThreadCommentPlugin } from '@univerjs-pro/bases-thread-comment'
import { UniverBasesThreadCommentUIPlugin } from '@univerjs-pro/bases-thread-comment-ui'

import '@univerjs-pro/bases-thread-comment/facade'
import '@univerjs/thread-comment/facade'
import '@univerjs-pro/bases-thread-comment-ui/lib/index.css'

univer.registerPlugin(UniverBasesThreadCommentPlugin)
univer.registerPlugin(UniverBasesThreadCommentUIPlugin)
```

合併留言套件的語言資源，並設定目前的應用程式使用者。UI 外掛加入記錄留言控制項，資料外掛則將留言與記錄關聯。

## 新增與讀取留言

```ts
const record = univerAPI.getActiveBase()?.getTables()[0]?.getRecords()[0]
if (!record) throw new Error('Create a record first')

await record.createCommentAsync('Please confirm the delivery date.')
const comments = record.getComments()
console.log(comments.map(({ root }) => root.id))
```

`getComments()` 讀取本機已載入的留言。使用遠端資料來源時，`await record.listCommentsAsync()` 會先同步已知的討論串，再回傳結果。回覆、更新與刪除方法請參閱[留言 Facade](https://docs.univer.ai/zh-TW/reference/facade/thread-comment.md)。

## 儲存與共用討論

本機留言可透過 `base.save()` 儲存為 Base 資源。還原快照前，請先註冊留言外掛。

共用討論還需要留言服務、資料來源與權限。文件協同本身不會接通留言功能。請參考 [Office 擴充模組](https://docs.univer.ai/zh-TW/server/collaboration/extensions.md)，並使用兩個帳號測試建立、讀取、權限與重新開啟。

## 通用評論操作

呼叫前同時匯入通用評論 Facade 與品類 Facade。品類方法建立穩定錨點，通用 API 負責回覆、修改、解決與刪除。`getComments()` 讀取本機狀態；`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-TW/server/collaboration/extensions.md) 整合 Comment Service 與 Endpoint，再於協同及授權設定之後註冊 `@univerjs-pro/thread-comment-datasource` 的 `UniverThreadCommentDataSourcePlugin`。內建資料來源存取 `/universer-api/comment/unit/{unitId}/...` 與 `/universer-api/user/list`，請由應用程式提供這些路由。服務端須獨立驗證評論讀寫權限，不能僅依賴文件編輯權限。
