From 0507945c6623ebd60bef346326e791d47a79ed27 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 4 Jan 2021 20:52:45 +0100 Subject: [PATCH] Performance improvement. --- .../forms/editors/code-editor.component.ts | 43 ++++++++----------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/frontend/app/framework/angular/forms/editors/code-editor.component.ts b/frontend/app/framework/angular/forms/editors/code-editor.component.ts index edc2b8fe2..121f210e2 100644 --- a/frontend/app/framework/angular/forms/editors/code-editor.component.ts +++ b/frontend/app/framework/angular/forms/editors/code-editor.component.ts @@ -28,10 +28,9 @@ export const SQX_CODE_EDITOR_CONTROL_VALUE_ACCESSOR: any = { changeDetection: ChangeDetectionStrategy.OnPush }) export class CodeEditorComponent extends StatefulControlComponent<{}, string> implements AfterViewInit, FocusComponent, OnChanges { - private valueChanged = new Subject(); private aceEditor: any; - private value: any; - private valueString: string; + private valueChanged = new Subject(); + private value = ''; private modelist: any; @ViewChild('editor', { static: false }) @@ -59,19 +58,16 @@ export class CodeEditorComponent extends StatefulControlComponent<{}, string> im } public writeValue(obj: string) { - this.value = obj; - if (this.valueMode === 'Json') { - this.value = obj; - try { - this.valueString = JSON.stringify(obj); + this.value = JSON.stringify(obj, undefined, 4); } catch (e) { - this.valueString = ''; + this.value = ''; } + } else if (Types.isString(obj)) { + this.value = obj; } else { - this.value = Types.isString(obj) ? obj : ''; - this.valueString = this.value; + this.value = ''; } if (this.aceEditor) { @@ -121,7 +117,7 @@ export class CodeEditorComponent extends StatefulControlComponent<{}, string> im this.aceEditor.setFontSize(14); this.setDisabledState(this.snapshot.isDisabled); - this.setValue(this.valueString); + this.setValue(this.value); this.setMode(); this.aceEditor.on('blur', () => { @@ -138,31 +134,30 @@ export class CodeEditorComponent extends StatefulControlComponent<{}, string> im } private changeValue() { - let newValue: any = null; - let newValueString: string; + let newValue = this.aceEditor.getValue(); + let newValueOut = newValue; if (this.valueMode === 'Json') { const isValid = this.aceEditor.getSession().getAnnotations().length === 0; if (isValid) { try { - newValue = JSON.parse(this.aceEditor.getValue()); + newValueOut = JSON.parse(newValue); } catch (e) { - newValue = null; + newValueOut = null; + newValue = ''; } + } else { + newValueOut = null; + newValue = ''; } - - newValueString = JSON.stringify(newValue); - } else { - newValueString = newValue = this.aceEditor.getValue(); } - if (this.valueString !== newValueString) { - this.callChange(newValue); + if (this.value !== newValue) { + this.callChange(newValueOut); } this.value = newValue; - this.valueString = newValueString; } private setMode() { @@ -178,7 +173,7 @@ export class CodeEditorComponent extends StatefulControlComponent<{}, string> im } private setValue(value: string) { - this.aceEditor.setValue(value || ''); + this.aceEditor.setValue(value); this.aceEditor.clearSelection(); } } \ No newline at end of file