Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How to monitor when a block is deleted in the workspace? I saw a 'destroy' mixin in the documentation, but it is also called when dragged and dropped

39 views
Skip to first unread message

孔令硕

unread,
Apr 17, 2025, 5:30:31 AMApr 17
to Blockly

WX20250417-172955@2x.png

Ronald Bourret

unread,
Apr 17, 2025, 1:09:23 PMApr 17
to blo...@googlegroups.com
Hi,

When a block is deleted, destroy() is called and the Blockly.Events.BLOCK_DELETE event is fired. This happens regardless of how the block is deleted -- it could be dragged to the trashcan or when you click "Delete Block" in the context menu.

It sounds like you want to do something only when "Delete Block" is clicked. Is this correct? If so, you could change the callback function for the "Delete Block" template in the context menu:

const deleteTemplate = Blockly.ContextMenuRegistry.registry.getItem('blockDelete');
oldDeleteCallback = deleteTemplate.callback;
deleteTemplate.callback = function(scope, e) {
  doSomething();
  oldDeleteCallback(scope, e);
};

This code will change the callback function for all blocks. If you only want to change the callback function for one type of block, see Customize context menus per block type or workspace.

I'm curious why it matters how the block is deleted -- I'm wondering if there is a better way to solve this.

Ronald Bourret
Technical Writer (Provided by Synergis)


On Thu, Apr 17, 2025 at 2:30 AM 孔令硕 <qq1007...@gmail.com> wrote:

WX20250417-172955@2x.png

--
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 visit https://groups.google.com/d/msgid/blockly/6dc913ff-295c-42fa-8c23-edd89d6daae8n%40googlegroups.com.

孔令硕

unread,
Apr 21, 2025, 1:40:21 AMApr 21
to Blockly

Thank you, I used Blockly The EventsBLOCK_DELETE event has been implemented. I need to clean up some resources after deleting the block again
Reply all
Reply to author
Forward
0 new messages