協同擴充模組
瞭解 History、Thread Comment、Worktree、File 和服務端 Office 檔案轉換如何擴充核心協同。
History、Thread Comment、File 與 Worktree 是可選的協同擴充套件模組,可以在不修改核心的前提下擴充套件協同能力。每個模組都提供自己的 Service、Middleware、Event、Database Adapter 和生命週期邊界,應用可以只啟用產品需要的模組。這些模組仍然可以共用 Transport、身分系統和同一個物理資料庫。
伺服器端 Office Exchange 建立在 File 與 Collaboration Service 之上。它負責在 Office 檔案和協同 Unit 之間相互轉換,並擁有自己的 Service、Endpoint 與 Middleware。
模組關係
| 擴充模組 | 與核心協同的關係 |
|---|---|
| History | 監聽 Core Event,生成一份派生的歷史索引 |
| Thread Comment | 複用 Core Unit Room;評論 anchor 仍屬於 Core 內容 |
| File | 儲存 Unit 引用的圖片、附件和匯入匯出檔案,Unit 資料只記錄檔案 ID |
| Office Exchange | 透過核心 Service 建立或讀取 Unit,透過 File 讀寫檔案 |
| Worktree | 複用 Core 的 Unit 與 OT 能力,把 draft 合入 trunk |
History、Thread Comment、File 與 Worktree 各自配置自己的 Middleware、Event 和 Database Adapter。
版本歷史
History 將 Unit 建立和 confirmed changeset 整理為使用者可見的歷史條目。歷史索引是派生資料:Unit 內容的權威來源始終是 Core Collaboration Service 中已確認的 changeset。
const historyService = new UniverHistoryService({ collabService, dbAdapter: historyDatabase, userProvider,});transport.register(new UniverHistoryEndpoint(historyService));建構函式會訂閱 Core Service 的 unitCreated 和 changesetCommitted Event 並據此更新索引,dispose() 負責解除這些訂閱。預設策略按時間視窗和特殊 mutation 歸組,不會為每條 changeset 都生成歷史條目。
History 提供 getHistoryList、listHistoryCreators 和 getHistoryChangesets 三個讀取 Middleware,索引工作在 Service 內部完成。userProvider 只補全姓名、頭像這類展示資訊,既不參與認證,也不參與授權。Event Listener 出錯不會回滾已經確認的 Core changeset,因此 History 應視為可以隨時重建的派生資料。
討論串留言
Comment Service 管理留言本文、回覆、編輯、刪除及解決狀態。
const commentService = new UniverCommentService({ database: commentDatabase, userProvider,});transport.register( new UniverCommentEndpoint({ service: commentService, roomHost: collabEndpoint, }),);評論正文屬於 Comment Adapter,隨 Sheet 或 Doc 內容移動的 anchor 仍然屬於 Core snapshot 和 changeset。Comment Endpoint 可以透過主 Collaboration Endpoint 的 Unit Room 釋出 comment_update,但這條即時訊息不是評論狀態的權威來源;Client 錯過該訊息後重新取得評論列表即可恢復。
Comment Service 提供 add、list、reply、solve/reopen、edit 和 delete 的 Middleware,併釋出一個 commentCommitted Event。Comment Service 不繼承 Core Service 的讀取策略或編輯策略。
檔案儲存
File 儲存 Univer 客戶端上傳的二進位制檔案,包括插入 Unit 的圖片和附件,以及匯入匯出的原始檔和結果檔案。Unit 資料只記錄檔案 ID,客戶端在載入 Unit 時用該 ID 換取下載地址,因此 snapshot 和 changeset 保持較小的體積。
const fileService = new UniverFileService({ database: fileDatabase, blobStore,});transport.register(new UniverFileEndpoint(fileService));後設資料和內容是兩套獨立的合同:IFileDatabaseAdapter 儲存檔案記錄,IFileBlobStore 儲存內容。匯入匯出的臨時檔案只有上傳者本人可以訪問,由 Service 定期清理。Unit 內的圖片和附件受 Unit 權限控制,由 upload、signUrl、download 三個 Middleware 執行校驗。
服務端 Office 檔案轉換
Exchange Service 將 Office 檔案匯入為協同 Unit,也將 Unit 匯出為檔案。轉換由 @univerjs-pro/exchange-node 完成,Unit 的建立和讀取透過 Collaboration Service 進行,原始檔和結果檔案透過 File Service 儲存:
const exchangeService = new UniverExchangeService({ collabService, fileService });transport.register(new UniverExchangeEndpoint(exchangeService));匯入和匯出都是 task:客戶端先建立 task,再輪詢結果。匯入經過 Core Service 的 createUnit Middleware,匯出經過 readUnitData;Exchange 自己的 import 和 export Middleware 負責限流、配額和審計。
| Unit | 匯入 | 匯出 |
|---|---|---|
| Sheet | xls, xlsx, csv, tsv | xlsx, csv, tsv |
| Doc | doc, docx | docx |
| Slide | ppt, pptx | pptx |
| Base | xls, xlsx, csv, tsv | xlsx, csv |
Worktree 草稿與合併
Worktree 為一個或多個 Unit 提供隔離的 draft、凍結、合入評估和逐 Unit 合併:
create → draft → ready → merging → merged ↑ │ └ reopen┘draft / ready → discardedconst worktreeService = new UniverCollabWorktreeService({ trunk: { service: collabService, dbAdapter: coreDatabase, }, dbAdapter: worktreeDatabase,});Worktree 按 (worktreeID, unitID) 儲存自己的 draft changeset,同時複用 Core Service 的 Unit、OT 和提交引擎。只有 draft 狀態可以繼續編輯。markReady() 凍結當前的 draft revision,mergeWorktree() 將 Unit 逐個合入 trunk。
Worktree Service 擁有獨立的生命週期、讀取 Middleware 和 draft submit/apply/commit Middleware,以及建立、狀態變化、合入結果和 draft changeset 提交這幾類 Event。最終寫入 trunk 時,仍然經過 Core Service 自己的 Middleware。
多個 Unit 一起合併時,跨 Unit 不具備原子性。應用應展示每個 Unit 的合入結果。
獨立擴充點
啟用擴充套件模組時,每一項邊界都需要單獨評估:
| 邊界 | 核心 | History | Comment | File | Worktree |
|---|---|---|---|---|---|
| Service | 獨立 | 獨立 | 獨立 | 獨立 | 獨立 |
| Middleware | 不共享 | 不繼承核心 | 不繼承核心 | 不繼承核心 | 不繼承核心 |
| Event | 核心生命週期 | 觀察核心 Events | commentCommitted | — | Worktree 生命週期/提交 |
| Database Adapter | 核心 Adapter | History Adapter | Comment Adapter | File Adapter 與 Blob Store | Worktree Adapter |
Office Exchange 複用 Core 與 File 的儲存。在 Exchange 自己的 import、export Middleware 執行之後,匯入匯出仍然經過 Core 與 File 的 Middleware。
這些 Adapter 可以共用同一個資料庫,但仍然各自是獨立的物件,擁有獨立的合同。Service 和 Adapter 也應分別釋放。
選擇能力
| 產品需求 | 模組 | 入門範例 |
|---|---|---|
| 使用者可見的版本歷史 | History Service、Endpoint、Adapter | pnpm example:history |
| Sheet 或 Doc 留言 | Comment Service、Endpoint、Adapter | pnpm example:comments |
| 圖片、附件與檔案儲存 | File Service、Endpoint、Adapter、Blob Store | pnpm example:file-service |
| 服務端 Office 匯入與匯出 | Exchange Service、Endpoint + File | pnpm example:exchange |
| 隔離草稿、審閱與合併 | Worktree Service、Endpoint、Client、Adapter | pnpm example:worktree |
完整的執行命令和頁面地址見範例索引。
連接 Web SDK 歷史介面
將 History Endpoint 與協同端點一同掛載。Web SDK 的 historyServerUrl 是 /{unitId}/list、/{unitId}/creators 和 /{unitId}/cs 的共同字首,預設值為 /universer-api/history。如果使用其他字首,宿主 History 外掛與品類 History UI 外掛必須配置相同的地址。
恢復版本透過協同 mutation 路徑完成,沒有單獨的歷史恢復 REST 介面。協同的載入、提交與授權配置必須保持可用。使用 createUnitComparisonEngine() 比較兩份完整快照是另一項獨立操作,該操作只讀,不需要歷史服務執行,也不負責儲存或恢復版本。
你覺得這篇文檔如何?