How to get the block object from toolboxItemDef

86 views
Skip to first unread message

Dummy PG

unread,
May 4, 2023, 9:24:47 AM5/4/23
to Blockly
I am creating toolbox search by creating custom toolbox category, i have everything working except for filtering the block.toString() or field.getText(). My question is, how can i get the block object from toolbox.contents_, i am using that as i was able to recursively get all block kind but its the json block def and missing the text fields I want to filter them with.

Christopher Allen

unread,
May 5, 2023, 12:02:38 PM5/5/23
to blo...@googlegroups.com
Hi Dummy PG,

(Oh boy does it feel wrong to address someone as "Hi Dummy", but I do try to call people by their preferred names…)

On Thu, 4 May 2023 at 14:24, Dummy PG <dummy...@gmail.com> wrote:
I am creating toolbox search by creating custom toolbox category, i have everything working except for filtering the block.toString() or field.getText(). My question is, how can i get the block object from toolbox.contents_, i am using that as i was able to recursively get all block kind but its the json block def and missing the text fields I want to filter them with.

 So, first of all: one of my colleagues is working on a plugin to provide toolbox search, so you could consider just waiting for that to be released—probably in a few weeks time.

In the meantime, if you can get the type field from the toolbox item, you can get the block definition by looking that up in Blockly.Blocks—e.g.:

Blockly.Blocks[category.contents[0].type]  // Block definition for the zeroth block in category.

Hope that helps!


Christopher


Dummy PG

unread,
May 8, 2023, 9:56:34 AM5/8/23
to Blockly
Hi Christopher,

Thanks for the response. Don't worry about the name :)
Anyway, i was able to try and get the block definition by getting the block type from the collection of Blockly.Blocks, however this definition does not have Block.inputList that I needed to filter the text using the field texts.
Is there a way to map that Block definition to a Block Class so I can use inputList?
I am really close in getting the custom toolbox to fully working If I can get those text somewhere :)

Thanks

Dummy PG

unread,
May 11, 2023, 3:13:09 AM5/11/23
to Blockly
Just posting an update. Was able to get the toolbox search working. I referenced the logic based on this old post here
Basically, after getting the block def toolbox content, i filter those block defs by creating a new headless workspace, and instantiate a blockObject = new Blockly.Block(headlessWorkspace, blockDef.type) then get the blockObject.toString() which gives all string in the rendered block.
Since I expect toolbox contents are static, we can just append the toString() values to the block defs at once. this will be then used to get the filtered blocks when searching text.
No need to get the block object from Blockly.Blocks since it didn't give the right toString() call.
Reply all
Reply to author
Forward
0 new messages