pxt icon indicating copy to clipboard operation
pxt copied to clipboard

Custom field dropdowns: shadow blocks are serializing functions not meant to be exposed

Open jfo8000 opened this issue 4 years ago • 4 comments

Custom field editors are serializing out the contents of their shadow blocks instead of their literal values.

This serialization problem is also appearing in the BuildBee MakeCode color picker. I had to expose the shadow block in the API so that it would not look strange. But it looks like the feature is broken?

** PXT Playground Example **

Load up PXT playground, example field-editors-dropdowns https://makecode.com/playground#field-editors-dropdowns Hit the run button image Add the "Say hello" block image Flip the editor to Javascript image

Expected Since the __wordPicker function is marked as blockHidden, and it is a shadow field editor, I expect the correct serialization to be time.say("Hello")

Actual It seems to generate javascript that can't be compiled (this is worse than what I'm seeing on BuildBee using number types)

jfo8000 avatar Jan 25 '21 23:01 jfo8000

Moreinfo: This is the block definition. WordPicker has blockHidden set to true? image

jfo8000 avatar Jan 25 '21 23:01 jfo8000

Ahh, to my knowledge that field editor isn't actually used anywhere besides that example -- it might have just been built as a test (or as an example of how someone could author a field editor themselves) and then completely forgotten about. For reference the src for that field editor is https://github.com/microsoft/pxt/blob/master/pxtblocks/fields/field_textdropdown.ts; is this one that you intended to use or did you just happen to run into the bug while looking through the playground?

re: the decompiled code including __wordPicker, //% blockHidden refers to whether the block itself is hidden (that is, from the toolbox), not a part of the decompiler -- if you want to elide it when compiling blocks -> javascript, that happens with //% shim=TD_ID

jwunderl avatar Jan 25 '21 23:01 jwunderl

Yeah I am not using the textdropdown field editor (seems like it needs love). The shim=TD_ID seems to be a clue as to my problem. (Will make second post about that below)

When I add it onto the playground example, notably the string does not serialize correctly.


    //% blockId=wordPicker block="$word"
    //% blockHidden=true
    //% colorSecondary="#FFFFFF"
    //% word.fieldEditor="textdropdown"
    //% word.fieldOptions.decompileLiterals=true
    //% word.fieldOptions.values='hi,hello'
    //% shim=TD_ID
    export function __wordPicker(word: string): string {
        return word;
    }

image

jfo8000 avatar Jan 26 '21 21:01 jfo8000

Ok oddly for my case this works in the playground editor but not in the actual runtime.

I just upgraded to pxt-core 6.0.1, but was reproing in 5. Richard says I should make the move to put my stuff in lib and out of sim, would that affect this?

//% blockId=colorsChooseColor block="%value"
    //% blockHidden=true
    //% shim=TD_ID colorSecondary="#FFFFFF"
    //% value.fieldEditor="colornumber" value.fieldOptions.decompileLiterals=true
    //% value.fieldOptions.colours='["#ED0A3F","#ED0A3F","#C32148","#FD0E35","#C62D42","#CC474B","#CC3336","#E12C2C","#D92121","#B94E48","#FF5349","#FE4C40","#FE6F5E","#B33B24","#CC553D","#E6735C","#FF9980","#E58E73","#FF7F49","#FF681F","#FF8833","#FFB97B","#ECB176","#E77200","#FFAE42","#F2BA49","#FBE7B2","#F2C649","#F8D568","#FCD667","#FED85D","#FBE870","#F1E788","#FFEB00","#B5B35C","#ECEBBD","#FAFA37","#FFFF99","#FFFF9F","#D9E650","#ACBF60","#AFE313","#BEE64B","#C5E17A","#5E8C31","#7BA05B","#9DE093","#63B76C","#4D8C57","#3AA655","#6CA67C","#5FA777","#93DFB8","#33CC99","#1AB385","#29AB87","#00CC99","#00755E","#8DD9CC","#01786F","#30BFBF","#00CCCC","#008080","#8FD8D8","#95E0E8","#6CDAE7","#2D383A","#76D7EA","#7ED4E6","#0095B7","#009DC4","#02A4D3","#47ABCC","#4997D0","#339ACC","#93CCEA","#2887C8","#00468C","#0066CC","#1560BD","#0066FF","#A9B2C3","#C3CDE6","#4570E6","#7A89B8","#4F69C6","#8D90A1","#8C90C8","#7070CC","#9999CC","#ACACE6","#766EC8","#6456B7","#3F26BF","#8B72BE","#652DC1","#6B3FA0","#8359A3","#8F47B3","#C9A0DC","#BF8FCC","#803790","#733380","#D6AEDD","#C154C1","#FC74FD","#732E6C","#E667CE","#E29CD2","#8E3179","#D96CBE","#EBB0D7","#C8509B","#BB3385","#D982B5","#A63A79","#A50B5E","#614051","#F653A6","#DA3287","#FF3399","#FBAED2","#FFB7D5","#FFA6C9","#F7468A","#E30B5C","#FDD7E4","#E62E6B","#DB5079","#FC80A5","#F091A9","#FF91A4","#A55353","#CA3435","#FEBAAD","#F7A38E","#E97451","#AF593E","#9E5B40","#87421F","#926F5B","#DEA681","#D27D46","#664228","#D99A6C","#EDC9AF","#FFCBA4","#805533","#FDD5B1","#EED9C4","#665233","#837050","#E6BC5C","#D9D6CF","#92926E","#E6BE8A","#C9C0BB","#DA8A67","#C88A65","#000000","#736A62","#8B8680","#C8C8CD", "#4ebed7"]'
    //% value.defl="#4ebed7"
    /**
     * choose a color from a number
     * @param value the hex number
     */
    export function choose(value: number): number {
        // For some reason I can't hide this block from writing to javascript.
        // It should have been a shadow block.
        // So I've made it part of the api.  :/
        return value;
    }


     //% blockId=color_block block="color $color=colorsChooseColor" 
    //% group="Colors"
    //% topblock=false
    //% help=colors/all
    //% handlerStatement=true
    //% weight=80
    /**
     * set the shapes inside this block to a particular color
     * @param color - color to set e.g. 0xc0c0c0
     */
    export function color(color: number) : number {

      return color

    }
Writes out as:
    colors.color(colors.choose(0xED0A3F), function () {
    shapes.cube(10, 10, 10)
})

jfo8000 avatar Jan 26 '21 21:01 jfo8000