how delete a block with javascript?

1,407 views
Skip to first unread message

César Ivan Delgado Silva

unread,
Jul 26, 2018, 1:11:53 PM7/26/18
to Blockly
I want delete the selected block pressing the delete button... how can i do it?
Captura de pantalla 2018-07-26 a la(s) 13.09.51.png

Rachel Fenichel

unread,
Jul 26, 2018, 4:06:22 PM7/26/18
to Blockly
You shouldn't need to do anything.  Blockly handles the backspace and delete keys, acting on the selected block.

Rachel

Juan Pablo Sandoval Alcocer

unread,
Jul 26, 2018, 6:24:48 PM7/26/18
to blo...@googlegroups.com
Hi Rachel,

I think Cesar wants to delete a block programatically.

Cheers,
Juan p.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Stanica

unread,
Jul 26, 2018, 6:26:32 PM7/26/18
to blo...@googlegroups.com
You're right I replied to the wrong group

On Thu, Jul 26, 2018 at 6:24 PM Juan Pablo Sandoval Alcocer <juam...@gmail.com> wrote:
Hi Rachel,

I think Cesar wants to delete a block programatically.

Cheers,
Juan p.
2018-07-26 16:06 GMT-04:00 'Rachel Fenichel' via Blockly <blo...@googlegroups.com>:
You shouldn't need to do anything.  Blockly handles the backspace and delete keys, acting on the selected block.

Rachel


On Thursday, July 26, 2018 at 10:11:53 AM UTC-7, César Ivan Delgado Silva wrote:
I want delete the selected block pressing the delete button... how can i do it?

--
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.

For more options, visit https://groups.google.com/d/optout.

--
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.

For more options, visit https://groups.google.com/d/optout.


--
- Robert

Rachel Fenichel

unread,
Jul 26, 2018, 7:21:14 PM7/26/18
to Blockly
To delete a block programmatically: myBlock.dispose(true).  The argument determines whether children are also deleted.  Documentation is here.

However, you should not need to implement anything to have a block be deleted when you press the delete key.  If you need to implement that yourself, something in Blockly is broken.

César Ivan Delgado Silva

unread,
Jul 30, 2018, 12:48:01 AM7/30/18
to Blockly
Well.. MyBlock.dispose() was good but, when undo. the block dont back last position... then I had to simulate the delete button

var e = new Event("Key down");
e.keyCode = 8;
document.dispatchEvent(e);
Blockly.onKeyDown_(e);

Erik Pasternak

unread,
Jul 30, 2018, 2:07:42 PM7/30/18
to Blockly
I think what you want is something like
if (Blockly.selected && !Blockly.selected.workspace.isFlyout) {
Blockly.Events.setGroup(true);
Blockly.hideChaff();
Blockly.selected.dispose(/* heal */ true, true);
Blockly.Events.setGroup(false);
} That's what happens when the user hits delete with a block selected.
Message has been deleted

Toha Assegaf

unread,
Jul 31, 2019, 12:23:45 PM7/31/19
to Blockly
how to delete block, and if i delete the block my temporary become decrement... PLEASE HELP....!!

Navpreet Devpuri

unread,
Jun 7, 2020, 5:04:28 AM6/7/20
to Blockly
now i can despose my block by myBlock.dispose(true) and it goes to recyclebin but now my question is hpw to remove it from recyclebin also that mean i want to totaly remove a block from both workspace and recyclebin.
How can i do this ?

Navpreet Devpuri

unread,
Jun 7, 2020, 5:22:38 AM6/7/20
to Blockly

Beka Westberg

unread,
Jun 7, 2020, 10:30:45 AM6/7/20
to blo...@googlegroups.com
Hello!

If you want to disable the trashcan you can do that by setting `maxTrashcanContents: 0` in the workspace config.

If you want to empty the trashcan you can do that using the emptyContents function.

If you want to remove a specific block from the trashcan, or disallow it from being added in the first place, I don't think that's currently possible :/ You could file a feature request for it though!

I hope that helps!
--Beka

R Jeevakumar

unread,
Jun 16, 2020, 8:04:15 PM6/16/20
to Blockly
You can use the `dispose` method on a block.

var block = workspace.getBlockById(e.blockId);
block
.dispose();

Reply all
Reply to author
Forward
0 new messages