UNPKG

@textbus/core

Version:

Textbus is a rich text editor and framework that is highly customizable and extensible to achieve rich wysiwyg effects.

130 lines (129 loc) 4.83 kB
import { Selection } from './selection'; import { Attribute, Component, ContentType, Formats, Formatter, FormatValue, Slot } from '../model/_api'; import { RootComponentRef } from './_injection-tokens'; import { Registry } from './registry'; import { Textbus } from '../textbus'; import { Adapter } from './adapter'; /** * 组件转换规则 */ export interface TransformRule { /** 目标组件的数据类型 */ targetType: ContentType; /** * 创建目标组件新插槽的工厂函数 * @param from */ slotFactory(from: Component<any>): Slot; /** * 创建组件状态的工厂函数 */ stateFactory(slots: Slot[], textbus: Textbus): Component<any>[]; } export declare class Commander { protected selection: Selection; protected adapter: Adapter; protected textbus: Textbus; protected registry: Registry; protected rootComponentRef: RootComponentRef; constructor(selection: Selection, adapter: Adapter, textbus: Textbus, registry: Registry, rootComponentRef: RootComponentRef); /** * 将选区内容转换为指定组件 * @param rule */ transform(rule: TransformRule): boolean; /** * 在当前选区插入新内容,当选区未闭合时,会先删除选区内容,再插入新的内容 * 在插入新内容时,write 方法还会把相邻的样式应用到新内容上 * @param content 新插入的内容 * @param formats 新的格式 */ write(content: string | Component, formats?: Formats): boolean; write<T extends FormatValue>(content: string | Component, formatter?: Formatter<T>, value?: T): boolean; /** * 在当前选区插入新内容,当选区未闭合时,会先删除选区内容,再插入新的内容 * @param content 新插入的内容 * @param formats 新的格式 */ insert(content: string | Component, formats?: Formats): boolean; insert<T extends FormatValue>(content: string | Component, formatter?: Formatter<T>, value?: T): boolean; /** * 触发删除操作,如当前选区未闭合,则删除选区内容。否则触发默认删除操作 * @param deleteBefore 默认为 `true`,当值为 `true` 时,则向前删除,否则向后删除 */ delete(deleteBefore?: boolean): boolean; delete(receiver: (slot: Slot) => void, deleteBefore?: boolean): boolean; /** * 在当前选区内触发换行操作,如果选区未闭合,则先删除选区内容,再触发回车操作 */ break(): boolean; /** * 在指定组件前插入新的组件 * @param newChild 要插入的组件 * @param ref 新组件插入组件位置的引用 */ insertBefore(newChild: Component, ref: Component): boolean; /** * 在指定组件后插入新的组件 * @param newChild 要插入的组件 * @param ref 新组件插入组件位置的引用 */ insertAfter(newChild: Component, ref: Component): boolean; /** * 用新组件替换旧组件 * @param oldComponent 要删除的组件 * @param newComponent 新插入的组件 */ replaceComponent(oldComponent: Component, newComponent: Component): boolean; /** * 复制当前选区内容 */ copy(): void; /** * 剪切当前选区内容 */ cut(): boolean; /** * 在当前选区粘贴新内容,当选区未闭合时,会先删除选区内容,再粘贴新内容 * @param pasteSlot 要粘贴的数据 * @param text 要粘贴的文本 */ paste(pasteSlot: Slot, text: string): boolean; /** * 清除当前选区的所有格式 * @param excludeFormatters 在清除格式时,排除的格式 */ cleanFormats(excludeFormatters?: Formatter<any>[] | ((formatter: Formatter<any>) => boolean)): void; /** * 给当前选区应用新的格式 * @param formatter 要应用的格式 * @param value 当前格式要应用的值 */ applyFormat<T extends FormatValue>(formatter: Formatter<T>, value: T): void; /** * 清除当前选区特定的格式 * @param formatter 要清除的格式 */ unApplyFormat(formatter: Formatter<any>): void; /** * 根据选区应用插槽属性 * @param attribute * @param value */ applyAttribute<T extends FormatValue>(attribute: Attribute<T>, value: T): void; /** * 根据选区清除插槽属性 * @param attribute */ unApplyAttribute(attribute: Attribute<any>): void; /** * 根据选区清除属性 */ cleanAttributes(excludeAttributes?: Attribute<any>[] | ((attribute: Attribute<any>) => boolean)): void; /** * 删除指定组件 * @param component */ removeComponent(component: Component): boolean; private transformByRange; }