Set else statement in a controls_if block programatically

204 views
Skip to first unread message

Rafa Menendez de la Vega

unread,
Nov 26, 2020, 7:02:18 AM11/26/20
to Blockly
Hi everyone!

I have some problems to understand how to work with issues like this one... I have a controls_if block and I'd like to know how can I set a new else/elseif statement in it programatically.

Any help will be welcome! Thanks in advance

Beka Westberg

unread,
Nov 26, 2020, 3:33:58 PM11/26/20
to blo...@googlegroups.com
Hello,

The controls_if block uses a mutator to control its shape. So to change the shape programmatically you'll need to pass some xml to the block's domToMutation function.

For example:
```
ifBlock.domToMutation(Blockly.textToDom('<mutation elseif="1" else="1"/>'));
```
Or maybe:
```
ifBlock.domToMutation(Blockly.textToDom('<xml><mutation elseif="1" else="1"/></xml>').firstChild);
```
(I can't remember if textToDom accepts strings that aren't wrapped in <xml></xml> tags)

I hope that helps! If you have any further questions please reply!
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/84007e9a-a458-4520-993e-9ff743e7695en%40googlegroups.com.

Rafa Menendez de la Vega

unread,
Nov 27, 2020, 11:19:04 AM11/27/20
to Blockly
I'm not being able to solve it. I get an error saying that Blockly.textToDom is not a function

I don't know too much about mutators so maybe is that the main issue :(

Beka Westberg

unread,
Nov 27, 2020, 6:26:34 PM11/27/20
to blo...@googlegroups.com
Hello,

Sorry that's my issue hehe. It should have been `Blockly.Xml.textToDom`.

And a note for in the future: If you are getting errors it is really helpful if you post the full stack trace of your error. Luckily this one wasn't too tricky to figure out. But that's not always the case!

I hope that helps! If you have any further questions (or my code has any further problems :P) please reply!
--Beka

Reply all
Reply to author
Forward
0 new messages