評論

使用者可在文件中新增評論和回覆,團隊成員可就文件內容展開討論。

外掛模式

安裝

Shell
pnpm 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'import { UniverDocsThreadCommentUIPlugin } from '@univerjs/docs-thread-comment-ui'import { UniverThreadCommentPlugin } from '@univerjs/thread-comment'import { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui'import DocsCommentLocale from '@univerjs/docs-thread-comment-ui/locale/zh-TW'import ThreadCommentUIZhTW from '@univerjs/thread-comment-ui/locale/zh-TW'import '@univerjs/docs-thread-comment/facade'import '@univerjs/docs-thread-comment-ui/lib/index.css'import '@univerjs/thread-comment-ui/lib/index.css'const univer = new Univer({  locale: LocaleType.ZH_TW,  locales: {    [LocaleType.ZH_TW]: mergeLocales(      ThreadCommentUIZhTW,      DocsCommentLocale,     ),  },})univer.registerPlugin(UniverThreadCommentPlugin)univer.registerPlugin(UniverThreadCommentUIPlugin)univer.registerPlugin(UniverDocsThreadCommentPlugin)univer.registerPlugin(UniverDocsThreadCommentUIPlugin)

配合協同編輯使用

使用協同編輯時,還需額外註冊資料來源外掛:

Shell
pnpm add @univerjs-pro/thread-comment-datasource
TypeScript
import { UniverThreadCommentDataSourcePlugin } from '@univerjs-pro/thread-comment-datasource'univer.registerPlugin(UniverThreadCommentDataSourcePlugin)

行動裝置支援

請先配置行動版編輯器。在本頁外掛模式的初始化程式碼中,將下列桌面版註冊替換為同一套件匯出的行動版外掛,替換須在開啟文件前完成。模型外掛、樣式、語言套件和 Facade 匯入均需保留,不要同時註冊兩個版本。

套件桌面版註冊行動版註冊
@univerjs/docs-thread-comment-uiUniverDocsThreadCommentUIPluginUniverDocsThreadCommentMobileUIPlugin
@univerjs/thread-comment-uiUniverThreadCommentUIPluginUniverThreadCommentMobileUIPlugin

預設模式

安裝

Shell
pnpm add @univerjs/preset-docs-thread-comment

使用

TypeScript
import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'import UniverPresetDocsCoreZhTW from '@univerjs/preset-docs-core/locales/zh-TW'import { UniverDocsThreadCommentPreset } from '@univerjs/preset-docs-thread-comment'import UniverPresetDocsThreadCommentZhTW from '@univerjs/preset-docs-thread-comment/locales/zh-TW'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-docs-core/lib/index.css'import '@univerjs/preset-docs-thread-comment/lib/index.css'const { univerAPI } = createUniver({  locale: LocaleType.ZH_TW,  locales: {    [LocaleType.ZH_TW]: mergeLocales(      UniverPresetDocsCoreZhTW,      UniverPresetDocsThreadCommentZhTW,     ),  },  presets: [    UniverDocsCorePreset(),    UniverDocsThreadCommentPreset(),   ],})

評論 Facade

呼叫這些方法前,需同時匯入通用評論 Facade 和品類 Facade。品類方法負責建立穩定的錨點,通用 API 負責回覆、修改、解決和刪除。getComments() 讀取本地狀態;listCommentsAsync() 透過已配置的資料來源同步已知執行緒,但不會發現伺服器端的全部執行緒。

TypeScript
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())}
TypeScript
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 接入 Comment Service 和 Endpoint,並在協同與許可證配置完成後註冊 @univerjs-pro/thread-comment-datasource 中的 UniverThreadCommentDataSourcePlugin。內建資料來源會請求 /universer-api/comment/unit/{unitId}/... 和 /universer-api/user/list,這兩個路由必須由應用對外提供。評論的讀取和寫入需要單獨的伺服器端權限,文件編輯權限不覆蓋這些權限。

圖片與圖表評論

先配置宿主的圖片或圖表外掛,再在開啟 Unit 前新增對應的整合外掛及其 Facade 入口。外掛模式和預設模式均需顯式註冊這些整合,它們不包含額外的樣式或語言套件。

Shell
pnpm add @univerjs-pro/docs-drawing-thread-comment @univerjs-pro/docs-chart-thread-comment
TypeScript
import { UniverDocsDrawingThreadCommentPlugin } from '@univerjs-pro/docs-drawing-thread-comment'import { UniverDocsChartThreadCommentPlugin } from '@univerjs-pro/docs-chart-thread-comment'import '@univerjs-pro/docs-drawing-thread-comment/facade'import '@univerjs-pro/docs-chart-thread-comment/facade'univer.registerPlugin(UniverDocsDrawingThreadCommentPlugin)univer.registerPlugin(UniverDocsChartThreadCommentPlugin)

開啟 Unit 後,可為其中已有的圖片和圖表新增評論。getComments() 返回本地已載入的執行緒;listCommentsAsync() 會先同步已知執行緒,再返回同步結果。

TypeScript
const host = univerAPI.getActiveDocument()const image = host?.getImages()[0]const chart = host?.getCharts()[0]await image?.createCommentAsync('Review this image.')await chart?.createCommentAsync('Verify the chart source.')

@univerjs-pro/docs-drawing-thread-comment · @univerjs-pro/docs-chart-thread-comment

你覺得這篇文檔如何?

© 2026 DreamNum Co., Ltd.