How to disable a blockly drop-down.

157 views
Skip to first unread message

Rupak Choudhury

unread,
Jun 17, 2021, 10:37:31 AM6/17/21
to blo...@googlegroups.com
Hi Team,

Can anyone suggest me how to disable a blockly drop-down?

Thanks,
Rupak

Jason Schanker

unread,
Jun 18, 2021, 2:12:45 AM6/18/21
to Blockly
Hi Rupak,

Is it sufficient to disable all dropdowns from that block?  If so, you could call setEditable on the block, passing `false` as an argument (i.e., block.setEditable(false)).  Doing this will set the block's private `editable_` property to `false` so that its isEditable method will return false.  This in turn means that any field on this block will have its isClickable method return false (this.sourceBlock_.isEditable()) so that the editor won't show when showEditor is called.

So if not, the simplest way to disable only one field dropdown on a specific block would be to override the isClickable method for that specific field dropdown.  E.g., for a math_arithmetic block, you can prevent the dropdown from appearing as follows:

// assuming you have one math_arithmetic_block in your main workspace
const block = Blockly.getMainWorkspace().getBlocksByType("math_arithmetic")[0];
// disable
block.getField("OP").isClickable = () => false;
// reset
block.getField("OP").isClickable = Blockly.Field.prototype.isClickable;

But a cleaner way would be to extend Blockly.FieldDropdown, adding a private variable like isEnabled_ and initializing it to true in the constructor, providing a public method setEnabled that could change it to false and then adding a method that overrides Field's isClickable with something like:

isClickable() {
  return this.isEnabled_ && Blockly.Field.prototype.isClickable.call(this);
}

I can elaborate if you have more questions on this approach.

Best,
Jason
Reply all
Reply to author
Forward
0 new messages