index.html 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Blockly Demo: Mirrored Blockly</title>
  6. <script src="../../blockly_compressed.js"></script>
  7. <script src="../../blocks_compressed.js"></script>
  8. <script src="../../msg/js/en.js"></script>
  9. <style>
  10. body {
  11. background-color: #fff;
  12. font-family: sans-serif;
  13. }
  14. h1 {
  15. font-weight: normal;
  16. font-size: 140%;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <h1><a href="https://developers.google.com/blockly/">Blockly</a> &gt;
  22. <a href="../index.html">Demos</a> &gt; Mirrored Blockly</h1>
  23. <p>This is a simple demo of a master Blockly that controls a slave Blockly.
  24. Open the JavaScript console to see the event passing.</p>
  25. <p>&rarr; More info on <a href="https://developers.google.com/blockly/guides/configure/web/events">events</a>&hellip;</p>
  26. <table width="100%">
  27. <tr>
  28. <td>
  29. <div id="masterDiv" style="height: 480px; width: 600px;"></div>
  30. </td>
  31. <td>
  32. <div id="slaveDiv" style="height: 480px; width: 430px;"></div>
  33. </td>
  34. </tr>
  35. </table>
  36. <xml id="toolbox" style="display: none">
  37. <block type="controls_if"></block>
  38. <block type="logic_compare"></block>
  39. <block type="controls_repeat_ext"></block>
  40. <block type="math_number"></block>
  41. <block type="math_arithmetic"></block>
  42. <block type="text"></block>
  43. <block type="text_print"></block>
  44. <block type="variables_get"><field name="VAR">i</field></block>
  45. <block type="variables_get"><field name="VAR">j</field></block>
  46. <block type="variables_get"><field name="VAR">k</field></block>
  47. </xml>
  48. <script>
  49. // Inject master workspace.
  50. var masterWorkspace = Blockly.inject('masterDiv',
  51. {media: '../../media/',
  52. toolbox: document.getElementById('toolbox')});
  53. // Inject slave workspace.
  54. var slaveWorkspace = Blockly.inject('slaveDiv',
  55. {media: '../../media/',
  56. readOnly: true});
  57. // Listen to events on master workspace.
  58. masterWorkspace.addChangeListener(mirrorEvent);
  59. function mirrorEvent(masterEvent) {
  60. if (masterEvent.type == Blockly.Events.UI) {
  61. return; // Don't mirror UI events.
  62. }
  63. // Convert event to JSON. This could then be transmitted across the net.
  64. var json = masterEvent.toJson();
  65. console.log(json);
  66. // Convert JSON back into an event, then execute it.
  67. var slaveEvent = Blockly.Events.fromJson(json, slaveWorkspace);
  68. slaveEvent.run(true);
  69. }
  70. </script>
  71. </body>
  72. </html>