Skip to content

Commit 1faebef

Browse files
authored
Merge pull request inveniosoftware-contrib#416 from harunurhan/fix-bugs
fix crash when config is changed on runtime and consistent undo
2 parents 23d78ac + 346e16a commit 1faebef

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

src/json-editor.component.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div id="ng2-json-editor" class="row editor-container" [ngClass]="shorterEditorContainerClass">
22
<div *ngIf="!config.compact" class="col-md-2 menu-container">
3-
<tree-menu [record]="_record" [schema]="schema"></tree-menu>
4-
<add-field-dropdown [fields]="keys$ | async" [pathString]="pathString" [schema]="schema">Add field</add-field-dropdown>
3+
<tree-menu [record]="_record" [schema]="fixedSchema"></tree-menu>
4+
<add-field-dropdown [fields]="keys$ | async" [pathString]="pathString" [schema]="fixedSchema">Add field</add-field-dropdown>
55
<hr>
66
<div *ngIf="config.enableAdminModeSwitch" class="admin-mode" tooltip="Allows editing all fields (use with care)">
77
<input id="admin-mode-checkbox" type="checkbox" [(ngModel)]="appGlobalsService.adminMode" />
@@ -11,13 +11,13 @@
1111
<bottom-console-badges (badgeClick)="openBottomConsole($event)"></bottom-console-badges>
1212
</div>
1313
<div id="middle-main-container" class="middle main-container" [ngClass]="middleContainerColMdClass" [shortcuts]="customShortcutKeys">
14-
<add-field-dropdown *ngIf="config.compact" [fields]="keys$ | async" [pathString]="pathString" [schema]="schema">Add field</add-field-dropdown>
14+
<add-field-dropdown *ngIf="config.compact" [fields]="keys$ | async" [pathString]="pathString" [schema]="fixedSchema">Add field</add-field-dropdown>
1515
<tabset *ngIf="config.tabsConfig">
1616
<tab *ngFor="let tabName of tabNames; trackBy:trackByElement" [heading]="tabName" (select)="activeTabName = tabName" [active]="isActiveTab(tabName)">
17-
<sub-record [value]="_record" [tabName]="tabName" [schema]="schema" [keys]="keys$ | async" [pathString]="pathString"></sub-record>
17+
<sub-record [value]="_record" [tabName]="tabName" [schema]="fixedSchema" [keys]="keys$ | async" [pathString]="pathString"></sub-record>
1818
</tab>
1919
</tabset>
20-
<sub-record *ngIf="!config.tabsConfig" [value]="_record" [schema]="schema" [keys]="keys$ | async" [pathString]="pathString"></sub-record>
20+
<sub-record *ngIf="!config.tabsConfig" [value]="_record" [schema]="fixedSchema" [keys]="keys$ | async" [pathString]="pathString"></sub-record>
2121
</div>
2222
<div id="right-main-container" *ngIf="!isPreviewerDisabled" [ngClass]="rightContainerColMdClass" class="main-container">
2323
<button id="btn-preview-toggle" type="button" class="btn btn-default btn-toggle" (click)="isPreviewerHidden = !isPreviewerHidden">{{isPreviewerHidden ? "Show Preview" : "Hide Preview"}}</button>

src/json-editor.component.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ import {
5252
ErrorsService
5353
} from './shared/services';
5454

55-
import { JsonEditorConfig, Preview, SchemaValidationErrors, JsonPatch, Shortcut, CustomShortcutKeys } from './shared/interfaces';
55+
import { JsonEditorConfig,
56+
Preview,
57+
SchemaValidationErrors,
58+
JsonPatch,
59+
Shortcut,
60+
CustomShortcutKeys,
61+
JSONSchema } from './shared/interfaces';
5662

5763

5864
@Component({
@@ -69,6 +75,7 @@ export class JsonEditorComponent extends AbstractTrackerComponent implements OnC
6975

7076
@Input() config: JsonEditorConfig;
7177
@Input() record: object;
78+
// original schema
7279
@Input() schema: any;
7380
@Input() errorMap: SchemaValidationErrors;
7481
@Input() jsonPatches: Array<JsonPatch>;
@@ -84,6 +91,8 @@ export class JsonEditorComponent extends AbstractTrackerComponent implements OnC
8491
isBottomConsoleOpen = false;
8592
bottomConsoleActiveTab = '';
8693
customShortcutKeys: CustomShortcutKeys;
94+
// altered schema enchanced with configs
95+
fixedSchema: JSONSchema;
8796

8897
// used to decide if the [record] is change caused by recordChange.emit or parent component
8998
private lastEmittedRecord: object;
@@ -103,7 +112,7 @@ export class JsonEditorComponent extends AbstractTrackerComponent implements OnC
103112

104113
ngOnInit() {
105114
this.appGlobalsService.adminMode$.subscribe(adminMode => {
106-
this.keysStoreService.buildKeysMap(this._record, this.schema);
115+
this.keysStoreService.buildKeysMap(this._record, this.fixedSchema);
107116
});
108117

109118
// listen for all changes on json
@@ -146,15 +155,15 @@ export class JsonEditorComponent extends AbstractTrackerComponent implements OnC
146155
const schemaChanged = changes['schema'] || changes['config'];
147156

148157
if (schemaChanged) {
149-
this.schema = this.schemaFixerService.fixSchema(this.schema, this.config.schemaOptions);
150-
this.jsonSchemaService.setSchema(this.schema);
158+
this.fixedSchema = this.schemaFixerService.fixSchema(this.schema, this.config.schemaOptions);
159+
this.jsonSchemaService.setSchema(this.fixedSchema);
151160
}
152161

153162
if (schemaChanged || recordChanged) {
154-
this.record = this.recordFixerService.fixRecord(this.record, this.schema);
163+
this.record = this.recordFixerService.fixRecord(this.record, this.fixedSchema);
155164
this._record = fromJS(this.record);
156165
this.jsonStoreService.setJson(this._record);
157-
this.keysStoreService.buildKeysMap(this._record, this.schema);
166+
this.keysStoreService.buildKeysMap(this._record, this.fixedSchema);
158167
}
159168

160169

src/shared/services/json-store.service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export class JsonStoreService {
8888
} else {
8989
list = list.insert(lastPathElement, value);
9090
}
91-
this.setIn(pathWithoutIndex, list);
91+
// allowUndo=false to avoid creating replace history patch when adding an item to a list.
92+
this.setIn(pathWithoutIndex, list, false);
9293
} else {
9394
this.setIn(path, value);
9495
}

src/shared/services/schema-fixer.service.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export class SchemaFixerService {
4040
* @param config - schema specific options
4141
* @return {JSONSchema} - fixed schema
4242
*/
43-
fixSchema(schema: JSONSchema, config?: SchemaOptions): JSONSchema {
43+
fixSchema(originalSchema: JSONSchema, config?: SchemaOptions): JSONSchema {
44+
let schema = _.cloneDeep(originalSchema);
4445
if (config) {
4546
schema = this.enrichSchemaWithConfig(schema, config);
4647
}
@@ -114,7 +115,7 @@ export class SchemaFixerService {
114115
// fixes that needs above fixes to be done deeply for the current schema
115116
schema.componentType = this.componentTypeService.getComponentType(schema);
116117

117-
return Object.assign({}, schema);
118+
return schema;
118119
}
119120

120121
/**

0 commit comments

Comments
 (0)