@jirkasa/code-box
Version:
Showcase code samples on the web with a container that lets users select and display different samples.
82 lines (81 loc) • 3.42 kB
TypeScript
import CodeBox from "../code-box/CodeBox";
import CodeBoxOptions from "../code-box/CodeBoxOptions";
/**
* Entry for a CodeBox created by code box creator along with the dataset of its corresponding root element.
* @template T The type of code box.
*/
export type CodeBoxCreatorEntry<T extends CodeBox> = {
/** Code box. */
codeBox: T;
/** Dataset of the root element of the code box. */
rootElementDataset: DOMStringMap;
};
/**
* Data for a code box created additionally.
* @template T The type of code box.
*/
export type AdditionalCodeBoxInfo<T extends CodeBox> = {
/** Created code box. */
codeBox: T;
/** Root element of created code box. */
rootElement: HTMLElement;
};
/**
* Manages the creation of code boxes.
* @template T The type of code box.
* @template U The type of code box options.
*/
declare abstract class CodeBoxCreator<T extends CodeBox, U extends CodeBoxOptions> {
/** Created code boxes stored by their root element. */
private createdCodeBoxes;
/** Default code box options used, when no options are provided to the created method. */
private defaultCodeBoxOptions;
/**
* Creates new code box creator.
* @param defaultCodeBoxOptions Default code box options to be used, when no options are provided to the create method.
*/
constructor(defaultCodeBoxOptions: U);
/**
* Creates code boxes based on the provided selector.
* @param selector Selector used to find HTML elements to create code boxes from.
* @param codeBoxOptions The options to be used, when creating code boxes. Defaults to the internal default options (provided to constructor).
* @returns Number of created code boxes.
*/
create(selector: string, codeBoxOptions?: U): number;
/**
* Returns created code box by id.
* @param id Id of created code box.
* @returns Code box (or null if code box was not found).
*/
abstract getCreatedCodeBoxById(id: string): T | null;
/**
* Returns all created code boxes along with the dataset of their root elements.
* @returns An array of objects, each containing a created code box and the dataset of its root element.
*/
getCreatedCodeBoxes(): CodeBoxCreatorEntry<T>[];
/**
* Returns the number of created code boxes.
* @returns The number of created code boxes.
*/
getCreatedCodeBoxesCount(): number;
/**
* Creates new code box.
* @param element Element from which to create code box.
* @param codeBoxOptions Options to use when creating code box.
* @returns Created code box, or null if code box could not be created.
*/
protected abstract createCodeBox(element: HTMLElement, codeBoxOptions: U): T | null;
/**
* Returns additionally created code boxes. This method is called at the end of create method after createCodeBox method has been called for all elements obtained by the selector.
* @param codeBoxOptions Options to be used when creating code boxes.
* @returns Data for code boxes created additionally.
*/
protected getAdditionallyCreatedCodeBoxes(codeBoxOptions: U): AdditionalCodeBoxInfo<T>[];
/**
* Returns copy of code box options.
* @param codeBoxOptions Code box options.
* @returns Copy of code box options.
*/
protected abstract createCodeBoxOptionsCopy(codeBoxOptions: U): U;
}
export default CodeBoxCreator;