FieldVariable dropdowns without Rename and Delete options

61 views
Skip to first unread message

Ludvig Linse

unread,
Aug 18, 2020, 7:23:44 AM8/18/20
to blo...@googlegroups.com
Hi!

What is the neatest way to remove the rename and delete options from certain FieldVariable  dropdown menus (like the one shown on the screenshot below)? I know I could overwrite the Blockly.FieldVariable.dropdownCreate function but I was hoping there is a neater way where I don't have to keep the state of each type of dropdown.

Big thanks :-)

/Ludvig

image.png

--
The dialog-first tool to build scalable chat and voice apps
https://narratory.io

José Luiz

unread,
Aug 18, 2020, 8:01:18 AM8/18/20
to Blockly
Hey there! How're you doing? =D

I did exactly same thing as you want to, I remove what I call 'variable managing options' from dropdown, accordingly to the selected variable type, cause on my application I have some 'system variables' that shouldn't be magaed by the user.

To achieve that I created an extension, you cand find out more about mutators and extensions here. This extension is always invoked when user click on dropdown to open it.

Inside this extensoin, I have a function that calculates and overrides the menuGenerator_ property of the field, see it below:

let thisBlock = (this as unknown) as Blockly.BlockSvg;
let thisField = thisBlock.getField('model_alias') as Blockly.FieldVariable;

if (!!thisField)
{
const MODEL_ALIAS_MENU_GENERATOR = (): string[][] =>
{
// Gather all valid model aliases to be shown
let options = Blockly.FieldVariable.dropdownCreate.call(thisField);

let selectedVar = thisField.getValue();
if (BlocklyHelper.isSystemVariable(selectedVar))
{
// If dropdown has a system variable as it's selected one, remove variable management options e.g 'Rename Variable', 'Delete Variable'

if (Blockly.mainWorkspace.getVariableById(selectedVar))
{
options.splice(
options.findIndex((opt) => opt[1] === Blockly.RENAME_VARIABLE_ID),
1
);

options.splice(
options.findIndex((opt) => opt[1] === Blockly.DELETE_VARIABLE_ID),
1
);
}
}
return options;
};

thisField.menuGenerator_ = MODEL_ALIAS_MENU_GENERATOR;
}



Hope this help you!
Reply all
Reply to author
Forward
0 new messages