I need to have blocks that have a varible number of inputs. For the dictionary like this:
const fns = {
abs: 1,
add: 2,
...
}
I could define a different type of block for each type of fns. Instead, what I want to do is to define a generic block with no inputs and add the required number of inputs during initialization. I almost made it work like this:
export const call_expression_json = {
type: 'kw-call_expression',
message0: '',
args0: [],
extensions: ['add_args']
};
Blockly.Extensions.registerMixin('add_args ', {
addArgs() {
for (let i=0; i<this.data.argsNumber; i++) {
this.appendValueInput('input' + i);
}
},
}
and now when I create the block like this:
block = this.workspace.newBlock('kw-call_expression');
block.data = {argsNumber: 3};
block.addArgs();
it's rendered in the workspace.
However, the problem is when I move the block, I get the error:
> ERROR Error: The insertion marker manager tried to create a marker but the result is missing an input. If you are using a mutator, make sure your domToMutation method is properly defined.
which I assume comes from the fact that when a block is moved it's cloned (de-serialized/serialized). When this happens, in my case, Blockly can't find the corresponding input in the cloned block, as it hasn't been added yet.
So as I understand, I'm forced to use serialization hooks, but I'd want to avoid that as it adds complexity.
The other option is to create inputs in the initialization phase, e.g. inside mixin function, but at this point I don't yet have `data` property added.
It'd be great to have a way to pass initiazation params to the block when creating it:
block = this.workspace.newBlock('kw-call_expression_T', {argsCount: 3});
any suggestions?