Skip to content

Commit

Permalink
JSON Editor: name and title are far from each other fix #5675 (#5685)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov authored Jul 15, 2024
1 parent fc80291 commit d964197
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/survey-creator-core/src/creator-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1978,13 +1978,28 @@ export class SurveyCreatorModel extends Base
if (!this.survey) return "";
var json = (<any>this.survey).toJSON();
json = this.singlePageJSON(json);
this.moveElementsToTheEnd(json);
const indent = settings.jsonEditor.indentation;
if (this.generateValidJSON) {
return JSON.stringify(json, null, indent);
}
return new SurveyJSON5().stringify(json, null, indent);
}

private moveElementsToTheEnd(json: any): void {
if(!json) return;
if(Array.isArray(json)) {
json.forEach(el => this.moveElementsToTheEnd(el));
} else {
if(typeof json === "object") {
if(!!json["elements"]) {
const els = json["elements"];
delete json["elements"];
json["elements"] = els;
}
Object.keys(json).forEach(key => this.moveElementsToTheEnd(json[key]));
}
}
}
protected setTextValue(value: string) {
if (!!this.setSurveyJSONTextCallback) {
this.setSurveyJSONTextCallback(value);
Expand Down
12 changes: 12 additions & 0 deletions packages/survey-creator-core/tests/tabs/json-editor.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,15 @@ test("export json to file", (done): any => {
};
editorPlugin.exportToFile(settings.jsonEditor.exportFileName);
});
test("Put elements into end of the JSON", () => {
const creator = new CreatorTester();
creator.JSON = {
elements: { type: "text", name: "q1" }
};
creator.survey.pages[0].title = "test";
const editor = new TextareaJsonEditorModel(creator);
const text = editor.text;
const elementsPos = text.indexOf("elements");
const titlePos = text.indexOf("title");
expect(elementsPos > titlePos).toBeTruthy();
});

0 comments on commit d964197

Please sign in to comment.