Adding categories dynamically toolbox

419 views
Skip to first unread message

frm...@gmail.com

unread,
Apr 21, 2022, 7:59:11 PM4/21/22
to Blockly
Is it possible to add categories dynamically to the Toolbox?
(Please say yes (and how ;) ).

Cheers,
Oded

Neil Fraser

unread,
Apr 21, 2022, 8:24:50 PM4/21/22
to blo...@googlegroups.com
Yes, absolutely.  The only catch is that Blockly needs to start with a category toolbox to start with, you can't dynamically change from a simple toolbox to a category toolbox or back again.

Here's the documentation you seek:

--
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/07719e6e-5b6d-4950-b9a2-80328a8c88d1n%40googlegroups.com.


--

frm...@gmail.com

unread,
Apr 21, 2022, 8:35:21 PM4/21/22
to Blockly
Thanks Neil!

Just asserting, that we are on the same page: can one do nested categories dynamically?
(The demo from the link you shared is shallow / not nested).

Neil Fraser

unread,
Apr 21, 2022, 8:42:24 PM4/21/22
to blo...@googlegroups.com
Yes, absolutely.  Branches of nested sub-categories can be changed at any time, assuming one started with a toolbox with categories.

frm...@gmail.com

unread,
Apr 21, 2022, 10:55:21 PM4/21/22
to Blockly
Cool!
Started following the link you shared.

Added the category as followed:
<category name="Projects"
  colour={UTILS.getColorPalette().EXAMPLES}>
  <category name="Colours" custom="COLOUR_PALLET">
  </category>
</category>

It appears as a menu item in the category as seen in attached screenshot.

Pasted the following log with some debugging logging:
var coloursFlyoutCallback = function (workspace: any) {
console.log("JJJ CALLBACK CALLED")
var colourList = ['#4286f4', '#ef0447'];
var blockList = [];
for (var i = 0; i < colourList.length; i++) {
blockList.push({
'kind': 'block',
'type': 'colour_picker', // Type is optional if you provide blockxml
'blockxml': '<block type="colour_picker">' +
'<field name="COLOUR">' + colourList[i] + '</field>' +
'</block>'
});
}
return blockList;
};
console.log("JJJ Before");
workspace.registerToolboxCategoryCallback(
'COLOUR_PALETTE', coloursFlyoutCallback);
console.log("JJJ After");
console.log(workspace.getToolbox());
this.props.setWorkAreaLoadingState(LoadingState.INITIALIZED,
0);

With the following console.log output:
JJJ Before
JJJ After
Blockly.Toolbox {workspace_: B…y.WorkspaceSvg, toolboxDef_: {…}, horizontalLayout_: false, HtmlDiv: div.blocklyToolboxDiv.blocklyNonSelectable, contentsDiv_: div.blocklyToolboxContents, …}


Yet clicking the menu item does not trigger the 
console.log("JJJ CALLBACK CALLED")
command

Instead an error is triggered with the following trace:

blockly_compressed.js:875 Uncaught TypeError: Couldn't find a callback function when opening a toolbox category.
    at Blockly.VerticalFlyout.Blockly.Flyout.getDynamicCategoryContents_ (blockly_compressed.js:875:1)
    at Blockly.VerticalFlyout.Blockly.Flyout.show (blockly_compressed.js:871:1)
    at Blockly.Toolbox.updateFlyout_ (blockly_compressed.js:980:1)
    at Blockly.Toolbox.setSelectedItem (blockly_compressed.js:978:1)
    at Blockly.Toolbox.onClick_ (blockly_compressed.js:965:1)
    at HTMLDivElement.h (blockly_compressed.js:829:1)

What am I doing wrong?
Screen Shot 2022-04-21 at 7.32.15 PM.png
Reply all
Reply to author
Forward
0 new messages