Monaco Code Editor for Angular
npm install @vality/ng-monaco-editor- Angular 20: 20.x.x
Using this Module you can utilize the Monaco Editor as an Angular Component. Feel free to contribute, raise feature requests and make it better.
Supports all the options available in monaco-editor Monaco Editor Options
https://ngx-monaco-editor-v2.surge.sh/
Install from npm repository:
```
npm install monaco-editor @vality/ng-monaco-editor --save
Add the glob to assets in angular.json
`typescript`
{
"apps": [
{
"assets": [
{ "glob": "*/", "input": "node_modules/monaco-editor", "output": "/assets/monaco/" }
],
...
}
...
],
...
}
Include MonacoEditorModule in Main Module and Feature Modules where you want to use the editor component.(eg: app.module.ts):
`typescript
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { AppComponent } from "./app.component";
import { MonacoEditorModule } from "@vality/ng-monaco-editor";
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
FormsModule,
MonacoEditorModule.forRoot(), // use forRoot() in main app module only.
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
`
Create Editor options in component.(eg: app.component.ts)
`typescript
import { Component } from "@angular/core";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
})
export class AppComponent {
editorOptions = { theme: "vs-dark", language: "javascript" };
code: string = 'function x() {\nconsole.log("Hello world!");\n}';
}
`
Include editor in html with options and ngModel bindings.(eg: app.component.html)
`html`
[(ngModel)]="code"
>
Include diff-editor in html with options.(eg: app.component.html)
`html`
[originalModel]="originalModel"
[modifiedModel]="modifiedModel"
>
`typescript
import { Component } from "@angular/core";
import { DiffEditorModel } from "@vality/ng-monaco-editor";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
})
export class AppComponent {
options = {
theme: "vs-dark",
};
originalModel: DiffEditorModel = {
code: "heLLo world!",
language: "text/plain",
};
modifiedModel: DiffEditorModel = {
code: "hello orlando!",
language: "text/plain",
};
}
`
To match height of container element add height: 100% and wrap in container
`html`
[options]="editorOptions"
[(ngModel)]="code"
>
Add class to editor tag. (eg. class="my-code-editor")
`html`
[options]="editorOptions"
[(ngModel)]="code"
>
Add styling in css/scss file:
`scss`
.my-code-editor {
.editor-container {
height: calc(100vh - 100px);
}
}
Set automaticLayout option to adjust editor size dynamically. Recommended when using in modal dialog or tabs where editor is not visible initially.
Output event (onInit) expose editor instance that can be used for performing custom operations on the editor.
`html`
[(ngModel)]="code"
(onInit)="onInit($event)"
>
`typescript`
export class AppComponent {
editorOptions = { theme: "vs-dark", language: "javascript" };
code: string = 'function x() {\nconsole.log("Hello world!");\n}';
onInit(editor) {
let line = editor.getPosition();
console.log(line);
}
}
forRoot() method of MonacoEditorModule accepts config of type NgxMonacoEditorConfig.
`typescript
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor-v2';
import { AppComponent } from './app.component';
const monacoConfig: NgxMonacoEditorConfig = {
baseUrl: 'app-name/assets', // configure base path for monaco editor. Starting with version 8.0.0 it defaults to './assets'. Previous releases default to '/assets'
defaultOptions: { scrollBeyondLastLine: false }, // pass default options to be used
onMonacoLoad: () => { console.log((
requireConfig: { preferScriptTags: true } // allows to oweride configuration passed to monacos loader
monacoRequire: (
};
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
MonacoEditorModule.forRoot(monacoConfig)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
`
onMonacoLoad property of NgxMonacoEditorConfig can be used to configure JSON default.
`typescript
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { BrowserModule } from "@angular/platform-browser";
import {
MonacoEditorModule,
NgxMonacoEditorConfig,
} from "@vality/ng-monaco-editor";
import { AppComponent } from "./app.component";
export function onMonacoLoad() {
console.log((window as any).monaco);
const uri = monaco.Uri.parse("a://b/foo.json");
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
validate: true,
schemas: [
{
uri: "http://myserver/foo-schema.json",
fileMatch: [uri.toString()],
schema: {
type: "object",
properties: {
p1: {
enum: ["v1", "v2"],
},
p2: {
$ref: "http://myserver/bar-schema.json",
},
},
},
},
{
uri: "http://myserver/bar-schema.json",
fileMatch: [uri.toString()],
schema: {
type: "object",
properties: {
q1: {
enum: ["x1", "x2"],
},
},
},
},
],
});
}
const monacoConfig: NgxMonacoEditorConfig = {
baseUrl: "assets",
defaultOptions: { scrollBeyondLastLine: false },
onMonacoLoad,
};
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
FormsModule,
MonacoEditorModule.forRoot(monacoConfig),
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
`
Now pass model config of type NgxEditorModel to Editor Component
`typescript
@Component({
selector: "app-root",
template:
[model]="model"
>,
styles: [],
})
export class AppComponent {
options = {
theme: "vs-dark",
};
jsonCode = ["{", ' "p1": "v3",', ' "p2": false', "}"].join("\n");
model: NgxEditorModel = {
value: this.jsonCode,
language: "json",
uri: monaco.Uri.parse("a://b/foo.json"),
};
}
`
If you expose node's require in your render process, monaco will try to use its NodeScriptLoader and fail to load its files. To presuade it to use its BrowserScriptLoader instead it is necessery to set preferScriptTags to true.
`typescript
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { BrowserModule } from "@angular/platform-browser";
import {
MonacoEditorModule,
NgxMonacoEditorConfig,
} from "@vality/ng-monaco-editor";
import { AppComponent } from "./app.component";
const monacoConfig: NgxMonacoEditorConfig = {
baseUrl: "assets",
requireConfig: { preferScriptTags: true },
};
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
FormsModule,
MonacoEditorModule.forRoot(monacoConfig),
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
`
If for some reason you want to load monaco yourself.
`html
You just need to save monaco
require function defined in loader.js somewhere and pass it to monacoRequire in configuration.`typescript
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { BrowserModule } from "@angular/platform-browser";import {
MonacoEditorModule,
NgxMonacoEditorConfig,
} from "@vality/ng-monaco-editor";
import { AppComponent } from "./app.component";
const monacoConfig: NgxMonacoEditorConfig = {
baseUrl: "assets",
requireConfig: { preferScriptTags: true },
monacoRequire: (window as any).monacoRequire,
};
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
FormsModule,
MonacoEditorModule.forRoot(monacoConfig),
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
``Monaco Editor
Monaco Editor Options
MIT © Miroslav Maksimovic