44 ElementRef ,
55 HostBinding ,
66 Input ,
7+ OnChanges ,
78 OnDestroy ,
9+ SimpleChanges ,
810} from '@angular/core' ;
911import ace from 'ace-builds' ;
1012import 'ace-builds/src-noconflict/mode-json' ;
@@ -15,7 +17,9 @@ import 'ace-builds/src-noconflict/theme-github_light_default';
1517 templateUrl : './json-editor.component.html' ,
1618 standalone : false ,
1719} )
18- export class JSONEditorComponent implements AfterViewInit , OnDestroy {
20+ export class JSONEditorComponent
21+ implements AfterViewInit , OnChanges , OnDestroy
22+ {
1923 @HostBinding ( 'class' ) classNames = 'col w-100p' ;
2024 private editor ! : ace . Ace . Editor ;
2125
@@ -45,8 +49,10 @@ export class JSONEditorComponent implements AfterViewInit, OnDestroy {
4549 this . editor . session . setMode ( 'ace/mode/json' ) ;
4650 this . editor . session . on ( 'change' , ( ) => {
4751 try {
48- JSON . stringify ( JSON . parse ( this . getValue ( ) ) ) ;
49- this . jsonError = '' ;
52+ if ( this . getValue ( ) ) {
53+ JSON . stringify ( JSON . parse ( this . getValue ( ) ) ) ;
54+ this . jsonError = '' ;
55+ }
5056 // eslint-disable-next-line @typescript-eslint/no-explicit-any
5157 } catch ( err : any ) {
5258 this . jsonError = err . message ;
@@ -59,6 +65,19 @@ export class JSONEditorComponent implements AfterViewInit, OnDestroy {
5965 } ) ;
6066 }
6167
68+ ngOnChanges ( changes : SimpleChanges ) : void {
69+ if ( changes [ 'value' ] && this . editor && ! this . editor . isFocused ( ) ) {
70+ const next = changes [ 'value' ] . currentValue ?? '' ;
71+ if ( next !== this . editor . getValue ( ) ) {
72+ const scrollTop = this . editor . session . getScrollTop ( ) ;
73+ const pos = this . editor . getCursorPosition ( ) ;
74+ this . editor . session . setValue ( next ) ;
75+ this . editor . session . setScrollTop ( scrollTop ) ;
76+ this . editor . moveCursorToPosition ( pos ) ;
77+ }
78+ }
79+ }
80+
6281 ngOnDestroy ( ) : void {
6382 this . editor . destroy ( ) ;
6483 this . editor . container . remove ( ) ;
0 commit comments