Blockly.Blocks['initialize_list_with_values'] = {
init: function () {
this.appendDummyInput()
.appendField(getTranslation('lists:initializeList'))
.appendField(
new Blockly.FieldVariable(
null,
function (variableId) {
this.sourceBlock_.variableChanged_(variableId);
},
['string[]', 'int[]', 'char[]', 'bool[]'],
'string[]'
),
'NAME'
)
.appendField(getTranslation('lists:withLength'))
.appendField(
new Blockly.FieldNumber(1, 1, 30, null, this.lengthChanged_.bind(this)),
'LENGTH'
)
.appendField(getTranslation('lists:withValues'));
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(COLORS.text);
this.setTooltip('');
this.setHelpUrl('');
},
variableChanged_: function (variableId) {
Blockly.Events.disable();
var variable = Blockly.Variables.getVariable(this.workspace, variableId);
var type = variable.type.slice(0, -2);
this.setColour(COLORS[type]);
var length = this.inputList.length - 1;
variable.length = length;
for (let i = 1; i <= length; i++) {
var otherConnection = this.inputList[i]?.connection?.targetConnection;
if (otherConnection) {
this.inputList[i].connection.disconnect();
this.inputList[i].setCheck(type);
if (
otherConnection.check_ &&
this.inputList[i].connection.check_ &&
otherConnection.check_[0] != this.inputList[i].connection.check_[0]
) {
otherConnection.sourceBlock_.dispose();
if (!otherConnection.sourceBlock_.isShadow()) {
var shadowBlockDom = this.inputList[i].connection.getShadowDom();
this.workspace.getBlockById(shadowBlockDom.id).dispose();
}
var shadowBlock = getShadowBlockByType(this.workspace, type);
this.inputList[i].connection.connect(shadowBlock.outputConnection);
continue;
}
this.inputList[i].connection.connect(otherConnection);
} else {
var shadowBlock = getShadowBlockByType(this.workspace, type);
this.inputList[i].connection.connect(shadowBlock.outputConnection);
}
}
Blockly.Events.enable();
},
lengthChanged_: function (length) {
var currentConnections = [];
var currentInputs = this.inputList.length;
for (let i = 0; i < currentInputs; i++) {
var otherConnection = this.inputList[i]?.connection?.targetConnection;
if (otherConnection) {
if (i > length) {
otherConnection.sourceBlock_.dispose();
continue;
}
this.inputList[i].connection.disconnect();
currentConnections.push({ index: i - 1, connection: otherConnection });
}
}
for (let i = 0; i < currentInputs; i++) {
this.removeInput('VALUE' + (i - 1), true);
}
var variable = Blockly.Variables.getVariable(
this.workspace,
this.inputList[0].fieldRow[1].getValue()
);
var type = variable ? variable.type.slice(0, -2) : null;
if (!type) {
return;
}
this.setColour(COLORS[type]);
if (variable) {
variable.length = length;
}
for (let i = 0; i < length; i++) {
this.appendValueInput('VALUE' + i)
.appendField(i + ':')
.setCheck(type);
var existingConnection = currentConnections
.map((x) => x.index)
.indexOf(i);
var shadowBlock = getShadowBlockByType(this.workspace, type);
if (shadowBlock) {
When removing this line of code, code generation works.
this.getInput('VALUE' + i).connection.connect(
shadowBlock.outputConnection
);
if (existingConnection > -1) {
this.inputList[existingConnection + 1].connection.connect(
currentConnections[existingConnection].connection
);
}
}
}
},
};
I attached a gif illustrating the problem.