评论

用户可在文档中添加评论和回复,团队成员可就文档内容展开讨论。

插件模式

安装

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-CN'import ThreadCommentUIZhCN from '@univerjs/thread-comment-ui/locale/zh-CN'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_CN,  locales: {    [LocaleType.ZH_CN]: mergeLocales(      ThreadCommentUIZhCN,      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 UniverPresetDocsCoreZhCN from '@univerjs/preset-docs-core/locales/zh-CN'import { UniverDocsThreadCommentPreset } from '@univerjs/preset-docs-thread-comment'import UniverPresetDocsThreadCommentZhCN from '@univerjs/preset-docs-thread-comment/locales/zh-CN'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_CN,  locales: {    [LocaleType.ZH_CN]: mergeLocales(      UniverPresetDocsCoreZhCN,      UniverPresetDocsThreadCommentZhCN,     ),  },  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.