範例

按能力執行 Collaboration SDK 範例,查看各範例指南、服務端與 Web 入口。

所有範例都位於 univer-collaboration-examples 倉庫。各範例共用同一套最小的 web/main.ts + server/main.ts 結構,均可獨立執行。請先完成 Quick Start,再根據接入任務選擇對應的範例。

範例主要概念命令
快速開始核心協同流程pnpm example:quick-start
資料庫配接器內建與自定義 Adapterpnpm example:database-adapter
權限身分與授權pnpm example:permissions
歷史衍生版本歷史pnpm example:history
留言獨立評論pnpm example:comments
Worktree隔離編輯與合併pnpm example:worktree
檔案服務檔案儲存pnpm example:file-service
檔案轉換Office 檔案轉換pnpm example:exchange
All Unit全部能力組合pnpm example:all-unit
多節點部署水平擴展pnpm example:multi-node

快速開始

組裝 Collaboration Client、Transport、Endpoint、Service 與 Memory Adapter。在兩個瀏覽器中開啟同一個 Sheet,可以驗證 HTTP 載入、WebSocket Session、Room、ACK 與廣播。

Bash
pnpm example:quick-start

開啟 http://127.0.0.1:3010/?unit=quick-start-sheet。

資料庫配接器

先使用內建的 SQLite Adapter,再更換為 PostgreSQL Adapter 或自定義 Adapter。

Bash
pnpm example:database-adapter

開啟 http://127.0.0.1:3010/?unit=persistent-sheet。

權限

分別以 Bob(編輯者)、Casey(檢視者)和 Dana(沒有文件權限)登入,比較每個帳號可讀取或修改的內容。Alice 是文件建立者。伺服器分別檢查讀取、加入房間和提交修改的權限。

Bash
pnpm example:permissions

開啟 http://127.0.0.1:3010。

歷史

在 Core 之外加入 History Service、獨立的 Adapter、History Endpoint 與瀏覽器歷史入口。Unit 建立與 changeset commit 兩個 Event 將 History 建立為可重建的派生索引。

Bash
pnpm example:history

開啟 http://127.0.0.1:3010/?unit=history-sheet,編輯 Sheet,然後選擇頁面上方的 History。

留言

加入 Thread Comment Service、獨立的 Adapter、Comment Endpoint 與前端評論入口。Comment Endpoint 複用主協同 Endpoint 的 Session 與 Unit Room。

Bash
pnpm example:comments

開啟 http://127.0.0.1:3010/?unit=comments-sheet,選中一個單元格,然後使用評論入口。

Worktree

完整演示獨立 draft、Ready、Reopen 與逐 Unit Merge 的生命週期。在 trunk 與 draft 之間切換,可以觀察到 Agent 的修改在合入前始終保持隔離。

Bash
pnpm example:worktree

開啟 http://127.0.0.1:3010/?unit=worktree-sheet&worktree=demo-worktree。

檔案服務

接入 File Service 與 File Endpoint。插入 Sheet 的圖片會上傳到伺服器端,Unit 資料只記錄檔案 ID。

Bash
pnpm example:file-service

開啟 http://127.0.0.1:3010/?unit=file-service-sheet,透過 Insert → Image → Float Image 插入圖片。

檔案轉換

組裝 Exchange Service 與 Exchange Endpoint,用於伺服器端 Sheet 匯入匯出、任務輪詢以及按已確認 revision 匯出。

Bash
pnpm example:exchange

開啟 http://127.0.0.1:3010/?unit=exchange-sheet,使用編輯器上方的 Import / Export 工具欄匯入和匯出。

All Unit

將六種 Unit、檔案、匯入匯出、嵌入附件、評論、歷史與列印組合在同一個應用中。

Bash
pnpm example:all-unit

開啟 http://127.0.0.1:3010,在側邊欄切換 Unit。

多節點部署

在 Nginx 後執行兩個協同節點。Redis 在節點之間轉發即時更新並共享 Session Ticket,因此任意節點都能處理任意請求。執行本範例還需要 Docker Compose 和一個覆蓋兩個節點的許可證。

Bash
pnpm example:multi-node

開啟 http://127.0.0.1:3010/,選擇 New Sheet 建立檔案。將完整 URL 複製到另一個標籤頁,驗證同一份文件的協同編輯。再建立若干檔案,觀察它們在不同節點上的分配情況。

範例不是生產應用

固定使用者、演示授權、Memory Adapter、本地金鑰和記憶體任務儲存只用於教學。身分、權限、持久化、金鑰、檔案、部署與產品互動仍由應用負責。

你覺得這篇文檔如何?

© 2026 DreamNum Co., Ltd.