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