import DataSource, { IWriteOptions } from "../editor-kernel/data-source.js"; import { HotkeyId } from "./hotkey.js"; import { HotkeyOptions, HotkeysEvent } from "../utils/hotkey/registerHotkey.js"; import { ILocaleKeys } from "./locale.js"; import { HistoryState, HistoryStateEntry } from "@lexical/history"; import { CommandListener, CommandListenerPriority, CommandPayloadType, EditorState, LexicalCommand, LexicalEditor, LexicalNode, LexicalNodeConfig } from "lexical"; type IDecoratorFunc = (_node: LexicalNode, _editor: LexicalEditor) => any; type INodeRegistrationTransform = (node: LexicalNodeConfig, index: number) => LexicalNodeConfig | null | undefined; type IBeforeEditorInitLifecycle = () => void; type IDecorator = { queryDOM: (_element: HTMLElement) => HTMLElement; render: IDecoratorFunc; } | IDecoratorFunc; /** * Service ID type */ type IServiceID = { readonly __serviceId: string; __serviceType?: Service; }; interface ISelectionObject { endNodeId: string; endOffset: number; startNodeId: string; startOffset: number; type: 'range' | 'node' | 'table'; } interface IDocumentOptions { [key: string]: unknown; keepHistory?: boolean; /** only work for json datasource */ keepId?: boolean; } interface IKernelEventMap { /** * Document change event */ documentChange: (type: string, content: any) => void; /** * Editor editable state change event */ editableChange: (editable: boolean) => void; /** * Editor error event */ error: (error: Error) => void; /** * Initialization event * @param editor Lexical editor instance * @returns */ initialized: (editor: LexicalEditor) => void; /** * handle markdown parse event */ markdownParse: (params: { cacheState: EditorState; historyState: HistoryStateEntry | null; markdown: string; matchedPatterns: string[]; score: number; }) => void; /** * handle paste event */ onPaste: (event: ClipboardEvent) => void; } /** * External API */ interface IEditor { /** * Lose focus */ blur(): void; /** * Clean editor content (clear all content) */ cleanDocument(): void; /** * Destroy editor instance */ destroy(): void; /** * Execute editor commands to manipulate editor content * @param type * @param payload */ dispatchCommand>(type: TCommand, payload: CommandPayloadType): boolean; /** * Focus editor */ focus(): void; /** * Get editor content of specified type */ getDocument(type: string, options?: IWriteOptions): DataSource | undefined; /** * Lexical history state (undo stack metadata) */ getHistoryState(): HistoryState; /** * Get Lexical editor instance */ getLexicalEditor(): LexicalEditor | null; /** * Get document editor root node */ getRootElement(): HTMLElement | null; /** * Get editor selection information */ getSelection(): ISelectionObject | null; /** * Get document editor selection content of specified type * @param type */ getSelectionDocument(type: string): unknown | null; /** * Get editor theme */ getTheme(): Record>; /** * Get headless editor instance */ initHeadlessEditor(): LexicalEditor | null; /** * Get node editor instance */ initNodeEditor(): LexicalEditor | null; /** * Check if editor is editable */ isEditable(): boolean; /** * Check if editor content is empty * @returns true if editor content is empty, false otherwise */ get isEmpty(): boolean; /** * Check if editor has active selection * @returns true if editor has selection, false otherwise */ get isSelected(): boolean; /** * Remove editor event listener * @param event * @param listener */ off(event: T, listener: IKernelEventMap[T]): this; /** * Add editor event listener * @param event * @param listener */ on(event: T, listener: IKernelEventMap[T]): this; /** * Listen to event once, automatically remove listener after trigger * @param event * @param listener */ once(event: T, listener: IKernelEventMap[T]): this; /** * Extends the priority level of Lexical commands. * Registers a listener that triggers when the provided command is dispatched * via {@link LexicalEditor.dispatch}. The listener is triggered based on its priority. * Listeners with higher priority can "intercept" commands and prevent them * from propagating to other handlers by returning true. * * Listeners are always invoked within {@link LexicalEditor.update} and can call dollar functions. * * Listeners registered at the same priority level will deterministically run * in the order of registration. * * @param command - The command that triggers the callback. * @param listener - The function executed when the command is dispatched. * @param priority - The relative priority of the listener. 0 | 1 | 2 | 3 | 4 * (or {@link COMMAND_PRIORITY_EDITOR} | * {@link COMMAND_PRIORITY_LOW} | * {@link COMMAND_PRIORITY_NORMAL} | * {@link COMMAND_PRIORITY_HIGH} | * {@link COMMAND_PRIORITY_CRITICAL}) * @returns A teardown function to clean up the listener. */ registerHighCommand

(command: LexicalCommand

, listener: CommandListener

, priority: CommandListenerPriority): () => void; /** * Register keyboard shortcut * @param hotkey * @param callback * @param options */ registerHotkey(hotkey: HotkeyId, callback: (event: KeyboardEvent, handler: HotkeysEvent) => void, options?: HotkeyOptions): () => void; /** * Register internationalization text * @param locale Internationalization text object */ registerLocale(locale: Partial>): void; /** * Register editor plugin */ registerPlugin(plugin: IEditorPluginConstructor, config?: T): IEditor; /** * Register multiple editor plugins */ registerPlugins(plugins: Array): IEditor; /** * Get editor Service, usually provided by plugins to extend certain functionalities * @param serviceId */ requireService(serviceId: IServiceID): T | null; /** * Set editor content, type is content type, content is content data * @param type * @param content */ setDocument(type: string, content: any, options?: IDocumentOptions): void; /** * Enable or disable editor editing capability * @param editable */ setEditable(editable: boolean): void; /** * Set document editor root node * @param dom */ setRootElement(dom: HTMLElement, editable?: boolean): LexicalEditor; setRootElement(dom: null, editable?: boolean): LexicalEditor | null; /** * set editor selection * @param selection */ setSelection(selection: ISelectionObject, opt?: { collapseToEnd?: boolean; collapseToStart?: boolean; }): Promise; /** * Get translation text * @param key Translation key * @param params Parameter replacement */ t(key: K, params?: Record): string; /** * Update editor theme * @param key * @param value */ updateTheme(key: string, value: string | Record): void; } /** * API provided to plugins */ interface IEditorKernel extends IEditor { /** * Clone the current editor kernel instance */ cloneNodeEditor(): IEditorKernel; emit(event: T, params: Parameters[0]): void; /** * Get editor Node decorator for specific Node rendering * @param name */ getDecorator(name: string): IDecorator | undefined; /** * Get all registered decorator names */ getRegisteredDecorators(): string[]; /** * Check if hot reload mode is enabled */ isHotReloadMode(): boolean; /** * Register a lifecycle hook that runs right before editor initialization. * @param hook * @returns unregister function */ registerBeforeEditorInit(hook: IBeforeEditorInitLifecycle): () => void; /** * Register data source for multi-format data conversion * @param dataSource */ registerDataSource(dataSource: DataSource): void; /** * Register editor node decorator * @param name * @param decorator */ registerDecorator(name: string, decorator: IDecorator): void; /** * Register a transform hook that runs before editor initialization * to rewrite registered nodes before createEditor/createHeadlessEditor is called. * @param transform * @returns unregister function */ registerNodeTransform(transform: INodeRegistrationTransform): () => void; /** * Register Lexical Node * @param nodes */ registerNodes(nodes: Array): void; /** * Register className(s) to the contenteditable root element. * @param className One or more class names separated by spaces * @returns unregister function */ registerRootClassName(className: string): () => void; /** * Register a callback for root attach/detach events. * * Call this after the kernel's Lexical editor has been initialized (usually * from plugin onInit). Lexical immediately invokes a newly registered root * listener with the current root, so listeners registered after the initial * setRootElement still receive the first root attach. Headless kernels * return a no-op cleanup because headless Lexical editors do not support * root listeners. */ registerRootListener(listener: (rootElement: HTMLElement | null) => void): () => void; /** * Register service * @param serviceId * @param service */ registerService(serviceId: IServiceID, service: T): void; /** * Register service with hot reload support - allows overriding existing services * @param serviceId Service identifier * @param service Service instance */ registerServiceHotReload(serviceId: IServiceID, service: T): void; /** * Register theme * @param themes */ registerThemes(themes: Record): void; /** * Enable or disable hot reload mode * @param enabled Whether to enable hot reload mode */ setHotReloadMode(enabled: boolean): void; /** * Unregister editor node decorator * @param name Decorator name */ unregisterDecorator(name: string): boolean; } /** * Plugin interface */ interface IEditorPlugin { config?: IConfig; /** * Editor destruction */ destroy(): void; /** * After editor document content is written by setDocument */ onDocumentChange?(type: string, content: any, options?: IDocumentOptions): void; /** * After Lexical editor instantiation * @param editor Lexical editor instance */ onInit?(editor: LexicalEditor): void; } /** * Plugin class interface */ interface IEditorPluginConstructor { readonly pluginName: string; new (kernel: IEditorKernel, config?: IConfig): IEditorPlugin; } type IPlugin = IEditorPluginConstructor | [IEditorPluginConstructor, T?]; export { IBeforeEditorInitLifecycle, IDecorator, IDecoratorFunc, IDocumentOptions, IEditor, IEditorKernel, IEditorPlugin, IEditorPluginConstructor, IKernelEventMap, INodeRegistrationTransform, IPlugin, ISelectionObject, IServiceID };