UNPKG

@veltdev/codemirror-crdt

Version:

CodeMirror CRDT library for Velt

106 lines (103 loc) 3.96 kB
import * as y_protocols_awareness from 'y-protocols/awareness'; import { Store, Version } from '@veltdev/crdt'; import * as Y from 'yjs'; import { Velt } from '@veltdev/types'; /** Configuration options for the VeltTipTapStore class */ interface VeltCodeMirrorStoreConfig { /** Unique document identifier for syncing */ editorId: string; /** Initial content for the editor */ initialContent?: string; /** Time in milliseconds to debounce updates */ debounceMs?: number; /** Velt client instance */ veltClient?: Velt; } /** * VeltCodeMirrorStore provides a wrapper around CodeMirror with real-time collaboration features. * It manages: * - Text synchronization using Yjs and database * - Presence awareness using CRDT core library * - Undo/redo functionality */ declare class VeltCodeMirrorStore { /** Store instance managing the document state */ private store; /** Awareness instance managing user presence and cursor positions */ /** Undo manager for the text document */ private undoManager; private veltClient; /** * Creates a new VeltCodeMirrorStore instance * @param {string} [docName='codemirror-local'] - Unique identifier for the document * @throws {Error} If the text type is not properly initialized */ constructor(config: VeltCodeMirrorStoreConfig); /** * Initializes the store and sets up synchronization with Firestore. * This must be called after constructing the VeltCodeMirrorStore instance. * @returns {Promise<void>} Resolves when initialization is complete * @throws {Error} If initialization fails or remote state cannot be loaded */ initialize(): Promise<void>; /** * Handles local awareness updates and syncs them to RTDB * @param {Object} params - Update parameters * @param {number[]} params.added - Client IDs that were added * @param {number[]} params.updated - Client IDs that were updated * @param {number[]} params.removed - Client IDs that were removed */ /** * Handles local awareness updates and syncs them to RTDB * @param {AwarenessUpdateParams} params - Update parameters * @throws {Error} If the update to RTDB fails */ private handleLocalAwarenessUpdate; /** * Sets up event listeners for awareness updates * - Listens for local awareness changes * - Listens for remote awareness updates from RTDB * - Handles cleanup on disconnect * @throws {Error} If RTDB operations fail */ private setupListeners; /** * Gets the underlying Store instance * @returns The Store instance managing the document state */ getStore(): Store<string>; /** * Gets the Y.Text instance from the store * @returns The Y.Text instance */ getYText(): Y.Text | null; /** * Gets the encoded state for version management * @returns Array representation of the Y.js document state */ getEncodedState(): number[]; /** * Sets the document state from a version * @param {Version} version - The version to restore * @throws {Error} If the version data is invalid * @returns {Promise<void>} Resolves when the state has been updated */ setStateFromVersion(version: Version): Promise<void>; /** * Gets the awareness instance managing user presence * @returns {Awareness} The awareness instance */ getAwareness(): y_protocols_awareness.Awareness; /** * Gets the underlying Yjs Document * @returns {Y.Doc} The Yjs Document instance */ getYDoc(): Y.Doc; /** * Gets the undo manager for the document * @returns {Y.UndoManager} The undo manager instance */ getUndoManager(): Y.UndoManager; } declare const createVeltCodeMirrorStore: (config: VeltCodeMirrorStoreConfig) => Promise<VeltCodeMirrorStore | null>; export { VeltCodeMirrorStore, createVeltCodeMirrorStore };