UNPKG

@textbus/core

Version:

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

234 lines (233 loc) 8.84 kB
import { Observable, Subject } from '@tanbo/stream'; import { Component, ComponentLiteral } from './component'; import { Content } from './content'; import { Format, FormatLiteral, FormatRange, FormatValue, Formats, FormatTree, FormatItem } from './format'; import { Attribute, Formatter } from './attribute'; import { ChangeMarker } from '../observable/change-marker'; import { Action } from './types'; import { VElement, VTextNode } from './element'; /** * 插槽渲染的工厂函数 */ export interface SlotRenderFactory { (children: Array<VElement | VTextNode | Component>): VElement; } export declare enum ContentType { Text = 1, InlineComponent = 2, BlockComponent = 3 } export interface SlotLiteral<T extends Record<string, any> = Record<string, any>> { schema: ContentType[]; state: T; content: Array<string | ComponentLiteral>; attributes: Record<string, FormatValue>; formats: FormatLiteral; } export interface DeltaInsert { insert: string | Component; formats: Formats; } export declare class DeltaLite extends Array<DeltaInsert> { attributes: Map<Attribute<any>, any>; } /** * Textbus 插槽类,用于管理组件、文本及格式的增删改查 */ export declare class Slot<T extends Record<string, any> = Record<string, any>> { static placeholder: string; static get emptyPlaceholder(): string; /** 插槽变更标记器 */ readonly __changeMarker__: ChangeMarker; readonly changeMarker: ChangeMarker; readonly onContentChange: Observable<Action[]>; readonly schema: ContentType[]; /** 插槽所属的组件 */ get parent(): Component | null; get parentSlot(): Slot | null; /** 插槽内容长度 */ get length(): number; /** 插槽内容是否为空 */ get isEmpty(): boolean; /** 插槽当前下标位置 */ get index(): number; /** * @internal * 插槽的 id,用于优化 diff 算法 */ readonly id: number; protected _index: number; protected content: Content; protected format: any; protected attributes: Map<Attribute<any>, any>; protected contentChangeEvent: Subject<Action[]>; protected applyFormatCoverChild: boolean; readonly state: T; constructor(schema: ContentType[], state?: T); /** * 设置属性 * @param attribute * @param value * @param canSet */ setAttribute(attribute: Attribute<any>, value: FormatValue, canSet?: (slot: Slot, attr: Attribute, value: any) => boolean): void; /** * 获取属性 * @param attribute */ getAttribute<T>(attribute: Attribute<T>): T | null; /** * 获取所有属性 */ getAttributes(): [Attribute<any>, any][]; /** * 删除属性 * @param attribute * @param canRemove */ removeAttribute(attribute: Attribute<any>, canRemove?: (slot: Slot, attr: Attribute<any>) => boolean): void; /** * 根据是否包含指定 Attribute * @param attribute */ hasAttribute(attribute: Attribute<any>): boolean; /** * 向插槽内写入内容,并根据当前位置的格式,自动扩展 * @param content * @param formats * @param canApply */ write(content: string | Component, formats?: Formats, canApply?: (slot: Slot, formatter: Formatter, value: any) => boolean): boolean; write<T>(content: string | Component, formatter?: Formatter<T>, value?: T, canApply?: (slot: Slot, formatter: Formatter, value: any) => boolean): boolean; /** * 向插槽内写入内容,并可同时应用格式 * @param content * @param formats * @param canApply */ insert(content: string | Component, formats?: Formats, canApply?: (slot: Slot, formatter: Formatter, value: any) => boolean): boolean; insert<T>(content: string | Component, formatter?: Formatter<T>, value?: T, canApply?: (slot: Slot, formatter: Formatter, value: any) => boolean): boolean; /** * 如果没有传入格式参数,则移动插槽下标到 offset * 如果有传入格式参数,则以当前下标位置向后增加 offset 的区间内设置样式 * @param offset */ retain(offset: number): boolean; retain(offset: number, formats: Formats, canApply?: (slot: Slot<T>, formatter: Formatter, value: any) => boolean): boolean; retain<U>(offset: number, formatter: Formatter<U>, value: U | null, canApply?: (slot: Slot<T>, formatter: Formatter, value: any) => boolean): boolean; /** * 从当前位置向后删除指定长度的内容 * @param count */ delete(count: number): boolean; /** * 给插槽应用新的格式,如果为块级样式,则应用到整个插槽,否则根据参数配置的范围应用 * @param formatter * @param data * @param canApply */ applyFormat<U extends FormatValue>(formatter: Formatter<U>, data: FormatRange<U>, canApply?: (slot: Slot<T>, formatter: Formatter, value: any) => boolean): void; /** * 在当前插槽内删除指定的组件 * @param component */ removeComponent(component: Component): boolean; /** * 剪切插槽内指定范围的内容 * @param startIndex * @param endIndex */ cut(startIndex?: number, endIndex?: number): Slot<T>; /** * 把当前插槽内指定范围的内容剪切到新插槽 * @param slot 新插槽 * @param startIndex * @param endIndex */ cutTo(slot: Slot<T>, startIndex?: number, endIndex?: number): Slot<T>; /** * 查找组件在插槽内的索引 * @param component */ indexOf(component: Component): number; /** * 查找指定下标位置的内容 * @param index */ getContentAtIndex(index: number): string | Component<import("./types").State>; /** * 切分出插槽内指定范围的内容 * @param startIndex * @param endIndex */ sliceContent(startIndex?: number, endIndex?: number): (string | Component<import("./types").State>)[]; /** * 获取传入格式在插槽指定内范围的集合 * @param formatter 指定的格式 * @param startIndex * @param endIndex */ getFormatRangesByFormatter<T extends Formatter<any>, U = T extends Formatter<infer V> ? V : never>(formatter: T, startIndex: number, endIndex: number): FormatRange<U>[]; /** * 获取插槽格式的数组集合 */ getFormats(): FormatItem[]; /** * 提取 index 下标位置的格式 * @param index */ extractFormatsByIndex(index: number): Formats; /** * 把插槽内容转换为 JSON */ toJSON(): SlotLiteral<T>; toString(): string; /** * 将插槽数据转换为 delta 表示 */ toDelta(): DeltaLite; /** * 根据 delta 插入内容 * @param delta * @param canApply */ insertDelta(delta: DeltaLite, canApply?: (slot: Slot, formatter: Formatter, value: any) => boolean): DeltaLite; /** * 清除插槽格式 * @param remainFormats 要保留的格式 * @param startIndex 开始位置 * @param endIndex 结束位置 * @param canApply */ cleanFormats(remainFormats?: Formatter<any>[] | ((formatter: Formatter<any>) => boolean), startIndex?: number, endIndex?: number, canApply?: (slot: Slot, formatter: Formatter, value: any) => boolean): void; /** * 当在回调函数中应用样式时,将把应用的样式作为子插槽的最低优化级合并 * @param fn */ background(fn: () => void): void; /** * 清除插槽属性 * @param remainAttributes 要保留的属性 * @param canRemove */ cleanAttributes(remainAttributes?: Attribute<any>[] | ((attribute: Attribute<any>) => boolean), canRemove?: (slot: Slot, attr: Attribute<any>) => boolean): void; /** * 根据插槽的格式数据,生成格式树 */ toTree(slotRenderFactory: SlotRenderFactory, renderEnv?: any): VElement; toTree(slotRenderFactory: SlotRenderFactory, customFormat: Format | null, renderEnv?: any): VElement; static toTree(slot: Slot, slotRenderFactory: SlotRenderFactory, formatTree: FormatTree, renderEnv?: any): VElement; private applyFormats; private static createVDomByFormatTree; private static createVDomByOverlapFormats; private static createVDomByContent; private static createActionByFormat; } export declare class SlotJSON<T extends Record<string, any> = Record<string, any>> implements SlotLiteral<T> { schema: ContentType[]; content: Array<string | ComponentLiteral>; attributes: Record<string, any>; formats: FormatLiteral; state: T; constructor(schema: ContentType[], content: Array<string | ComponentLiteral>, attributes: Record<string, any>, formats: FormatLiteral, state: T); }