Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Edit block based on dropdown menu.

48 views
Skip to first unread message

Tim Miguletz

unread,
Dec 23, 2024, 11:10:15 AM12/23/24
to Blockly
Hi,

I’m developing a custom workspace for generating Ansible playbooks.

One key feature I aim to implement is a task block with the following functionality:
  • Display all available modules, stored in a JSON string, within a dropdown menu.
  • Dynamically update its structure based on the options and arguments of the selected module.

I’ve tried to use mutators and the onchange function but encountered issues with triggering the events effectively.

I’d appreciate any feedback or suggestions on how to resolve this or alternative approaches to achieve the desired functionality.

I’ve included my blocks file in this post for reference.

Thanks in advance.

Kind regards,
Tim Miguletz
ansible.txt

Christopher Allen

unread,
Dec 24, 2024, 6:32:03 PM12/24/24
to blo...@googlegroups.com
Hi Tim,

One key feature I aim to implement is a task block with the following functionality:
  • Display all available modules, stored in a JSON string, within a dropdown menu.
  • Dynamically update its structure based on the options and arguments of the selected module.

That's a pretty typical situation where a block has a mutator (i.e. any code that updates its structure) but not a mutator icon.  The most usual way of implementing that is using a field validator on the dropdown field, where the validator initiates mutation of block rather than merely approving/rejecting the new field value.

A good example of a block that does exactly this sort of thing—albeit with fairly simple mutations—is the math_number_property block (displayed in the toolbox as "[0] is [even ▾]"), which uses the math_is_divisibleby_mutator (definition starts here with IS_DIVISIBLEBY_MUTATOR_MIXIN) to add or remove an additional input depending on which item is selected in the dropdown.


Best wishes,

Christopher

Tim Miguletz

unread,
Dec 26, 2024, 11:29:33 AM12/26/24
to Blockly

Good evening, Christopher,

Thank you so much for your suggestion!

In the meantime, I’ve strayed away from using createFromJSON and instead defined the blocks directly via JSON.
This approach helped a lot and made the process much clearer.

I now have a working blocks file and hope it will be helpful to others in their projects. :)

Best regards,
Tim

ansible.txt
Reply all
Reply to author
Forward
0 new messages