Skip to content

Commit 1b7770e

Browse files
Edit component JSON: remove toggle and show textarea when tab is opened.
1 parent 94ec8b3 commit 1b7770e

3 files changed

Lines changed: 128 additions & 203 deletions

File tree

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
<div class="flex flex-row justify-start items-center gap-2">
2-
<mat-label class="node__label--vertical-alignment" i18n>JSON</mat-label>
3-
<button
4-
mat-raised-button
5-
color="primary"
6-
class="topButton"
7-
(click)="toggleJSONView()"
8-
matTooltip="Show JSON"
9-
i18n-matTooltip
10-
matTooltipPosition="above"
11-
>
12-
<mat-icon>code</mat-icon>
13-
</button>
14-
</div>
15-
@if (showJSONAuthoring) {
16-
<div class="flex flex-col gap-2">
17-
<span i18n style="color: red">Close the JSON view to save the changes</span>
18-
<mat-form-field class="w-full">
19-
<mat-label i18n>Edit Activity JSON</mat-label>
20-
<textarea
21-
matInput
22-
cdkTextareaAutosize
23-
[(ngModel)]="componentContentJSONString"
24-
(ngModelChange)="jsonChanged.next($event)"
25-
></textarea>
26-
</mat-form-field>
1+
<div class="pt-4">
2+
<div class="mb-2">
3+
<mat-icon color="warn" class="align-bottom">warning</mat-icon>
4+
<span i18n class="warn">
5+
Editing the JSON directly is generally not recommended, as errors can break the unit. If you
6+
need assistance, please contact WISE staff.
7+
</span>
278
</div>
28-
}
9+
<mat-form-field class="w-full" appearance="outline" style="margin-top: 10px">
10+
<mat-label i18n>Edit Activity JSON</mat-label>
11+
<textarea
12+
class="mat-body-2"
13+
matInput
14+
cdkTextareaAutosize
15+
[(ngModel)]="componentContentJSONString"
16+
(ngModelChange)="jsonChanged.next($event)"
17+
></textarea>
18+
</mat-form-field>
19+
</div>

src/app/authoring-tool/edit-component-json/edit-component-json.component.ts

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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
})
2929
export 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

Comments
 (0)