-
Notifications
You must be signed in to change notification settings - Fork 10
/
monaco-editor.component.ts
92 lines (77 loc) · 2.55 KB
/
monaco-editor.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { ChangeDetectionStrategy, Component, forwardRef, Input } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { NuMonacoEditorBase } from './monaco-editor-base.component';
import { NuMonacoEditorModel } from './monaco-editor.types';
@Component({
selector: 'nu-monaco-editor',
template: ``,
exportAs: 'nuMonacoEditor',
host: {
'[style.display]': `'block'`,
'[style.height]': 'height',
},
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NuMonacoEditorComponent),
multi: true,
},
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NuMonacoEditorComponent extends NuMonacoEditorBase implements ControlValueAccessor {
private _value = '';
@Input() model?: NuMonacoEditorModel | null;
get editor(): monaco.editor.IStandaloneCodeEditor {
return this._editor as monaco.editor.IStandaloneCodeEditor;
}
private onChange = (_: string) => {};
private onTouched = () => {};
initMonaco(options: monaco.editor.IStandaloneEditorConstructionOptions, initEvent: boolean): void {
const hasModel = !!this.model;
if (hasModel) {
const model = monaco.editor.getModel(this.model!.uri! || '');
if (model) {
options.model = model;
options.model.setValue(this._value);
} else {
const { value, language, uri } = this.model!;
options.model = monaco.editor.createModel(value || this._value, language, uri);
}
}
if (this._disabled != null) options.readOnly = this._disabled;
const editor = (this._editor = monaco.editor.create(this.el.nativeElement, options));
if (!hasModel) {
editor.setValue(this._value);
}
editor.onDidChangeModelContent(() => {
const value = editor.getValue();
this.ngZone.run(() => {
this._value = value;
this.onChange(value);
});
});
editor.onDidBlurEditorWidget(() => this.onTouched());
this.registerResize();
editor
.getAction('editor.action.formatDocument')
.run()
.then(() => this.notifyEvent(initEvent ? 'init' : 're-init'));
}
writeValue(value: string): void {
this._value = value || '';
if (this._editor) {
(this._editor as monaco.editor.IStandaloneCodeEditor).setValue(this._value);
}
}
registerOnChange(fn: (_: string) => void): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState(_isDisabled: boolean): void {
this.disabled = _isDisabled;
this.setDisabled();
}
}