Skip to content

Commit c43f1a9

Browse files
tsv2013tsv2013
andauthored
Introduce an option to disable inplace editing for form string elements (#7152)
* Introduce an option to disable inplace editing for form string elements Fixes #6264 Fixes #7148 * Fixed u-test --------- Co-authored-by: tsv2013 <[email protected]>
1 parent 591eeef commit c43f1a9

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

packages/survey-creator-core/src/creator-base.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4675,6 +4675,17 @@ export class SurveyCreatorModel extends Base
46754675
* @see showTranslationTab
46764676
*/
46774677
public clearTranslationsOnSourceTextChange: boolean = false;
4678+
4679+
/**
4680+
* An event that is raised when creator decides whether certain string is inplace editable on design surface or not.
4681+
*/
4682+
public onGetIsStringInplacelyEditable: EventBase<SurveyCreatorModel, { element: Base, name: string, allowEdit: boolean }> = this.addCreatorEvent<SurveyCreatorModel, { element: Base, name: string, allowEdit: boolean }>();
4683+
4684+
public isStringInplacelyEditable(element: Base, stringName: string) {
4685+
const options = { element, name: stringName, allowEdit: !this.readOnly && !!isStringEditable(element, stringName) };
4686+
this.onGetIsStringInplacelyEditable.fire(this, options);
4687+
return options.allowEdit;
4688+
}
46784689
}
46794690

46804691
export class CreatorBase extends SurveyCreatorModel { }
@@ -4712,14 +4723,14 @@ export function initializeDesignTimeSurveyModel(model: any, creator: SurveyCreat
47124723
opt.data = opt.data || data;
47134724
});
47144725
model.getRendererForString = (element: Base, name: string): string => {
4715-
if (!creator.readOnly && isStringEditable(element, name)) {
4726+
if (creator.isStringInplacelyEditable(element, name)) {
47164727
return editableStringRendererName;
47174728
}
47184729
return undefined;
47194730
};
47204731

47214732
model.getRendererContextForString = (element: Base, locStr: LocalizableString): any => {
4722-
if (!creator.readOnly && isStringEditable(element, locStr.name)) {
4733+
if (creator.isStringInplacelyEditable(element, locStr.name)) {
47234734
return {
47244735
creator: creator,
47254736
element,

packages/survey-creator-core/tests/creator-base-1.tests.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,26 +1711,6 @@ test("getElementWrapperComponentName for inner component elements", () => {
17111711
ComponentCollection.Instance.clear();
17121712
});
17131713

1714-
test("isStringEditable", (): any => {
1715-
expect(isStringEditable({ isContentElement: true }, "")).toBeFalsy();
1716-
expect(isStringEditable({}, "")).toBeTruthy();
1717-
expect(
1718-
isStringEditable({ isEditableTemplateElement: true }, "")
1719-
).toBeTruthy();
1720-
expect(
1721-
isStringEditable(
1722-
{ isContentElement: true, isEditableTemplateElement: true },
1723-
""
1724-
)
1725-
).toBeTruthy();
1726-
});
1727-
test("isStringEditable for matrix dynamic", (): any => {
1728-
const matrix = new QuestionMatrixDynamicModel("q1");
1729-
matrix.addColumn("col1");
1730-
matrix.rowCount = 1;
1731-
expect(isStringEditable(matrix.columns[0].templateQuestion, "")).toBeTruthy();
1732-
expect(isStringEditable(matrix.visibleRows[0].cells[0].question, "")).toBeFalsy();
1733-
});
17341714
test("Test plug-ins in creator", (): any => {
17351715
const creator = new CreatorTester({
17361716
showTranslationTab: true,

packages/survey-creator-core/tests/creator-base-2.tests.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,3 +807,52 @@ test("License text for default locale and another default locale", (): any => {
807807

808808
editorLocalization.defaultLocale = "en";
809809
});
810+
811+
test("isStringEditable", (): any => {
812+
expect(isStringEditable({ isContentElement: true }, "")).toBeFalsy();
813+
expect(isStringEditable({}, "")).toBeTruthy();
814+
expect(
815+
isStringEditable({ isEditableTemplateElement: true }, "")
816+
).toBeTruthy();
817+
expect(
818+
isStringEditable(
819+
{ isContentElement: true, isEditableTemplateElement: true },
820+
""
821+
)
822+
).toBeTruthy();
823+
});
824+
test("isStringEditable for matrix dynamic", (): any => {
825+
const matrix = new QuestionMatrixDynamicModel("q1");
826+
matrix.addColumn("col1");
827+
matrix.rowCount = 1;
828+
expect(isStringEditable(matrix.columns[0].templateQuestion, "")).toBeTruthy();
829+
expect(isStringEditable(matrix.visibleRows[0].cells[0].question, "")).toBeFalsy();
830+
});
831+
test("onGetIsStringEditable", (): any => {
832+
const creator = new CreatorTester();
833+
let lastEditableValue;
834+
let callCount = 0;
835+
let newValue;
836+
creator.onGetIsStringInplacelyEditable.add((s, o) => {
837+
lastEditableValue = o.allowEdit;
838+
callCount++;
839+
if (newValue !== undefined) {
840+
o.allowEdit = newValue;
841+
}
842+
});
843+
expect(lastEditableValue).toBeUndefined();
844+
expect(callCount).toBe(0);
845+
846+
expect(creator.isStringInplacelyEditable({ isContentElement: true } as any, "")).toBeFalsy();
847+
expect(lastEditableValue).toBeFalsy();
848+
expect(callCount).toBe(1);
849+
850+
expect(creator.isStringInplacelyEditable({ } as any, "")).toBeTruthy();
851+
expect(lastEditableValue).toBeTruthy();
852+
expect(callCount).toBe(2);
853+
854+
newValue = true;
855+
expect(creator.isStringInplacelyEditable({ isContentElement: true } as any, "")).toBeTruthy();
856+
expect(lastEditableValue).toBeFalsy();
857+
expect(callCount).toBe(3);
858+
});

0 commit comments

Comments
 (0)