-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy patheditor.component.ts
executable file
·101 lines (82 loc) · 2.69 KB
/
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
93
94
95
96
97
98
99
100
101
import { Component, forwardRef, Inject, Input, NgZone } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { BaseEditor } from './base-editor';
import { NGX_MONACO_EDITOR_CONFIG, NgxMonacoEditorConfig } from './config';
import { NgxEditorModel } from './types';
import { fromEvent } from 'rxjs';
import { MonacoService } from './monaco.service';
@Component({
selector: 'ngx-monaco-editor',
template: '<div class="editor-container" #editorContainer></div>',
styles: [`
:host {
display: block;
height: 200px;
}
.editor-container {
width: 100%;
height: 98%;
}
`],
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => EditorComponent),
multi: true
}]
})
export class EditorComponent extends BaseEditor implements ControlValueAccessor {
private _value: string = '';
propagateChange = (_: any) => {};
onTouched = () => {};
@Input('model')
set model(model: NgxEditorModel) {
this.options.model = model;
if (this._editor) {
this._editor.dispose();
this.initMonaco(this.options);
}
}
constructor(monacoService: MonacoService, private zone: NgZone, @Inject(NGX_MONACO_EDITOR_CONFIG) private editorConfig: NgxMonacoEditorConfig) {
super(monacoService, editorConfig);
}
writeValue(value: any): void {
this._value = value || '';
// Fix for value change while dispose in process.
setTimeout(() => {
if (this._editor && !this.options.model) {
this._editor.setValue(this._value);
}
});
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
protected initMonaco(options: any): void {
const hasModel = !!options.model;
if (hasModel) {
options.model = monaco.editor.createModel(options.model.value, options.model.language, options.model.uri);
}
this._editor = monaco.editor.create(this._editorContainer.nativeElement, options);
if (!hasModel) {
this._editor.setValue(this._value);
}
this._editor.onDidChangeModelContent((e: any) => {
const value = this._editor.getValue();
this.propagateChange(value);
// value is not propagated to parent when executing outside zone.
this.zone.run(() => this._value = value);
});
this._editor.onDidBlurEditor((e: any) => {
this.onTouched();
});
// refresh layout on resize event.
if (this._windowResizeSubscription) {
this._windowResizeSubscription.unsubscribe();
}
this._windowResizeSubscription = fromEvent(window, 'resize').subscribe(() => this._editor.layout());
this.onInit.emit(this._editor);
}
}