diff --git a/packages/yjs/package.json b/packages/yjs/package.json index 178bb93d07..794711a484 100644 --- a/packages/yjs/package.json +++ b/packages/yjs/package.json @@ -62,6 +62,7 @@ "platejs": ">=49.2.21", "react": ">=18.0.0", "react-dom": ">=18.0.0", + "y-indexeddb": ">=9.0.12", "y-webrtc": "10.3.0" }, "publishConfig": { diff --git a/packages/yjs/src/lib/providers/indexeddb-provider.ts b/packages/yjs/src/lib/providers/indexeddb-provider.ts new file mode 100644 index 0000000000..d2fdd7b3f4 --- /dev/null +++ b/packages/yjs/src/lib/providers/indexeddb-provider.ts @@ -0,0 +1,37 @@ +import type { Awareness } from 'y-protocols/awareness'; +import type * as Y from 'yjs'; + +import { IndexeddbPersistence } from 'y-indexeddb'; + +import type { IndexeddbProviderConfig, UnifiedProvider } from './types'; + +export class IndexeddbProviderWrapper implements UnifiedProvider { + public _synced = false; + public readonly awareness: Awareness; + connect = () => { + return; + }; + destroy = () => { + this.provider.destroy(); + }; + disconnect = () => { + return; + }; + public readonly document: Y.Doc; + public readonly provider: IndexeddbPersistence; + public readonly type = 'indexeddb'; + constructor({ options }: IndexeddbProviderConfig) { + this.provider = new IndexeddbPersistence(options.docName, options.ydoc); + this.document = this.provider.ydoc; + this.provider.once('synced', () => { + this._synced = true; + }); + this.awareness = options.awareness; + } + get isConnected() { + return this._synced; + } + get isSynced() { + return this._synced; + } +} diff --git a/packages/yjs/src/lib/providers/types.ts b/packages/yjs/src/lib/providers/types.ts index 695cbe554e..57cde68659 100644 --- a/packages/yjs/src/lib/providers/types.ts +++ b/packages/yjs/src/lib/providers/types.ts @@ -94,6 +94,16 @@ export type WebRTCProviderOptions = { signaling?: string[]; }; +export type IndexeddbProviderConfiguration = { + docName: string; + ydoc?: Y.Doc; +}; + +export type IndexeddbProviderConfig = BaseYjsProviderConfig & { + options: IndexeddbProviderConfiguration; + type: 'indexeddb'; +}; + export type YjsConfig = PluginConfig< 'yjs', { @@ -138,7 +148,10 @@ export type YjsConfig = PluginConfig< >; // Union type for all known provider configurations -export type YjsProviderConfig = HocuspocusProviderConfig | WebRTCProviderConfig; // Add custom config types here if needed +export type YjsProviderConfig = + | HocuspocusProviderConfig + | IndexeddbProviderConfig + | WebRTCProviderConfig; // Add custom config types here if needed // Extensible provider type that can include custom types export type YjsProviderType = DefaultYjsProviderType | string; diff --git a/yarn.lock b/yarn.lock index 513ece0674..09fb50be70 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3801,6 +3801,7 @@ __metadata: platejs: ">=49.2.21" react: ">=18.0.0" react-dom: ">=18.0.0" + y-indexeddb: ">=9.0.12" y-webrtc: 10.3.0 languageName: unknown linkType: soft