Custom dropdown menu support for Colors and Crayons library

Edit: Split off from "Dropdown menus via list variables."

Awesome, thank you!

My application is in the Colors and Crayons library, where I have these two blocks:


I'd really like to combine them, so "crayon" would be one more option in the block on the left, but still get the dropdown menu for the second input when you choose crayon for the first one. And also have a different dropdown menu for color numbers that would only have relatively few entries, e.g.,
20 spectral red
25 darkest red
30 saddle brown
35 darkest brown
40 spectral orange
etc.

The reason this matters is that libraries don't include variables, but only procedures. The Colors library, the first time you use one of its blocks, uses the Variables library to make a bunch of global variables (which someday will use some different mechanism so as not to risk collision with the user's variable names). But that happens once the script is running, too late to look inside global variables to get the specification for the dropdown.

So I'd sort of like to drop a list on that second (variable name) input, except that including large-ish lists in the definition of a block makes it take forever to load, save, or edit the block. So I guess what I really need is to be able to put a JSON text in that slot, and have you convert it to a list dynamically. The JSON-to-list conversion is of course already in the Snap! source code. And I guess putting a list in the variable slot should work too.

Does that sound plausible? Thanks.

The issue to do this is that currently, I'm getting a "simple nested" evaluated Snap! list object from a global variable, that "all" I have to do is convert to nested arrays and then reconvert back to a menu object
(Might be able to miss off the double conversion but I farmed out the recursive JS to a kind friend and kept it simple for them by pre-converting Snap! lists to arrays so that they don't have to deal with them)

Anyway, if I try to use the 2nd slot contents directly, then I go down a deep,deep,deep,deep nested object hole (and did I mention its very, very, very twisty as well!) and although I can access the values of the slot eventually in the JS console log - there is no chance of me being able to recursively code up how to get there :frowning:

But, for your use case, I think it would be sufficient to "hard code" the menus as JSON into the menu JS function as a custom custom reporter for this sort of library block stuff that isn't dynamic
e.g

function () {
var menuArray = [ "colours", ["red", "green",....}, "crayons",[...] ...]
... rest of static code to convert menuArray to Snap! menu object

and just use a convenient JSON editor :slight_smile:

userMenuFromList3 script pic

and copy/paste the result in if/when you need to update it

Yes, that'll work. OK, thanks!

I'll knock up a POC for it

Just done the simple POC
Here it is (in fromJSON sprite)

image

Just need to change JS menu var to suit

image

ProjectName=userMenuFromList3

So you need to name the menus you want to see in 1st slot using same name as the manual entries in the 2nd slot

image

image

image

image

[edit - project updated 14Apr2021 15:00UTC to fix caching issue when block used for 1st time]

ProjectName=userMenuFromList4a

Excellent! Thanks!

Can you split the thread in two please just at this point

https://forum.snap.berkeley.edu/t/dropdown-menus-via-list-variables/6182/10

So we can keep the "hard coded" block discussion separate from my main reporter development