Hi Jakob,
Do all the dropdowns share the same Array of options? If so, the easiest way is to initialize a variable to your options Array and reference that Array in your block definition:
const workspace = Blockly.getMainWorkspace();
const optionsArr = [['Default option', 'DEFAULT'], ['Option B', 'OPTIONB'], ['Option C', 'OPTIONC']];
let currentOptionsArr = optionsArr.slice(); // used for comparison
Blockly.Blocks['sharedOptionsBlock'] = {
init: function() {
this.appendDummyInput().appendField(new Blockly.FieldDropdown(optionsArr), 'OPTS');
}
};
If the renaming of an option only occurs on a workspace event, you could add a listener to the workspace to do a comparison of currentOptionsArr to optionsArr. If there was a change, you can iterate over all the blocks of the specified type, selecting the appropriate new field value based on the differing index and then update the currentOptionsArr to be a (shallow) copy of optionsArr.
workspace.addChangeListener((e) => {
const changedIndex = optionsArr.findIndex((option, index) => option[0] !== currentOptionsArr[index][0] || option[1] !== currentOptionsArr[index][1]);
if (changedIndex !== -1) {
Blockly.getMainWorkspace().getBlocksByType('sharedOptionsBlock').forEach(b => {
b.setFieldValue(optionsArr[changedIndex][1], 'OPTS')
});
currentOptionsArr = optionsArr.slice();
}
});
Best,
Jason