Code View
CodeView is one of the core components of the library. While it is used by code boxes, it can also be created independently. It simply represents a code sample.
Usage Example
The following example demonstrates how to create a CodeView instance. Simply instantiate the CodeView class by passing a reference to the <pre> element that contains the <code> element. You can also provide additional options as a second parameter.
<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";
new CodeView(document.getElementById("MyCodeView"), {
highlight: "1-2,6"
});
let x = 1;
let y = 2;
let result = x + y;
console.log("result: " + result);
If you want to create multiple code views, you can use the CodeViewCreator. For more information, refer to the chapter on creators.
Options
CodeView offers options to customize its appearance. These options can be set either by passing an object to the constructor or by using data attributes. When both are provided, data attributes will override the values specified in the options object.
highlight | Specifies lines of code to be highlighted. |
---|---|
showGutter | Specifies whether the gutter (the area where line numbers appear) should be visible. |
showLineNumbers | Specifies whether line numbers should be visible. |
lineHeight | Specifies the line height. |
lineHeightUnit | Specifies the unit for the line height. |
cssClasses | CSS classes that should be added to the root element of the code view. |
highlight : string
Default value: undefined
Data attribute alternative: data-cb-highlight
Specifies lines of code to be highlighted. For example, the string format can be "1" to highlight the first line, "1-5" to highlight lines 1 through 5, or "2-4,6-8" to highlight lines 2 through 4 and 6 through 8.
new CodeView(document.getElementById("MyCodeView"), {
highlight: "1-3,7"
});
let x = 1;
let y = 2;
let z = 5;
let result = x + y + z;
console.log("result: " + result);
showGutter : boolean
Default value: true
Data attribute alternative: data-cb-show-gutter
Specifies whether the gutter (the area where line numbers appear) should be visible.
new CodeView(document.getElementById("MyCodeView"), {
showGutter: false
});
let x = 1;
let y = 2;
let z = 5;
let result = x + y + z;
console.log("result: " + result);
showLineNumbers : boolean
Default value: true
Data attribute alternative: data-cb-show-line-numbers
Specifies whether line numbers should be visible.
new CodeView(document.getElementById("MyCodeView"), {
showLineNumbers: false
});
let x = 1;
let y = 2;
let z = 5;
let result = x + y + z;
console.log("result: " + result);
lineHeight : number
Default value: 2
Data attribute alternative: data-cb-line-height
Specifies the line height. The unit for the line height can be set using the lineHeightUnit option.
new CodeView(document.getElementById("MyCodeView"), {
lineHeight: 4
});
let x = 1;
let y = 2;
let z = 5;
let result = x + y + z;
console.log("result: " + result);
lineHeightUnit : string
Default value: "rem"
Data attribute alternative: data-cb-line-height-unit
Specifies the unit for line height. The line height value itself can be set using the lineHeight option.
cssClasses : string[]
Default value: undefined
Data attribute alternative: data-cb-css-classes
Specifies CSS classes to be added to the root element of the code view. When setting this option using a data attribute, separate multiple CSS classes with spaces.
Properties
CodeView 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
appendTo | Appends code view to element. |
---|---|
detach | Detaches code view from its parent element. |
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. |
appendTo
appendTo(element: HTMLElement): void
Appends code view to element.
Params
element : HTMLElement | Element to append code view to. |
---|
detach
detach(): void
Detaches code view from its parent element.
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. |
---|