Skip to content

Base Classes

The fundamental classes for the editor's operation, its models, and events are EventManager and BaseModel. All other models, including BlockModel, ToolModel, ActionModel, ExtensionModel, and FileActionModel, inherit from BaseModel.

BaseModel

The base class for models, providing core functionality, event management, and lifecycle hooks.

Base Model Methods

FunctionSignatureDescription
Constructorpublic constructor(editor: Texditor)Creates a new instance of the base model.
getEventIdpublic getEventId(): stringReturns a unique identifier for the event listener instance.
getConfigpublic getConfig<K extends keyof BaseModelConfig>(key: K, defaultValue?: BaseModelConfig[K]): BaseModelConfig[K]Retrieves a configuration value by key.
getOptionpublic getOption<T = unknown>(key: string, defaultValue?: T): T | nullRetrieves an option value by key.
setOptionpublic setOption(key: string, value: unknown): voidSets an option value.
setOptionspublic setOptions(options: Record<string, unknown>): voidSets multiple options (merges with existing ones).
getOptionspublic getOptions(): Record<string, unknown>Returns a copy of all options.
removeOptionpublic removeOption(key: string): booleanRemoves an option by key. Returns true if the option existed and was removed.
clearOptionspublic clearOptions(): voidClears all options.
setStorepublic setStore(key: string, value: unknown): thisSets a value in the store. Returns the current instance for chaining.
getStorepublic getStore(key: string | null): unknownRetrieves a value from the store. If key is null, returns the entire store.
getNamepublic getName(): stringReturns the model name.
getElementTagNamepublic getElementTagName(): stringReturns the tag name of the model element.
getIdpublic getId(): stringReturns the unique identifier of the model.
getElementpublic getElement(): TElementReturns the DOM element of the model.
getClassNamepublic getClassName(): stringReturns the CSS class name.
getRootClassNamepublic getRootClassName(): stringReturns the root CSS class name.
getTranslationpublic getTranslation(): stringReturns the translated string for localization.
getIconpublic getIcon(): RenderIconContentReturns the icon content for the model button.
getIconWidthpublic getIconWidth(): numberReturns the width of the icon in pixels.
getIconHeightpublic getIconHeight(): numberReturns the height of the icon in pixels.
getModelCodepublic getModelCode(): stringReturns the model code.
isVisibleTitlepublic isVisibleTitle(): booleanChecks if the title is always visible.
isAttributeTitlepublic isAttributeTitle(): booleanChecks if the title attribute is visible.
isVisibleIconpublic isVisibleIcon(): booleanChecks if the icon is always visible.
isActivepublic isActive(): booleanChecks if the model is active.
isVisiblepublic isVisible(): booleanChecks if the model is visible.
destroypublic destroy(): voidDestroys the instance and clears resources.

Protected and Static Methods of the Base Model

FunctionSignatureDescription
setuppublic static setup(config: Partial<BaseModelConfig>): ModelConstructorSets global configuration for the model.
createElementprotected createElement(): TElementCreates the DOM element of the model.
onCreateElementprotected onCreateElement(_el: TElement): voidHook called after creating the model element.
parentOnCreateElementprotected parentOnCreateElement(_el: TElement): voidParent hook called after creating the model element.
onLoadprotected onLoad(): voidHook called when the model is loaded.
onClickprotected onClick(_evt: MouseEvent): voidHook called when the model element is clicked.
parentOnClickprotected parentOnClick(evt: MouseEvent): voidParent hook called after clicking the model element.
onMountprotected onMount(_el: TElement): voidHook called after mounting to the DOM.
parentOnMountprotected parentOnMount(_el: TElement): voidParent hook called after mounting to the DOM.
onConstructprotected onConstruct(_editor: Texditor): voidHook called during constructor creation.
parentConfigprotected parentConfig(): Partial<BaseModelConfig>Returns the configuration of the parent model.
configureprotected configure(): Partial<BaseModelConfig>Configures the model configuration.
parentDestroyprotected parentDestroy(): voidParent hook called before destroying the element.

Types and Interfaces of the Base Model

typescript
/**
 * Interface of a DOM element extending HTMLElement with a reference to the base model.
 */
export interface BaseElement extends HTMLElement {
 /** Reference to the base model instance */
 baseModel: BaseModel;
}

/**
 * Interface for model configuration.
 * @property name - Model name
 * @property elementTagName - Tag name of the model element
 * @property translation - Translation key for localization
 * @property icon - Icon content for the button
 * @property visibleIcon - Whether the icon is visible
 * @property iconWidth - Width of the icon in pixels
 * @property iconHeight - Height of the icon in pixels
 * @property className - CSS class name
 * @property attributeTitle - Whether the title attribute is visible
 * @property visibleTitle - Whether the title is visible
 * @property __modelCode - Unique model code
 */
export interface BaseModelConfig {
 name: string;
 elementTagName: string;
 translation: string;
 icon: RenderIconContent;
 visibleIcon: boolean;
 iconWidth: number;
 iconHeight: number;
 className: string;
 attributeTitle: boolean;
 visibleTitle: boolean;
 __modelCode: string;
 [key: string]: unknown;
}

/**
 * Generic interface for a model constructor.
 * @template T - Type of the model interface extending BaseModel
 * @template C - Type of the model configuration extending BaseModelConfig
 */
export interface ModelConstructor<T extends BaseModel = BaseModel, C extends BaseModelConfig = BaseModelConfig> {
 /**
  * Creates a new instance of the model.
  * @param editor - Editor instance
  * @returns Model instance of type T
  */
 new (editor: Texditor): T;
 /**
  * Sets global configuration for the model.
  * @param config - Configuration object
  * @returns Model constructor
  */
 setup?(config: Partial<C>): ModelConstructor<T, C>;
}

/**
 * Type of the base model constructor.
 */
export type BaseModelConstructor = ModelConstructor<BaseModel, BaseModelConfig>;

/**
 * Interface of the base model.
 */
export interface BaseModel<TElement extends BaseElement = BaseElement> extends EventManager {
 getEventId(): string;
 getName(): string;
 getElementTagName(): string;
 getId(): string;
 getElement(): TElement;
 getClassName(): string;
 getRootClassName(): string;
 getTranslation(): string;
 getIcon(): RenderIconContent;
 getIconWidth(): number;
 getIconHeight(): number;
 getModelCode(): string;
 isVisibleTitle(): boolean;
 isAttributeTitle(): boolean;
 isVisibleIcon(): boolean;
 isActive(): boolean;
 isVisible(): boolean;
 destroy(): void;
 getConfig<K extends keyof BaseModelConfig>(key: K, defaultValue?: BaseModelConfig[K]): BaseModelConfig[K];
 getConfig(key: string, defaultValue: unknown): unknown;
 getOption<T = unknown>(key: string, defaultValue?: T): T | null;
 setOption(key: string, value: unknown): void;
 setOptions(options: Record<string, unknown>): void;
 getOptions(): Record<string, unknown>;
 removeOption(key: string): boolean;
 clearOptions(): void;
 setStore(key: string, value: unknown): this;
 getStore(key: string | null): unknown;
}

Example of Extending the Base Model Class

typescript
import { type BaseModelConfig } from "texditor";
import { BaseModel } from "texditor/core/base";

export default class CustomModel extends BaseModel {
 protected onClick(evt: MouseEvent): void {
  console.log('CustomModel clicked!', evt);
 }

 protected configure(): Partial<BaseModelConfig> {
  return {
   __modelCode: 'custom', // Required unique internal model code
   name: 'CustomModel',
   icon: '<svg>...</svg>',
   className: 'custom-model-class',
  };
 }
}
javascript
import { BaseModel } from "texditor/core/base";

export default class CustomModel extends BaseModel {
 onClick(evt) {
  console.log('CustomModel clicked!', evt);
 }

 configure() {
  return {
   __modelCode: 'custom', // Required unique internal model code
   name: 'CustomModel',
   icon: '<svg>...</svg>',
   className: 'custom-model-class',
  };
 }
}

EventManager

A class for managing events.

Event Management Methods

FunctionSignatureDescription
onpublic on(name: string, callback: CallableFunction): voidAdds an event handler.
hasEventpublic hasEvent(name: string): booleanChecks if an event handler exists.
offpublic off(name: string, id?: string): booleanRemoves an event handler. Returns true if the removal was successful.
triggerpublic trigger(name: string, params?: TexditorEventBase): voidTriggers all handlers for the event.

Types and Interfaces for Event Management

typescript
/**
 * Structure for storing event triggers.
 * Maps event names to collections of callback functions with unique identifiers.
 */
export type EventTriggerObject = {
 [name: string]: {
  [id: string]: CallableFunction;
 };
};

/**
 * Base interface for editor events.
 * @property type - Event type
 */
export interface TexditorEventBase {
 type: string;
 [key: string]: unknown;
}

/**
 * Interface for managing events.
 */
export interface EventManager {
 on(name: string, callback: CallableFunction): void;
 hasEvent(name: string): boolean;
 off(name: string, id?: string): boolean;
 trigger(name: string, params?: TexditorEventBase): void;
}

Example of Extending the Event Manager

Since the event manager is an abstract class, let's consider it using the example of a custom model that extends the BaseModel class, which in turn extends the EventManager class.

typescript
import {
 BaseElement,
 type TexditorEvent,
 type BaseModelConfig
} from "texditor";
import { BaseModel } from "texditor/core/base";

// BaseModel already has all EventManager methods
export default class CustomModel extends BaseModel {
 protected onClick(evt: MouseEvent): void {
  // Create a trigger
  this.trigger('customClicked', {
   domEvent: evt,
   // any free parameter or editor event parameters
  });
 }

 protected configure(): Partial<BaseModelConfig> {
  return {
   __modelCode: 'custom',
   name: 'CustomModel',
   icon: '<svg>...</svg>',
   className: 'custom-model-class',
  };
 }

 protected onLoad(): void {
  this.on('customMount', (evt: TexditorEvent) => {
   console.log('Subscribed to customMount event');
  });
 }

 protected onMount(el: BaseElement): void {
  this.trigger('customClicked');
 }
}
javascript
import { BaseElement } from "texditor";
import { BaseModel } from "texditor/core/base";

// BaseModel already has all EventManager methods
export default class CustomModel extends BaseModel {
 onClick(evt) {
  // Create a trigger
  this.trigger('customClicked', {
   domEvent: evt,
   // any free parameter or editor event parameters
  });
 }

 configure() {
  return {
   __modelCode: 'custom',
   name: 'CustomModel',
   icon: '<svg>...</svg>',
   className: 'custom-model-class',
  };
 }

 onLoad() {
  this.on('customMount', (evt) => {
   console.log('Subscribed to customMount event');
  });
 }

 onMount(el) {
  this.trigger('customClicked');
 }
}

dev@priveted.com | priveted.com