留言
評論可用於討論任務、訂單等記錄。評論始終與對應記錄繫結,在網格、看板、畫廊、日曆和甘特圖檢視中都指向同一份討論,切換檢視不會新建討論。
註冊外掛
在 Bases 核心外掛和 UI 外掛之後註冊以下外掛:
pnpm add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs-pro/bases-thread-comment @univerjs-pro/bases-thread-comment-uinpm install @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs-pro/bases-thread-comment @univerjs-pro/bases-thread-comment-uiyarn add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs-pro/bases-thread-comment @univerjs-pro/bases-thread-comment-uibun add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs-pro/bases-thread-comment @univerjs-pro/bases-thread-comment-uiimport { UniverBasesThreadCommentPlugin } from '@univerjs-pro/bases-thread-comment'import { UniverBasesThreadCommentUIPlugin } from '@univerjs-pro/bases-thread-comment-ui'import ProductLocale from '@univerjs-pro/bases-thread-comment-ui/locale/zh-TW'import { LocaleType, mergeLocales } from '@univerjs/core'import SharedLocale from '@univerjs/thread-comment-ui/locale/zh-TW'import '@univerjs-pro/bases-thread-comment/facade'import '@univerjs/thread-comment/facade'import '@univerjs/thread-comment-ui/lib/index.css'import '@univerjs-pro/bases-thread-comment-ui/lib/index.css'univerAPI.loadLocales(LocaleType.ZH_TW, mergeLocales(SharedLocale, ProductLocale))univer.registerPlugin(UniverBasesThreadCommentPlugin)univer.registerPlugin(UniverBasesThreadCommentUIPlugin)合併評論相關包提供的語言資源,併為應用配置當前使用者。UI 外掛為記錄新增評論入口,資料外掛負責將評論與記錄關聯起來。
新增與讀取留言
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。
儲存與共用討論
應用呼叫 base.save() 時,本地建立的評論會隨 Base 資源一同儲存。恢復快照前應先註冊評論外掛。
多人共享評論還需要評論服務、資料來源和權限。僅有文件協同並不會連線評論,應先按 Office 協同擴充套件模組配置後端,再用兩個賬號驗證建立、讀取、權限和重新開啟等操作。
通用評論操作
呼叫這些方法前,需要匯入通用評論 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,這些路由需要由應用提供。伺服器端必須單獨控制評論的讀取和寫入權限,不能只依據文件編輯權限。
你覺得這篇文檔如何?