Comments
Users can leave comments and replies on a document, so a team can discuss the content in one place.
Plugin mode
Installation
pnpm add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/sheets-thread-comment @univerjs/sheets-thread-comment-uinpm install @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/sheets-thread-comment @univerjs/sheets-thread-comment-uiyarn add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/sheets-thread-comment @univerjs/sheets-thread-comment-uibun add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/sheets-thread-comment @univerjs/sheets-thread-comment-uiUsage
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverSheetsThreadCommentPlugin } from '@univerjs/sheets-thread-comment'import { UniverSheetsThreadCommentUIPlugin } from '@univerjs/sheets-thread-comment-ui'import SheetsThreadCommentEnUS from '@univerjs/sheets-thread-comment/locale/en-US'import SheetsThreadCommentUIEnUS from '@univerjs/sheets-thread-comment-ui/locale/en-US'import { UniverThreadCommentPlugin } from '@univerjs/thread-comment'import { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui'import ThreadCommentUIEnUS from '@univerjs/thread-comment-ui/locale/en-US'import '@univerjs/sheets-thread-comment-ui/lib/index.css'import '@univerjs/thread-comment-ui/lib/index.css'import '@univerjs/sheets-thread-comment/facade'const univer = new Univer({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( ThreadCommentUIEnUS, SheetsThreadCommentEnUS, SheetsThreadCommentUIEnUS, ), },})univer.registerPlugin(UniverThreadCommentPlugin)univer.registerPlugin(UniverThreadCommentUIPlugin)univer.registerPlugin(UniverSheetsThreadCommentPlugin)univer.registerPlugin(UniverSheetsThreadCommentUIPlugin)Using with collaboration feature
If Collaboration is also used, register the data source plugin as well:
pnpm add @univerjs-pro/thread-comment-datasourcenpm install @univerjs-pro/thread-comment-datasourceyarn add @univerjs-pro/thread-comment-datasourcebun add @univerjs-pro/thread-comment-datasourceimport { UniverThreadCommentDataSourcePlugin } from '@univerjs-pro/thread-comment-datasource'univer.registerPlugin(UniverThreadCommentDataSourcePlugin)Mobile support
Configure the mobile editor first. In the plugin-mode setup on this page, replace the desktop registrations below with the mobile exports from the same packages, and do so before opening a document. Keep the model plugins, styles, locales, and Facade imports, and do not register both variants.
| Package | Desktop registration | Mobile registration |
|---|---|---|
@univerjs/sheets-thread-comment-ui | UniverSheetsThreadCommentUIPlugin | UniverSheetsThreadCommentMobileUIPlugin |
@univerjs/thread-comment-ui | UniverThreadCommentUIPlugin | UniverThreadCommentMobileUIPlugin |
Preset mode
Installation
pnpm add @univerjs/preset-sheets-thread-commentnpm install @univerjs/preset-sheets-thread-commentyarn add @univerjs/preset-sheets-thread-commentbun add @univerjs/preset-sheets-thread-commentUsage
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { UniverSheetsThreadCommentPreset } from '@univerjs/preset-sheets-thread-comment'import UniverPresetSheetsThreadCommentEnUS from '@univerjs/preset-sheets-thread-comment/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-thread-comment/lib/index.css'const { univerAPI } = createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetSheetsCoreEnUS, UniverPresetSheetsThreadCommentEnUS, ), }, presets: [ UniverSheetsCorePreset(), UniverSheetsThreadCommentPreset(), ],})Facade API
The complete Facade API type definitions are available in the FacadeAPI.
Importing
Plugin mode note
The Facade package is imported manually in plugin mode only. Preset mode already includes it, so no additional import is required.
import '@univerjs/sheets-thread-comment/facade'Create cell comment
univerAPI.newTheadComment() returns an FTheadCommentBuilder for a new comment. Chain methods on the builder to set the properties of the comment.
The following are some member methods of FTheadCommentBuilder:
| Method | Description |
|---|---|
| setContent | Sets the comment content |
| setPersonId | Sets the person ID of the comment |
| setDateTime | Sets the date and time of the comment |
| setId | Sets the comment ID |
| setThreadId | Sets the thread ID of the comment |
// Create a new commentconst richText = univerAPI.newRichText().insertText('hello univer')const commentBuilder = univerAPI.newTheadComment() .setContent(richText)console.log(commentBuilder.content.toPlainText())// Add the comment to cell A1const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const cell = fWorksheet.getRange('A1')const result = await cell.addCommentAsync(commentBuilder)Get cell comments
These methods return an FThreadComment, which can then be updated, deleted, or resolved.
FWorkbook.getComments(): gets all comments in the workbookFfWorksheet.getComments(): gets all comments in the worksheetFRange.getComment(): gets the comment of the top-left cell in the rangeFRange.getComments(): gets all comments in the range
The following are some member methods of FThreadComment:
| Method | Description |
|---|---|
| getIsRoot | Whether the comment is a root comment |
| getCommentData | Gets the comment data |
| getReplies | Gets the list of replies to the comment |
| getRange | Gets the range of the comment |
| getRichText | Gets the rich text content of the comment |
| deleteAsync | Deletes the comment and its replies |
| updateAsync | Updates the comment content |
| resolveAsync | Resolves the comment |
| replyAsync | Replies to the comment |
// Get all comments in the workbookconst fWorkbook = univerAPI.getActiveWorkbook()fWorkbook.getComments()// Get all comments in the active worksheetconst fWorksheet = fWorkbook.getActiveSheet()fWorksheet.getComments()const fRange = fWorksheet.getRange('A1:B2')// Get the comment of cell A1fRange.getComment()// Get all comments in the range A1:B2fRange.getComments()Clear cell comments
FWorkbook.clearComments(): clears all comments in the workbookFWorksheet.clearComments(): clears all comments in the worksheetFRange.clearCommentAsync(): clears the comment of the top-left cell in the rangeFRange.clearCommentsAsync(): clears all comments in the range
// Clear all comments in the workbookconst fWorkbook = univerAPI.getActiveWorkbook()await fWorkbook.clearComments()// Clear all comments in the active worksheetconst fWorksheet = fWorkbook.getActiveSheet()await fWorksheet.clearComments()const fRange = fWorksheet.getRange('A1:B2')// Clear the comment of cell A1await fRange.clearCommentAsync()// Clear all comments in the range A1:B2await fRange.clearCommentsAsync()Get comment replies list
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Get all comments in the worksheetconst comments = fWorksheet.getComments()comments.forEach((comment) => { // If the comment is a root comment, get its replies if (comment.getIsRoot()) { const replies = comment.getReplies() replies.forEach((reply) => { console.log(reply.getCommentData()) }) }})Update/delete/resolve/reply to comments
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Create a new comment and add it to cell A1const richText = univerAPI.newRichText().insertText('hello univer')const commentBuilder = univerAPI.newTheadComment() .setContent(richText) .setId('mock-comment-id')const cell = fWorksheet.getRange('A1')await cell.addCommentAsync(commentBuilder)// Update the comment and reply after 3 secondssetTimeout(async () => { // Get the comment by ID and update its content const comment = fWorksheet.getCommentById('mock-comment-id') const newRichText = univerAPI.newRichText().insertText('Hello Univer AI') await comment.updateAsync(newRichText) // Create a new reply comment and set its content const replyText = univerAPI.newRichText().insertText('Hello Univer AI! GO! GO! GO!') const reply = univerAPI.newTheadComment().setContent(replyText) await comment.replyAsync(reply)}, 3000)// Resolve the comment and delete it after 6 secondssetTimeout(async () => { const comment = fWorksheet.getCommentById('mock-comment-id') await comment.resolveAsync() await comment.deleteAsync()}, 6000)Event listeners
The full event type definitions are in Events.
The comment module emits events for comment additions, updates, deletions, and resolution status changes. Subscribe to any of these events with univerAPI.addEvent().
Comment addition events
univerAPI.Event.CommentAdded fires after a comment is added.
const disposable = univerAPI.addEvent(univerAPI.Event.CommentAdded, (params) => { const { comment, workbook, worksheet, row, col } = params})// Remove the event listener, use `disposable.dispose()`univerAPI.Event.BeforeCommentAdd fires before a comment is added.
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentAdd, (params) => { const { comment, workbook, worksheet, row, col } = params // Cancel the comment addition operation params.cancel = true})// Remove the event listener, use `disposable.dispose()`Comment update events
univerAPI.Event.CommentUpdated fires after a comment is updated.
const disposable = univerAPI.addEvent(univerAPI.Event.CommentUpdated, (params) => { const { comment, workbook, worksheet, row, col } = params})// Remove the event listener, use `disposable.dispose()`univerAPI.Event.BeforeCommentUpdate fires before a comment is updated.
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentUpdate, (params) => { const { comment, workbook, worksheet, row, col, newContent } = params // Cancel the comment update operation params.cancel = true})// Remove the event listener, use `disposable.dispose()`Comment deletion events
univerAPI.Event.CommentDeleted fires after a comment is deleted.
const disposable = univerAPI.addEvent(univerAPI.Event.CommentDeleted, (params) => { const { commentId, workbook, worksheet } = params})// Remove the event listener, use `disposable.dispose()`univerAPI.Event.BeforeCommentDelete fires before a comment is deleted.
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentDelete, (params) => { const { comment, workbook, worksheet, row, col } = params // Cancel the comment deletion operation params.cancel = true})// Remove the event listener, use `disposable.dispose()`Comment resolution status events
univerAPI.Event.CommentResolved fires after the resolution status of a comment changes.
const disposable = univerAPI.addEvent(univerAPI.Event.CommentResolved, (params) => { const { comment, row, col, resolved, workbook, worksheet } = params})// Remove the event listener, use `disposable.dispose()`univerAPI.Event.BeforeCommentResolve fires before the resolution status of a comment changes.
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentResolve, (params) => { const { comment, row, col, resolved, workbook, worksheet } = params // Cancel the comment resolution operation params.cancel = true})// Remove the event listener, use `disposable.dispose()`All events carry the following common parameters:
workbook: the current workbookworksheet: the current worksheetrow: the row index of the commentcol: the column index of the commentcomment: the comment object (commentIdonly in deletion events)
Some events add parameters of their own:
BeforeCommentUpdatealso carriesnewContent, the new comment content (RichTextValue type)CommentResolvedandBeforeCommentResolvealso carryresolved, the comment's resolution status
Every Before callback can set params.cancel = true to stop the operation.
Shared comment operations
Import both the shared comment Facade and the product Facade before calling these methods. The product helpers create stable anchors, and the shared API handles replies, updates, resolution, and deletion. getComments() reads local state only. listCommentsAsync() synchronizes the threads it already knows about through the configured data source; it does not discover every thread on the server.
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
Shared discussions require the Comment Service and Endpoint, integrated through Server SDK. After collaboration and the license are configured, register UniverThreadCommentDataSourcePlugin from @univerjs-pro/thread-comment-datasource. The supplied data source uses /universer-api/comment/unit/{unitId}/... and /universer-api/user/list, so the application must expose those routes.
Comments on images and charts
After configuring the host drawing or chart plugin, add the matching integration below together with its Facade entry before opening the Unit. Plugin mode and preset mode both require these integrations to be registered explicitly. They add no extra styles or locales.
pnpm add @univerjs-pro/sheets-drawing-thread-comment @univerjs-pro/sheets-chart-thread-commentnpm install @univerjs-pro/sheets-drawing-thread-comment @univerjs-pro/sheets-chart-thread-commentyarn add @univerjs-pro/sheets-drawing-thread-comment @univerjs-pro/sheets-chart-thread-commentbun add @univerjs-pro/sheets-drawing-thread-comment @univerjs-pro/sheets-chart-thread-commentimport { UniverSheetsDrawingThreadCommentPlugin } from '@univerjs-pro/sheets-drawing-thread-comment'import { UniverSheetsChartThreadCommentPlugin } from '@univerjs-pro/sheets-chart-thread-comment'import '@univerjs-pro/sheets-drawing-thread-comment/facade'import '@univerjs-pro/sheets-chart-thread-comment/facade'univer.registerPlugin(UniverSheetsDrawingThreadCommentPlugin)univer.registerPlugin(UniverSheetsChartThreadCommentPlugin)With the Unit open, comments can be added to existing images or charts. getComments() returns the threads loaded locally; listCommentsAsync() synchronizes the threads it knows about before returning them.
const host = univerAPI.getActiveWorkbook()?.getActiveSheet()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/sheets-drawing-thread-comment · @univerjs-pro/sheets-chart-thread-comment
How is this guide?