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.

highlightSpecifies lines of code to be highlighted.
showGutterSpecifies whether the gutter (the area where line numbers appear) should be visible.
showLineNumbersSpecifies whether line numbers should be visible.
lineHeightSpecifies the line height.
lineHeightUnitSpecifies the unit for the line height.
cssClassesCSS 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 : numberLine height of code.
lineHeightUnit : stringLine height unit of code.
linesCount : numberNumber of lines of code.

Methods

appendToAppends code view to element.
detachDetaches code view from its parent element.
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.

appendTo

appendTo(element: HTMLElement): void

Appends code view to element.

Params

element : HTMLElementElement 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

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.