summaryrefslogtreecommitdiff
path: root/blockly/demos/mirror/index.html
blob: 0afd27618d070b5626c04e4c5f894cca098c2226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Blockly Demo: Mirrored Blockly</title>
  <script src="../../blockly_compressed.js"></script>
  <script src="../../blocks_compressed.js"></script>
  <script src="../../msg/js/en.js"></script>
  <style>
    body {
      background-color: #fff;
      font-family: sans-serif;
    }
    h1 {
      font-weight: normal;
      font-size: 140%;
    }
  </style>
</head>
<body>
  <h1><a href="https://developers.google.com/blockly/">Blockly</a> &gt;
    <a href="../index.html">Demos</a> &gt; Mirrored Blockly</h1>

  <p>This is a simple demo of a master Blockly that controls a slave Blockly.
  Open the JavaScript console to see the event passing.</p>

  <p>&rarr; More info on <a href="https://developers.google.com/blockly/guides/configure/web/events">events</a>&hellip;</p>

  <table width="100%">
    <tr>
      <td>
        <div id="masterDiv" style="height: 480px; width: 600px;"></div>
      </td>
      <td>
        <div id="slaveDiv" style="height: 480px; width: 430px;"></div>
      </td>
    </tr>
  </table>

  <xml id="toolbox" style="display: none">
    <block type="controls_if"></block>
    <block type="logic_compare"></block>
    <block type="controls_repeat_ext"></block>
    <block type="math_number"></block>
    <block type="math_arithmetic"></block>
    <block type="text"></block>
    <block type="text_print"></block>
  </xml>

  <script>
    // Inject master workspace.
    var masterWorkspace = Blockly.inject('masterDiv',
        {media: '../../media/',
         toolbox: document.getElementById('toolbox')});
    // Inject slave workspace.
    var slaveWorkspace = Blockly.inject('slaveDiv',
        {media: '../../media/',
         readOnly: true});
    // Listen to events on master workspace.
    masterWorkspace.addChangeListener(mirrorEvent);

    function mirrorEvent(masterEvent) {
      if (masterEvent.type == Blockly.Events.UI) {
        return;  // Don't mirror UI events.
      }
      // Convert event to JSON.  This could then be transmitted across the net.
      var json = masterEvent.toJson();
      console.log(json);
      // Convert JSON back into an event, then execute it.
      var slaveEvent = Blockly.Events.fromJson(json, slaveWorkspace);
      slaveEvent.run(true);
    }
  </script>

</body>
</html>