Scratch-blocks: Blockly.JavaScript is undefined

Created on 25 Feb 2018  路  5Comments  路  Source: LLK/scratch-blocks

Expected Behavior

print JavaScript code from blocks

Actual Behavior

Uncaught TypeError: Cannot read property 'workspaceToCode' of undefined

ie. JavaScript property do not exists in Blockly instance

Steps to Reproduce

  1. git clone
  2. npm install
  3. run tests/vertical_playground.html
  4. press to JavaScript button

Operating System and Browser

Win10, Chrome 64

Most helpful comment

You need to import the JavaScript generator to have access to Blockly.Javascript

<script src="../javascript_compressed.js"></script>

This will not allow you to convert the blocks to JavaScript as generators are not being used for scratch-blocks.

All 5 comments

You need to import the JavaScript generator to have access to Blockly.Javascript

<script src="../javascript_compressed.js"></script>

This will not allow you to convert the blocks to JavaScript as generators are not being used for scratch-blocks.

Scratch-blocks has the basic generator files, which say what a function looks like, what a variable declaration looks like, etc. for the given language. But you also need a generator for each block, and those do not exist for the Scratch 3.0 blocks.

The basic generator files are included for projects that are using the scratch-blocks library without the Scratch runtime. Those projects are expected to define their own blocks and per-block generators.

Thank you for quick response.

javascript_compressed - fixes issue (it was my mistake, it is mentioned at Blocky manual)
<script src="../javascript_compressed.js"></script>

Code to define generator for block

      Blockly.JavaScript['my_control_name'] = function (block) {
           var code = "my_js_code();\n"
           return code;
      };

Blockly current version of _javascript_compressed.js_ doesn't work anymore because Blockly.Variables.allUsedVarModels is undefined in scratch-blocks but used in Google Blockly now.

Adding following code before attempting to generate Javascript code from blocks solve the issue:

if (!Blockly.Variables.allUsedVarModels) {
  Blockly.Variables.allUsedVarModels = function(a) {
    var b = a.getAllBlocks(!1);
    a = Object.create(null);
    for (var c = 0; c < b.length; c++) {
      var d = b[c].getVarModels();
      if (d)
      for (var e = 0; e < d.length; e++) {
        var f = d[e];
        f.getId() && (a[f.getId()] = f)
      }
    }
    b = [];
    for (var g in a)
    b.push(a[g]);
    return b
  }
};

Also It appears that Blockly.Variables.allUsedVariables is now deprecated in Google Blockly.

You need to import the JavaScript generator to have access to Blockly.Javascript

<script src="../javascript_compressed.js"></script>

This will not allow you to convert the blocks to JavaScript as generators are not being used for scratch-blocks.

How to get javascript_compressed.js file?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brianpoor5775 picture brianpoor5775  路  4Comments

tmickel picture tmickel  路  3Comments

gengshenghong picture gengshenghong  路  3Comments

towerofnix picture towerofnix  路  4Comments

towerofnix picture towerofnix  路  6Comments