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