示例

按能力运行 Collaboration SDK 示例,并直达每个示例的说明、Server 与 Web 入口源码。

所有示例都位于 univer-collaboration-examples 仓库。各示例共用同一套最小的 web/main.ts + server/main.ts 结构,均可独立运行。请先完成 Quick Start,再根据接入任务选择对应的示例。

示例重点概念运行命令
Quick Start核心协同链路pnpm example:quick-start
Database Adapter内置与自定义 Adapterpnpm example:database-adapter
Permissions身份与权限pnpm example:permissions
History派生版本历史pnpm example:history
Comments独立评论pnpm example:comments
Worktree隔离编辑与合入pnpm example:worktree
File Service文件存储pnpm example:file-service
ExchangeOffice 文件交换pnpm example:exchange
All Unit全部能力组合pnpm example:all-unit
Multi-node水平扩展pnpm example:multi-node

Quick Start

组装 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。

Database Adapter

先使用内置的 SQLite Adapter,再更换为 PostgreSQL Adapter 或自定义 Adapter。

Bash
pnpm example:database-adapter

打开 http://127.0.0.1:3010/?unit=persistent-sheet。

Permissions

分别以 Bob(编辑者)、Casey(查看者)和 Dana(无文档权限)登录,比较每个账号能读取或修改的内容。Alice 是文档创建者。服务端分别检查读取、加入房间和提交修改的权限。

Bash
pnpm example:permissions

打开 http://127.0.0.1:3010。

History

在 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。

Comments

加入 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 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

组装 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。

Multi-node

在 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.