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
getStart | Returns start line of highlight. |
---|---|
getEnd | Returns end line of highlight. |
setRange | Sets new range for highlight. |
remove | Removes highlight. |
getStart
getStart(): number
Returns start line of highlight.
Returns
number | Start line of highlight. |
---|
getEnd
getEnd(): number
Returns end line of highlight.
Returns
number | End line of highlight. |
---|
setRange
setRange(start: number, end: number = start): void
Sets new range for highlight.
Params
start : number | Start line. |
---|---|
end : number | End 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 : number | Line height of code. |
---|---|
lineHeightUnit : string | Line height unit of code. |
linesCount : number | Number of lines of code. |
Methods
getIdentifier | Returns identifier of code view. |
---|---|
changeIdentifier | Changes identifier of code view. |
setAsActive | Sets code view as active. |
remove | Removes code view from code box. |
reset | Resets code view to its initial state. |
createMemento | Creates memento. |
applyMemento | Applies memento. |
clone | Creates copy of code view. |
addHighlight | Adds new highlight. |
removeHighlights | Removes highlights based on provided range. |
getHighlightBoxes | Returns all highlight boxes in provided range. |
showGutter | Shows gutter. |
hideGutter | Hides gutter. |
isGutterVisible | Checks whether gutter is visible. |
showLineNumbers | Shows line numbers. |
hideLineNumbers | Hides line numbers. |
areLineNumbersVisible | Checks whether line numbers are visible. |
getCode | Returns displayed code. |
getIdentifier
getIdentifier(): string | null
Returns identifier of code view.
Returns
string | null | Identifier. |
---|
changeIdentifier
changeIdentifier(newIdentifier: string): boolean
Changes identifier of code view.
Params
newIdentifier : string | New identifier. |
---|
Returns
boolean | Indicates 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
CodeViewMemento | Memento (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 : CodeViewMemento | Memento (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
CodeView | Copy of code view. |
---|
addHighlight
addHighlight(start: number, end: number = start): HighlightBox
Adds new highlight.
Params
start : number | Start line of highlight. |
---|---|
end ?: number | End line of highlight (defaults to the same as the start line). |
Returns
HighlightBox | Created 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 | null | Start line. Passing null or leaving it empty defaults to the first line. |
---|---|
end ?: number | null | End 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 | null | Start line. Passing null or leaving it empty defaults to the first line. |
---|---|
end ?: number | null | End 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
boolean | Indicates 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
boolean | Indicates whether line numbers are visible. |
---|
getCode
getCode(): string
Returns displayed code.
Returns
string | Displayed 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 | |
---|---|
getButtonPosition | Returns position of code view button. |
setButtonPosition | Changes 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 | null | Position 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 : number | Position of code view or file button (starting from 0) with which should be code view button swapped. |
---|
Returns
boolean | Indicates 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 | |
---|---|
getFolderPath | Returns path to folder in which the code view is located. |
moveToFolder | Moves code view to folder. |
getFileName | Returns file name of code view. |
changeFileName | Changes file name of code view. |
getPackage | Returns package of code view. |
changePackage | Changes package of code view. |
removePackage | Removes code view from package. |
getFolderPath
getFolderPath(): string | null
Returns path to folder in which the code view is located.
Returns
string | null | Path 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 : string | Path to folder. |
---|
Returns
boolean | Indicates 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 | null | File name of code view. |
---|
changeFileName
changeFileName(newName: string): boolean
Changes file name of code view.
Params
newName : string | New file name. |
---|
Returns
boolean | Indicates 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 | undefined | Package 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 | null | Package name (null for default package). If package does not exist, it is created. |
---|---|
keepFolderPath : boolean | Determines whether code view should stay in the same folder (if false, code view can be moved to different folder based on package). |
Returns
boolean | Indicates 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
getIdentifier | Returns identifier of file. |
---|---|
changeIdentifier | Changes identifier of file. |
remove | Removes file from code box. |
getDownloadLink | Returns download link of file. |
setDownloadLink | Changes download link of file or sets it as non-downloadable. |
getIdentifier
getIdentifier(): string | null
Returns identifier of file.
Returns
string | null | Identifier. |
---|
changeIdentifier
changeIdentifier(newIdentifier: string): boolean
Changes identifier of file.
Params
newIdentifier : string | New identifier. |
---|
Returns
boolean | Indicates 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
getDownloadLink(): string | null
Returns download link of file.
Returns
string | null | Download link or null if file is not downloadable. |
---|
setDownloadLink
setDownloadLink(downloadLink: string | null): void
Changes download link of file or sets it as non-downloadable.
Params
downloadLink : string | null | Download 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 | |
---|---|
getButtonPosition | Returns position of file button. |
setButtonPosition | Changes 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 | null | Position 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 : number | Position of code view or file button (starting from 0) with which should be file button swapped. |
---|
Returns
boolean | Indicates 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 | |
---|---|
getFolderPath | Returns path to folder in which the file is located. |
moveToFolder | Moves file to folder. |
getFileName | Returns file name of file. |
changeFileName | Changes file name of file. |
getPackage | Returns package of file. |
changePackage | Changes package of file. |
removePackage | Removes file from package. |
getFolderPath
getFolderPath(): string | null
Returns path to folder in which the file is located.
Returns
string | null | Path 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 : string | Path to folder. |
---|
Returns
boolean | Indicates 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 | null | File name. |
---|
changeFileName
changeFileName(newName: string): boolean
Changes name of file.
Params
newName : string | New file name. |
---|
Returns
boolean | Indicates 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 | undefined | Package 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 | null | Package name (null for default package). If package does not exist, it is created. |
---|---|
keepFolderPath : boolean | Determines whether file should stay in the same folder (if false, file can be moved to different folder based on package). |
Returns
boolean | Indicates 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 : CodeView | Code view. |
---|---|
preElementDataset : DOMStringMap | Dataset 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 CodeBox | Code box. |
---|---|
rootElementDataset : DOMStringMap | Dataset of the root element of the code box. |