UNPKG

@jirkasa/code-box

Version:

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

154 lines (153 loc) 5.16 kB
import CodeViewOptions from "./CodeViewOptions"; import HighlightBox from "./HighlightBox"; import CodeViewMemento from "./CodeViewMemento"; /** Represents component displaying a code example. */ declare class CodeView { /** Initial options. */ private initialOptions; /** Root element of code view. */ private rootElement; /** Gutter element (container for line numbers). */ private gutterElement; /** Container element. */ private containerElement; /** Line number elements. */ private lineNumberElements; /** Pre element. */ private preElement; /** Indicates whether line numbers are visible. */ private lineNumberElementsVisible; /** Stores highlight boxes along with their managers. */ private highlightBoxEntries; /** Event source to remove highlight box. */ private removeHighlightBoxEventSource; /** Line height of code. */ readonly lineHeight: number; /** Line height unit of code. */ readonly lineHeightUnit: string; /** Number of lines of code. */ readonly linesCount: number; /** * Creates new code view. * @param element Pre element with code element child. * @param options Code view options. */ constructor(element: HTMLPreElement, options?: CodeViewOptions); /** * Appends code view to element. * @param element Element to append code view to. */ appendTo(element: HTMLElement): void; /** * Detaches code view from its parent element. */ detach(): void; /** * Resets code view to its initial state. */ reset(): void; /** * Creates memento. * @returns Memento. */ createMemento(): CodeViewMemento; /** * Applies memento. * @param memento Memento. */ applyMemento(memento: CodeViewMemento): void; /** * Creates copy of code view. * @param clearHighlights Indicates whether highlights should be cleared in created copy (defaults to false). * @returns Copy of code view. */ clone(clearHighlights?: boolean): CodeView; /** * Adds new highlight. * @param start Start line of highlight. * @param end End line of highlight (default is the same as start line). * @param customCssClass Custom CSS class(es) to be added to highlight element. * @returns Created highlight box. */ addHighlight(start: number, end?: number, customCssClass?: string | string[] | null): HighlightBox; /** * Removes highlights based on passed range (all intersecting highlights are removed). * @param start Start line. * @param end End line (default is the same as start line). */ removeHighlights(start?: number | null, end?: number | null): void; /** * Returns all highlight boxes in passed range (if no parameters are passed or null is passed, all highlight boxes are returned). * @param start Start line. * @param end End line (default is the same as start line). * @returns Highlight boxes. */ getHighlightBoxes(start?: number | null, end?: number | null): Array<HighlightBox>; /** * Shows gutter. */ showGutter(): void; /** * Hides gutter. */ hideGutter(): void; /** * Checks whether gutter is visible. * @returns Indicates whether gutter is visible. */ isGutterVisible(): boolean; /** * Shows line numbers. */ showLineNumbers(): void; /** * Hides line numbers. */ hideLineNumbers(): void; /** * Checks whether line numbers are visible. * @returns Indicates whether line numbers are visible. */ areLineNumbersVisible(): boolean; /** * Returns displayed code. * @returns Displayed code. */ getCode(): string; /** * Called by highlight boxes when their remove method is called. * @param highlightBox Highlight box to be removed. */ private onRemoveHighlightBox; /** * Initializes highlights based on passed string with highlights definitions. * @param highlightString String with highlights definitions (for example: "1", "1-5", "2-4,6-8", "1-5(custom-css-class another-css-class)"...). */ private initHighlights; /** * Fills passed container element by line number elements and returns them. * @param container Container in which should be created line number elements. * @param hidden Determines whether line numbers should be hidden. * @returns Created line number elements. */ private fillLineNumbers; /** * Returns lines count of passed code element. * @param codeElement Code element. * @returns Lines count. */ private getLinesCount; /** * Fills code view options based on dataset. * @param options Code view options. * @param dataset Dataset. */ private fillOptionsFromDataset; /** * Returns child code element of pre element. If pre element does not contains code element, error is returned. * @param preElement Pre element. * @returns Code element. */ private getCodeElement; } export default CodeView;