Tableau UI for Angular
Monaco Editor
The Monaco editor, the code editor of VS Code, as a form control or with a value binding.
Set up
- Install
monaco-editor, an optional peer that only this entry point needs:
npm install monaco-editor@^0.52.2- Serve its files at
monaco-edit, where the editor loads them from: add the second entry here to theassetsof the app's build options inangular.json, after the app's own, such as thepublicfolder a new app has.
"assets": [
{ "glob": "**/*", "input": "public" },
{ "glob": "**/*", "input": "node_modules/monaco-editor/min", "output": "/monaco-edit" }
]- Import
TableauUiMonacoModulefromtableau-ui-angular/monaco.
Monaco loads in the browser only, once, after the first render; on the server the editor is an empty element.
Value binding
tab-monaco-editor fills its element, which needs a height.
| Input or output | What it does |
|---|---|
uri | Required. Names the document: Monaco keeps one per URI, so each editor on a page needs its own |
language | Required. The language that colours the text, such as json or sql |
[(value)] | The text. (valueChange) emits the user's edits only, never a form's writes |
options | Monaco's editor options, such as minimap, readOnly or ariaLabel; a change starts the editor again |
theme | auto, the default, follows the theme service's light or dark mode; or one of Monaco's, such as vs-dark. Monaco has one theme for the whole page, so give every editor on a page the same |
onMonacoLoad | A function that gets Monaco before the editor starts, such as to set a language's diagnostics |
disposeModelOnDestroy | On by default: the document goes with the editor. Off, Monaco keeps it by its URI for another editor |
(editorLoaded) | Emits Monaco's editor once it runs |
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import type { MonacoConstructionOptions } from 'tableau-ui-angular/monaco';
import { TableauUiMonacoModule } from 'tableau-ui-angular/monaco';
@Component({
selector: 'app-monaco-value-example',
imports: [TableauUiMonacoModule],
templateUrl: './monaco-value.example.html',
styleUrl: './monaco-value.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MonacoValueExampleComponent {
protected readonly $json = signal('{\n "name": "my-app",\n "private": true\n}\n');
// Monaco's own options; ariaLabel names its text box for screen readers
protected readonly options: MonacoConstructionOptions = {
ariaLabel: 'package.json',
minimap: { enabled: false },
scrollBeyondLastLine: false,
};
protected readonly $valid = computed(() => {
try {
JSON.parse(this.$json());
return true;
} catch {
return false;
}
});
}Reactive forms
Bind [formControl] or formControlName. The control is touched when the editor loses focus, and a disabled control makes the editor read-only.
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { TableauUiCheckboxModule } from 'tableau-ui-angular/checkbox';
import { FB, TableauUiFormModule } from 'tableau-ui-angular/form';
import type { MonacoConstructionOptions } from 'tableau-ui-angular/monaco';
import { TableauUiMonacoModule } from 'tableau-ui-angular/monaco';
@Component({
selector: 'app-monaco-forms-example',
imports: [ReactiveFormsModule, TableauUiCheckboxModule, TableauUiFormModule, TableauUiMonacoModule],
templateUrl: './monaco-forms.example.html',
styleUrl: './monaco-forms.example.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MonacoFormsExampleComponent {
private readonly fb = inject(FB);
protected readonly query = this.fb.control<string>('SELECT region, SUM(sales)\nFROM orders\nGROUP BY region\n');
protected readonly options: MonacoConstructionOptions = {
ariaLabel: 'Query',
minimap: { enabled: false },
scrollBeyondLastLine: false,
};
// A disabled control makes the editor read-only
protected setReadOnly(readOnly: boolean | 'partial') {
if (readOnly === true) {
this.query.metaFn.disable();
} else {
this.query.metaFn.enable();
}
}
}