Skip to content

Commit

Permalink
Do not select element on deleting/duplicating/required in model #5634 (
Browse files Browse the repository at this point in the history
…#5642)

* Do not select element on deleting/duplicating/required in model #5634

* Add a functional test #5634

* Add functional test on focusing/deleting #5634

* Change function test name #5634

* Use action onFocus #5634

* Use property instead of stopPropagation #5634
  • Loading branch information
andrewtelnov authored Jul 10, 2024
1 parent 9700e0a commit 98561ec
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,8 @@ export class SurveyElementAdornerBase<T extends SurveyElement = SurveyElement> e
title: this.creator.getLocString("survey.duplicate"),
visibleIndex: 10,
iconSize: 16,
action: () => {
this.duplicate();
}
action: () => this.duplicate(),
onFocus: (isMouse: boolean, event: any) => this.disableActionFocusing(isMouse, event)
})
);

Expand Down Expand Up @@ -266,10 +265,20 @@ export class SurveyElementAdornerBase<T extends SurveyElement = SurveyElement> e
iconSize: 16,
action: () => {
this.delete();
}
},
onFocus: (isMouse: boolean, event: any) => this.disableActionFocusing(isMouse, event)
})
);
}
isDisableSelecting: boolean;
protected disableActionFocusing(isMouse: boolean, event: any): void {
this.isDisableSelecting = isMouse;
}
protected canSelectElement(): boolean {
const res = !this.isDisableSelecting;
this.isDisableSelecting = false;
return res;
}
public get allowEdit(): boolean {
return !!this.creator && !this.creator.readOnly && this.allowEditOption;
}
Expand Down
11 changes: 5 additions & 6 deletions packages/survey-creator-core/src/components/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ export class QuestionAdornerViewModel extends SurveyElementAdornerBase {
get element() {
return this.surveyElement;
}

protected canSelectElement(): boolean {
return super.canSelectElement() && this.surveyElement.isInteractiveDesignElement;
}
select(model: QuestionAdornerViewModel, event: IPortableEvent) {
if (!model.surveyElement.isInteractiveDesignElement) {
return;
}
if (!model.canSelectElement()) return;
const creator = model.creator;
const selEl = model.surveyElement;
const el: any = document?.activeElement;
Expand Down Expand Up @@ -531,8 +531,7 @@ export class QuestionAdornerViewModel extends SurveyElementAdornerBase {
}
protected duplicate(): void {
setTimeout(() => {
var newElement = this.creator.fastCopyQuestion(this.surveyElement);
this.creator.selectElement(newElement);
this.creator.fastCopyQuestion(this.surveyElement, true);
}, 1);
}
addNewQuestion(): void {
Expand Down
15 changes: 11 additions & 4 deletions packages/survey-creator-core/src/creator-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2406,11 +2406,12 @@ export class SurveyCreatorModel extends Base
}

/**
* Creates a copy of a specified question and inserts the copy next to this question.
* Creates a copy of a specified question, inserts the copy next to this question and optionally select it in the designer.
* @param question A question to copy.
* @param selectNewElement Select a new element if true
* @returns The instance of a new question.
*/
public fastCopyQuestion(question: Base): IElement {
public fastCopyQuestion(question: Base, selectNewElement?: boolean): IElement {
var newElement = this.copyElement(question);
var index = !!question["parent"]
? question["parent"].elements.indexOf(question) + 1
Expand All @@ -2422,6 +2423,9 @@ export class SurveyCreatorModel extends Base
}
}
this.doClickQuestionCore(newElement, "ELEMENT_COPIED", index, question["parent"]);
if(selectNewElement) {
this.selectElement(newElement);
}
return newElement;
}
/**
Expand Down Expand Up @@ -2863,8 +2867,9 @@ export class SurveyCreatorModel extends Base
}

protected deletePanelOrQuestion(obj: Base): void {
const changeSelection = obj === this.selectedElement;
var parent = obj["parent"];
var elements = parent.elements;
const elements = parent.elements;
var objIndex = elements.indexOf(obj);
if (objIndex == elements.length - 1) {
objIndex--;
Expand All @@ -2876,7 +2881,9 @@ export class SurveyCreatorModel extends Base
if (parent.isPage && (this.pageEditMode === "single" || elements.length === 0)) {
parent = this.survey;
}
this.selectElement(objIndex > -1 ? elements[objIndex] : parent);
if(changeSelection) {
this.selectElement(objIndex > -1 ? elements[objIndex] : parent);
}
}
hiddenProperties: any = {};
protected onCanShowObjectProperty(
Expand Down
42 changes: 42 additions & 0 deletions packages/survey-creator-core/tests/creator-base.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4048,6 +4048,48 @@ test("Remove carry-forward property on deleting a question", (): any => {
creator.deleteElement(q1);
expect(q2.choicesFromQuestion).toBeFalsy();
});
test("Keep selection on deleting another question, #5634", (): any => {
const creator = new CreatorTester();
creator.JSON = {
elements: [
{ type: "text", name: "q1" },
{ type: "text", name: "q2" },
{ type: "text", name: "q3" }
]
};
let counter = 0;
creator.onSelectedElementChanged.add((sender, options) => {
counter ++;
});
creator.selectQuestionByName("q1");
expect(counter).toBe(1);
expect(creator.selectedElementName).toEqual("q1");
const q2 = creator.survey.getQuestionByName("q2");
creator.deleteElement(q2);
expect(creator.selectedElementName).toEqual("q1");
expect(counter).toBe(1);
});
test("Do not select a duplicated question if it is not selected, #5634", (): any => {
const creator = new CreatorTester();
creator.JSON = {
elements: [
{ type: "text", name: "q1" },
{ type: "text", name: "q2" },
{ type: "text", name: "q3" }
]
};
let counter = 0;
creator.onSelectedElementChanged.add((sender, options) => {
counter ++;
});
creator.selectQuestionByName("q1");
expect(counter).toBe(1);
expect(creator.selectedElementName).toEqual("q1");
const q2 = creator.survey.getQuestionByName("q2");
creator.fastCopyQuestion(q2, true);
expect(creator.selectedElementName).toEqual("question1");
expect(counter).toBe(2);
});
test("Do not focus title on mobile", (): any => {
const creator = new CreatorTester();
creator.JSON = {
Expand Down
18 changes: 17 additions & 1 deletion testCafe/designer/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,23 @@ test("Select survey on deleting the last question", async t => {
.wait(2000)
.expect(Selector(".sv-string-viewer").withExactText("Show the title").exists).notOk();
});

test("Do not select the deleting question if it was not selected", async t => {
await setJSON({
"elements": [
{ "type": "text", "name": "q1" },
{ "type": "text", "name": "q2" },
{ "type": "text", "name": "q3" }
]
});
await t
.maximizeWindow()
.click(getVisibleElement(".svc-question__content").find("span").withText("q1"), { offsetX: 100, offsetY: 5 })
.expect(getVisibleElement(".svc-question__content--selected").find("span").withText("q1").exists).ok()
.hover(getVisibleElement(".svc-question__content").withText("q2"), { offsetX: 50, offsetY: 50 })
.click(getVisibleElement(".svc-question__content").withText("q2").find("span").withText("Delete"))
.expect(getVisibleElement(".svc-question__content").find("span").withText("q2").exists).notOk()
.expect(getVisibleElement(".svc-question__content--selected").find("span").withText("q1").exists).ok();
});
test("Keyboard tab navigation between questions", async (t) => {
const json = {
"logoPosition": "right",
Expand Down

0 comments on commit 98561ec

Please sign in to comment.