简体   繁体   中英

blockly : code variables

I've just discovered "blockly" and it is exacly what i was looking for to take my webApp to the next level. The problem i have is that i don't realy understand how i can initiate python or js code variables.

Here is my block :

Blockly.Language.myapp_ifdo = {
  helpUrl: '',
  init: function() {
    this.setColour(210);
    this.appendDummyInput()
        .appendTitle("if")
        .appendTitle(new Blockly.FieldDropdown([["Temperature", "Temperature"], ["Humidity", "Humidity"]]), "SENSOR")
        .appendTitle(" ")
        .appendTitle(new Blockly.FieldDropdown([["=", "="], ["≠", "≠"], ["<", "<"], ["≤", "≤"], [">", ">"], ["≥", "≥"]]), "OPERATOR")
        .appendTitle(" ")
        .appendTitle(new Blockly.FieldTextInput("0"), "SENSORVALUE");
    this.appendStatementInput("DO")
        .appendTitle("do");
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setTooltip('');
  }
};

The rendering is :

在此处输入图片说明

Dropdown list content :

在此处输入图片说明

What i'm trying to do :

If "temperature" is selected then i want to initialize variable at the beginning of the generated code :

temperature = None
if temperature <= '30':
  pass

Just the same if "humidity" is selected :

humidity = None
if humidity >= '60':
  pass

In my "template.soy" file i have this :

<block type="myapp_ifdo"></block>

Hope i am clear enough... Thanks for your help !

Regards,

The built-in python generator will take care of this for you, if you use built-in Blockly variables and other constructs.

The code demo uses the function Blockly.Generator.workspaceToCode to generate code from the blocks. Once all the blocks have been processed, it calls into the generator's finish function to prepend variable declarations.

You can see the finish for yourself in python.js :

/**
 * Prepend the generated code with the variable definitions.
 * @param {string} code Generated code.
 * @return {string} Completed code.
 */
Blockly.Python.finish = function(code) { 
...

You will need to roll your own generator code if you can't use the built-in constructs from Blockly. You may be able to use this code from Blockly as a starting point, although it will be tricky because you will need to maintain your own list of variable declarations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM