Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic dropdown "forgets" option keys when reloaded #2135

Closed
Marclev78 opened this issue Nov 22, 2018 · 3 comments
Closed

Dynamic dropdown "forgets" option keys when reloaded #2135

Marclev78 opened this issue Nov 22, 2018 · 3 comments

Comments

@Marclev78
Copy link

Marclev78 commented Nov 22, 2018

Given a dynamic dropdown, i.e. one where options is set to a function instead of a fixed array, if that function returns a list of options where the option key is different to the option value, the option key is lost upon reloading the workspace.

Expected Behavior

A dynamic dropdown should always show the option key to the user after a workspace using it is reloaded, not the option value.

Actual Behavior

Given a dynamic dropdown, if the workspace containing it is persisted to XML and then later reloaded, the option keys are lost and the option value is shown to the user in the workspace.

Steps to Reproduce

Given a dynamic dropdown function like:

function dynamicOptions() {
   return [["a", "b"]];

If you have a block on your workspace that uses it, it will show a as the option to the user upon being dragged onto the workspace.

If you then write the workspace to XML with Blockly.Xml, create a new empty workspace and populate it from the XML (again with Blockly.Xml) it will show the same block with the same dropdown, but this time the option will be shown as b to the user, i.e. Blockly is treating the option value as the key for display purposes.

Additional Information

From what I can see the issue is that the option keys are not represented in the workspace XML and the dropdown does not infer them from the dynamic option function when deserialized.

Strangely, if you open the dropdown to see the options, it highlights the correct one by key.

In my real life use case, the option values are a JSON datastructure, so this looks pretty ugly. Is there any way to force the dropdown to re-initialize the options upon deserialization?

@Marclev78 Marclev78 changed the title Dynamic dropdown "forgets" option keys Dynamic dropdown "forgets" option keys when reloaded Nov 22, 2018
@Marclev78
Copy link
Author

Marclev78 commented Nov 26, 2018

Upon some more investigation it seems that the issue is slightly more complex than my initial post assumed.

In my real life use case, the contents of the dropdown is dependent on the parent block, so is empty if the block isn't attached.

My dynamic dropdown function reads something like this:

if (this.sourceBlock_ && this.sourceBlock_.getSurroundParent()) {
   .....
   ....
} else {
   return [["", ""]];
}

It seems this that causes the options to be displayed incorrectly when the work-space is deserialized as the if statement above returns false during deserialization.

Is there any easy way to reinitialise the drop-down contents after the drop-down has been attached to a block and the block has been attached to a parent block?

@rachel-fenichel
Copy link
Collaborator

There's no especially easy way. You would just have to have a bit of extra code that runs after the workspace is loaded that goes through and reinitializes the dropdowns, I think.

I've filed #2142 to have us fire an event when the workspace finishes loading. You would then be able to listen for that event and run your code in response.

@moniika
Copy link
Contributor

moniika commented Mar 17, 2021

With the recent xml parsing changes, this issue seems to be fixed.

I tested repro using the new test block "test_dropdowns_dynamic_connect_dependant"
and loaded the following xml and found that the block was able to load with the expected values:

<xml xmlns="https://developers.google.com/blockly/xml">
  <block type="test_basic_limit_instances" id="uMmyq16f2k,H{[V}!~gF" x="188" y="213">
    <statement name="STATEMENT">
      <block type="test_dropdowns_dynamic_connect_dependant" id="u{}uYz-fUZCGUq)R=$*(">
        <field name="OPTIONS">uMmyq16f2k,H{[V}!~gF_type_key</field>
        <next>
          <block type="test_dropdowns_dynamic_connect_dependant" id="Hn:ftJ|CD],_5nmpuw4k">
            <field name="OPTIONS">uMmyq16f2k,H{[V}!~gF_key</field>
            <next>
              <block type="test_dropdowns_dynamic_connect_dependant" id="Bm0yYVTbcmtWh2CJrI2!">
                <field name="OPTIONS">u{}uYz-fUZCGUq)R=$*(_type_key</field>
                <next>
                  <block type="test_dropdowns_dynamic_connect_dependant" id="3l:!H1*]6mq/#F:rN8}u">
                    <field name="OPTIONS">u{}uYz-fUZCGUq)R=$*(_key</field>
                    <next>
                      <block type="test_dropdowns_dynamic_connect_dependant" id=".-@O.P;|_)Dfj%DOqZq?">
                        <field name="OPTIONS">CONNECTED_KEY</field>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </statement>
  </block>
  <block type="test_dropdowns_dynamic_connect_dependant" id="86A58t)2N-XkGYldNVq}" x="213" y="463">
    <field name="OPTIONS">UNCONNECTED_KEY</field>
  </block>
</xml>

@moniika moniika closed this as completed Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants