Deletion of blocks

121 views
Skip to first unread message

Chetna Bulbul

unread,
Oct 30, 2023, 11:15:02 AM10/30/23
to Blockly
Hi Team,

We have a requirement where we have created nested input blocks. This nesting can be at nth level. When the block is connected, we create some expression based on the connected blocks. When the nth block is deleted, we need to modify the expression . For this we need to know the deleted block was connected to which parent block(not immediate parent, we can call it as root block i guess). 
We see that the deletion event is getting called after the block is deleted, in this case, we are not able to find out, the block was connected to which parent block.

Is there any way to know the previous image or something like that?

Thanks,
Chetna

ewpa...@gmail.com

unread,
Oct 31, 2023, 2:55:53 PM10/31/23
to Blockly
A deletion event should be preceded by a move event indicating that the descendant block was disconnected from its parent block and this should happen in the same groupId. If you see a delete event, you could look at the undo stack for additional events in the same group and if any move events have the same blockId, you could look at the oldParentId to determine the parent that needs to be updated (possibly recursively if you're mainly concerned with the root block).

Regards,
Evan

Chetna Bulbul

unread,
Feb 15, 2024, 5:29:06 AM2/15/24
to Blockly
Thanks Evan. It worked.

However, I see that when the deletion event is fired programmatically i.e. by calling block.dispose(), group is coming as empty string in the event. 
There are few cases where we need to call dispose() and the behaviour should be same as deletion. Since group is coming as blank, this is not working in that case.Any thoughts?

Thanks,
Chetna

Mark Friedman

unread,
Feb 15, 2024, 3:59:27 PM2/15/24
to blo...@googlegroups.com
Assuming that it is your code that is calling dispose directly, probably the cleanest solution would be to handle the expression creation explicitly at the point that you call dispose (in addition to creating the expression in your event handler, for the UI initiated deletions).  

Another solution would be to use an event handler as you are and also look back in the undo stack for immediately preceding move events of the same block in the undo stack, regardless of group ID.  This is a bit brittle, because I don't think that Blockly promises not to change the set of events created by calls to dispose. But then again I'm not sure that they promise not to do so for UI initiatives deletion either, so maybe it's not so bad.

By the way, in case you don't know about it the Blockly Advanced Playground is a great tool for, among other things, playing around with Blockly and seeing the event stream.  Just enable the "Log Events" setting near the bottom of the settings panel (in the upper right of the display). The events will then be printed in the browser console log.

Hope this helps.

-Mark


--
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/5acce4b0-4f7e-4d72-a418-ae03c90928a0n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages