評論
評論功能允許使用者在文件中新增評論和回覆,便於團隊成員之間的交流和協作。
外掛模式
安裝
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-ui使用
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverSheetsThreadCommentPlugin } from '@univerjs/sheets-thread-comment'import { UniverSheetsThreadCommentUIPlugin } from '@univerjs/sheets-thread-comment-ui'import SheetsThreadCommentUIZhTW from '@univerjs/sheets-thread-comment-ui/locale/zh-TW'import { UniverThreadCommentPlugin } from '@univerjs/thread-comment'import { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui'import ThreadCommentUIZhTW from '@univerjs/thread-comment-ui/locale/zh-TW'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.ZH_TW, locales: { [LocaleType.ZH_TW]: mergeLocales( ThreadCommentUIZhTW, SheetsThreadCommentUIZhTW, ), },})univer.registerPlugin(UniverThreadCommentPlugin)univer.registerPlugin(UniverThreadCommentUIPlugin)univer.registerPlugin(UniverSheetsThreadCommentPlugin)univer.registerPlugin(UniverSheetsThreadCommentUIPlugin)配合協同編輯使用
如使用協同編輯功能,需參照以下方式配置:
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)行動裝置支援
先設定行動版編輯器。在本頁外掛模式的初始化程式碼中,將下列桌面版註冊替換為同一套件匯出的行動版外掛,並在開啟文件前完成。保留模型外掛、樣式、語言套件與 Facade 匯入,請勿同時註冊兩個版本。
| 套件 | 桌面版註冊 | 行動版註冊 |
|---|---|---|
@univerjs/sheets-thread-comment-ui | UniverSheetsThreadCommentUIPlugin | UniverSheetsThreadCommentMobileUIPlugin |
@univerjs/thread-comment-ui | UniverThreadCommentUIPlugin | UniverThreadCommentMobileUIPlugin |
預設模式
安裝
pnpm add @univerjs/preset-sheets-thread-commentnpm install @univerjs/preset-sheets-thread-commentyarn add @univerjs/preset-sheets-thread-commentbun add @univerjs/preset-sheets-thread-comment使用
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreZhTW from '@univerjs/preset-sheets-core/locales/zh-TW'import { UniverSheetsThreadCommentPreset } from '@univerjs/preset-sheets-thread-comment'import UniverPresetSheetsThreadCommentZhTW from '@univerjs/preset-sheets-thread-comment/locales/zh-TW'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.ZH_TW, locales: { [LocaleType.ZH_TW]: mergeLocales( UniverPresetSheetsCoreZhTW, UniverPresetSheetsThreadCommentZhTW, ), }, presets: [ UniverSheetsCorePreset(), UniverSheetsThreadCommentPreset(), ],})Facade API
完整 Facade API 型別定義,請檢視 FacadeAPI
引入
外掛模式提示
僅外掛模式需要手動引入 Facade 套件。預設模式已內建對應的 Facade 套件,無需額外引入。
import '@univerjs/sheets-thread-comment/facade'建立單元格評論
univerAPI.newTheadComment() 建立一個新的評論構建器,返回一個 FTheadCommentBuilder 例項,可以透過鏈式呼叫生成評論。
以下是 FTheadCommentBuilder 上的一些成員方法:
| 方法 | 描述 |
|---|---|
| setContent | 設定評論的內容 |
| setPersonId | 設定評論的人員 id |
| setDateTime | 設定評論的日期時間 |
| setId | 設定評論的 id |
| setThreadId | 設定評論的主題 id |
// 建立一個新的評論const richText = univerAPI.newRichText().insertText('hello univer')const commentBuilder = univerAPI.newTheadComment() .setContent(richText)console.log(commentBuilder.content.toPlainText())// 新增評論到 A1 單元格const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const cell = fWorksheet.getRange('A1')const result = await cell.addCommentAsync(commentBuilder)獲取單元格評論
可以透過以下方法獲取評論,返回 FThreadComment 例項,可以進行評論的更新、刪除、解決等操作。
FWorkbook.getComments(): 獲取工作簿的所有評論FfWorksheet.getComments(): 獲取工作表的所有評論FRange.getComment(): 獲取範圍左上角單元格的評論FRange.getComments(): 獲取範圍內所有單元格的評論
以下是 FThreadComment 上的一些成員方法:
| 方法 | 描述 |
|---|---|
| getIsRoot | 是否是根評論 |
| getCommentData | 獲取評論資料 |
| getReplies | 獲取評論的回覆列表 |
| getRange | 獲取評論的範圍 |
| getRichText | 獲取評論的富文字內容 |
| deleteAsync | 刪除評論及其回覆 |
| updateAsync | 更新評論內容 |
| resolveAsync | 解決評論 |
| replyAsync | 回覆評論 |
// 獲取工作簿的所有評論const fWorkbook = univerAPI.getActiveWorkbook()fWorkbook.getComments()// 獲取活動工作表的所有評論const fWorksheet = fWorkbook.getActiveSheet()fWorksheet.getComments()const fRange = fWorksheet.getRange('A1:B2')// 獲取 A1 單元格的評論fRange.getComment()// 獲取 A1:B2 範圍的所有評論fRange.getComments()清除單元格評論
FWorkbook.clearComments(): 清除工作簿的所有評論FWorksheet.clearComments(): 清除工作表的所有評論FRange.clearCommentAsync(): 清除範圍左上角單元格的評論FRange.clearCommentsAsync(): 清除範圍內所有單元格的評論
// 清除工作簿的所有評論const fWorkbook = univerAPI.getActiveWorkbook()await fWorkbook.clearComments()// 清除活動工作表的所有評論const fWorksheet = fWorkbook.getActiveSheet()await fWorksheet.clearComments()const fRange = fWorksheet.getRange('A1:B2')// 清除 A1 單元格的評論await fRange.clearCommentAsync()// 清除 A1:B2 範圍的所有評論await fRange.clearCommentsAsync()獲取評論回覆列表
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// 獲取工作表的所有評論const comments = fWorksheet.getComments()comments.forEach((comment) => { // 如果該評論是根評論,則獲取其回覆列表 if (comment.getIsRoot()) { const replies = comment.getReplies() replies.forEach((reply) => { console.log(reply.getCommentData()) }) }})更新/刪除/解決/回覆評論
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// 建立一個新的評論並新增到 A1 單元格const 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)// 3 秒後更新評論並回復setTimeout(async () => { // 更新評論 const comment = fWorksheet.getCommentById('mock-comment-id') const newRichText = univerAPI.newRichText().insertText('Hello Univer AI') await comment.updateAsync(newRichText) // 回覆評論 const replyText = univerAPI.newRichText().insertText('Hello Univer AI! GO! GO! GO!') const reply = univerAPI.newTheadComment().setContent(replyText) await comment.replyAsync(reply)}, 3000)// 6 秒後解決評論並刪除setTimeout(async () => { const comment = fWorksheet.getCommentById('mock-comment-id') await comment.resolveAsync() await comment.deleteAsync()}, 6000)事件監聽
完整事件型別定義,請檢視 Events。
評論模組提供了一系列事件用於監聽評論的新增、更新、刪除和解決狀態變更。所有事件都可以透過 univerAPI.addEvent() 進行監聽。
評論新增事件
univerAPI.Event.CommentAdded: 評論新增後觸發
const disposable = univerAPI.addEvent(univerAPI.Event.CommentAdded, (params) => { const { comment, workbook, worksheet, row, col } = params})// 移除事件監聽器,使用 `disposable.dispose()`univerAPI.Event.BeforeCommentAdd: 評論新增前觸發
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentAdd, (params) => { const { comment, workbook, worksheet, row, col } = params // 取消評論新增操作 params.cancel = true})// 移除事件監聽器,使用 `disposable.dispose()`評論更新事件
univerAPI.Event.CommentUpdated: 評論更新後觸發
const disposable = univerAPI.addEvent(univerAPI.Event.CommentUpdated, (params) => { const { comment, workbook, worksheet, row, col } = params})// 移除事件監聽器,使用 `disposable.dispose()`univerAPI.Event.BeforeCommentUpdate: 評論更新前觸發
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentUpdate, (params) => { const { comment, workbook, worksheet, row, col, newContent } = params // 取消評論更新操作 params.cancel = true})// 移除事件監聽器,使用 `disposable.dispose()`評論刪除事件
univerAPI.Event.CommentDeleted: 評論刪除後觸發
const disposable = univerAPI.addEvent(univerAPI.Event.CommentDeleted, (params) => { const { commentId, workbook, worksheet } = params})// 移除事件監聽器,使用 `disposable.dispose()`univerAPI.Event.BeforeCommentDelete: 評論刪除前觸發
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentDelete, (params) => { const { comment, workbook, worksheet, row, col } = params // 取消評論刪除操作 params.cancel = true})// 移除事件監聽器,使用 `disposable.dispose()`評論解決狀態事件
univerAPI.Event.CommentResolved: 評論解決狀態變更後觸發
const disposable = univerAPI.addEvent(univerAPI.Event.CommentResolved, (params) => { const { comment, row, col, resolved, workbook, worksheet } = params})// 移除事件監聽器,使用 `disposable.dispose()`univerAPI.Event.BeforeCommentResolve: 評論解決狀態變更前觸發
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentResolve, (params) => { const { comment, row, col, resolved, workbook, worksheet } = params // 取消評論解決操作 params.cancel = true})// 移除事件監聽器,使用 `disposable.dispose()`每個事件都包含以下通用參數:
workbook: 當前工作簿例項worksheet: 當前工作表例項row: 評論所在的行索引col: 評論所在的列索引comment: 評論物件(刪除後事件僅包含commentId)
特殊參數:
BeforeCommentUpdate事件包含newContent: 新的評論內容(RichTextValue 型別)CommentResolved和BeforeCommentResolve事件包含resolved: 評論的解決狀態
所有 Before 字首的事件回撥函式都可以返回 params.cancel = true 來阻止對應的操作。
通用評論操作
呼叫前同時匯入通用評論 Facade 與品類 Facade。品類方法建立穩定錨點,通用 API 負責回覆、修改、解決與刪除。getComments() 讀取本機狀態;listCommentsAsync() 透過已設定的資料來源同步已知討論串,不會探索伺服器上的全部討論串。
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,請由應用程式提供這些路由。服務端須獨立驗證評論讀寫權限,不能僅依賴文件編輯權限。
你覺得這篇文檔如何?