Skip to content

Commit ec4829d

Browse files
committed
Fix: Changes into new added question onQuestionAdded event generates addition undo transactions #972
1 parent 3481192 commit ec4829d

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/editor.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,9 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
18791879
question.name = this.generateUniqueName(question, question.name);
18801880
var page = this.getPageByElement(question);
18811881
var options = { question: question, page: page };
1882+
this.addingObject = question;
18821883
this.onQuestionAdded.fire(this, options);
1884+
this.addingObject = null;
18831885
}
18841886
if (parentPanel.elements.indexOf(question) !== -1) {
18851887
this.surveyObjects.addElement(question, parentPanel);
@@ -1892,15 +1894,19 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
18921894
if (!this.dragDropHelper.isMoving) {
18931895
var page = this.getPageByElement(panel);
18941896
var options = { panel: panel, page: page };
1897+
this.addingObject = panel;
18951898
this.onPanelAdded.fire(this, options);
1899+
this.addingObject = null;
18961900
}
18971901
if (parentPanel.elements.indexOf(panel) !== -1) {
18981902
this.surveyObjects.addElement(panel, parentPanel);
18991903
}
19001904
}
19011905
private doOnPageAdded(page: Survey.Page) {
19021906
var options = { page: page };
1907+
this.addingObject = page;
19031908
this.onPageAdded.fire(this, options);
1909+
this.addingObject = null;
19041910
}
19051911
private getErrorOnPropertyChanging(
19061912
obj: Survey.Base,
@@ -2216,13 +2222,15 @@ export class SurveyCreator implements ISurveyObjectEditorOptions {
22162222
return;
22172223
return this[name].apply(this, [].slice.call(arguments, 1));
22182224
}
2225+
private addingObject: Survey.Base;
22192226
private onSurveyPropertyValueChangedCallback(
22202227
name: string,
22212228
oldValue: any,
22222229
newValue: any,
22232230
sender: Survey.Base,
22242231
arrayChanges: Survey.ArrayChanges
22252232
) {
2233+
if (this.addingObject == sender) return;
22262234
this.undoRedoManager.startTransaction(name + " changed");
22272235
this.undoRedoManager.onPropertyValueChanged(
22282236
name,

tests/surveyCreatorTests.ts

+55
Original file line numberDiff line numberDiff line change
@@ -1626,3 +1626,58 @@ QUnit.test("creator.onConditionQuestionsGetList, Bug#957", function (assert) {
16261626
assert.ok(editorItem, "Editor item is created");
16271627
assert.equal(editorItem.nameQuestion.choices.length, 1, "One text question");
16281628
});
1629+
1630+
QUnit.test("creator.onAddQuestion and undo-redo manager, Bug#972", function (
1631+
assert
1632+
) {
1633+
var creator = new SurveyCreator();
1634+
creator.onQuestionAdded.add(function (sender, options) {
1635+
options.question.title = "new title";
1636+
});
1637+
creator.JSON = {};
1638+
creator.survey.currentPage.addNewQuestion("text", "q1");
1639+
creator.survey.currentPage.addNewQuestion("text", "q2");
1640+
creator.undo();
1641+
creator.undo();
1642+
assert.equal(
1643+
creator.survey.getAllQuestions().length,
1644+
0,
1645+
"We added two questions and then undo adding two questions"
1646+
);
1647+
});
1648+
QUnit.test("creator.onAddPage and undo-redo manager, Bug#972", function (
1649+
assert
1650+
) {
1651+
var creator = new SurveyCreator();
1652+
creator.onPageAdded.add(function (sender, options) {
1653+
options.page.title = "new title";
1654+
});
1655+
creator.JSON = {};
1656+
creator.survey.addNewPage("p2");
1657+
creator.survey.addNewPage("p3");
1658+
creator.undo();
1659+
creator.undo();
1660+
assert.equal(
1661+
creator.survey.pages.length,
1662+
1,
1663+
"We added two pages and then undo adding two pages"
1664+
);
1665+
});
1666+
QUnit.test("creator.onAddPanel and undo-redo manager, Bug#972", function (
1667+
assert
1668+
) {
1669+
var creator = new SurveyCreator();
1670+
creator.onPanelAdded.add(function (sender, options) {
1671+
options.panel.title = "new title";
1672+
});
1673+
creator.JSON = {};
1674+
creator.survey.currentPage.addNewPanel("panel1");
1675+
creator.survey.currentPage.addNewPanel("panel2");
1676+
creator.undo();
1677+
creator.undo();
1678+
assert.equal(
1679+
creator.survey.getAllPanels().length,
1680+
0,
1681+
"We added two panels and then undo adding two panels"
1682+
);
1683+
});

0 commit comments

Comments
 (0)