Hello,
I'm trying to make a dynamic dropdown that features all the procedures within the workspace.
So far I have the list generating the options and whenever the selected procedure is deleted it removes it resets the dropdown to it's default option.
My current problem is that I want to use the renameProcedure hook to update the selected option, well, when they rename the procedure. The problem I'm running into is that the options don't regenerate before the renameProcedure hook is called (They actually don't regenerate at all, before or after, not until the user manually clicks the dropdown list).
What I'm wondering is if there is a way to manually regenerate the list, so that I can update the field. I tried field.getOptions(false), in hopes that it would regenerate the internal options as well, but it seems that it doesn't (which seems like it should).
Bellow will be my current code for any recommendations:
// Option Generator
function (this: Blockly.Field) {
const sourceBlock = this.getSourceBlock()
const list = [['nothing', 'null']] as [string, string][]
if (!sourceBlock || !sourceBlock.workspace) return list
const tuple = Blockly.Procedures.allProcedures(sourceBlock.workspace)
for (let i = 0; i < tuple.length; i++) for (let j = 0; j < tuple[i].length; j++) list.push([tuple[i][j][0], tuple[i][j][0]])
return list
}
// Rename Procedure Hook
function (this: Blockly.Block, oldName: string, newName: string) {
const field = this.getField(fieldName) as Blockly.FieldDropdown
const value = field.getValue()
if (!field || !value || !Blockly.Names.equals(oldName, value)) return
field.setValue(newName)
}
// On Change Listener
function (this: Blockly.Block, event: Blockly.Events.BlockChange) {
if (!this.workspace || this.workspace.isFlyout || event.type !== Blockly.Events.BLOCK_DELETE) return
const value = this.getFieldValue(fieldName)
if (value === 'null' || Blockly.Procedures.getDefinition(value, this.workspace)) return
this.setFieldValue('null', fieldName)
}
I'm using a helper function which wraps these functions, which is where fieldName is defined.