Skip to content

Commit 0647f97

Browse files
authored
operators.js -- fix ctrl Z on expandables
1 parent 754c5a5 commit 0647f97

1 file changed

Lines changed: 63 additions & 4 deletions

File tree

blocks_vertical/operators.js

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Blockly.Blocks['operator_expandableMath'] = {
157157
},
158158

159159
fillInBlock: Blockly.scratchBlocksUtils.generateMutatorShadow,
160-
menuGenerator: function() {
160+
menuGenerator: function () {
161161
const dropdown = new Blockly.FieldDropdown(function () {
162162
return [
163163
["+", "+"], ["-", "-"],
@@ -200,15 +200,45 @@ Blockly.Blocks['operator_expandableMath'] = {
200200
const inputCount = Number(xmlElement.getAttribute("inputcount"));
201201
const menuValues = String(xmlElement.getAttribute("menuvalues"));
202202
this.inputs_ = isNaN(inputCount) ? 0 : inputCount;
203+
let needsActionConnect = false, oldConnections;
204+
205+
if (this.inputList.length > 0) {
206+
// this was a control z action
207+
needsActionConnect = true;
208+
oldConnections = this.getConnections_().map(c => c.targetBlock());
209+
210+
// clear block
211+
for (var i = this.inputList.length; i--;) {
212+
if (i === 0) break;
213+
const input = this.inputList[i];
214+
if (input.connection.targetBlock()) input.connection.disconnect();
215+
this.removeInput(input.name);
216+
}
217+
}
218+
203219
for (let i = 0; i < this.inputs_; i++) {
204220
const input = this.appendValueInput(`NUM${i + 1}`);
205221
if (i > 0) {
206222
const menu = input.appendField(this.menuGenerator());
207223
menu.fieldRow[0].setValue(menuValues[i - 1] ? menuValues[i - 1] : "+", true);
208224
}
209-
// vm will automatically replace empty inputs with saved shadows
225+
// vm will automatically replace2 empty inputs with saved shadows
210226
}
211227
this.oldInputs_ = this.inputs_;
228+
229+
if (needsActionConnect) {
230+
let index = 0;
231+
for (const input of this.inputList) {
232+
const oldBlock = oldConnections[index];
233+
if (oldBlock) {
234+
try {
235+
const connector = oldBlock.outputConnection;
236+
input.connection.connect(connector);
237+
} catch(e) {}
238+
}
239+
index++;
240+
}
241+
}
212242
},
213243

214244
onExpandableButtonClicked_: function (isAdding) {
@@ -634,7 +664,7 @@ Blockly.Blocks['operator_join3'] = {
634664

635665
Blockly.Blocks['operator_expandablejoininputs'] = {
636666
/**
637-
* pm: Block for joining strings together (determined by user)
667+
* pm: Block for joining n number of strings together
638668
* @this Blockly.Block
639669
*/
640670
init: function () {
@@ -678,12 +708,41 @@ Blockly.Blocks['operator_expandablejoininputs'] = {
678708
// on load
679709
if (this.oldInputs_ === this.inputs_) return;
680710
const inputCount = Number(xmlElement.getAttribute("inputcount"));
711+
let needsActionConnect = false, oldConnections;
712+
if (this.inputList.length > 1) {
713+
// this was a control z action
714+
needsActionConnect = true;
715+
oldConnections = this.getConnections_().map(c => c.targetBlock());
716+
717+
// clear block
718+
for (var i = this.inputList.length; i--;) {
719+
if (i === 0) break;
720+
const input = this.inputList[i];
721+
if (input.connection.targetBlock()) input.connection.disconnect();
722+
this.removeInput(input.name);
723+
}
724+
}
725+
681726
this.inputs_ = isNaN(inputCount) ? 0 : inputCount;
682727
for (let i = 0; i < this.inputs_; i++) {
683728
// vm will automatically replace empty inputs with saved shadows
684-
this.appendValueInput(`INPUT${i + 1}`);
729+
if (!this.getInput(`INPUT${i + 1}`)) this.appendValueInput(`INPUT${i + 1}`);
685730
}
686731
this.oldInputs_ = this.inputs_;
732+
733+
if (needsActionConnect) {
734+
let index = 0;
735+
for (const input of this.inputList) {
736+
const oldBlock = oldConnections[index];
737+
if (oldBlock) {
738+
try {
739+
const connector = oldBlock.outputConnection;
740+
input.connection.connect(connector);
741+
} catch(e) {}
742+
}
743+
index++;
744+
}
745+
}
687746
},
688747

689748
onExpandableButtonClicked_: function (isAdding) {

0 commit comments

Comments
 (0)