mutators in json definition

165 views
Skip to first unread message

Bart Butenaers

unread,
Aug 20, 2018, 4:43:36 AM8/20/18
to Blockly
Hi everybody,

I want to convert all my block definitions to JSON format, but now I'm stuck on the SWITCH block that I used from this link.
In that Block two mutators (?) are defined:

this.setMutator(new Blockly.Mutator(['case_incaseof','case_default']));

However if I want to convert that to JSON, I see this in the documentation
Mutators are the only way to provide custom serializable state on a block. 
They are declared on a block's JSON definition using the mutator key. 
Only one mutator may be declared on a block.

{
 
//...,
 
"mutator": "controls_if_mutator",
}

Is it possible to do this in JSON?

Thanks again !!
Bart

Bart Butenaers

unread,
Aug 20, 2018, 7:59:52 AM8/20/18
to Blockly
Hi folks,

case closed.  I misunderstood the concept entirely.  Seems that it is a single mutator that we need to pass the two sub-blocks it will use in this case.

For anybody ever making the same stupid mistake, here is what I did (I used the mutator code of this one):

1. Created a mutator object will all the required functions:

const switchCaseMutator = {
  mutationToDom
: function() {
   
...
 
},

  domToMutation
: function(xmlElement) {
   
...
 
},

 
...
};

 2. Register this mutator object, with fourth parameter an array of all sub-objects used in the mutator:

Blockly.Extensions.registerMutator('switch_case_mutator', switchCaseMutator, null, ['case_incaseof', 'case_default']);

3. At least use the "mutator" in the JSON definition of your block:

Blockly.Blocks['switch_case'] = {
    init
: function() {
       
       
this.jsonInit({
           
"type": "switch_case",
           
"mutator":["switch_case_mutator"],
           
"colour": "#5C81A6",
           
"tooltip": Blockly.Msg.SWITCH_TOOLTIP,
           
"helpUrl": null
       
});
}}

Then it seems to be working fine ...

Bart
Reply all
Reply to author
Forward
0 new messages