isDeletable=false in toolbox json

83 views
Skip to first unread message

Spyros Economopoulos

unread,
Jul 9, 2022, 9:10:10 AM7/9/22
to Blockly
Hello,

I am not sure if this question is related to this thread:


What I am trying to accomplish is to create a set of composite blocks in my (JSON defined) toolbox the user should not be able to change.

A part of this is to prevent the user from deleting child blocks. So far the only way I have found is with code similar to the below:

const workspace = Blockly.inject('blocklyDiv', { maxBlocks: 3, toolbox: toolboxDef });
  function onBlockCreate(event) {
    if (event.type == Blockly.Events.BLOCK_CREATE) {
      const block = workspace.getBlockById(event.ids[0]);
      const child = block.getChildren()[0];
      child.setDeletable(false);
      console.debug(child);
    }
  }
  workspace.addChangeListener(onBlockCreate);

Is this the preferred aproach, since JSON is the preferred toolbox definition format?

Thank you in advance,

Spyros

Beka Westberg

unread,
Jul 11, 2022, 10:25:38 AM7/11/22
to blo...@googlegroups.com
Hello Spyros,

This question is indeed related to that linked post :D Thank you for digging it up to provide context!

First of all, I'd like to say that "glueing" blocks together isn't really supported at the moment :/ If you want it to be impossible to delete or disconnect blocks from each other, I recommend making them a single block. You can use setDeletable(false) as you've done, and setMovable(false) as well. But setMovable(false) still allows other blocks to be inserted between immovable blocks.

That being said, if you still want to pursue this design, a JSON toolbox is always recommended, because the XML system is iceboxed. To solve your particular problem, I would recommend implementing Option 1 from the linked forum post (if possible) "You can define the deletability, movability, and editability of your blocks using the setDeletable, setMovable, and setEditable functions within your block definition."

If that's not possible then I would recommend implementing Option 2.

While your event listener may work right now, it will cause problems down the line. For example, if you delete a block, and then undo that deletion a create event is fired. So any child blocks of the deleted block will become impossible to delete, even if they weren't originally supposed to be.

I hope that helps! If you have any further questions please reply!
--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/454c4e37-3ae3-4af5-abba-7f86eef99551n%40googlegroups.com.

Spyros Economopoulos

unread,
Jul 16, 2022, 1:41:18 AM7/16/22
to Blockly
Hi Beka,

thank you very much for your prompt reply.

My only concern with the first option is that for any possible combination of 'primitive' blocks one needs to create a new block definition.

Being able to do that 'dynamically' in the toolbox level would be nice. 

In any case using your suggestions it will solve my problem for now.

Thanks again for your time and positive energy!
Reply all
Reply to author
Forward
0 new messages