Skip to content

Models ​

Models are used to implement specific editor functionality, inherited from the base class BaseModel. Below are descriptions of the main models (excluding methods defined in the base class): ActionModel, BlockModel, ExtensionModel, ToolModel, and FileActionModel.

ActionModel ​

Block action model.

ActionModel Methods ​

FunctionSignatureDescription
getBlockElementpublic getBlockElement(): BlockElement | nullReturns the parent block element associated with this action.
isDropdownpublic isDropdown(): booleanChecks if a dropdown list of actions is displayed on click.
isConfirmpublic isConfirm(): booleanChecks if the action requires confirmation before execution.

Protected and Static Methods of ActionModel ​

FunctionSignatureDescription
setuppublic static setup(config: Partial<ActionModelConfig>): ActionModelConstructorSets global configuration for the model.
parentConfigprotected parentConfig(): Partial<ActionModelConfig>Returns the configuration of the parent model.
parentOnCreateElementprotected parentOnCreateElement(el: ActionElement): voidParent hook called after creating the model element.
confirmprotected confirm(evt: MouseEvent): voidHandles the confirmation flow for actions requiring user verification.
parentOnClickprotected parentOnClick(evt: MouseEvent): voidParent hook called after clicking the model element.
dropdownprotected dropdown(): HTMLElementCreates and returns a dropdown menu element.

Types and Interfaces of ActionModel ​

typescript
/**
 * Type of the block action model constructor.
 */
export type ActionModelConstructor = ModelConstructor<ActionModel, ActionModelConfig>;

/**
 * Interface of the action DOM element.
 */
export interface ActionElement extends BaseElement {
 /** Reference to the action model instance */
 baseModel: ActionModel;
}

/**
 * Interface for the block action model configuration.
 * @property dropdown - Whether to display a dropdown on click
 * @property confirm - Whether the action requires confirmation before execution
 */
export interface ActionModelConfig extends BaseModelConfig {
 dropdown: boolean;
 confirm: boolean;
}

/**
 * Interface of the block action model.
 */
export interface ActionModel extends BaseModel<ActionElement> {
 getBlockElement(): BlockElement | null;
 isDropdown(): boolean;
 isConfirm(): boolean;
}

Examples of Working with Action Models ​

Block Actions

BlockModel ​

Block model.

BlockModel Methods ​

FunctionSignatureDescription
changepublic change(name: string, params?: TexditorEventBase, globalParams?: TexditorEventBase): voidTriggers a model change event.
isEnterCreatepublic isEnterCreate(): booleanChecks if pressing Enter creates a new block.
isAutoMergepublic isAutoMerge(): booleanChecks if auto-merge with adjacent blocks is enabled.
isAutoParsepublic isAutoParse(): booleanChecks if automatic parsing of content is enabled.
getRelatedNamespublic getRelatedNames(): string[]Returns an array of related block names and tags.
getSupportedNamespublic getSupportedNames(): string[]Returns an array of all supported block name types.
getPlaceholderpublic getPlaceholder(): stringReturns the block placeholder.
getContentClassNamepublic getContentClassName(): stringReturns the CSS class name for the content element.
getContentElementpublic getContentElement(): HTMLElementReturns the DOM element of the block content.
getTagNamepublic getTagName(): stringReturns the block tag name.
getGroupCodepublic getGroupCode(): stringReturns the block group code.
getItemTagNamepublic getItemTagName(): stringReturns the list item tag name.
getItemBodyClassNamepublic getItemBodyClassName(): stringReturns the CSS class name for the list item body.
getItemClassNamepublic getItemClassName(): stringReturns the CSS class name for the list item.
getItemNamepublic getItemName(): stringReturns the list item name.
getMaxItemspublic getMaxItems(): numberReturns the maximum allowed number of items.
getItemRelatedNamespublic getItemRelatedNames(): string[]Returns an array of related list item name types.
getItemSupportedNamespublic getItemSupportedNames(): string[]Returns an array of all supported list item name types.
isSortableItemspublic isSortableItems(): booleanChecks if item sorting is enabled.
getDragZoneClassNamepublic getDragZoneClassName(): stringReturns the CSS class name for the drag zone.
getItemIndexpublic getItemIndex(itemElement?: HTMLElement): numberReturns the index of the item.
showActionspublic showActions(): voidShows the block actions menu.
hideActionspublic hideActions(): voidHides the block actions menu.
createItempublic createItem(content?: string | unknown, index?: number, skipEvents?: boolean): booleanCreates a new list item.
removeItempublic removeItem(index?: number): booleanRemoves a list item.
getItempublic getItem(index: number): HTMLElement | nullReturns a list item by index.
getItemspublic getItems(): HTMLElement[]Returns an array of all list items.
getItemBodypublic getItemBody(index: number): HTMLElement | nullReturns the body of a list item by index.
moveItempublic moveItem(index: number, targetIndex: number, skipEvents?: boolean): voidMoves a list item to a new position.
getItemsLengthpublic getItemsLength(): numberReturns the number of items in the list.
canCreateItempublic canCreateItem(): booleanChecks if a new item can be created without exceeding the limit.
isEmptypublic isEmpty(): booleanChecks if the block is empty.
isEmptyItempublic isEmptyItem(index: number): booleanChecks if a specific list item is empty.
isEmptyDetectpublic isEmptyDetect(): booleanChecks if empty block detection is enabled.
isBackspaceRemovepublic isBackspaceRemove(): booleanChecks if the block is removed when Backspace is pressed.
isEditablepublic isEditable(): booleanChecks if the block content is editable.
isEditableItemspublic isEditableItems(): booleanChecks if list items are editable.
isRawpublic isRaw(): booleanChecks if the content is treated as raw text.
isNormalizepublic isNormalize(): booleanChecks if content normalization is enabled.
isSingleItempublic isSingleItem(): booleanChecks if the block supports only one list item.
isConvertiblepublic isConvertible(): booleanChecks if the block can be converted.
isCustomSavepublic isCustomSave(): booleanChecks if custom saving is enabled.
isNoDatapublic isNoData(): booleanChecks whether it is allowed to use a block without data.
isVisibleToolspublic isVisibleTools(): booleanChecks if block tools are displayed.
getAvailableToolspublic getAvailableTools(): string[]Returns an array of available tools.
isSanitizerpublic isSanitizer(): booleanChecks if HTML sanitization is enabled.
sanitizepublic sanitize(): voidSanitizes the block element from unsafe HTML.
toNormalizepublic toNormalize(): BlockElement | HTMLElement | HTMLElement[] | nullReturns the element for normalization.
toSanitizepublic toSanitize(): BlockElement | HTMLElement | HTMLElement[] | nullReturns the element for sanitization.
getSanitizerConfigpublic getSanitizerConfig(): SanitizerConfigReturns the HTML sanitizer configuration.
getToastsClassNamepublic getToastsClassName(): stringReturns the CSS class name for notifications.
getToastTimeoutpublic getToastTimeout(): numberReturns the auto-hide timeout for notifications in milliseconds.
toastspublic toasts(): ToastsReturns an instance of the notification service.

Protected and Static Methods of BlockModel ​

FunctionSignatureDescription
setuppublic static setup(config: Partial<BlockModelConfig>): BlockModelConstructorSets global configuration for the model.
parentConfigprotected parentConfig(): Partial<BlockModelConfig>Returns the configuration of the parent model.
parentOnCreateElementprotected parentOnCreateElement(el: BlockElement): voidParent hook called after creating the model element.
refreshSortableItemsprotected refreshSortableItems(): voidUpdates the functionality of sortable items.
composeprotected compose(createSchema?: BlockCreateSchema): BlockElementForms content from the schema before mounting.
onComposeprotected onCompose(_createSchema?: BlockCreateSchema): voidHook called after completing composition.
parseprotected parse(item: BlockSchema): BlockCreateSchemaParses the block schema, preparing it for composition.
mergeprotected merge(): HTMLElement | nullMethod for processing block merging with an adjacent block.
makeItemDragZoneprotected makeItemDragZone(): HTMLSpanElementCreates a drag zone for a list item.
makeItemElementprotected makeItemElement(content: string | unknown = ''): HTMLElementCreates a list item.
saveprotected save(blockSchema: BlockSchema, _blockElement?: BlockElement): BlockSchemaSaves block data in output format.
parentOnMountprotected parentOnMount(): voidParent hook called after mounting to the DOM.
onPasteprotected onPaste(_evt: Event, _map: PasteMap): booleanHandles the paste event.
onKeyDownprotected onKeyDown(_evt: KeyboardEvent): booleanHandles the keydown event.
onKeyUpprotected onKeyUp(_evt: KeyboardEvent): booleanHandles the keyup event.
onFocusprotected onFocus(_evt: FocusEvent): booleanHandles the focus event.
onBlurprotected onBlur(_evt: FocusEvent): booleanHandles the blur event.
onSelectionChangeprotected onSelectionChange(_evt: Event, _range: Range): booleanHandles the selection change event.
onDragStartprotected onDragStart(_evt: DragEvent): booleanHandles the drag start event.
onDragLeaveprotected onDragLeave(_evt: DragEvent): booleanHandles the drag leave event.
onDragOverprotected onDragOver(_evt: DragEvent): booleanHandles the drag over event.
onDragprotected onDrag(_evt: DragEvent): booleanHandles the drag event.
onDragEndprotected onDragEnd(_evt: DragEvent): booleanHandles the drag end event.
onDropprotected onDrop(_evt: DragEvent): booleanHandles the drop event.
beforeConvertprotected beforeConvert(blockElement: BlockElement, targetModel: BlockModel): [BlockElement, BlockModel]Hook called before block conversion.
afterConvertprotected afterConvert(newBlockElement: BlockElement): BlockElementHook called after block conversion.
parentDestroyprotected parentDestroy(): voidParent hook called before destroying the element.

Types and Interfaces of BlockModel ​

typescript
/**
 * Type of the block model constructor.
 */
export type BlockModelConstructor = ModelConstructor<BlockModel, BlockModelConfig>;

/**
 * Interface of the block DOM element.
 */
export interface BlockElement extends BaseElement {
 /** Reference to the block model instance */
 baseModel: BlockModel;
}

/**
 * Interface of the block schema for registration.
 * @property constructor - Reference to the constructor
 * @property model - Model instance
 */
export interface BlockModelSchema {
 constructor: BlockModelConstructor;
 model: BlockModel;
}

/**
 * Interface for the block model configuration.
 * @property autoMerge - Automatically merge with adjacent blocks
 * @property autoParse - Automatically parse content
 * @property groupCode - Block group code
 * @property contentClassName - CSS class name for the content element
 * @property backspaceRemove - Remove block when Backspace is pressed
 * @property visibleTools - Show tools for the block
 * @property availableTools - List of available tools
 * @property editable - Content is editable
 * @property editableItems - Individual items are editable
 * @property singleItem - Block supports only one item
 * @property enterCreate - Pressing Enter creates a new block
 * @property raw - Treat content as raw text
 * @property sanitizer - Enable HTML sanitization
 * @property sanitizerConfig - HTML sanitizer configuration
 * @property tagName - HTML tag of the block
 * @property itemTagName - HTML tag of the list item
 * @property itemName - Item type
 * @property itemRelatedNames - Related item names
 * @property itemClassName - CSS class of the item
 * @property itemBodyClassName - CSS class of the item body
 * @property maxItems - Maximum number of items
 * @property sortableItems - Enable item sorting
 * @property dragZoneClassName - CSS class name for the drag zone
 * @property relatedNames - Related block names
 * @property emptyDetect - Enable empty block detection
 * @property customSave - Use custom save logic
 * @property noData - Allow using the block without data
 * @property normalize - Enable content normalization
 * @property placeholder - Placeholder text
 * @property convertible - Block can be converted
 * @property toastTimeout - Auto-hide timeout for notifications in milliseconds
 * @property toastsClassName - CSS class name for the notifications element
 */
export interface BlockModelConfig extends BaseModelConfig {
 autoMerge: boolean;
 autoParse: boolean;
 groupCode?: string;
 backspaceRemove: boolean;
 visibleTools: boolean;
 availableTools: string[];
 editable: boolean;
 editableItems: boolean;
 singleItem: boolean;
 enterCreate: boolean;
 raw: boolean;
 sanitizer: boolean;
 sanitizerConfig: Record<string, unknown>;
 tagName: string;
 itemTagName: string;
 itemName: string;
 itemRelatedNames: string[];
 itemClassName: string;
 itemBodyClassName: string;
 maxItems: number;
 sortableItems: boolean;
 dragZoneClassName: string;
 relatedNames: string[];
 emptyDetect: boolean;
 customSave: boolean;
 normalize: boolean;
 placeholder?: string;
 convertible: boolean;
 contentClassName: string;
 toastTimeout: number;
 toastsClassName: string;
 [key: string]: unknown;
}

/**
 * Interface of the block model.
 */
export interface BlockModel extends BaseModel<BlockElement> {
 change(name: string, params?: TexditorEventBase, globalParams?: TexditorEventBase): void;
 isEnterCreate(): boolean;
 isAutoMerge(): boolean;
 isAutoParse(): boolean;
 getRelatedNames(): string[];
 getSupportedNames(): string[];
 getPlaceholder(): string;
 getContentClassName(): string;
 getContentElement(): HTMLElement;
 getTagName(): string;
 getGroupCode(): string;
 getItemTagName(): string;
 getItemBodyClassName(): string;
 getItemClassName(): string;
 getItemName(): string;
 getMaxItems(): number;
 getItemRelatedNames(): string[];
 getItemSupportedNames(): string[];
 isSortableItems(): boolean;
 getDragZoneClassName(): string;
 getItemIndex(itemElement?: HTMLElement): number;
 showActions(): void;
 hideActions(): void;
 createItem(content?: string | unknown, index?: number, skipEvents?: boolean): boolean;
 removeItem(index?: number): boolean;
 getItem(index: number): HTMLElement | null;
 getItems(): HTMLElement[];
 getItemBody(index: number): HTMLElement | null;
 moveItem(index: number, targetIndex: number, skipEvents?: boolean): void;
 getItemsLength(): number;
 canCreateItem(): boolean;
 isEmpty(): boolean;
 isEmptyItem(index: number): boolean;
 isEmptyDetect(): boolean;
 isBackspaceRemove(): boolean;
 isEditable(): boolean;
 isEditableItems(): boolean;
 isRaw(): boolean;
 isNormalize(): boolean;
 isSingleItem(): boolean;
 isConvertible(): boolean;
 isCustomSave(): boolean;
 isNoData(): boolean;
 isVisibleTools(): boolean;
 getAvailableTools(): string[];
 isSanitizer(): boolean;
 sanitize(): void;
 toNormalize(): BlockElement | HTMLElement | HTMLElement[] | null;
 toSanitize(): BlockElement | HTMLElement | HTMLElement[] | null;
 getSanitizerConfig(): SanitizerConfig;
 getToastsClassName(): string;
 getToastTimeout(): number;
 toasts(): Toasts;
 setup?(config: Partial<BlockModelConfig>): BlockModelConstructor;
}

/** Block data: plain text, array of strings, or child blocks. */
export type BlockSchemaData = string | string[] | BlockChildSchema[];

/** HTML attributes of the block as key-value pairs. */
export type BlockSchemaAttr = Record<string, string>;

/** Full block model with type, data, attributes, and metadata. */
export interface BlockSchema {
 type: string;
 data: BlockSchemaData;
 attr?: BlockSchemaAttr;
 lang?: string;
 caption?: string;
 layout?: string;
 style?: string;
 [key: string]: unknown;
}

/** Child element of the block */
export interface BlockChildSchema {
 type: string;
 data: BlockSchemaData | [];
 attr?: BlockSchemaAttr;
 url?: string;
 desc?: string;
 caption?: string;
 size?: number;
 [key: string]: unknown;
}

/** Simplified model for creating a block — type is provided by context, only data is required. */
export interface BlockCreateSchema extends Omit<BlockSchema, 'type'> {
 data: string | BlockCreateItemSchema[] | unknown[];
}

/** Simplified model of a child element for creation — only type and string data are required. */
export interface BlockCreateItemSchema extends Omit<BlockChildSchema, 'data'> {
 type: string;
 data: string;
}

Example of Extending the Block Model ​

Blocks

ExtensionModel ​

Extension model.

ExtensionModel Methods ​

FunctionSignatureDescription
isToggleActivepublic isToggleActive(): booleanChecks if the extension toggles the active state on click.
getGroupNamepublic getGroupName(): stringReturns the group name.

Protected and Static Methods of ExtensionModel ​

FunctionSignatureDescription
setuppublic static setup(config: Partial<ExtensionModelConfig>): ExtensionModelConstructorSets global configuration for the model.
parentConfigprotected parentConfig(): Partial<ExtensionModelConfig>Returns the configuration of the parent model.
parentOnClickprotected parentOnClick(evt: MouseEvent): voidParent hook called after clicking the model element.

Types and Interfaces of ExtensionModel ​

typescript
/**
 * Type of the extension model constructor.
 */
export type ExtensionModelConstructor = ModelConstructor<ExtensionModel, ExtensionModelConfig>;

/**
 * Interface of the extension DOM element.
 */
export interface ExtensionElement extends BaseElement {
 /** Reference to the extension model instance */
 baseModel: ExtensionModel;
}

/**
 * Interface for the extension model configuration.
 * @property toggleActive - Whether the extension toggles the active state on click
 * @property groupName - Group name
 */
export interface ExtensionModelConfig extends BaseModelConfig {
 toggleActive: boolean;
 groupName: string;
}

/**
 * Interface of the extension model.
 */
export interface ExtensionModel extends BaseModel<ExtensionElement> {
 getGroupName(): string;
 isToggleActive(): boolean;
}

Example of Extending the Extension Model ​

Extensions

ToolModel ​

Text formatting tool model.

ToolModel Methods ​

FunctionSignatureDescription
getBlockElementpublic getBlockElement(): BlockElement | nullReturns the parent block element associated with this tool.
getTagNamepublic getTagName(): stringReturns the tag name of the tool for formatting.
formatpublic format(): voidApplies formatting to the selected content.
forcedFormatpublic forcedFormat(): voidForcefully creates formatting on the selected content.
removeFormatpublic removeFormat(): voidRemoves formatting from the selected content.
isOverridepublic isOverride(): booleanChecks if the tool tag is overridable.

Protected and Static Methods of ToolModel ​

FunctionSignatureDescription
setuppublic static setup(config: Partial<ToolModelConfig>): ToolModelConstructorSets global configuration for the model.
parentConfigprotected parentConfig(): Partial<ToolModelConfig>Returns the configuration of the parent model.
onFormatprotected onFormat(_tags: HTMLElement[]): voidHook called after applying formatting.
onClickprotected onClick(_evt: MouseEvent): voidHandles the click event.
isVisibleprotected isVisible(): booleanChecks if the tool model element is visible.

Types and Interfaces of ToolModel ​

typescript
/**
 * Type of the tool model constructor.
 */
export type ToolModelConstructor = ModelConstructor<ToolModel, ToolModelConfig>;

/**
 * Interface of the tool DOM element.
 */
export interface ToolElement extends BaseElement {
 /** Reference to the tool model instance */
 baseModel: ToolModel;
}

/**
 * Interface for the tool model configuration.
 * @property tagName - HTML tag name of the tool for formatting
 * @property override - Override tag
 */
export interface ToolModelConfig extends BaseModelConfig {
 tagName: string;
 override: boolean;
}

/**
 * Interface of the tool model.
 */
export interface ToolModel extends BaseModel<ToolElement> {
 getTagName(): string;
 getBlockElement(): BlockElement | null;
 format(onlyRemove?: boolean): void;
 forcedFormat(): void;
 removeFormat(): void;
 isOverride(): boolean;
}

Example of Extending the Tool Model ​

Tools

FileActionModel ​

File action model.

FileActionModel Methods ​

FunctionSignatureDescription
getItemElementpublic getItemElement(): FileItemElement | nullReturns the file element associated with this action.
getBlockElementpublic getBlockElement(): BlockElement | nullReturns the parent block element associated with this action.

Protected and Static Methods of FileActionModel ​

FunctionSignatureDescription
setuppublic static setup(config: Partial<FileActionModelConfig>): FileActionModelConstructorSets global configuration for the model.
parentConfigprotected parentConfig(): Partial<FileActionModelConfig>Returns the configuration of the parent model.

Types and Interfaces of FileActionModel ​

typescript
/**
 * Type of the file action model constructor.
 */
export type FileActionModelConstructor = ModelConstructor<FileActionModel, FileActionModelConfig>;

/**
 * Interface of the file action DOM element.
 */
export interface FileActionElement extends BaseElement {
 /** Reference to the file action model instance */
 baseModel: FileActionModel;
}

/**
 * Interface for the file action model configuration.
 * @property actions - List of file actions
 */
export interface FileActionModelConfig extends BaseModelConfig {
 actions: FileActionModelConstructor[];
}

/**
 * Interface of the file action model.
 */
export interface FileActionModel extends BaseModel<FileActionElement> {
 getBlockElement(): BlockElement | null;
 getItemElement(): FileItemElement | null;
}

dev@priveted.com | priveted.com