How to remove bragged blocks by clicking button in UI using program.

171 views
Skip to first unread message

SUCHITRA GIRI

unread,
Aug 16, 2021, 11:19:49 AM8/16/21
to Blockly
Hi,
I want to remove dragged blocks by clicking the reset button but instead of all dragged blocks only "ask_me_a_question" block is deleted.
Why my parent "Bot" block is not deleted??Screenshot (328).png
Here is my code..
```
function reset() {
  if (Blockly.selected && !Blockly.selected.workspace.isFlyout) {
    Blockly.Events.setGroup(true);
    Blockly.hideChaff();
    Blockly.selected.dispose(/* heal */false);
    Blockly.Events.setGroup(false);
}
  delete inputTextValue;
  redrawUi();
   //Blockly.selected.dispose(true);
}
```
Also, if possible explain these line of code could'n understand more!|
Thanks:)

SUCHITRA GIRI

unread,
Aug 17, 2021, 4:55:48 AM8/17/21
to Blockly
Please respond, by mistaken I wrote "bragged" instead of "dragged".
Message has been deleted

Abby Schmiedt

unread,
Aug 17, 2021, 8:33:45 PM8/17/21
to Blockly
Hello! 

You will need to dispose of all blocks instead of only disposing of the selected block. You can get all the blocks by using this method.

It looks like this topic is coming up repeatedly as part of a class, lesson, or other organized activity. Could you please share a link to the activity, or contact information for the organizer? Or, could you ask the organizer to get in touch with us by commenting on this forum? We have a few recommendations we would like to share with the organizer.

Thanks! 
Abby

Prabha Kumari

unread,
Sep 10, 2021, 5:33:35 PM9/10/21
to Blockly
Hey suchitra,
Have you done this or is it still showing the same? I want your help in this particular problem. I hope this message reaches out to you and you reply me as soon as possible.
Thank you!

Jason Schanker

unread,
Sep 12, 2021, 12:39:43 AM9/12/21
to Blockly
Hi,

As Abby had mentioned, you can get an Array of all the blocks present in the workspace by using the Workspace's getAllBlocks method.  To remove all of the blocks, you can use the Array's forEach method to iterate over the list of blocks, disposing of each one.  The line in red represents the change you can make to do this:

```
function reset() {
  if (Blockly.selected && !Blockly.selected.workspace.isFlyout) {
    Blockly.Events.setGroup(true);
    Blockly.hideChaff();
    Blockly.selected.workspace.getAllBlocks().forEach(block => block.dispose(/* heal */false));
    Blockly.Events.setGroup(false);
  }
  delete inputTextValue;
  redrawUi();
}
```

Also, in response to the original request for an explanation:

The if statement is used to make sure that a block is selected and this block is not present in the flyout (i.e., a toolbox).  If so, hideChaff will close all the open tooltips, context menus, etc. and dispose will be called on the selected block to remove it along with any attached input blocks or ones attached below it.  Passing true instead of false to dispose would try to "heal" these gaps both horizontally and vertically once it has been removed, if possible.  So for example, if the block is sandwiched inside of a vertical stack, then all of the blocks below it would move up to the one above it:

A                                                                   
B <== Deleted block
C
D

(After deletion with heal, assuming the previous connection of C can attach to the next connection of A):

A
C
D

I haven't investigated the setGroup method enough to know of its implications, but my top-level understanding is that it's used to prevent multiple firings of events.  So I think that anything called between setGroup(true) and setGroup(false) would now only cause one event to be fired even if it would ordinarily cause more, but I could be completely wrong about this.

Best,
Jason
Reply all
Reply to author
Forward
0 new messages