|
| 1 | +import type FluentEditor from '../../fluent-editor' |
| 2 | +import type { YjsOptions } from './types' |
| 3 | +import { Awareness } from 'y-protocols/awareness' |
| 4 | +import { QuillBinding } from 'y-quill' |
| 5 | +import * as Y from 'yjs' |
| 6 | +import { setupAwareness } from './awareness' |
| 7 | +import { setupIndexedDB } from './awareness/y-indexeddb' |
| 8 | +import { createProvider } from './provider/customProvider' |
| 9 | + |
| 10 | +export class CollaborativeEditor { |
| 11 | + private ydoc: Y.Doc = new Y.Doc() |
| 12 | + private provider: any |
| 13 | + private awareness: Awareness |
| 14 | + private _isConnected = false // 插件级别 |
| 15 | + private _isSynced = false |
| 16 | + |
| 17 | + constructor( |
| 18 | + public quill: FluentEditor, |
| 19 | + private options: YjsOptions, |
| 20 | + ) { |
| 21 | + this.ydoc = this.options.ydoc || new Y.Doc() |
| 22 | + |
| 23 | + if (this.options.awareness) { |
| 24 | + this.awareness = setupAwareness(this.options.awareness, new Awareness(this.ydoc)) |
| 25 | + } |
| 26 | + |
| 27 | + if (this.options.provider) { |
| 28 | + const providerConfig = this.options.provider |
| 29 | + try { |
| 30 | + // Create provider with shared handlers, Y.Doc, and Awareness |
| 31 | + const provider = createProvider({ |
| 32 | + doc: this.ydoc, |
| 33 | + options: providerConfig.options, |
| 34 | + type: providerConfig.type, |
| 35 | + awareness: this.awareness, |
| 36 | + onConnect: () => { |
| 37 | + this._isConnected = true |
| 38 | + this.options.onConnect?.() |
| 39 | + }, |
| 40 | + onDisconnect: () => { |
| 41 | + this._isConnected = false |
| 42 | + this.options.onDisconnect?.() |
| 43 | + }, |
| 44 | + onError: (error) => { |
| 45 | + this.options.onError?.(error) |
| 46 | + }, |
| 47 | + onSyncChange: (isSynced) => { |
| 48 | + this._isSynced = isSynced |
| 49 | + this.options.onSyncChange?.(isSynced) |
| 50 | + }, |
| 51 | + }) |
| 52 | + this.provider = provider |
| 53 | + } |
| 54 | + catch (error) { |
| 55 | + console.warn( |
| 56 | + `[yjs] Error creating provider of type ${providerConfig.type}:`, |
| 57 | + error, |
| 58 | + ) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (this.provider) { |
| 63 | + const ytext = this.ydoc.getText('tiny-editor') |
| 64 | + new QuillBinding( |
| 65 | + ytext, |
| 66 | + this.quill, |
| 67 | + this.awareness, |
| 68 | + ) |
| 69 | + } |
| 70 | + else { |
| 71 | + console.error('Failed to initialize collaborative editor: no valid provider configured') |
| 72 | + } |
| 73 | + |
| 74 | + if (this.options.offline) { |
| 75 | + setupIndexedDB(this.ydoc, typeof this.options.offline === 'object' ? this.options.offline : undefined) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public getAwareness(): Awareness { |
| 80 | + return this.awareness |
| 81 | + } |
| 82 | + |
| 83 | + public getProvider() { |
| 84 | + return this.provider |
| 85 | + } |
| 86 | + |
| 87 | + public getYDoc() { |
| 88 | + return this.ydoc |
| 89 | + } |
| 90 | + |
| 91 | + get isConnected() { |
| 92 | + return this._isConnected |
| 93 | + } |
| 94 | + |
| 95 | + get isSynced() { |
| 96 | + return this._isSynced |
| 97 | + } |
| 98 | +} |
0 commit comments