Deleting a block does not trigger event

585 views
Skip to first unread message

Becca Daywalker

unread,
Sep 15, 2017, 3:14:39 AM9/15/17
to Blockly
Hey guys, 

I am quite new to blockly and trying to detect if a block of a certain type is already in the workspace. 

This is the function for detecting if it was created or deleted (This seemed like the best way until now, if you know something better, please let me know)

function toggleStatus(choice) {
var key = choice.xml.attributes[0].value;

if (choice.type === "create" && choice.group !== "") {
created[key] = true;
console.log("create: ", key);
}
if (choice.type === "delete" && choice.group !== "") {
deleted[key] = true;
console.log("delete: ", key);
}

if (choice === Blockly.Events.Create()) console.log(Blockly.Events.Create.xml.attributes[0].value);
if (choice === Blockly.Events.Delete()) console.log(Blockly.Events.Create.oldXml.attributes[0].value);
}

The console.log for create works fine, but I have got some troubles with the delete function. 

If I only have one block of the same type in the workspace and delete it, the event will not be triggered. 
If there are two in the workspace it only triggers if I delete one of them (once). 
With three blocks of the same type in the workspace, the delete event fires twice, as I delete the first one (independet of the order of creation) and once for the second one and not at all for the last one. 

I hope you can help me.
Thank you so far :)

Regards, 
Becca

picklesrus

unread,
Sep 15, 2017, 1:18:04 PM9/15/17
to Blockly
Hi Becca,

Are you finding that Blockly doesn't fire a delete event at all in some cases or that your code can't catch them?  Does it matter what type of blocks you use? Especially if you're using blocks from Blockly's standard block set and blockly is not actually firing delete events at all, maybe you could provide the xml of your workspace and repro steps including what method you use to delete the blocks (dragging over flyout, trash can, right click menu, etc) so I can try and see what you mean.

That said, one thing you might try is to use the constants defined for event types to do your comparison instead of the strings. To test for a specific event type usually folks will do something like: event.type == Blockly.Events.CHANGE  

Also, is there any reason you're testing that the group is not empty? I don't think it matters in this case, but I'm not 100% sure.

Becca Daywalker

unread,
Sep 18, 2017, 5:52:59 AM9/18/17
to Blockly
Hey, 

Thank you for your response :)

I found a better way to catch if a block is already in the workspace.
In my opinions this topic may be closed. 
(for the interested ones:
 var items = Object.values(workspace.blockDB_);
Here the variable contains an Array with all blocks inside the workspace)

To your question(s):

a) I'm not sure if it is Blockly or my code. As I first encountered this problem, I thought the events are fired by the elements which are there (a deleted element can not fire anything anymore) and not of the lement which is the "center of attention", if you know what I mean. 

b) My 'purpose':
I don't use blockly the way it is intended ^^"
Actually I want to use it to help people "building" their website and the blocks are the elements inside the website (like logo and stuff).
So i don't think it is necessary which type I use, because this is only an identifier. 
The reason for this is, that there is no way a website should have 2 navigation bars at the top. 

In the end I want to extract a Json Object which cointains all the settings for the website. 

Now I have another problem with my code, but this does not belong in this topic I guess.

Thank you very much for helping anyways :)

Regards, 
Becca

Rachel Fenichel

unread,
Sep 18, 2017, 1:36:27 PM9/18/17
to Blockly

Hi Becca,

Looking at all of the blocks in the workspace is reasonable, but please use workspace.getAllBlocks() instead of directly examining the blockDB_. 

Things that end in underscores are private and not part of our API, so we reserve the right to change them without prior notice.

getAllBlocks() will do the exact same thing, but through our public API.

Cheers,
Rachel
Reply all
Reply to author
Forward
0 new messages