協同編輯
讓多位使用者編輯同一份 簡報,並即時看到彼此的修改。先完成協同整合,接入後端、身分驗證、權限與儲存,再設定授權。
設定與載入文件
參考 協同快速開始,為現有的 Slides 編輯器加入 Collaboration Client 設定。該範例使用 Sheets;你的後端必須建立並儲存 Slides 文件,再將其 ID 傳回瀏覽器。
先設定目前使用者,再載入文件;詳見協同整合。
TypeScript
import '@univerjs-pro/slides/facade'import { UserManagerService } from '@univerjs/core'import '@univerjs-pro/collaboration-client/facade'univer.__getInjector().get(UserManagerService).setCurrentUser({ userID, name, avatar })const document = await univerAPI.getCollaboration().loadSlideAsync('your-document-id')if (!document) throw new Error('Document could not be loaded')重新開啟或分享文件時,請使用相同的 ID。不要以建立本機空白文件取代載入伺服器文件。
透過同步狀態、重送與衝突復原實作儲存提示,並在重新整理前保護尚未確認的修改。
移除內建 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)註冊此外掛後,由外掛負責載入文件,無需再為同一份文件呼叫 loadSlideAsync()。如果應用程式已透過 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。在其中一個工作階段編輯文字或移動元素,並確認另一個收到修改。驗證重新整理頁面、重新連線與重啟服務後,內容仍然保留。使用檢視者與未授權帳號,確認伺服器會拒絕未獲允許的寫入與讀取。
你覺得這篇文檔如何?