Hi
I am trying to follow the codelab 'Build a custom generator' but instead of using the advanced playground, I would like to build a simple version without the advanced playground.
Currently, I am having trouble with the line:
playground.addGenerator('Codelab', codelabGenerator)
as I do not have the playground object, and I do not understand where it is coming from when I look at the advanced_playground.html example.
I have followed the rest of the codelab, however, it doesn't work because the blocks are not connected to the generator code.
I also do not understand where I can find documentation around the "addGenerator" function.
I have only two files. The first one is detail.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Blockly Demo: Generating JSON</title>
<script src="C:\Users\IkumWindows\Documents\Uni\QLDiss\DjangoProject\mysite\polls\static\custom_generator.js"></script>
<!-- Include the Blockly blocks and generators -->
<style>
body {
background-color: #fff;
font-family: sans-serif;
}
h1 {
font-weight: normal;
font-size: 140%;
}
</style>
</head>
<body>
<p>This is a simple demo of generating code from blocks and running
the code using eval.</p>
<p>
<button onclick="showCode()">Show JavaScript</button>
</p>
<div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
<script>
var toolbox = codelabToolbox;
var demoWorkspace = Blockly.inject('blocklyDiv',
toolbox: toolbox});
function showCode() {
// Generate JavaScript code and display it.
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
var code = Blockly.JavaScript.workspaceToCode(demoWorkspace);
alert(code);
}
</script>
</body>
</html>
second file - custom_generator.js:
const codelabGenerator = new Blockly.Generator('JSON');
Blockly.common.defineBlocksWithJsonArray([{
"type": "object",
"message0": "{ %1 %2 }",
"args0": [
{
"type": "input_dummy"
},
{
"type": "input_statement",
"name": "MEMBERS"
}
],
"output": null,
"colour": 230,
},
{
"type": "member",
"message0": "%1 %2 %3",
"args0": [
{
"type": "field_input",
"name": "MEMBER_NAME",
"text": ""
},
{
"type": "field_label",
"name": "COLON",
"text": ":"
},
{
"type": "input_value",
"name": "MEMBER_VALUE"
}
],
"previousStatement": null,
"nextStatement": null,
"colour": 230,
}]);
var codelabToolbox = {
'kind': 'flyoutToolbox',
'contents': [
{
'kind': 'block',
'type': 'object'
},
{
'kind': 'block',
'type': 'member'
},
{
'kind': 'block',
'type': 'math_number'
},
{
'kind': 'block',
'type': 'text'
},
{
'kind': 'block',
'type': 'logic_boolean'
},
{
'kind': 'block',
'type': 'logic_null'
},
{
'kind': 'block',
'type': 'lists_create_with'
},
]
}
codelabGenerator['object'] = function(block) {
const statement_members = codelabGenerator.statementToCode(block, 'MEMBERS');
const code = '{\n' + statement_members + '\n}';
return [code, codelabGenerator.PRECEDENCE];
};
codelabGenerator['member'] = function(block) {
const name = block.getFieldValue('MEMBER_NAME');
const value = codelabGenerator.valueToCode(
block, 'MEMBER_VALUE', codelabGenerator.PRECEDENCE);
const code = '"' + name + '": ' + value;
return code;
};
Please could someone tell me how to connect my generator to my blocks and also where I can find more information about the addGenerator function and creating a custom generator.