Debian Codegraph @

blockly/demos/generator/index.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Blockly Demo: Generating JavaScript</title>
6 <script src="../../blockly_compressed.js"></script>
7 <script src="../../blocks_compressed.js"></script>
8 <script src="../../javascript_compressed.js"></script>
9 <script src="../../msg/js/en.js"></script>
10 <style>
11 body {
12 background-color: #fff;
13 font-family: sans-serif;
14 }
15 h1 {
16 font-weight: normal;
17 font-size: 140%;
18 }
19 </style>
20 </head>
21 <body>
22 <h1><a href="https://developers.google.com/blockly/">Blockly</a> &gt;
23 <a href="../index.html">Demos</a> &gt; Generating JavaScript</h1>
24
25 <p>This is a simple demo of generating code from blocks.</p>
26
27 <p>&rarr; More info on <a href="https://developers.google.com/blockly/installation/code-generators">Code Generators</a>...</p>
28
29 <p>
30 <button onclick="showCode()">Show JavaScript</button>
31 <button onclick="runCode()">Run JavaScript</button>
32 </p>
33
34 <div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
35
36 <xml id="toolbox" style="display: none">
37 <category name="Logic">
38 <block type="controls_if"></block>
39 <block type="logic_compare"></block>
40 <block type="logic_operation"></block>
41 <block type="logic_negate"></block>
42 <block type="logic_boolean"></block>
43 </category>
44 <category name="Loops">
45 <block type="controls_repeat_ext">
46 <value name="TIMES">
47 <block type="math_number">
48 <field name="NUM">10</field>
49 </block>
50 </value>
51 </block>
52 <block type="controls_whileUntil"></block>
53 </category>
54 <category name="Math">
55 <block type="math_number"></block>
56 <block type="math_arithmetic"></block>
57 <block type="math_single"></block>
58 </category>
59 <category name="Text">
60 <block type="text"></block>
61 <block type="text_length"></block>
62 <block type="text_print"></block>
63 </category>
64 </xml>
65
66 <xml id="startBlocks" style="display: none">
67 <block type="controls_if" inline="false" x="20" y="20">
68 <mutation else="1"></mutation>
69 <value name="IF0">
70 <block type="logic_compare" inline="true">
71 <field name="OP">EQ</field>
72 <value name="A">
73 <block type="math_arithmetic" inline="true">
74 <field name="OP">MULTIPLY</field>
75 <value name="A">
76 <block type="math_number">
77 <field name="NUM">6</field>
78 </block>
79 </value>
80 <value name="B">
81 <block type="math_number">
82 <field name="NUM">7</field>
83 </block>
84 </value>
85 </block>
86 </value>
87 <value name="B">
88 <block type="math_number">
89 <field name="NUM">42</field>
90 </block>
91 </value>
92 </block>
93 </value>
94 <statement name="DO0">
95 <block type="text_print" inline="false">
96 <value name="TEXT">
97 <block type="text">
98 <field name="TEXT">Don't panic</field>
99 </block>
100 </value>
101 </block>
102 </statement>
103 <statement name="ELSE">
104 <block type="text_print" inline="false">
105 <value name="TEXT">
106 <block type="text">
107 <field name="TEXT">Panic</field>
108 </block>
109 </value>
110 </block>
111 </statement>
112 </block>
113 </xml>
114
115 <script>
116 var workspace = Blockly.inject('blocklyDiv',
117 {media: '../../media/',
118 toolbox: document.getElementById('toolbox')});
119 Blockly.Xml.domToWorkspace(workspace,
120 document.getElementById('startBlocks'));
121
122 function showCode() {
123 // Generate JavaScript code and display it.
124 Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
125 var code = Blockly.JavaScript.workspaceToCode(workspace);
126 alert(code);
127 }
128
129 function runCode() {
130 // Generate JavaScript code and run it.
131 window.LoopTrap = 1000;
132 Blockly.JavaScript.INFINITE_LOOP_TRAP =
133 'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
134 var code = Blockly.JavaScript.workspaceToCode(workspace);
135 Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
136 try {
137 eval(code);
138 } catch (e) {
139 alert(e);
140 }
141 }
142 </script>
143
144 </body>
145 </html>