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

In a user-defined enumeration, the item is not selected with Blocks. #6037

Open
jp-96 opened this issue Aug 22, 2023 · 2 comments
Open

In a user-defined enumeration, the item is not selected with Blocks. #6037

jp-96 opened this issue Aug 22, 2023 · 2 comments
Labels
block authoring bug Something isn't working

Comments

@jp-96
Copy link

jp-96 commented Aug 22, 2023

FieldUserEnum

Describe the bug
No items are selected a user-defined enumerations of Radio Broadcast extensions and playground example.

playground example

Creating enumerations with blocks
https://makecode.com/defining-blocks#creating-enumerations-with-blocks

Playground example : Create Enums from Blocks

Sample: Creating Enum
Editor: micro:bit

On Blocks editor
Place the blocks as follows:
image
Member Red (=0) is not selected, but the display will be 0

JavaScript editor → Blocks editor
On the JavaScript editor, code as follows:
image

When I convert to Blocks, it shows Red instead of Blue and nothing is selected.
image
However, since 1 is displayed on the simulator, it appears that Blue (=1) is selected internally.
It appears that internal values are retained when blocks are copied.

Additional context

value
The format of the name is 10mem, where "10" is the value and "mem" is the enumerated member.
Therefore, it is not hit by item selection and does not become selected.

https://github.com/microsoft/pxt/blob/master/pxtblocks/fields/field_userenum.ts#L152

menu generator
The fake line is called in the createMenuGenerator function.
https://github.com/microsoft/pxt/blob/master/pxtblocks/fields/field_userenum.ts#L62

@jp-96
Copy link
Author

jp-96 commented Aug 23, 2023

Why value+name ?

JavaScript editor → Blocks editor

Web browser console log:

pxtblockly.js:2271
Cannot set the dropdown's value to an unavailable option. Block type: color_enum_shim, Field name: MEMBER, Value: Blue

more details

When the createMenuGenerator callback is called in the constructor, the Fake part is executed.
Then the Workspace part is executed immediately and the dropdown list value is set to value+name as expected.
However, the custom block color_enum_shim does not seem to return value+name, only name.

function call log:

pxtblockly.js:17248 FieldUserEnum1
pxtblockly.js:17297 createMenuGenerator
pxtblockly.js:17311 createMenuGenerator<fake>
pxtblockly.js:17316 createMenuGenerator (4) [Array(2), Array(2), Array(2), Array(2)]0: (2) ['Red', 'Red']1: (2) ['Blue', 'Blue']2: (2) ['Yellow', 'Yellow']3: (2) ['Add a new color...', 'CREATE']length: 4[[Prototype]]: Array(0)
pxtblockly.js:17270 doClassValidation_
pxtblockly.js:17251 FieldUserEnum2
pxtblockly.js:17302 createMenuGenerator<workspace>
pxtblockly.js:17316 createMenuGenerator (4) [Array(2), Array(2), Array(2), Array(2)]0: (2) ['Red', '0Red']1: (2) ['Blue', '1Blue']2: (2) ['Yellow', '2Yellow']3: (2) ['Add a new color...', 'CREATE']length: 4[[Prototype]]: Array(0)
pxtblockly.js:17270 doClassValidation_
pxtblockly.js:17302 createMenuGenerator
pxtblockly.js:17316 createMenuGenerator (4) [Array(2), Array(2), Array(2), Array(2)]0: (2) ['Red', '0Red']1: (2) ['Blue', '1Blue']2: (2) ['Yellow', '2Yellow']3: (2) ['Add a new color...', 'CREATE']length: 4[[Prototype]]: Array(0)
pxtblockly.js:17248 FieldUserEnum1
pxtblockly.js:17297 createMenuGenerator
pxtblockly.js:17311 createMenuGenerator<fake>
pxtblockly.js:17316 createMenuGenerator (4) [Array(2), Array(2), Array(2), Array(2)]0: (2) ['Red', 'Red']1: (2) ['Blue', 'Blue']2: (2) ['Yellow', 'Yellow']3: (2) ['Add a new color...', 'CREATE']length: 4[[Prototype]]: Array(0)
pxtblockly.js:17270 doClassValidation_
pxtblockly.js:17251 FieldUserEnum2
pxtblockly.js:17302 createMenuGenerator<workspace>
pxtblockly.js:17316 createMenuGenerator (4) [Array(2), Array(2), Array(2), Array(2)]0: (2) ['Red', '0Red']1: (2) ['Blue', '1Blue']2: (2) ['Yellow', '2Yellow']3: (2) ['Add a new color...', 'CREATE']length: 4[[Prototype]]: Array(0)
pxtblockly.js:17270 doClassValidation_
pxtblockly.js:2271 Cannot set the dropdown's value to an unavailable option. Block type: color_enum_shim, Field name: MEMBER, Value: Blue
pxtblockly.js:17254 init
pxtblockly.js:17277 initVariables

@jp-96
Copy link
Author

jp-96 commented Aug 23, 2023

Suggest modifying the FieldUserEnum class

If it is only name, change to value+name.

https://github.com/microsoft/pxt/blob/master/pxtblocks/fields/field_userenum.ts#L23


        doClassValidation_(value: any) {
            console.log("doClassValidation_1", value)   // DEBUG:LOG
            // The format of the name is 10mem where "10" is the value and "mem" is the enum member
            if (this.sourceBlock_ && this.sourceBlock_.workspace) {
                const options = this.sourceBlock_.workspace.getVariablesOfType(this.opts.name);
                options.some(model => {
                    const [name,] = parseName(model);
                    if (name === value) {
                        value = model.name
                        return true
                    }
                    return false
                });
            }
            console.log("doClassValidation_2", value)   // DEBUG:LOG
            // update cached option list when adding a new kind
            if (this.opts?.initialMembers && !this.opts.initialMembers.find(el => el == value)) this.getOptions();
            return super.doClassValidation_(value);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
block authoring bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants