constructor workspace.newBlock() doesn't display shadow blocks

96 views
Skip to first unread message

C Ben

unread,
Mar 2, 2021, 1:12:03 PM3/2/21
to Blockly

Hi all,
For documentation/tutorial purpose, I've implemented workspace.newBlock() method to trigger display of blocks in small workspace (I used this method to generate my doc : https://groups.google.com/u/1/g/blockly/c/37OqFtkhXKU/m/cnpyqQJHAwAJ ).

I use a toolbox.xml which implements shadows blocks. This same toolbox is also use for my main  blockly editor and shadows blocks display well with "regular" usage.

Unfortunelatly when I want to add block automaticly by code, the shadows block don't display.
I use the following code :
var newBlock = workspace.newBlock('myblockwithshadow');
newBlock.initSvg();
newBlock.render();

Thanks for your help,
Chris

Beka Westberg

unread,
Mar 2, 2021, 5:46:17 PM3/2/21
to blo...@googlegroups.com
Hi Chris,

I think the behavior you're seeing makes sense. If you load a block using workspace.newBlock() it doesn't know anything about the xml you're using for your toolbox. It's just instantiating a block with that type and mixing everything from your block definition into it. So it newBlock() wouldn't know what shadows to load even if it wanted to load them hehe.

If you want to load your block with shadow blocks programmatically, I recommend using Blockly.Xml.domToWorkspace (possibly in combination with Blockly.Xml.textToDom). That way you can just pass the same xml as is used in your toolbox, and the block will look exactly the same =)

Alternatively, you could access the connections you want to add shadows to using nextConnection, or getInput and connection, and then call setShadowDom on the connection to add the shadow. But this would probably be more of a hassle so I don't recommend it :/

I hope that helps! Sounds like you're creating some really nice docs for your users :D I hope the rest of the process goes smoothly hehe.

Best wishes,
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/1566ed8b-66c4-4ac4-a8b0-dfb909cbcee5n%40googlegroups.com.

C Ben

unread,
Mar 3, 2021, 5:44:15 AM3/3/21
to Blockly
Hi Beka,
Thanks a lot for your explanation, I've missed that behavior. Thus, I try to implement your advice, but what I would like, is to avoid duplicating all blocks defined in my toolbox in other xml strings to keep maintenance easy.
That's why I've implemented a parser of my "central" toolbox as to retrieve all information (categories, blocks etc.):
Here is the code :
var xmlToolbox = Blockly.Xml.textToDom(mytoolbox);
//Here I get categories (to generate H1 title tag in my live HTML doc) : works great
this.categories = Array.from(
xmlToolbox.getElementsByTagName("category"))
//Then I get all blocks by category : works great
this.categories.map((category) => {
Array.from(category.getElementsByTagName("block")).map((block) => {
Blockly.Xml.domToWorkspace(block, mysmallWorkspacededicatedtothisblock); //doesn't work : no warning, but the workspace doesn't render it !
})
Unfortunately, it doesn't work, maybe it misses the root xml element.
I try to put the block within a xml doc created as follow :
 xmldoc = document.implementation.createDocument(
"xml",
null
);
xmldoc.appendChild(block); //generate a bug
It seems that I'm close, but it doesn't work neither...

Thanks for your encouragement for the docs :-)
Chris

Beka Westberg

unread,
Mar 3, 2021, 5:42:50 PM3/3/21
to blo...@googlegroups.com
Yeah that looks really close! I think if you use `Blockly.utils.xml.createElement('xml');` instead it should work. That's what workspaceToDom uses.

I hope that helps!
--Beka

C Ben

unread,
Mar 4, 2021, 4:23:02 PM3/4/21
to Blockly
That works great, Thanks Beka.
Chris

Beka Westberg

unread,
Mar 4, 2021, 5:25:44 PM3/4/21
to blo...@googlegroups.com
My pleasure! Glad I could help =)
--Beka

Reply all
Reply to author
Forward
0 new messages