import { TElement, TNode, TText, Value, Path } from 'platejs'; import { TPlateEditor } from 'platejs/react'; /** * 编辑器 API 接口 * 定义常用的编辑器 API 方法 */ export interface EditorApi { /** 遍历节点 */ nodes: (options?: { at?: Path | null; match?: (node: TNode, path: Path) => boolean; mode?: 'all' | 'highest' | 'lowest'; }) => Generator<[T, Path]> | null; /** 获取块节点 */ blocks: (options?: { mode?: 'all' | 'highest' | 'lowest'; }) => Generator<[TElement, Path]> | null; /** HTML 反序列化 */ html: { deserialize: (options: { element: Element; collapseWhiteSpace?: boolean; }) => Value; }; /** 获取当前块 */ block: (options?: { highest?: boolean; }) => [TElement, Path] | null; /** 转换为 DOM 节点 */ toDOMNode: (editor: TPlateEditor) => HTMLElement | undefined; } /** * 编辑器转换接口 * 定义编辑器的转换操作方法 */ export interface EditorTransforms { /** 聚焦 */ focus: () => void; /** 失焦 */ blur: () => void; /** 插入文本 */ insertText: (text: string) => void; /** 插入节点 */ insertNodes: (nodes: TNode | TNode[], options?: { at?: Path; }) => void; /** 删除 */ delete: () => void; /** 选择 */ select: (selection: { anchor: { path: Path; offset: number; }; focus: { path: Path; offset: number; }; }) => void; /** 包裹节点 */ wrapNodes: (element: TElement, options?: unknown) => void; /** 设置节点属性 */ setNodes: (props: Record, options?: { at?: unknown; match?: unknown; split?: boolean; }) => void; /** 切换标记 */ toggleMark: (key: string) => void; /** 合并操作 */ withMerging: (fn: () => void) => void; /** 不保存到历史记录 */ withoutSaving: (fn: () => void) => void; /** 保持滚动 */ withScrolling: (fn: () => void) => void; } /** * 可操作的编辑器类型 * 使用泛型约束而不是扩展接口 */ export type OperableEditor = TPlateEditor & { children: Value; selection: { anchor: { path: Path; offset: number; }; focus: { path: Path; offset: number; }; } | null; }; /** * 编辑器节点接口(带 ID) */ export interface EditorNode extends TElement { id?: string; type: string; } /** * 文本节点类型守卫 */ export declare function isTextNode(node: TElement | TText): node is TText; /** * 元素节点类型守卫 */ export declare function isElementNode(node: TNode): node is TElement;