協同編輯
多個使用者可以開啟同一個 Base,共同編輯表、欄位、記錄和檢視。每個使用者可以在不同的檢視中工作,所有改動都落在同一份文件上。
整合步驟
- 先按協同整合執行協同範例,再接入登入、權限和資料庫儲存。
- 在現有的 Bases 編輯器中加入 Collaboration Client 配置。瀏覽器端參照協同快速開始,但建立和儲存的文件型別必須是 Bases,不能直接套用範例中的 Sheets 資料。
- Base 文件由業務後端建立,後端再把返回的 ID 交給瀏覽器載入。
開啟伺服器上的 Base
載入文件前需設定當前使用者,詳見協同整合。
TypeScript
import '@univerjs-pro/bases/facade'import { UserManagerService } from '@univerjs/core'import '@univerjs-pro/collaboration-client/facade'univer.__getInjector().get(UserManagerService).setCurrentUser({ userID, name, avatar })const base = await univerAPI.getCollaboration().loadBaseAsync('your-base-id')if (!base) throw new Error('Base could not be loaded')重新開啟或分享文件時沿用同一個 ID。不要用 createBase() 新建本地空白文件來代替伺服器端載入。
儲存提示可以基於同步狀態、重發與衝突恢復實現,重新整理前要保護尚未確認的修改。
移除內建 URL 載入協同文件
注意事項
從 1.2.0 版本開始,Univer 不再內建協同文件的 URL 載入,不再讀取 URL 中的 unit 和 type 參數來決定載入哪個文件。
如果應用依賴此行為,可以用自定義外掛將其恢復。
接入自訂外掛
將下面四個檔案按以下目錄結構複製到專案中:
text
custom-url-load/├── plugin.ts├── controllers/│ └── data-loader.controller.ts└── services/ ├── url.service.ts └── web-url.service.ts編輯器和協同外掛初始化完成後,先設定當前使用者,再註冊 CustomUrlLoadPlugin。
TypeScript
import { UserManagerService } from '@univerjs/core'import { CustomUrlLoadPlugin } from './custom-url-load/plugin'univer.__getInjector().get(UserManagerService).setCurrentUser({ userID, name, avatar })univer.registerPlugin(CustomUrlLoadPlugin)註冊後由外掛負責載入文件,同一份文件不需要再呼叫 loadBaseAsync()。如果應用已經透過 Facade 載入文件,沿用上一節的做法,URL 的處理方式按業務需要決定。
完整外掛程式碼
TypeScript
import { UniverCollaborationClientPlugin } from '@univerjs-pro/collaboration-client'import { DependentOn, Inject, Injector, Plugin } from '@univerjs/core'import { CollaborationDataLoaderController } from './controllers/data-loader.controller'import { IURLService } from './services/url.service'import { WebURLService } from './services/web-url.service'@DependentOn(UniverCollaborationClientPlugin)export class CustomUrlLoadPlugin extends Plugin { static override pluginName = 'CUSTOM_URL_LOAD_PLUGIN' constructor( _config: undefined, @Inject(Injector) protected override readonly _injector: Injector ) { super() } override onStarting(): void { this._injector.add([IURLService, { useClass: WebURLService }]) this._injector.add([CollaborationDataLoaderController]) this.disposeWithMe(this._injector.get(IURLService)) this.disposeWithMe(this._injector.get(CollaborationDataLoaderController)) }}TypeScript
import { DataLoaderService } from '@univerjs-pro/collaboration-client'import { ILogService, Inject, RxDisposable } from '@univerjs/core'import { IURLService } from '../services/url.service'/** * 透過 URL 參數載入協同文件。 */export class CollaborationDataLoaderController extends RxDisposable { constructor( @IURLService private readonly _urlService: IURLService, @ILogService private readonly _logService: ILogService, @Inject(DataLoaderService) private readonly _dataLoaderService: DataLoaderService ) { super() const unitId = this._urlService.getParam('unit') const type = this._urlService.getParam('type') if (!unitId || !type) { return } this._dataLoaderService.loadUnit(unitId, Number(type)) .catch((error: unknown) => { this._logService.error('[CollaborationDataLoaderController]', error) }) }}TypeScript
import type { IDisposable } from '@univerjs/core'import type { Observable } from 'rxjs'import { createIdentifier } from '@univerjs/core'export interface IURLService extends IDisposable { getParam: (key: string) => string | undefined setParam: (key: string, value: string, useReplace?: boolean) => void removeParam: (key: string, useReplace?: boolean) => void readonly urlChange$: Observable<void>}export const IURLService = createIdentifier<IURLService>('demo.custom-url-load.url-service')TypeScript
import type { IURLService } from './url.service'import { RxDisposable } from '@univerjs/core'import { fromEvent, map, shareReplay, takeUntil } from 'rxjs'export class WebURLService extends RxDisposable implements IURLService { readonly urlChange$ = fromEvent(window, 'popstate').pipe( map(() => undefined), takeUntil(this.dispose$), shareReplay({ bufferSize: 1, refCount: true }) ) getParam(key: string): string | undefined { return new URL(window.location.href).searchParams.get(key) ?? undefined } setParam(key: string, value: string, useReplace = false): void { const url = new URL(window.location.href) url.searchParams.set(key, value) this._updateUrl(url, useReplace) } removeParam(key: string, useReplace = false): void { const url = new URL(window.location.href) url.searchParams.delete(key) this._updateUrl(url, useReplace) } private _updateUrl(url: URL, useReplace: boolean): void { if (useReplace) { window.history.replaceState(window.history.state, '', url.toString()) } else { window.history.pushState(window.history.state, '', url.toString()) } }}驗證同步與儲存
用兩個獨立的瀏覽器會話開啟同一個 ID,在一個會話中新增記錄或修改欄位,確認另一個會話能收到改動。再重新整理頁面並重啟服務,確認改動仍然存在。
檢視者和無權限使用者必須由伺服器端校驗。本地只讀設定隻影響編輯器的行為。匯入匯出、評論和歷史記錄各自需要相應的後端能力,見匯入匯出、評論和歷史記錄。
你覺得這篇文檔如何?