import { BaseEditor, Editor, EditorWordOptions, Node, Path, Point, Range } from 'slate'; import { TextDiff } from '../utils/diff-text'; import { Key } from '../utils/key'; type DOMNode = globalThis.Node; type DOMRange = globalThis.Range; type DOMSelection = globalThis.Selection; type DOMStaticRange = globalThis.StaticRange; type DOMPoint = [globalThis.Node, number]; /** * A DOM-specific version of the `Editor` interface. */ export interface DOMEditor extends BaseEditor { hasEditableTarget: (editor: Editor, target: EventTarget | null) => target is DOMNode; hasSelectableTarget: (editor: Editor, target: EventTarget | null) => boolean; hasTarget: (editor: Editor, target: EventTarget | null) => target is DOMNode; insertData: (data: DataTransfer) => void; insertFragmentData: (data: DataTransfer) => boolean; insertTextData: (data: DataTransfer) => boolean; isTargetInsideNonReadonlyVoid: (editor: Editor, target: EventTarget | null) => boolean; onDOMError: (error: unknown) => void; setFragmentData: (data: DataTransfer, originEvent?: "drag" | "copy" | "cut") => void; } export interface DOMEditorInterface { /** * Experimental and android specific: Get pending diffs */ androidPendingDiffs: (editor: Editor) => TextDiff[] | undefined; /** * Experimental and android specific: Flush all pending diffs and cancel composition at the next possible time. */ androidScheduleFlush: (editor: Editor) => void; /** * Blur the editor. */ blur: (editor: Editor) => void; /** * Deselect the editor. */ deselect: (editor: Editor) => void; /** * Find the DOM node that implements DocumentOrShadowRoot for the editor. */ findDocumentOrShadowRoot: (editor: Editor) => Document | ShadowRoot; /** * Get the target range from a DOM `event`. * * The range is resolved from the event's coordinates, which can point * outside of the editor even when the event's target lies inside it. With * `suppressThrow`, `null` is returned instead of throwing in that case. */ findEventRange: (editor: Editor, event: any, options?: { suppressThrow?: T; }) => T extends true ? Range | null : Range; /** * Find a key for a Slate node. */ findKey: (editor: Editor, node: Node) => Key; /** * Find the path of Slate node. */ findPath: (editor: Editor, node: Node) => Path; /** * Focus the editor. */ focus: (editor: Editor, options?: { retries: number; }) => void; /** * Return the host window of the current editor. */ getWindow: (editor: Editor) => Window; /** * Check if a DOM node is within the editor. */ hasDOMNode: (editor: Editor, target: DOMNode, options?: { editable?: boolean; }) => boolean; /** * Check if the target is editable and in the editor. */ hasEditableTarget: (editor: Editor, target: EventTarget | null) => target is DOMNode; /** * */ hasRange: (editor: Editor, range: Range) => boolean; /** * Check if the target can be selectable */ hasSelectableTarget: (editor: Editor, target: EventTarget | null) => boolean; /** * Check if the target is in the editor. */ hasTarget: (editor: Editor, target: EventTarget | null) => target is DOMNode; /** * Insert data from a `DataTransfer` into the editor. */ insertData: (editor: Editor, data: DataTransfer) => void; /** * Insert fragment data from a `DataTransfer` into the editor. */ insertFragmentData: (editor: Editor, data: DataTransfer) => boolean; /** * Insert text data from a `DataTransfer` into the editor. */ insertTextData: (editor: Editor, data: DataTransfer) => boolean; /** * Check if the user is currently composing inside the editor. */ isComposing: (editor: Editor) => boolean; getWord: (editor: Editor, options?: EditorWordOptions) => { range: Range; text: string; } | undefined; /** * Check if the editor is focused. */ isFocused: (editor: Editor) => boolean; isMounted: (editor: Editor) => boolean; /** * Check if the editor is in read-only mode. */ isReadOnly: (editor: Editor) => boolean; /** * Check if the target is inside void and in an non-readonly editor. */ isTargetInsideNonReadonlyVoid: (editor: Editor, target: EventTarget | null) => boolean; /** * Sets data from the currently selected fragment on a `DataTransfer`. */ setFragmentData: (editor: Editor, data: DataTransfer, originEvent?: "drag" | "copy" | "cut") => void; resolveDOMPoint: (editor: Editor, point: Point) => DOMPoint | null; resolveDOMRange: (editor: Editor, range: Range) => DOMRange | null; resolveEventRange: (editor: Editor, event: any) => Range | null; resolveDOMNode: (editor: Editor, node: Node) => HTMLElement | null; resolveSlateNode: (editor: Editor, domNode: DOMNode) => Node | null; resolveSlatePoint: (editor: Editor, domPoint: DOMPoint, options?: { exactMatch?: boolean; searchDirection?: "forward" | "backward"; }) => Point | null; resolveSlateRange: (editor: Editor, domRange: DOMRange | DOMStaticRange | DOMSelection, options?: { exactMatch?: boolean; }) => Range | null; /** * Find the native DOM element from a Slate node. */ toDOMNode: (editor: Editor, node: Node) => HTMLElement; /** * Find a native DOM selection point from a Slate point. */ toDOMPoint: (editor: Editor, point: Point) => DOMPoint; /** * Find a native DOM range from a Slate `range`. * * Notice: the returned range will always be ordinal regardless of the direction of Slate `range` due to DOM API limit. * * there is no way to create a reverse DOM Range using Range.setStart/setEnd * according to https://dom.spec.whatwg.org/#concept-range-bp-set. */ toDOMRange: (editor: Editor, range: Range) => DOMRange; /** * Find a Slate node from a native DOM `element`. */ toSlateNode: (editor: Editor, domNode: DOMNode) => Node; /** * Find a Slate point from a DOM selection's `domNode` and `domOffset`. */ toSlatePoint: (editor: Editor, domPoint: DOMPoint, options: { exactMatch: boolean; suppressThrow: T; /** * The direction to search for Slate leaf nodes if `domPoint` is * non-editable and non-void. */ searchDirection?: "forward" | "backward"; }) => T extends true ? Point | null : Point; /** * Find a Slate range from a DOM range or selection. */ toSlateRange: (editor: Editor, domRange: DOMRange | DOMStaticRange | DOMSelection, options: { exactMatch: boolean; suppressThrow: T; }) => T extends true ? Range | null : Range; } export declare const DOMEditor: DOMEditorInterface; export {};