Browse Source

Performance improvement.

pull/614/head
Sebastian 5 years ago
parent
commit
0507945c66
  1. 43
      frontend/app/framework/angular/forms/editors/code-editor.component.ts

43
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 changeDetection: ChangeDetectionStrategy.OnPush
}) })
export class CodeEditorComponent extends StatefulControlComponent<{}, string> implements AfterViewInit, FocusComponent, OnChanges { export class CodeEditorComponent extends StatefulControlComponent<{}, string> implements AfterViewInit, FocusComponent, OnChanges {
private valueChanged = new Subject();
private aceEditor: any; private aceEditor: any;
private value: any; private valueChanged = new Subject();
private valueString: string; private value = '';
private modelist: any; private modelist: any;
@ViewChild('editor', { static: false }) @ViewChild('editor', { static: false })
@ -59,19 +58,16 @@ export class CodeEditorComponent extends StatefulControlComponent<{}, string> im
} }
public writeValue(obj: string) { public writeValue(obj: string) {
this.value = obj;
if (this.valueMode === 'Json') { if (this.valueMode === 'Json') {
this.value = obj;
try { try {
this.valueString = JSON.stringify(obj); this.value = JSON.stringify(obj, undefined, 4);
} catch (e) { } catch (e) {
this.valueString = ''; this.value = '';
} }
} else if (Types.isString(obj)) {
this.value = obj;
} else { } else {
this.value = Types.isString(obj) ? obj : ''; this.value = '';
this.valueString = this.value;
} }
if (this.aceEditor) { if (this.aceEditor) {
@ -121,7 +117,7 @@ export class CodeEditorComponent extends StatefulControlComponent<{}, string> im
this.aceEditor.setFontSize(14); this.aceEditor.setFontSize(14);
this.setDisabledState(this.snapshot.isDisabled); this.setDisabledState(this.snapshot.isDisabled);
this.setValue(this.valueString); this.setValue(this.value);
this.setMode(); this.setMode();
this.aceEditor.on('blur', () => { this.aceEditor.on('blur', () => {
@ -138,31 +134,30 @@ export class CodeEditorComponent extends StatefulControlComponent<{}, string> im
} }
private changeValue() { private changeValue() {
let newValue: any = null; let newValue = this.aceEditor.getValue();
let newValueString: string; let newValueOut = newValue;
if (this.valueMode === 'Json') { if (this.valueMode === 'Json') {
const isValid = this.aceEditor.getSession().getAnnotations().length === 0; const isValid = this.aceEditor.getSession().getAnnotations().length === 0;
if (isValid) { if (isValid) {
try { try {
newValue = JSON.parse(this.aceEditor.getValue()); newValueOut = JSON.parse(newValue);
} catch (e) { } catch (e) {
newValue = null; newValueOut = null;
} newValue = '';
} }
newValueString = JSON.stringify(newValue);
} else { } else {
newValueString = newValue = this.aceEditor.getValue(); newValueOut = null;
newValue = '';
}
} }
if (this.valueString !== newValueString) { if (this.value !== newValue) {
this.callChange(newValue); this.callChange(newValueOut);
} }
this.value = newValue; this.value = newValue;
this.valueString = newValueString;
} }
private setMode() { private setMode() {
@ -178,7 +173,7 @@ export class CodeEditorComponent extends StatefulControlComponent<{}, string> im
} }
private setValue(value: string) { private setValue(value: string) {
this.aceEditor.setValue(value || ''); this.aceEditor.setValue(value);
this.aceEditor.clearSelection(); this.aceEditor.clearSelection();
} }
} }
Loading…
Cancel
Save