How to add back the right click "New variable..."

294 views
Skip to first unread message

Anson Au

unread,
Jun 8, 2022, 12:15:05 PM6/8/22
to Blockly
I have a project that was created with Blockly 2017, when I migration the project with blocky 2022, I found that when I right clicked on the block, the "New variable..." was gone. I searched the whole document and couldn't find how to add it.

blockly 2017 vs blockly 2022.jpg

Beka Westberg

unread,
Jun 13, 2022, 1:56:23 PM6/13/22
to Blockly
Hello!

So this was originally removed when Blockly made it so that you would create variables via a button in the flyout. But I think it should be fine to add it back as long as you're not using typed variables. If you are using typed variables, you'll have to figure out a way to know what type to create when the user clicks on the "New variable" option.

Adding the option back in will be tricky, but should be possible.

To add it back you will want to create a custom field that is a subclass of the variable field. You will want to pass `Field.SKIP_SETUP` to the superclass, and then assign your own menu generator to the `menuGenerator_` property. The built-in menu generator is called `dropdownCreate`. You will probably want yours to look similar, but to include a "New variable..." option. You can then override the `onItemSelected_` method to handle the behavior of your new option.

I hope that helps! If you have any further questions please reply!
--Beka

fu6...@gmail.com

unread,
Jun 15, 2022, 2:34:23 AM6/15/22
to Blockly
Hello, I had added it in my project successfully. You can refer to my code.

https://github.com/fustyles/webduino/tree/master/SPduino_tool

ash...@gmail.com 在 2022年6月9日 星期四凌晨12:15:05 [UTC+8] 的信中寫道:
addVariable.mp4

fu6...@gmail.com

unread,
Aug 12, 2022, 11:24:22 PM8/12/22
to Blockly
Hi, hope this helps. 
https://github.com/fustyles/webduino/blob/master/SpBlocklyJS/js/blockly_compressed.js


Blockly.Msg["NEW_VARIABLE"] = "Create variable...";
Blockly.Msg["NEW_VARIABLE_TITLE"] = "New variable name:";


module$exports$Blockly$internalConstants.ADD_VARIABLE_ID="ADD_VARIABLE_ID";



module$exports$Blockly$FieldVariable.FieldVariable.prototype.onItemSelected_=function(a,b){
    a=b.getValue();
    if(this.sourceBlock_&&this.sourceBlock_.workspace){
        if(a===module$exports$Blockly$internalConstants.RENAME_VARIABLE_ID){
            (0,$.module$exports$Blockly$Variables.renameVariable)(this.sourceBlock_.workspace,this.variable_);
            return
        }
        if(a===module$exports$Blockly$internalConstants.DELETE_VARIABLE_ID){
            this.sourceBlock_.workspace.deleteVariableById(this.variable_.getId());
            return
        }

        if(a===module$exports$Blockly$internalConstants.ADD_VARIABLE_ID){
            (0,$.module$exports$Blockly$Variables.createVariableButtonHandler)(this.sourceBlock_.workspace);
            var v=this.sourceBlock_.workspace.getVariablesOfType("");
            v.sort(Blockly.VariableModel.compareByName);
            if(v.length>0)
                this.setValue(v[v.length-1].id_);
            return
        }

    }
    this.setValue(a)
};



module$exports$Blockly$FieldVariable.FieldVariable.dropdownCreate=function(){
    if(!this.variable_)throw Error("Tried to call dropdownCreate on a variable field with no variable selected.");
    var a=this.getText(),b=[];
    if(this.sourceBlock_&&this.sourceBlock_.workspace)
        for(var c=this.getVariableTypes_(),d=0;d<c.length;d++){
            var e=this.sourceBlock_.workspace.getVariablesOfType(c[d]);
            b=b.concat(e)
        }
    b.sort(module$exports$Blockly$VariableModel.VariableModel.compareByName);
    c=[];
    for(d=0;d<b.length;d++)
        c[d]=[b[d].name,b[d].getId()];
    c.push([$.module$exports$Blockly$Msg.Msg.RENAME_VARIABLE,module$exports$Blockly$internalConstants.RENAME_VARIABLE_ID]);
    $.module$exports$Blockly$Msg.Msg.DELETE_VARIABLE&&c.push([$.module$exports$Blockly$Msg.Msg.DELETE_VARIABLE.replace("%1",a),
    module$exports$Blockly$internalConstants.DELETE_VARIABLE_ID]);

   
    c.push([$.module$exports$Blockly$Msg.Msg.NEW_VARIABLE,module$exports$Blockly$internalConstants.ADD_VARIABLE_ID]);
    return c
};



$.Blockly.ADD_VARIABLE_ID=module$exports$Blockly$internalConstants.ADD_VARIABLE_ID;


Best wishes

fu6...
Reply all
Reply to author
Forward
0 new messages