Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance/activetab json to designer #6028

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,9 @@ export abstract class TabJsonEditorBasePlugin implements ICreatorPlugin {
}
public deactivate(): boolean {
if (this.model) {
const textWorker: SurveyTextWorker = new SurveyTextWorker(this.model.text);
if (!textWorker.isJsonCorrect) {
return false;
}
if (!this.model.readOnly && this.model.isJSONChanged) {
this.creator.selectedElement = undefined;
this.creator.text = this.model.text;
this.creator.changeText(this.model.text, false, true);
this.creator.selectedElement = this.creator.survey;
this.creator.setModified({ type: "JSON_EDITOR" });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export class TabTranslationPlugin implements ICreatorPlugin {
this.mergeLocaleWithDefaultAction.visible = false;
this.importCsvAction.visible = false;
this.exportCsvAction.visible = false;

return true;
}
private createMergeLocaleWithDefaultActionTitleUpdater(): any {
Expand Down
27 changes: 17 additions & 10 deletions packages/survey-creator-core/src/creator-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1197,16 +1197,12 @@ export class SurveyCreatorModel extends Base
const chaningOptions = { tabName: viewName, allow: allow, model: this.currentPlugin?.model };
this.onActiveTabChanging.fire(this, chaningOptions);
if (!chaningOptions.allow) return;
if (!this.canSwitchViewType()) return false;
if(!!this.currentPlugin?.deactivate && !this.currentPlugin.deactivate()) return;
const plugin = this.activatePlugin(viewName);
this.viewType = viewName;
this.onActiveTabChanged.fire(this, { tabName: viewName, plugin: plugin, model: !!plugin ? plugin.model : undefined });
return true;
}
private canSwitchViewType(): boolean {
const plugin: ICreatorPlugin = this.currentPlugin;
return !plugin || !plugin.deactivate || plugin.deactivate();
}
private activatePlugin(newType: string): ICreatorPlugin {
const plugin: ICreatorPlugin = this.getPlugin(newType);
if (!!plugin) {
Expand Down Expand Up @@ -2137,20 +2133,31 @@ export class SurveyCreatorModel extends Base
}
}

public changeText(value: string, clearState = false): void {
public changeText(value: string, clearState = false, trustJSON?: boolean): void {
this.setTextValue(value);
if (!value) {
this.initSurveyWithJSON(undefined, clearState);
} else {
const textWorker = new SurveyTextWorker(value);
if (textWorker.isJsonCorrect || !!textWorker.survey) {
this.initSurveyWithJSON(textWorker.survey.toJSON(), clearState);
let jsonValue = trustJSON ? this.parseJSON(value) : undefined;
if(!trustJSON) {
const textWorker = new SurveyTextWorker(value);
if(textWorker.isJsonCorrect) {
jsonValue = this.parseJSON(value);
}
else if(!!textWorker.survey) {
jsonValue = textWorker.survey.toJSON();
}
}
if (!!jsonValue) {
this.initSurveyWithJSON(jsonValue, clearState);
} else {
this.viewType = "editor";
}
}
}

private parseJSON(val: string): any {
return new SurveyJSON5().parse(val);
}
/**
* A survey JSON schema as a string.
*
Expand Down
10 changes: 3 additions & 7 deletions packages/survey-creator-core/src/plugins/undo-redo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ export class UndoRedoPlugin implements ICreatorPlugin {
});
}
public model: any = undefined;
public activate(): void {
}
public deactivate(): boolean {
return true;
}
public update(): void {
}
public activate(): void {}
public deactivate(): boolean { return true; }
public update(): void {}
public addFooterActions() {
this.model.undoAction && (this.creator.footerToolbar.actions.splice(2, 0, this.model.undoAction));
this.model.redoAction && (this.creator.footerToolbar.actions.splice(3, 0, this.model.redoAction));
Expand Down
4 changes: 4 additions & 0 deletions packages/survey-creator-core/src/textWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export class SurveyTextWorkerJsonError extends SurveyTextWorkerError {
}

export class SurveyTextWorker {
public static onProcessJson: ((json: any) => void) | undefined;
public static newLineChar: string = "\n";
public errors: Array<SurveyTextWorkerError>;
private surveyValue: SurveyModel;
Expand Down Expand Up @@ -198,6 +199,9 @@ export class SurveyTextWorker {
}
if (this.jsonValue != null) {
this.updateJsonPositions(this.jsonValue);
if(!!SurveyTextWorker.onProcessJson) {
SurveyTextWorker.onProcessJson(this.jsonValue);
}
this.surveyValue = new SurveyForTextWorker(this.jsonValue);
const jsonErrors = this.surveyValue.jsonErrors;
if (Array.isArray(jsonErrors)) {
Expand Down
18 changes: 17 additions & 1 deletion packages/survey-creator-core/tests/tabs/json-editor.tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TabJsonEditorTextareaPlugin, TextareaJsonEditorModel } from "../../src/components/tabs/json-editor-textarea";
import { CreatorTester } from "../creator-tester";
import { JsonEditorBaseModel } from "../../src/components/tabs/json-editor-plugin";
import { settings } from "../../src/creator-settings";
import { SurveyTextWorker } from "../../src/textWorker";

test("JsonEditor & showErrors/errorList", () => {
const creator = new CreatorTester();
Expand Down Expand Up @@ -248,3 +248,19 @@ test("Put elements into end of the JSON", () => {
const titlePos = text.indexOf("title");
expect(elementsPos > titlePos).toBeTruthy();
});
test("We should have one SurveyTextWorker.fromJSON/toJSON", () => {
const json = { requiredText: "###" };
const creator = new CreatorTester();
creator.activeTab ="editor";
const editorPlugin: TabJsonEditorTextareaPlugin = <TabJsonEditorTextareaPlugin>creator.getPlugin("editor");
editorPlugin.model.text = JSON.stringify(json);
let counter = 0;
SurveyTextWorker.onProcessJson = (json: any): void => {
if(json?.requiredText === "###") {
counter ++;
}
};
creator.activeTab = "designer";
expect(counter).toBe(1);
SurveyTextWorker.onProcessJson = undefined;
});