Dinamically add block to a target category

77 views
Skip to first unread message

Alejandro Ferrante

unread,
Nov 20, 2021, 1:31:41 PM11/20/21
to Blockly
Hello everyone, i know this has been asked before, nevertheless i am not able to achieve i.
Here is the sample code i've got so far:

var  xml = '<block type="logic_boolean"><field name="BOOL">TRUE</field></block>';
$('#toolbox').find('category')[5].append(xml);

'toolbox' is the id i set for the toolbox, i'm still having trouble to find the category by name so i harcoded the [5] for now.
The only thing left to do is to update the toolbox.
i suppose the way to do it is by calling Blockly.getMainWorkspace().updateToolbox

i tried 

Blockly.getMainWorkspace().updateToolbox(document.getElementById('toolbox'));
and
Blockly.getMainWorkspace().updateToolbox( Blockly.getMainWorkspace().getToolbox() );
but both of them throw an error.

Any suggestions?

Beka Westberg

unread,
Nov 21, 2021, 10:17:21 AM11/21/21
to blo...@googlegroups.com
Hello,

Have you looked into using a dynamic toolbox category? That works for most use cases =) Otherwise, you were definitely on the right track with updateToolbox. The first option you posted should work. If you decide to use updateToolbox instead of the dynamic category, you can post the error it's throwing and we can try to debug together!

Best wishes! I hope you have a lovely week =)
--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/37a0629c-ddd9-46b4-aa60-cbadef3c917fn%40googlegroups.com.
Message has been deleted

fu6...@gmail.com

unread,
Nov 22, 2021, 12:19:47 AM11/22/21
to Blockly
Hi, share my sample code.

var myWorkspace = Blockly.getMainWorkspace();
var xml = document.getElementById("toolbox");
var xmlDoc = new DOMParser().parseFromString('<block type="text"><field name="TEXT">Hello World</field></block>', 'text/xml');
xml.childNodes[0].appendChild(xmlDoc.documentElement);
myWorkspace.updateToolbox(xml);

fu6...@gmail.com

unread,
Nov 22, 2021, 12:30:14 AM11/22/21
to Blockly
If you want to insert the block before a specific node ...

xml.childNodes[0].insertBefore(xmlDoc.documentElement, xml.childNodes[0].childNodes[1]);

fu6...@gmail.com

unread,
Nov 22, 2021, 9:55:13 AM11/22/21
to Blockly
If you want to remove a specific node ..

var myWorkspace = Blockly.getMainWorkspace();
var xml = document.getElementById("toolbox");
var xmlDoc = new DOMParser().parseFromString('<block type="text"><field name="TEXT">Hello World</field></block>', 'text/xml');

//xml.childNodes[0].appendChild(xmlDoc.documentElement);
//xml.childNodes[0].insertBefore(xmlDoc.documentElement, xml.childNodes[0].childNodes[1]);
xml.childNodes[0].removeChild(xml.childNodes[0].childNodes[1]);

myWorkspace.updateToolbox(xml);

Alejandro Ferrante

unread,
Nov 23, 2021, 7:59:47 AM11/23/21
to Blockly
Thank you both a lot, you were of great help!
Thanks to your suggestions, this is the code that ended up working for me:


        var myWorkspace = Blockly.getMainWorkspace();
var xml = document.getElementById("toolbox");
var cat;
var categories = $('#toolbox').find('category'); 
for(var i = 0; i < categories.length; i++){
if(categories[i].attributes['name'].value == 'CUSTOM' ){
cat = categories[i];
break;
}
}
var xmlDoc = new DOMParser().parseFromString('<block type="text"><field name="TEXT">Hello World</field></block>', 'text/xml');
cat.appendChild(xmlDoc.documentElement);
myWorkspace.updateToolbox(xml);
Blockly.getMainWorkspace().getToolbox().clearSelection();
Reply all
Reply to author
Forward
0 new messages