UNPKG

@jirkasa/code-box

Version:

Showcase code samples on the web with a container that lets users select and display different samples.

284 lines (283 loc) 11.8 kB
import CodeView from "../code-view/CodeView"; import CodeViewOptions from "../code-view/CodeViewOptions"; import CodeBoxBuilder from "./CodeBoxBuilder"; import CodeBoxCodeView from "./CodeBoxCodeView"; import CodeBoxFile from "./CodeBoxFile"; import CodeBoxMemento from "./CodeBoxMemento"; import CodeBoxOptions from "./CodeBoxOptions"; /** Informations about code view. */ export type CodeViewInfo = { /** Dataset of pre element used to create code view. */ dataset: DOMStringMap; /** Code view. */ codeView: CodeView; }; /** Informations about file. */ export type FileInfo = { /** Dataset of element used to create file. */ dataset: DOMStringMap; /** Name of file. */ name: string; /** File download link (or null if no download link was set). */ downloadLink: string | null; }; /** Code box item informations. */ export type CodeBoxItemInfo = { /** Type of item. */ type: "CodeViewInfo" | "FileInfo" | "HTMLElement"; /** Informations about code view (if type is "CodeViewInfo"). */ codeViewInfo?: CodeViewInfo; /** Informations about file (if type is "FileInfo"). */ fileInfo?: FileInfo; /** HTML element (if type is "HTMLElement"). */ element?: HTMLElement; }; /** Base class for code boxes. */ declare abstract class CodeBox { protected static readonly CODE_BOX_NOT_INITIALIZED_ERROR = "Code box is not initialized."; /** Root element of code box. */ private readonly rootElement; /** Element in which code view are displayed. */ private readonly codeViewContainer; /** CSS class that is used to hide code view container. */ private readonly codeViewContainerCSSHiddenClass; /** Element that is displayed when no code view is displayed. */ private readonly noCodeViewSelectedElement; /** CSS class that is used to hide element that is displayed when no code view is displayed. */ private readonly noCodeViewSelectedCSSHiddenClass; /** Minimal number of code view lines count (used for setting minimal height of code view container). */ private minCodeViewLinesCount; /** Default code view options. */ private defaultCodeViewOptions; /** Currently displayed code view. */ private activeCodeView; /** Indicates whether code box is initialized. */ private initialized; /** Initialization data that is passed to subclasses on initialization. */ private initializationData; /** Placeholder element for lazy initialization. */ private lazyInitPlaceholderElement; /** Indicates whether lazy initialization is enabled. */ protected readonly isLazyInitializationEnabled: boolean; /** Functions called after initialization of code box. */ private onInitCallbacks; /** Functions called when code view is changed. */ private onActiveCodeViewChangeCallbacks; /** * Creates new code box. * @param element Code box root element. * @param options Code box options. * @param codeBoxBuilder Code box builder that should be used to build code box. * @param customLazyInitPlaceholderElementHeight Custom height value for lazy initialization placeholder element (this can be useful in some situation, when for example new code view is set right after initialization). */ constructor(element: HTMLElement, options: CodeBoxOptions, codeBoxBuilder: CodeBoxBuilder, customLazyInitPlaceholderElementHeight?: string | null); /** * Appends code box to element. * @param element Element to append code box to. */ appendTo(element: HTMLElement): void; /** * Detaches code box from its parent element. */ detach(): void; /** * Initializes code box if it hasn't been initialized yet. (When lazy initialization is disabled, code box is initialized immediately after it is created.) */ init(): void; /** * Checks whether code box is initialized. * @returns Indicates whether code box is initialized. */ isInitialized(): boolean; /** * Registeres function to be called when code box is initialized. * @param callback Function to be called after initialization of code box. */ addOnInitListener(callback: () => void): void; /** * Adds new code view to code box (copy of passed code view is made). * @param identifier Identifier under which the code view should be added to code box. * @param codeView Code view. * @returns Indicates whether code view has been successfully added. */ abstract addCodeView(identifier: string, codeView: CodeView): boolean; /** * Returns all code views of code box. * @returns Code views. */ abstract getCodeViews(): CodeBoxCodeView[]; /** * Returns code view based on identifier. * @param identifier Identifier of code view. * @returns Code view (or null if code view wasn't found). */ abstract getCodeView(identifier: string): CodeBoxCodeView | null; /** * Removes code view from code box. * @param identifier Identifier of code view to be removed. * @returns Indicates whether code view has been removed. */ abstract removeCodeView(identifier: string): boolean; /** * Removes all code views from code box. */ abstract removeAllCodeViews(): void; /** * Changes identifier of code view in code box. * @param identifier Identifier of code view whose identifier should be changed. * @param newIdentifier New identifier. * @returns Indicates whether change has been successfully completed (if passed new identifier already belongs to some other code view in code box, it returns false). */ abstract changeCodeViewIdentifier(identifier: string, newIdentifier: string): boolean; /** * Sets code view as active (displays it in code box). * @param identifier Identifier of code view which should be set as active. * @returns Indicates whether code view was found and has been successfully set as active. */ abstract setActiveCodeView(identifier: string): boolean; /** * Displays no code view in code box. */ abstract setNoActiveCodeView(): void; /** * Returns currently active code view. * @returns Active code view or null if no code view is set as active. */ abstract getActiveCodeView(): CodeBoxCodeView | null; /** * Registers function to be called when active code view is changed. * @param callback Function to be called when active code view is changed. */ addOnActiveCodeViewChangeListener(callback: () => void): void; /** * Removes function that was registered to be called when active code view is changed. * @param callback Function to be removed. */ removeOnActiveCodeViewChangeListener(callback: () => void): void; /** * Adds new file to code box. * @param identifier Identifier under which the file should be added to code box. * @param downloadLink Download link (or null if file should not be downloadable). * @returns Indicates whether file has been successfully added. */ abstract addFile(identifier: string, downloadLink: string | null): boolean; /** * Returns all files of code box. * @returns Files. */ abstract getFiles(): CodeBoxFile[]; /** * Returns file based on identifier. * @param identifier Identifier of file. * @returns File (or null if file wasn't found). */ abstract getFile(identifier: string): CodeBoxFile | null; /** * Removes file from code box. * @param identifier Identifier of file to be removed. * @returns Indicates whether file has been removed. */ abstract removeFile(identifier: string): boolean; /** * Removes all files from code box. */ abstract removeAllFiles(): void; /** * Changes identifier of file in code box. * @param identifier Indentifier of file whose identifier should be changed. * @param newIdentifier New identifier. * @returns Indicates whether change has been successfully completed (if passed new identifier already belongs to some other file in code box, it returns false). */ abstract changeFileIdentifier(identifier: string, newIdentifier: string): boolean; /** * Changes download link of file. * @param identifier Identifier of file whose download link should be changed. * @param newDownloadLink Download link (or null if file should not be downloadable). * @returns Indicates whether file was found and its download link has been successfully changed. */ abstract changeFileDownloadLink(identifier: string, newDownloadLink: string | null): boolean; /** * Resets code box to its post-initialization state. */ abstract reset(): void; /** * Creates memento. * @returns Memento. */ abstract createMemento(): CodeBoxMemento; /** * Applies memento. * @param memento Memento. */ abstract applyMemento(memento: CodeBoxMemento): void; /** * Called on initialization. * @param codeBoxItemInfos Info objects about code box items. */ protected abstract onInit(codeBoxItemInfos: CodeBoxItemInfo[]): void; /** * Called right after initialization. Can be implemented in subclasses. */ protected onAfterInit(): void; /** * Changes active code view displayed in code box. * @param codeView Code view that should be displayed in code box or null if no code view should be displayed. */ protected changeActiveCodeView(codeView: CodeView | null): void; /** * Returns currently active instance of CodeView. * @returns Currently active instance of CodeView. */ protected getCurrentlyActiveCodeView(): CodeView | null; /** * Can be used to get pre elements before initialization of code box. * @returns Pre elements or null if code box is already initialized. */ protected getPreElementsBeforeInitialization(): HTMLPreElement[] | null; /** * Shows element with no selected code view message. */ private showNoCodeViewSelectedMessage; /** * Hides element with no selected code view message. */ private hideNoCodeViewSelectedMessage; /** * Called when intersection between placeholder element and viewport changes. * @param isIntersecting Indicates whether placeholder element and viewport are intersecting. */ private onLazyInitPlaceholderElementIntersectionChange; /** * Returns number of lines count in code element. * @param codeElement Code element. * @returns Number of lines count in code element. */ protected static getLinesCount(codeElement: HTMLElement): number; /** * Fills code box options based on dataset. * @param options Code box options. * @param dataset Dataset. */ private fillOptionsFromDataset; /** * Returns child code element of pre element. * @param preElement Pre element. * @returns Code element. */ protected static getCodeElement(preElement: HTMLPreElement): HTMLElement | null; /** * Returns code view line height. * @param preElement Pre element. * @param defaultCodeViewOptions Default code view options. * @returns Code view line height. */ protected static getCodeViewLineHeight(preElement: HTMLPreElement, defaultCodeViewOptions: CodeViewOptions): number; /** * Returns code view line height unit. * @param preElement Pre element. * @param defaultCodeViewOptions Default code view options. * @returns Code view line height unit. */ protected static getCodeViewLineHeightUnit(preElement: HTMLPreElement, defaultCodeViewOptions: CodeViewOptions): string; } export default CodeBox;