Other Components

This page provides information about components that are not covered in any other chapter of the documentation.

Highlight Box

HighlightBox represents a highlight in CodeView. It is returned when you add a new highlight using the addHighlight method, or you can retrieve it using the getHighlightBoxes method. The following example demonstrates its usage.

<pre id="MyCodeView"><code>let x = 1;
let y = 2;

let result = x + y;

console.log("result: " + result);</code></pre>
import { CodeView } from "@jirkasa/code-box";

const codeView = new CodeView(document.getElementById("MyCodeView"));

const highlightBox = codeView.addHighlight("1");
highlightBox.setRange(4, 6);
let x = 1;
let y = 2;

let result = x + y;

console.log("result: " + result);

Methods

getStartReturns start line of highlight.
getEndReturns end line of highlight.
setRangeSets new range for highlight.
removeRemoves highlight.

getStart

getStart(): number

Returns start line of highlight.

Returns

numberStart line of highlight.

getEnd

getEnd(): number

Returns end line of highlight.

Returns

numberEnd line of highlight.

setRange

setRange(start: number, end: number = start): void

Sets new range for highlight.

Params

start : numberStart line.
end : numberEnd line.

remove

remove(): void

Removes highlight.

Code View Memento

CodeViewMemento represents saved state of a CodeView. You can create a memento using the createMemento method and then apply it using the applyMemento method as demonstrated in the following example. This allows you to save the current state of the code view and easily restore it later.

<pre id="MyCodeView"><code>let x = 1;
let y = 2;

let result = x + y;

console.log("result: " + result);</code></pre>
import { CodeView } from "@jirkasa/code-box";

const codeView = new CodeView(document.getElementById("MyCodeView"));

// create memento (saved state of code view)
const codeViewMemento = codeView.createMemento();

// do some changes
codeView.addHighlight("4");

// apply memento (restore to the saved state of code view)
codeView.applyMemento(codeViewMemento);
let x = 1;
let y = 2;

let result = x + y;

console.log("result: " + result);

Alternatively, you can apply a memento by calling its apply method and passing the code view as a parameter.

const codeViewMemento = codeView.createMemento();

codeViewMemento.apply(codeView);

Code Box Memento

CodeBoxMemento represents saved state of a CodeBox. You can create a memento using the createMemento method and then apply it using the applyMemento method as demonstrated in the following example. This allows you to save the current state of the code box and easily restore it later.

<div id="MyCodeBox">
    <pre data-cb-name="addition" data-cb-active><code>let x = 1;
let y = 2;

console.log("result: " + (x + y));</code></pre>
    <pre data-cb-name="connecting strings"><code>let string1 = "This is";
let string2 = " string.";

console.log(string1 + string2);</code></pre>
</div>
import { TabCodeBox } from "@jirkasa/code-box";

const codeBox = new TabCodeBox(document.getElementById("MyCodeBox"), {
    lazyInit: false,
    svgSpritePath: "./img/icon-sprite.svg",
    svgSpriteIcons: {
        codeFile: "file",
        file: "file-2",
        download: "download"
    }
});

// create memento (saved state of code box)
const codeBoxMemento = codeBox.createMemento();

// do some changes
codeBox.removeCodeView("addition");

// apply memento (restore to the saved state of code box)
codeBox.applyMemento(codeBoxMemento);
let x = 1;
let y = 2;

console.log("result: " + (x + y));
let string1 = "This is";
let string2 = " string.";

console.log(string1 + string2);

Alternatively, you can apply a memento by calling its apply method and passing the code box as a parameter.

const codeBoxMemento = codeBox.createMemento();

codeBoxMemento.apply(codeBox);

Code Box Code View

CodeBoxCodeView represents a CodeView within a CodeBox. It internally uses a CodeView, retaining most of its methods while adding a few additional ones specific to CodeBox.

The following example demonstrates how you can obtain an instance of CodeBoxCodeView using the getCodeView method.

const codeView = codeBox.getCodeView("main.js");

Properties

CodeBoxCodeView has a few readonly properties. Do not change them when using plain JavaScript.

lineHeight : numberLine height of code.
lineHeightUnit : stringLine height unit of code.
linesCount : numberNumber of lines of code.

Methods

getIdentifierReturns identifier of code view.
changeIdentifierChanges identifier of code view.
setAsActiveSets code view as active.
removeRemoves code view from code box.
resetResets code view to its initial state.
createMementoCreates memento.
applyMementoApplies memento.
cloneCreates copy of code view.
addHighlightAdds new highlight.
removeHighlightsRemoves highlights based on provided range.
getHighlightBoxesReturns all highlight boxes in provided range.
showGutterShows gutter.
hideGutterHides gutter.
isGutterVisibleChecks whether gutter is visible.
showLineNumbersShows line numbers.
hideLineNumbersHides line numbers.
areLineNumbersVisibleChecks whether line numbers are visible.
getCodeReturns displayed code.

getIdentifier

getIdentifier(): string | null

Returns identifier of code view.

Returns

string | nullIdentifier.

changeIdentifier

changeIdentifier(newIdentifier: string): boolean

Changes identifier of code view.

Params

newIdentifier : stringNew identifier.

Returns

booleanIndicates whether change has been successfully completed (if passed new identifier already belongs to some other code view in code box, it returns false).

setAsActive

setAsActive(): void

Sets code view as active (displays it in code box).

remove

remove(): void

Removes code view from code box.

reset

reset(): void

Resets code view to its initial state.

createMemento

createMemento(): CodeViewMemento

Creates memento (saved state of code view).

Returns

CodeViewMementoMemento (saved state of code view).

applyMemento

applyMemento(memento: CodeViewMemento): void

Applies memento (sets code view to the state defined by the provided memento).

Params

memento : CodeViewMementoMemento (saved state of code view).

clone

clone(): CodeView

Creates a copy of code view. The copy reflects the state after initialization with the original options and does not include any changes made by subsequent method calls.

Returns

CodeViewCopy of code view.

addHighlight

addHighlight(start: number, end: number = start): HighlightBox

Adds new highlight.

Params

start : numberStart line of highlight.
end ?: numberEnd line of highlight (defaults to the same as the start line).

Returns

HighlightBoxCreated highlight box.

removeHighlights

removeHighlights(start: number | null = null, end: number | null = start): void

Removes highlights based on provided range (all intersecting highlights are removed). If no parameters are provided, all highlights are removed.

Params

start ?: number | nullStart line. Passing null or leaving it empty defaults to the first line.
end ?: number | nullEnd line (defaults to the same as the start line). Passing null defaults to the last line.

getHighlightBoxes

getHighlightBoxes(start: number | null = null, end: number | null = start): HighlightBox[]

Returns all highlight boxes (which represent highlights) in provided range. If no parameters are provided, all highlight boxes are returned.

Params

start ?: number | nullStart line. Passing null or leaving it empty defaults to the first line.
end ?: number | nullEnd line (defaults to the same as the start line). Passing null defaults to the last line.

Returns

HighlightBox[]Highlight boxes.

showGutter

showGutter(): void

Shows gutter (the area where line numbers appear).

hideGutter

hideGutter(): void

Hides gutter (the area where line numbers appear).

isGutterVisible

isGutterVisible(): boolean

Checks whether gutter (the area where line numbers appear) is visible.

Returns

booleanIndicates whether gutter is visible.

showLineNumbers

showLineNumbers(): void

Shows line numbers.

hideLineNumbers

hideLineNumbers(): void

Hides line numbers.

areLineNumbersVisible

areLineNumbersVisible(): boolean

Checks whether line numbers are visible.

Returns

booleanIndicates whether line numbers are visible.

getCode

getCode(): string

Returns displayed code.

Returns

stringDisplayed code.

Tab Code Box Code View

TabCodeBoxCodeView is a type of CodeBoxCodeView used within a TabCodeBox. It includes additional methods to set and get the position of its code view button.

Methods

...all methods from CodeBoxCodeView class
getButtonPositionReturns position of code view button.
setButtonPositionChanges position of code view button by swapping it with different code view or file button.

getButtonPosition

getButtonPosition(): number | null

Returns position of code view button.

Returns

number | nullPosition of code view button starting from 0.

setButtonPosition

setButtonPosition(position: number): boolean

Changes position of code view button by swapping it with different code view or file button.

Params

position : numberPosition of code view or file button (starting from 0) with which should be code view button swapped.

Returns

booleanIndicates whether position has been successfully changed.

Project Code Box Code View

ProjectCodeBoxCodeView is a type of CodeBoxCodeView used within a ProjectCodeBox. It includes additional methods related to ProjectCodeBox.

Methods

...all methods from CodeBoxCodeView class
getFolderPathReturns path to folder in which the code view is located.
moveToFolderMoves code view to folder.
getFileNameReturns file name of code view.
changeFileNameChanges file name of code view.
getPackageReturns package of code view.
changePackageChanges package of code view.
removePackageRemoves code view from package.

getFolderPath

getFolderPath(): string | null

Returns path to folder in which the code view is located.

Returns

string | nullPath to folder in which the code view is located.

moveToFolder

moveToFolder(folderPath: string): boolean

Moves code view to folder. If folder does not exist, it is created. Package is not changed.

Params

folderPath : stringPath to folder.

Returns

booleanIndicates whether code view has been successfully moved to folder (it can return false if there already is code view with the same name).

getFileName

getFileName(): string | null

Returns file name of code view.

Returns

string | nullFile name of code view.

changeFileName

changeFileName(newName: string): boolean

Changes file name of code view.

Params

newName : stringNew file name.

Returns

booleanIndicates whether file name of code view has been successfuly changed (it can return false if there already is code view with the same name in the same folder).

getPackage

getPackage(): string | null | undefined

Returns package of code view.

Returns

string | null | undefinedPackage of code view. If null is returned, code view belongs to default package. If undefined is returned, code view doesn't belong to any package.

changePackage

changePackage(packageName: string | null, keepFolderPath: boolean): boolean

Changes package of code view.

Params

packageName : string | nullPackage name (null for default package). If package does not exist, it is created.
keepFolderPath : booleanDetermines whether code view should stay in the same folder (if false, code view can be moved to different folder based on package).

Returns

booleanIndicates whether change has been successfully completed.

removePackage

removePackage(): void

Removes code view from package.

Code Box File

CodeBoxFile represents a file within a CodeBox. The following example demonstrates how you can obtain an instance of CodeBoxFile using the getFile method.

const file = codeBox.getFile("image.png");

Methods

getIdentifierReturns identifier of file.
changeIdentifierChanges identifier of file.
removeRemoves file from code box.
getDownloadLinkReturns download link of file.
setDownloadLinkChanges download link of file or sets it as non-downloadable.

getIdentifier

getIdentifier(): string | null

Returns identifier of file.

Returns

string | nullIdentifier.

changeIdentifier

changeIdentifier(newIdentifier: string): boolean

Changes identifier of file.

Params

newIdentifier : stringNew identifier.

Returns

booleanIndicates whether change has been successfully completed (if passed new identifier already belongs to some other file in code box, it returns false).

remove

remove(): void

Removes file from code box.

getDownloadLink(): string | null

Returns download link of file.

Returns

string | nullDownload link or null if file is not downloadable.

setDownloadLink(downloadLink: string | null): void

Changes download link of file or sets it as non-downloadable.

Params

downloadLink : string | nullDownload link or null if file should not be downloadable.

Tab Code Box File

TabCodeBoxFile is a type of CodeBoxFile used within a TabCodeBox. It includes additional methods to set and get the position of its file button.

Methods

...all methods from CodeBoxFile class
getButtonPositionReturns position of file button.
setButtonPositionChanges position of file button by swapping it with different code view or file button.

getButtonPosition

getButtonPosition(): number | null

Returns position of file button.

Returns

number | nullPosition of file button starting from 0.

setButtonPosition

setButtonPosition(position: number): boolean

Changes position of file button by swapping it with different code view or file button.

Params

position : numberPosition of code view or file button (starting from 0) with which should be file button swapped.

Returns

booleanIndicates whether position has been successfully changed.

Project Code Box File

ProjectCodeBoxFile is a type of CodeBoxFile used within a ProjectCodeBox. It includes additional methods related to ProjectCodeBox.

Methods

...all methods from CodeBoxFile class
getFolderPathReturns path to folder in which the file is located.
moveToFolderMoves file to folder.
getFileNameReturns file name of file.
changeFileNameChanges file name of file.
getPackageReturns package of file.
changePackageChanges package of file.
removePackageRemoves file from package.

getFolderPath

getFolderPath(): string | null

Returns path to folder in which the file is located.

Returns

string | nullPath to folder in which the file is located.

moveToFolder

moveToFolder(folderPath: string): boolean

Moves file to folder. If folder does not exist, it is created. Package is not changed.

Params

folderPath : stringPath to folder.

Returns

booleanIndicates whether file has been successfully moved to folder (it can return false if there already is file with the same name).

getFileName

getFileName(): string | null

Returns name of file

Returns

string | nullFile name.

changeFileName

changeFileName(newName: string): boolean

Changes name of file.

Params

newName : stringNew file name.

Returns

booleanIndicates whether name of file has been successfuly changed (it can return false if there already is file with the same name in the same folder).

getPackage

getPackage(): string | null | undefined

Returns package of file.

Returns

string | null | undefinedPackage of file. If null is returned, file belongs to default package. If undefined is returned, file doesn't belong to any package.

changePackage

changePackage(packageName: string | null, keepFolderPath: boolean): boolean

Changes package of file.

Params

packageName : string | nullPackage name (null for default package). If package does not exist, it is created.
keepFolderPath : booleanDetermines whether file should stay in the same folder (if false, file can be moved to different folder based on package).

Returns

booleanIndicates whether change has been successfully completed.

removePackage

removePackage(): void

Removes file from package.

Code View Creator Entry

When you invoke the getCreatedCodeViews method of CodeViewCreator, it returns an array of CodeViewCreatorEntry objects. CodeViewCreatorEntry object has the following properties:

codeView : CodeViewCode view.
preElementDataset : DOMStringMapDataset of HTML pre element used to create the code view.

Code Box Creator Entry

When you invoke the getCreatedCodeBoxes method of CodeBoxCreator, it returns an array of CodeBoxCreatorEntry objects. CodeBoxCreatorEntry object has the following properties (type of codeBox property depends on specific type of creator):

codeBox : T extends CodeBoxCode box.
rootElementDataset : DOMStringMapDataset of the root element of the code box.