index.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. <p>This is a simple demo of generating code from blocks.</p>
  25. <p>&rarr; More info on <a href="https://developers.google.com/blockly/guides/configure/web/code-generators">Code Generators</a>&hellip;</p>
  26. <p>
  27. <button onclick="showCode()">Show JavaScript</button>
  28. <button onclick="runCode()">Run JavaScript</button>
  29. </p>
  30. <div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
  31. <div id="blocklyDiv2" style="height: 480px; width: 600px;"></div>
  32. <xml id="toolbox" style="display: none">
  33. <category name="Logic">
  34. <block type="controls_if"></block>
  35. <block type="logic_compare"></block>
  36. <block type="logic_operation"></block>
  37. <block type="logic_negate"></block>
  38. <block type="logic_boolean"></block>
  39. </category>
  40. <category name="Loops">
  41. <block type="controls_repeat_ext">
  42. <value name="TIMES">
  43. <block type="math_number">
  44. <field name="NUM">10</field>
  45. </block>
  46. </value>
  47. </block>
  48. <block type="controls_whileUntil"></block>
  49. </category>
  50. <category name="Math">
  51. <block type="math_number"></block>
  52. <block type="math_arithmetic"></block>
  53. <block type="math_single"></block>
  54. </category>
  55. <category name="Text">
  56. <block type="text"></block>
  57. <block type="text_length"></block>
  58. <block type="text_print"></block>
  59. </category>
  60. </xml>
  61. <xml id="startBlocks" style="display: none">
  62. <block type="controls_if" inline="false" x="20" y="20">
  63. <mutation else="1"></mutation>
  64. <value name="IF0">
  65. <block type="logic_compare" inline="true">
  66. <field name="OP">EQ</field>
  67. <value name="A">
  68. <block type="math_arithmetic" inline="true">
  69. <field name="OP">MULTIPLY</field>
  70. <value name="A">
  71. <block type="math_number">
  72. <field name="NUM">6</field>
  73. </block>
  74. </value>
  75. <value name="B">
  76. <block type="math_number">
  77. <field name="NUM">7</field>
  78. </block>
  79. </value>
  80. </block>
  81. </value>
  82. <value name="B">
  83. <block type="math_number">
  84. <field name="NUM">42</field>
  85. </block>
  86. </value>
  87. </block>
  88. </value>
  89. <statement name="DO0">
  90. <block type="text_print" inline="false">
  91. <value name="TEXT">
  92. <block type="text">
  93. <field name="TEXT">Don't panic</field>
  94. </block>
  95. </value>
  96. </block>
  97. </statement>
  98. <statement name="ELSE">
  99. <block type="text_print" inline="false">
  100. <value name="TEXT">
  101. <block type="text">
  102. <field name="TEXT">Panic</field>
  103. </block>
  104. </value>
  105. </block>
  106. </statement>
  107. </block>
  108. </xml>
  109. <script>
  110. var workspace = Blockly.inject('blocklyDiv',
  111. {media: '../../media/',
  112. toolbox: document.getElementById('toolbox')});
  113. var workspace2 = Blockly.inject('blocklyDiv2',
  114. {media: '../../media/',
  115. toolbox: document.getElementById('toolbox')});
  116. Blockly.Xml.domToWorkspace(workspace,
  117. document.getElementById('startBlocks'));
  118. function showCode() {
  119. // Generate JavaScript code and display it.
  120. Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
  121. var code = Blockly.JavaScript.workspaceToCode(workspace);
  122. alert(code);
  123. }
  124. function runCode() {
  125. // Generate JavaScript code and run it.
  126. window.LoopTrap = 1000;
  127. Blockly.JavaScript.INFINITE_LOOP_TRAP =
  128. 'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
  129. var code = Blockly.JavaScript.workspaceToCode(workspace);
  130. Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
  131. try {
  132. eval(code);
  133. } catch (e) {
  134. alert(e);
  135. }
  136. }
  137. </script>
  138. </body>
  139. </html>