@@ -23,37 +23,26 @@ import { MatButtonModule } from '@angular/material/button';
2323 MatTooltipModule
2424 ] ,
2525 selector : 'edit-component-json' ,
26- styles : [ 'div { margin-top: 10px; margin-bottom: 10px; } .mat-icon { margin: 0px; }' ] ,
26+ styles : [ '.mat-icon { margin: 0px; }' ] ,
2727 templateUrl : 'edit-component-json.component.html'
2828} )
2929export class EditComponentJsonComponent {
3030 @Input ( ) component : WISEComponent ;
3131 protected componentContentJSONString : string ;
3232 protected jsonChanged : Subject < string > = new Subject < string > ( ) ;
33- protected showJSONAuthoring : boolean = false ;
3433 private subscriptions : Subscription = new Subscription ( ) ;
35- private validComponentContentJSONString : string ;
3634
3735 constructor (
3836 private notificationService : NotificationService ,
3937 private projectService : TeacherProjectService
4038 ) { }
4139
4240 ngOnInit ( ) : void {
43- this . setComponentContentJsonString ( ) ;
44- this . subscriptions . add (
45- this . jsonChanged . pipe ( debounceTime ( 1000 ) , distinctUntilChanged ( ) ) . subscribe ( ( ) => {
46- if ( this . isJSONValid ( ) ) {
47- this . rememberRecentValidJSON ( ) ;
48- this . notificationService . showJSONValidMessage ( ) ;
49- } else {
50- this . notificationService . showJSONInvalidMessage ( ) ;
51- }
52- } )
53- ) ;
41+ this . componentContentJSONString = JSON . stringify ( this . component . content , null , 4 ) ;
5442 this . subscriptions . add (
55- this . projectService . nodeChanged$ . subscribe ( ( ) => {
56- this . setComponentContentJsonString ( ) ;
43+ this . jsonChanged . pipe ( debounceTime ( 1000 ) , distinctUntilChanged ( ) ) . subscribe ( ( newText ) => {
44+ this . componentContentJSONString = newText ;
45+ this . saveChanges ( ) ;
5746 } )
5847 ) ;
5948 }
@@ -62,55 +51,15 @@ export class EditComponentJsonComponent {
6251 this . subscriptions . unsubscribe ( ) ;
6352 }
6453
65- private setComponentContentJsonString ( ) : void {
66- this . componentContentJSONString = JSON . stringify ( this . component . content , null , 4 ) ;
67- }
68-
69- protected toggleJSONView ( ) : void {
70- if ( this . showJSONAuthoring ) {
71- if ( this . isJSONValid ( ) ) {
72- this . saveChanges ( ) ;
73- this . showJSONAuthoring = false ;
74- } else {
75- const doRollback = confirm (
76- $localize `The JSON is invalid. Invalid JSON will not be saved.\nClick "OK" to revert back to the last valid JSON.\nClick "Cancel" to keep the invalid JSON open so you can fix it.`
77- ) ;
78- if ( doRollback ) {
79- this . rollbackToRecentValidJSON ( ) ;
80- this . saveChanges ( ) ;
81- }
82- }
83- } else {
84- this . showJSONAuthoring = true ;
85- this . rememberRecentValidJSON ( ) ;
86- }
87- }
88-
89- private isJSONValid ( ) : boolean {
90- try {
91- JSON . parse ( this . componentContentJSONString ) ;
92- return true ;
93- } catch ( e ) {
94- return false ;
95- }
96- }
97-
9854 private saveChanges ( ) : void {
9955 try {
10056 this . projectService
10157 . getNode ( this . component . nodeId )
10258 . replaceComponent ( this . component . id , JSON . parse ( this . componentContentJSONString ) ) ;
10359 this . projectService . componentChanged ( ) ;
60+ this . notificationService . showJSONValidMessage ( ) ;
10461 } catch ( e ) {
10562 this . notificationService . showJSONInvalidMessage ( ) ;
10663 }
10764 }
108-
109- private rememberRecentValidJSON ( ) : void {
110- this . validComponentContentJSONString = this . componentContentJSONString ;
111- }
112-
113- private rollbackToRecentValidJSON ( ) : void {
114- this . componentContentJSONString = this . validComponentContentJSONString ;
115- }
11665}
0 commit comments