Read user entered input in blockly block

2k Views Asked by At

I have the below code in my blockly.js file

Blockly.Blocks['account_number'] = {
  // Other type.
  init: function() {
    this.jsonInit({
      "message0": "account_number %1",
      "args0": [{"type": "field_input", "name": "TYPE", "text": ""}],
      "output": "Type",
      "colour": 320,
      "tooltip": "Custom type to allow.",
      "helpUrl": "https://www.youtube.com/watch?v=s2_xaEvcVI0#t=702"
    });
  }
};

I am using this in my index.html as follows

<category name="sender" colour="%{BKY_MATH_HUE}">
    <block type="account_number" name="accnum"></block>
</category>

How can I get the user entered number in the account number block in this js file?

1

There are 1 best solutions below

1
On

The short answer:

Blockly.JavaScript['account_number'] = function(block) {
    var code = block.getFieldValue('TYPE');
    return code;
}

(You appear to have named your field "TYPE", by the way - are you entirely sure that's what you want to name it?)

The medium answer: include the above, and generate the JavaScript for your workspace by getting your workspace and using:

var generatedCode = Blockly.JavaScript.workspaceToCode(workspace);

in wherever you are trying to generate your JavaScript.

The long answer is that if that isn't enough to get you started, I'll need to see a bit more about how you're trying to generate your code and what you're going to do with it.

These links may also be helpful, if you haven't checked them out already: https://developers.google.com/blockly/guides/configure/web/custom-blocks#add_generator_function https://developers.google.com/blockly/guides/create-custom-blocks/generating-code