Hello Beka,
Top support! Couldn't have accomplished it without your help ...
Thanks to your example code, it became clear which way I had to solve it.
Will share my code here, so perhaps somebody else can learn from it.
Since my workspace can changed continiously (due to a normal mode / full screen mode setup), I wanted separate the code:
- Add the listener to the workspace, where the workspace is created.
- Execute the listener handler functionality inside my block definitions, to have the block related logic inside the block definition.
To accomplish that, I have added an oncreate function to my blocks:
Blockly.Blocks['set_timeout'] = {
init: function() {
this.jsonInit({
"type": "set_timeout",
"message0": Blockly.Msg.SET_TIMEOUT,
"args0": [
{
"type": "field_input",
"name": "NAME",
"text": findLegalName(Blockly.Msg.SET_TIMOUT_NAME, this),
"spellcheck": false
}
]
"inputsInline": false,
"previousStatement": null,
"nextStatement": null,
"colour": "#BB8FCE",
"tooltip": Blockly.Msg.SET_TIMEOUT_TOOLTIP,
"helpUrl": null
});
},
oncreated: function() {
var prefix = Blockly.Msg.SET_TIMOUT_NAME;
var newUniqueName = getUniqueName(prefix, this.type, this.workspace)
this.setFieldValue(newUniqueName, 'NAME');
}
};
That oncreated function should be called after this block has been created.
To do that, the block functions are being triggered in the event handler (which is registered where the workspace is created):
// Create the workspace
myWorkspace = Blockly.inject('blocklyDiv', {... });
// Change listener that calls the oncreate function of a block, after the block has been created in the workspace
myWorkspace.addChangeListener(function(event) {
if (event.type != Blockly.Events.BLOCK_CREATE) {
return; // we only care about create events.
}
for (var i = 0; i < event.ids.length; i++) { // .ids lists the ids of all of the blocks that were created during this event.
var block = node.workspace.getBlockById(event.ids[i]);
if (block.oncreated && typeof block.oncreated === 'function') {
block.oncreated();
}
}
});
And that seems to be working fine:
Have a nice day!!!
Bart