# API of `@koobiq/components/code-block`

Documented on [Code block](https://koobiq.io/en/components/code-block.md). NgModule: `KbqCodeBlockModule`.

## Components

### KbqCodeBlock (component)

Component which highlights blocks of code.

```ts
@Component({ selector: 'kbq-code-block', exportAs: 'kbqCodeBlock' })
class KbqCodeBlock {
    readonly lineNumbers = input<boolean>(
        this.defaultOptions?.lineNumbers ?? false
    );
    readonly filled = input<boolean>(false);
    readonly canToggleSoftWrap = input<boolean>(false);
    readonly softWrap = model<boolean>(false);
    readonly viewAll = model<boolean>(false);
    readonly maxHeight = input<number | undefined>();
    readonly canDownload = input<boolean>(false);
    readonly canCopy = input<boolean>(true);
    readonly alwaysShowActionbar = input<boolean>(
        this.defaultOptions?.alwaysShowActionbar ?? false
    );
    readonly files = input<KbqCodeBlockFile[]>([]);
    readonly activeFileIndex = model<number>(0);
    readonly noBorder = input<boolean>(false);
    readonly hideTabs = model<boolean>(false);
    toggleViewAll(): void;
    scrollTo(options: KbqScrollbarScrollToOptions): void;
    toggleSoftWrap(): void;
}
```

- `[lineNumbers]`: `boolean` — Whether to display line numbers. Defaults to `lineNumbers` of `KBQ_CODE_BLOCK_DEFAULT_OPTIONS`, or `false`.
- `[filled]`: `boolean` — Whether the code block should be filled.
- `[canToggleSoftWrap]`: `boolean` — Added soft wrap toggle button.
- `[(softWrap)]`: `boolean` — Whether sequences of whitespace should be preserved.
- `[(viewAll)]`: `boolean` — Allows to view all the code, otherwise it will be hidden.
  Works only with `maxHeight` property.
- `[maxHeight]`: `number | undefined` — Maximum height of the code block content, other parts will be hidden.
  Can be toggled by `viewAll` property.
- `[canDownload]`: `boolean` — Added download code button.
- `[canCopy]`: `boolean` — Added copy code button.
- `[alwaysShowActionbar]`: `boolean` — Whether the actionbar should remain visible when tabs are hidden.
- `[files]`: `KbqCodeBlockFile[]` — Files to display.
- `[(activeFileIndex)]`: `number` — Defines which file (index) is active.
- `[noBorder]`: `boolean` — Whether to hide border.
- `[(hideTabs)]`: `boolean` — Whether to hide header tabs. A single file without a filename hides them regardless - read
  `tabsHidden` for what the header actually does.
  Makes actionbar floating if tabs are hidden.
- `[localeOverrides]`: `KbqPartialLocaleData | undefined` — Localized strings for this element and its subtree, keyed by locale section. Every key is optional at
  every depth.
- `toggleViewAll()`: `void` — Toggles the `viewAll` property.

  When `viewAll` is set to `true`, the content of the code block will be
  displayed in its entirety. When set to `false`, the content will be
  limited to the `maxHeight` property. If the content exceeds the
  `maxHeight`, it will be scrolled to the top and the `viewAll` button
  will be displayed.
- `scrollTo()`: `void` — Scrolls the code content to the specified position.
- `toggleSoftWrap()`: `void` — Toggles `softWrap` property.

  When `softWrap` is set to `true`, the content of the code block will be
  wrapped if it exceeds the height of the component. When set to `false`
  the content will not be wrapped.

## Directives

### KbqCodeBlockTabLinkContent (directive)

Marks a template as a custom tab link.

```ts
@Directive({
    selector: 'ng-template[kbqCodeBlockTabLinkContent]',
    exportAs: 'kbqCodeBlockTabLinkContent'
})
class KbqCodeBlockTabLinkContent {}
```

## Types

### KbqCodeBlockDefaultOptions (type)

Default options for `kbq-code-block`.

```ts
type KbqCodeBlockDefaultOptions = Partial<{
    alwaysShowActionbar: boolean;
    lineNumbers: boolean;
}>;
```

- `alwaysShowActionbar?`: `boolean` — Whether the actionbar should remain visible when tabs are hidden. Defaults to `false`.
- `lineNumbers?`: `boolean` — Whether to display line numbers. Defaults to `false`.

### KbqCodeBlockFile (type)

Code block file object.

```ts
type KbqCodeBlockFile = {
    content: string;
    filename?: string;
    language?: string;
    link?: string;
};
```

- `content`: `string` — Code content.
- `filename?`: `string` — File name, which will be displayed in UI and when downloading.
  If not provided, will be set to `KBQ_CODE_BLOCK_FALLBACK_FILE_NAME`.
- `language?`: `string` — File language, required for correct syntax highlighting. It has to be registered in `languages` of
  `KbqCodeBlockHighlightJsConfig`, unless the full bundle of highlight.js is loaded. If not provided or not
  registered, will be set to `KBQ_CODE_BLOCK_FALLBACK_FILE_LANGUAGE`, or to `plaintext` if that is not registered
  either.

  See the [list of supported languages](https://github.com/highlightjs/highlight.js/blob/11-stable/SUPPORTED_LANGUAGES.md).
- `link?`: `string` — Link to the file, which will be opened in a new window.
  Adds `openExternalSystem` button.

### KbqCodeBlockHighlightJsConfig (type)

Configuration of highlight.js, which highlights the code of a code block: how to load it and which languages to
register. Without a configuration, the code block loads the highlight.js core, which has no languages, and shows the
code as plain text. Provide it with `kbqCodeBlockHighlightJsConfigProvider`.

```ts
type KbqCodeBlockHighlightJsConfig = Partial<{
    core: () => Promise<{ default: HLJSApi }>;
    languages: Record<string, () => Promise<{ default: LanguageFn }>>;
}>;
```

- `core?`: `() => Promise<{ default: HLJSApi }>` — Lazy loader for the highlight.js instance. The core has no languages: register the ones the files use in
  `languages`, or load all of them with `() => import('highlight.js')`, the full bundle of about 1 MB.
  Defaults to `() => import('highlight.js/lib/core')`.
- `languages?`: `Record<string, () => Promise<{ default: LanguageFn }>>` — Map of language name to lazy loader for that language's LanguageFn. A language of highlight.js loads from
  `highlight.js/lib/languages/<name>`, where `<name>` is the name of its file in
  [src/languages](https://github.com/highlightjs/highlight.js/tree/11-stable/src/languages). Plain text needs no
  entry: the code block registers it as the fallback language.

### KbqTabLinkTemplateContext (type)

Context provided to the tab link template: `$implicit` is the code block file for the current tab, and
`fallbackFileName` is displayed when the file has no explicit name.

```ts
type KbqTabLinkTemplateContext = {
    $implicit: KbqCodeBlockFile;
    fallbackFileName: string;
};
```

## Constants

### KBQ_CODE_BLOCK_DEFAULT_OPTIONS (const)

Injection token used to configure the default options for all `kbq-code-block` components.

```ts
const KBQ_CODE_BLOCK_DEFAULT_OPTIONS: InjectionToken<KbqCodeBlockDefaultOptions>;
```

### KBQ_CODE_BLOCK_FALLBACK_FILE_LANGUAGE (const)

Fallback language for code block if language is not supported/specified. It has to be registered in `languages` of
`KbqCodeBlockHighlightJsConfig`, unless it is plain text or the full bundle of highlight.js is loaded, or the code
block falls back to `plaintext`.

See the [list of supported languages](https://github.com/highlightjs/highlight.js/blob/11-stable/SUPPORTED_LANGUAGES.md).

```ts
const KBQ_CODE_BLOCK_FALLBACK_FILE_LANGUAGE: InjectionToken<string>;
```

### KBQ_CODE_BLOCK_FALLBACK_FILE_NAME (const)

Fallback file name for code block if file name is not specified.

```ts
const KBQ_CODE_BLOCK_FALLBACK_FILE_NAME: InjectionToken<string>;
```

### KBQ_CODE_BLOCK_HIGHLIGHT_JS_CONFIG (const)

Injection token for configuring `KbqCodeBlockHighlightJsConfig`.

```ts
const KBQ_CODE_BLOCK_HIGHLIGHT_JS_CONFIG: InjectionToken<KbqCodeBlockHighlightJsConfig>;
```

### KBQ_CODE_BLOCK_LOCALE_CONFIGURATION (const)

Localization configuration provider.

```ts
const KBQ_CODE_BLOCK_LOCALE_CONFIGURATION: InjectionToken<KbqCodeBlockLocaleConfiguration>;
```

### kbqCodeBlockDefaultOptionsProvider (const)

Utility provider for `KBQ_CODE_BLOCK_DEFAULT_OPTIONS`.

```ts
const kbqCodeBlockDefaultOptionsProvider: (
    options: KbqCodeBlockDefaultOptions
) => Provider;
```

### kbqCodeBlockFallbackFileLanguageProvider (const)

Utility provider for `KBQ_CODE_BLOCK_FALLBACK_FILE_LANGUAGE`.

```ts
const kbqCodeBlockFallbackFileLanguageProvider: (
    language: string
) => Provider;
```

### kbqCodeBlockFallbackFileNameProvider (const)

Utility provider for `KBQ_CODE_BLOCK_FALLBACK_FILE_NAME`.

```ts
const kbqCodeBlockFallbackFileNameProvider: (
    fileName: string
) => Provider;
```

### kbqCodeBlockHighlightJsConfigProvider (const)

Utility provider for `KBQ_CODE_BLOCK_HIGHLIGHT_JS_CONFIG`.

```ts
providers: [
  kbqCodeBlockHighlightJsConfigProvider({
    languages: {
      typescript: () => import('highlight.js/lib/languages/typescript'),
      css: () => import('highlight.js/lib/languages/css'),
      html: () => import('highlight.js/lib/languages/xml')
    }
  })
]
```

```ts
const kbqCodeBlockHighlightJsConfigProvider: (
    options: KbqCodeBlockHighlightJsConfig
) => Provider;
```

### kbqCodeBlockLocaleConfigurationProvider (const)

Utility provider for `KBQ_CODE_BLOCK_LOCALE_CONFIGURATION`. Only the strings you pass are overridden;
the rest keep following the active locale.

```ts
const kbqCodeBlockLocaleConfigurationProvider: (
    configuration: KbqDeepPartial<KbqCodeBlockLocaleConfiguration>
) => Provider;
```
