ENABLE BLOCKS

95 views
Skip to first unread message

Eric

unread,
Mar 28, 2023, 6:56:06 AM3/28/23
to Blockly
quetion.jpg

Good morning Blockly team.

Query, I have a category with 6 blocks, of which I have 1 enabled and the rest disabled (I did this with the JSON format, with the "disabled" property). My query is the following, how do I make sure that when the user drags the block that is enabled to the workspace, the rest of the blocks in the category are enabled? and apart from that, that in turn, that "parent" block, once dragged to the workspace, is disabled so that it is not used.

Maribeth Bottorff

unread,
Mar 28, 2023, 11:55:04 AM3/28/23
to Blockly
Hello,

I can think of a few ways to accomplish this so I'll share them all.

1. Always enable the child blocks, and when one is created, also create the parent block if it doesn't already exist. We have similar logic when you paste a procedure call block that doesn't have a matching definition block.
2. Dynamic categories. You can make this toolbox category a dynamic category that shows one of two states each time the category is opened. The dynamic category callback would check if the parent block already exists in the workspace, and pick the corresponding state to show.
3. Event listeners. You can attach an event listener to the workspace that listens for block create and delete events and checks if they are about this type of block. If so, then update the toolbox category correspondingly.

Another thing you will likely want is to use the `maxInstances` injection option. This allows you to say that the parent block should only ever have one instance on the workspace, and Blockly will automatically prevent the block from being duplicated or pasted on the workspace, and actually will disable it in the toolbox automatically for you.

Another edge case you will want to look out for is if you have both parent and child blocks on the workspace already, and then the user deletes the parent block. You may already handle this by only allowing the child blocks to be connected to a stack of other LED-related blocks, and by disabling orphan blocks, which would happen if there was a child block that wasn't connected to the parent block. Otherwise, you'll want to use event listeners to disable the child blocks on the workspace. 

I would go with method 1 combined with the `maxInstances` option. That will be easiest to make sure your toolbox state is always valid, and maybe more straightforward for your users who won't have to wonder why the blocks are all disabled in the toolbox.

Hope that helps! Please let us know if you need additional assistance.
Maribeth

Eric

unread,
Apr 4, 2023, 5:57:29 AM4/4/23
to Blockly
Thank's! Mari :)
Reply all
Reply to author
Forward
0 new messages