Blockly.Blocks['MyWonderfulBlock'] = {
init: function() {},
myLovelyFunction: function() {},
mutationToDom: function() {},
// etc..
}var myElementList = [["Button-1", "Button-1"], ["DropDown-1", "DropDown-1"]];
const propsList = [['Label', 'Label'],['Ranges', 'Ranges']];
const rangesList = [['Range1', 'Range1'],['Range2', 'Range2']];
Blockly.Blocks['PropertyUpdate2'] = {
init: function() {
this.appendValueInput("then")
.setAlign(Blockly.ALIGN_CENTRE)
.appendField("Update")
.appendField(new Blockly.FieldDropdown(myElementList), "element4")
.appendField("Property")
.appendField(new Blockly.FieldDropdown(propsList), "Property4")
.appendField("To");
this.setNextStatement(true, null);
this.setPreviousStatement(true, null);
this.setColour(230);
},
mutationToDom: function() {
var container = Blockly.utils.xml.createElement('mutation');
var propertyInput = this.getFieldValue('Property4');
container.setAttribute('property_input', propertyInput);
return container;
},
domToMutation: function(xmlElement) {
var propertyInput = xmlElement.getAttribute('property_input');
this.updateShape_(propertyInput);
},
updateShape_: function(propertyInput) {
var inputExists = this.getInput('rangeNo');
if (propertyInput == "Ranges"){
if (!inputExists){
this.appendDummyInput("EMPTY")
.appendField(new Blockly.FieldDropdown(rangesList), "rangeNo")
}
} else if (inputExists) {
this.removeInput('rangeNo');
}
}
};
Here is how i am adding this block in toolbox xml in my html sample page:
<category name="InatantAR" color="150">
<block type="PropertyUpdate2">
<mutation property_input="Ranges"></mutation>
<field name="property_input">Ranges</field>
</block>
</category>
When I click on toolbox, it does show the additional dropdown field added to the block. But when I select the block, add it to blockly workspace, and select Ranges from second dropdown, I do not see mutationToDom or domToMutation being called. hence the additional blocks do not get added as updateShare_ is not called.
I must be missing something like registering the mutator or event or something? Here is how i am adding the workspace to my sample html file:
<script>
var workspace = Blockly.inject('blocklyDiv',
{toolbox: document.getElementById('toolbox'),
grid:
{spacing: 20,
length: 2,
colour: '#ccc',
snap: true},
move:{
scrollbars: true,
drag: true,
wheel: false},
zoom:
{controls: true,
wheel: true,
startScale: 1.0,
maxScale: 3,
minScale: 0.3,
scaleSpeed: 1.2},
trashcan: true});
//workspace.addChangeListener(myUpdateFunction);
</script>