forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-editor.component.ts
323 lines (279 loc) · 10.2 KB
/
code-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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
import { Platform } from '@angular/cdk/platform';
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
ElementRef,
EventEmitter,
forwardRef,
Input,
NgZone,
OnDestroy,
Output,
TemplateRef,
ViewEncapsulation
} from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { BehaviorSubject, combineLatest, fromEvent, Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged, filter, map, takeUntil } from 'rxjs/operators';
import { editor, IDisposable } from 'monaco-editor';
import { warn } from 'ng-zorro-antd/core/logger';
import { BooleanInput, NzSafeAny, OnChangeType, OnTouchedType } from 'ng-zorro-antd/core/types';
import { inNextTick, InputBoolean } from 'ng-zorro-antd/core/util';
import { NzCodeEditorService } from './code-editor.service';
import { DiffEditorOptions, EditorOptions, JoinedEditorOptions, NzEditorMode } from './typings';
// Import types from monaco editor.
import ITextModel = editor.ITextModel;
import IStandaloneCodeEditor = editor.IStandaloneCodeEditor;
import IStandaloneDiffEditor = editor.IStandaloneDiffEditor;
declare const monaco: NzSafeAny;
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
selector: 'nz-code-editor',
exportAs: 'nzCodeEditor',
template: `
<div class="ant-code-editor-loading" *ngIf="nzLoading">
<nz-spin></nz-spin>
</div>
<div class="ant-code-editor-toolkit" *ngIf="nzToolkit">
<ng-template [ngTemplateOutlet]="nzToolkit"></ng-template>
</div>
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NzCodeEditorComponent),
multi: true
}
]
})
export class NzCodeEditorComponent implements OnDestroy, AfterViewInit {
static ngAcceptInputType_nzLoading: BooleanInput;
static ngAcceptInputType_nzFullControl: BooleanInput;
@Input() nzEditorMode: NzEditorMode = 'normal';
@Input() nzOriginalText = '';
@Input() @InputBoolean() nzLoading = false;
@Input() @InputBoolean() nzFullControl = false;
@Input() nzToolkit?: TemplateRef<void>;
@Input() set nzEditorOption(value: JoinedEditorOptions) {
this.editorOption$.next(value);
}
@Output() readonly nzEditorInitialized = new EventEmitter<IStandaloneCodeEditor | IStandaloneDiffEditor>();
editorOptionCached: JoinedEditorOptions = {};
private readonly el: HTMLElement;
private destroy$ = new Subject<void>();
private resize$ = new Subject<void>();
private editorOption$ = new BehaviorSubject<JoinedEditorOptions>({});
private editorInstance: IStandaloneCodeEditor | IStandaloneDiffEditor | null = null;
private value = '';
private modelSet = false;
private onDidChangeContentDisposable: IDisposable | null = null;
constructor(
private nzCodeEditorService: NzCodeEditorService,
private ngZone: NgZone,
elementRef: ElementRef,
private platform: Platform
) {
this.el = elementRef.nativeElement;
this.el.classList.add('ant-code-editor');
}
/**
* Initialize a monaco editor instance.
*/
ngAfterViewInit(): void {
if (!this.platform.isBrowser) {
return;
}
this.nzCodeEditorService
.requestToInit()
.pipe(takeUntil(this.destroy$))
.subscribe(option => this.setup(option));
}
ngOnDestroy(): void {
if (this.onDidChangeContentDisposable) {
this.onDidChangeContentDisposable.dispose();
this.onDidChangeContentDisposable = null;
}
if (this.editorInstance) {
this.editorInstance.dispose();
this.editorInstance = null;
}
this.destroy$.next();
this.destroy$.complete();
}
writeValue(value: string): void {
this.value = value;
this.setValue();
}
registerOnChange(fn: OnChangeType): NzSafeAny {
this.onChange = fn;
}
registerOnTouched(fn: OnTouchedType): void {
this.onTouch = fn;
}
onChange: OnChangeType = (_value: string) => {};
onTouch: OnTouchedType = () => {};
layout(): void {
this.resize$.next();
}
private setup(option: JoinedEditorOptions): void {
// The `setup()` is invoked when the Monaco editor is loaded. This may happen asynchronously for the first
// time, and it'll always happen synchronously afterwards. The first `setup()` invokation is outside the Angular
// zone, but further invokations will happen within the Angular zone. We call the `setModel()` on the editor
// instance, which tells Monaco to add event listeners lazily internally (`mousemove`, `mouseout`, etc.).
// We should avoid adding them within the Angular zone since this will drastically affect the performance.
this.ngZone.runOutsideAngular(() =>
inNextTick()
.pipe(takeUntil(this.destroy$))
.subscribe(() => {
this.editorOptionCached = option;
this.registerOptionChanges();
this.initMonacoEditorInstance();
this.registerResizeChange();
this.setValue();
if (!this.nzFullControl) {
this.setValueEmitter();
}
if (this.nzEditorInitialized.observers.length) {
this.ngZone.run(() => this.nzEditorInitialized.emit(this.editorInstance!));
}
})
);
}
private registerOptionChanges(): void {
combineLatest([this.editorOption$, this.nzCodeEditorService.option$])
.pipe(takeUntil(this.destroy$))
.subscribe(([selfOpt, defaultOpt]) => {
this.editorOptionCached = {
...this.editorOptionCached,
...defaultOpt,
...selfOpt
};
this.updateOptionToMonaco();
});
}
private initMonacoEditorInstance(): void {
this.ngZone.runOutsideAngular(() => {
this.editorInstance =
this.nzEditorMode === 'normal'
? monaco.editor.create(this.el, { ...this.editorOptionCached })
: monaco.editor.createDiffEditor(this.el, {
...(this.editorOptionCached as DiffEditorOptions)
});
});
}
private registerResizeChange(): void {
this.ngZone.runOutsideAngular(() => {
fromEvent(window, 'resize')
.pipe(debounceTime(300), takeUntil(this.destroy$))
.subscribe(() => {
this.layout();
});
this.resize$
.pipe(
takeUntil(this.destroy$),
filter(() => !!this.editorInstance),
map(() => ({
width: this.el.clientWidth,
height: this.el.clientHeight
})),
distinctUntilChanged((a, b) => a.width === b.width && a.height === b.height),
debounceTime(50)
)
.subscribe(() => {
this.editorInstance!.layout();
});
});
}
private setValue(): void {
if (!this.editorInstance) {
return;
}
if (this.nzFullControl && this.value) {
warn(`should not set value when you are using full control mode! It would result in ambiguous data flow!`);
return;
}
if (this.nzEditorMode === 'normal') {
if (this.modelSet) {
const model = this.editorInstance.getModel() as ITextModel;
this.preservePositionAndSelections(() => model.setValue(this.value));
} else {
(this.editorInstance as IStandaloneCodeEditor).setModel(
monaco.editor.createModel(this.value, (this.editorOptionCached as EditorOptions).language)
);
this.modelSet = true;
}
} else {
if (this.modelSet) {
const model = (this.editorInstance as IStandaloneDiffEditor).getModel()!;
this.preservePositionAndSelections(() => {
model.modified.setValue(this.value);
model.original.setValue(this.nzOriginalText);
});
} else {
const language = (this.editorOptionCached as EditorOptions).language;
(this.editorInstance as IStandaloneDiffEditor).setModel({
original: monaco.editor.createModel(this.nzOriginalText, language),
modified: monaco.editor.createModel(this.value, language)
});
this.modelSet = true;
}
}
}
/**
* {@link editor.ICodeEditor}#setValue resets the cursor position to the start of the document.
* This helper memorizes the cursor position and selections and restores them after the given
* function has been called.
*/
private preservePositionAndSelections(fn: () => unknown): void {
if (!this.editorInstance) {
fn();
return;
}
const position = this.editorInstance.getPosition();
const selections = this.editorInstance.getSelections();
fn();
if (position) {
this.editorInstance.setPosition(position);
}
if (selections) {
this.editorInstance.setSelections(selections);
}
}
private setValueEmitter(): void {
const model = (
this.nzEditorMode === 'normal'
? (this.editorInstance as IStandaloneCodeEditor).getModel()
: (this.editorInstance as IStandaloneDiffEditor).getModel()!.modified
) as ITextModel;
// The `onDidChangeContent` returns a disposable object (an object with `dispose()` method) which will cleanup
// the listener. The callback, that we pass to `onDidChangeContent`, captures `this`. This leads to a circular reference
// (`nz-code-editor -> monaco -> nz-code-editor`) and prevents the `nz-code-editor` from being GC'd.
this.onDidChangeContentDisposable = model.onDidChangeContent(() => {
this.emitValue(model.getValue());
});
}
private emitValue(value: string): void {
if (this.value === value) {
// If the value didn't change there's no reason to send an update.
// Specifically this may happen during an update from the model (writeValue) where sending an update to the model would actually be incorrect.
return;
}
this.value = value;
// We're re-entering the Angular zone only if the value has been changed since there's a `return` expression previously.
// This won't cause "dead" change detections (basically when the `tick()` has been run, but there's nothing to update).
this.ngZone.run(() => {
this.onChange(value);
});
}
private updateOptionToMonaco(): void {
if (this.editorInstance) {
this.editorInstance.updateOptions({ ...this.editorOptionCached });
}
}
}