import { StandardSchemaV1 } from '@standard-schema/spec'; import { DocNode, Fragment, Selection } from './doc/types.js'; import { Operation } from './doc/operation.js'; import { Parser } from './dom/parser.js'; type EditorCommandOrPlugin = (editor: Editor, ...args: A) => void | undefined; type EditorQuery = (editor: Editor, ...args: A) => V; /** * Options of {@link createEditor}. */ export interface EditorOptions | void = void> { /** * Optional [Standard Schema](https://github.com/standard-schema/standard-schema) to validate unsafe edits. */ schema?: S; /** * Initial document. */ doc: T; /** * The state editable or not. */ readonly?: boolean; /** * TODO */ isBlock?: (node: HTMLElement) => boolean; /** * Callback invoked when errors happen. * * @default console.warn */ onWarn?: (message: string) => void; /** * Callback invoked when errors happen. * * @default `throw new Error(message)` */ onError?: (message: string) => never; } export type EditorContext<_> = {}; type EditorEventMap = { change: () => void; selectionchange: () => void; readonly: () => void; }; /** * Functions to handle keyboard events. * * Return `true` if you want to stop propagation. */ export type KeyboardHook = (keyboard: KeyboardEvent) => boolean | void; /** * Functions to handle copy events */ export type CopyHook = (dataTransfer: DataTransfer) => void; /** * Functions to handle paste / drop events. * * Return `true` if you want to stop propagation. */ export type PasteHook = (dataTransfer: DataTransfer) => string | Fragment | true | null; type EditorHookMap = { /** * Call `next(op)` to continue applying the operation, or `next()` with a nullish value to cancel it with its transaction. * `next` returns the operation applied to the document after the following hooks, or null if it was cancelled or failed to apply. The transaction may still be discarded afterwards. * * If the hook returns without calling `next`, the operation is passed through as is. */ apply: (op: Operation, next: (op?: Operation | null) => Operation | null) => void; mount: (element: HTMLElement, parser: Parser) => void | (() => void); keyboard: KeyboardHook; copy: CopyHook; paste: PasteHook; }; /** * The editor instance. */ export interface Editor { readonly doc: T; selection: Selection; /** * The getter/setter for the editor's read-only state. * `true` to read-only. `false` to editable. */ readonly: boolean; /** * Applies editing operations as a transaction, which is discarded if one of them is cancelled or throws, or the result is rejected by the schema. * @param op {@link Operation} */ apply(op: Operation | readonly Operation[]): this; /** * Executes a function with editor bound as context. * @param fn {@link EditorCommandOrPlugin} or {@link EditorQuery} * @param args arguments of the function */ exec(fn: EditorCommandOrPlugin, ...args: A): this; exec(fn: EditorQuery, ...args: A): V; /** * A function to subscribe editor events. * @returns cleanup function */ on(key: K, callback: EditorEventMap[K]): () => void; /** * A function to register editor hooks. * @returns cleanup function */ hook(key: K, callback: EditorHookMap[K]): () => void; /** * Get a value from the context. */ get(key: EditorContext): V; /** * Set a value for the context. */ set(key: EditorContext, value: V): this; /** * A function to make DOM editable. * @returns A function to stop subscribing DOM changes and restores previous DOM state. */ input: (element: HTMLElement) => () => void; } /** * A function to initialize {@link Editor}. */ export declare const createEditor: | void = void>({ doc, readonly, schema, isBlock, onWarn, onError, }: EditorOptions) => Editor; export {};