index.html 4.3 KB

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