forked from google/blockly-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
127 lines (121 loc) · 3.12 KB
/
index.html
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Blockly Demo: Mirrored Blockly</title>
<script src="./node_modules/blockly/blockly_compressed.js"></script>
<script src="./node_modules/blockly/blocks_compressed.js"></script>
<script src="./node_modules/blockly/msg/en.js"></script>
<style>
body {
background-color: #fff;
font-family: sans-serif;
}
h1 {
font-weight: normal;
font-size: 140%;
}
</style>
</head>
<body>
<p>This is a simple demo of a primary Blockly instance that controls a secondary Blockly instance with events.
Open the JavaScript console to see the event passing.</p>
<p>→ More info on <a href="https://developers.google.com/blockly/guides/configure/web/events">events</a>…</p>
<table width="100%">
<tr>
<td>
<div id="primaryDiv" style="height: 480px; width: 600px;"></div>
</td>
<td>
<div id="secondaryDiv" style="height: 480px; width: 430px;"></div>
</td>
</tr>
</table>
<script>
var toolbox = {
"kind": "flyoutToolbox",
"contents": [
{
"kind": "block",
"type": "controls_if"
},
{
"kind": "block",
"type": "logic_compare"
},
{
"kind": "block",
"type": "controls_repeat_ext"
},
{
"kind": "block",
"type": "math_number",
"fields": {
NUM: 123
}
},
{
"kind": "block",
"type": "math_arithmetic"
},
{
"kind": "block",
"type": "text"
},
{
"kind": "block",
"type": "text_print"
},
{
"kind": "block",
"type": "variables_get",
"fields": {
"VAR": {
"name": "i"
}
}
},
{
"kind": "block",
"type": "variables_get",
"fields": {
"VAR": {
"name": "j"
}
}
},
{
"kind": "block",
"type": "variables_get",
"fields": {
"VAR": {
"name": "k"
}
}
},
]
};
// Inject primary workspace.
var primaryWorkspace = Blockly.inject('primaryDiv',
{media: './node_modules/blockly/media/',
toolbox: toolbox});
// Inject secondary workspace.
var secondaryWorkspace = Blockly.inject('secondaryDiv',
{media: './node_modules/blockly/media/',
readOnly: true});
// Listen to events on primary workspace.
primaryWorkspace.addChangeListener(mirrorEvent);
function mirrorEvent(primaryEvent) {
if (primaryEvent.isUiEvent) {
return; // Don't mirror UI events.
}
// Convert event to JSON. This could then be transmitted across the net.
var json = primaryEvent.toJson();
console.log(json);
// Convert JSON back into an event, then execute it.
var secondaryEvent = Blockly.Events.fromJson(json, secondaryWorkspace);
secondaryEvent.run(true);
}
</script>
</body>
</html>