Share my sample code. Hope this helps.
Blockly.Blocks["mutation_container"] = {
init: function() {
this.appendDummyInput()
.appendField("Hello World");
this.setNextStatement(true, null);
}
};
Blockly.Blocks["mutation_test"] = {
init: function() {
this.appendValueInput("inputValue")
.setCheck("Number");
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(100);
this.updateShape_();
this.setMutator(new Blockly.Mutator(""));
},
mutationToDom: function (workspace) {
var container = document.createElement('mutation');
return container;
},
domToMutation: function (xmlElement) {
this.updateShape_();
},
decompose: function (workspace) {
var containerBlock = workspace.newBlock('mutation_container');
containerBlock.initSvg();
var myBlock1 = workspace.newBlock("controls_if");
myBlock1.initSvg();
containerBlock.nextConnection.connect(myBlock1.previousConnection);
var myBlock2 = workspace.newBlock("logic_compare");
myBlock2.initSvg();
myBlock2.setFieldValue("GT","OP");
myBlock1.getInput("IF0").connection.connect(myBlock2.outputConnection);
var myBlock3 = workspace.newBlock("math_number");
myBlock3.initSvg();
myBlock2.getInput("B").connection.connect(myBlock3.outputConnection);
if (this.getInputTargetBlock("inputValue")) {
var myBlock4 = workspace.newBlock(this.getInputTargetBlock("inputValue").type);
myBlock4.initSvg();
myBlock2.getInput("A").connection.connect(myBlock4.outputConnection);
if (this.getInputTargetBlock("inputValue").type=="math_number") {
myBlock4.setFieldValue(this.getInputTargetBlock("inputValue").getFieldValue("NUM"),"NUM");
}
}
return containerBlock;
},
compose: function(containerBlock) {
this.updateShape_();
},
saveConnections: function(containerBlock) {
},
updateShape_: function() {
}
};