print JavaScript code from blocks
Uncaught TypeError: Cannot read property 'workspaceToCode' of undefined
ie. JavaScript property do not exists in Blockly instance
Win10, Chrome 64
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?
Most helpful comment
You need to import the JavaScript generator to have access to
Blockly.JavascriptThis will not allow you to convert the blocks to JavaScript as generators are not being used for scratch-blocks.