forked from google/blockly-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
167 lines (149 loc) · 4.97 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blockly Demo: Custom Turtle Field</title>
<script src="./node_modules/blockly/blockly_compressed.js"></script>
<script src="blocks.js"></script>
<script src="field_turtle.js"></script>
<script src="https://unpkg.com/blockly/msg/en.js"></script>
<style>
body {
margin: 0 10%;
background-color: #fff;
font-family: sans-serif;
}
h1 {
font-weight: normal;
font-size: 140%;
}
td {
padding: 1ex;
}
img {
border: none;
}
</style>
<link rel="stylesheet" type="text/css" href="turtle.css">
</head>
<body onload="start()">
<p>This is a demo of creating custom block fields. In this case the field
is used to define a turtle.
</p>
<p>→ More info on <a href="https://developers.google.com/blockly/guides/create-custom-blocks/fields/customizing-fields/creating">creating a custom field</a>…</p>
<p>Click on the blocks' comment icons to learn what they are demonstrating.
Use the buttons below to see how the fields react to changes.
</p>
<p>
<input type="button" value="set random style" onclick="setRandomStyle()">
<input type="button" value="toggle shadow" onclick="toggleShadow()">
<input type="button" value="toggle enabled" onclick="toggleEnabled()">
<input type="button" value="toggle editable" onclick="toggleEditable()">
<input type="button" value="toggle collapsed" onclick="toggleCollapsed()">
</p>
<p>
<input type="button" value="Export to XML" onclick="toXml()">
<input type="button" value="Import from XML" onclick="fromXml()">
</p>
<table>
<tr>
<td>
<textarea id="importExport"
style="width: 200px; height: 480px;"
onchange="textAreaChange();"
onkeyup="textAreaChange()"></textarea>
</td>
<td>
<div id="blocklyDiv" style="width: 600px; height: 480px;"></div>
</td>
</tr>
</table>
<script>
function toXml() {
var output = document.getElementById('importExport');
var xml = Blockly.Xml.workspaceToDom(workspace);
output.value = Blockly.Xml.domToPrettyText(xml);
output.focus();
output.select();
}
function fromXml() {
var input = document.getElementById('importExport');
var xml = Blockly.utils.xml.textToDom(input.value);
Blockly.Xml.domToWorkspace(xml, workspace);
}
function setRandomStyle() {
var blocks = workspace.getAllBlocks(false);
var styles =
Object.keys(workspace.getRenderer().getConstants().blockStyles);
styles.splice(styles.indexOf(blocks[0].getStyleName()), 1);
var style = styles[Math.floor(Math.random() * styles.length)];
for(var i = 0, block; block = blocks[i]; i++) {
block.setStyle(style);
}
}
function toggleShadow() {
var blocks = workspace.getAllBlocks(false);
for(var i = 0, block; block = blocks[i]; i++) {
block.setShadow(!block.isShadow());
}
}
function toggleEnabled() {
var blocks = workspace.getAllBlocks(false);
for(var i = 0, block; block = blocks[i]; i++) {
block.setEnabled(!block.isEnabled());
}
}
function toggleEditable() {
Blockly.hideChaff();
var blocks = workspace.getAllBlocks(false);
for(var i = 0, block; block = blocks[i]; i++) {
block.setEditable(!block.isEditable());
}
}
function toggleCollapsed() {
Blockly.hideChaff();
var blocks = workspace.getAllBlocks(false);
for(var i = 0, block; block = blocks[i]; i++) {
block.setCollapsed(!block.isCollapsed());
}
}
function appendDom() {
var blocks = document.getElementById('workspace-blocks');
if (blocks.firstElementChild) {
Blockly.Xml.appendDomToWorkspace(blocks, workspace);
}
}
function start() {
workspace = Blockly.inject('blocklyDiv', options);
appendDom();
workspace.scrollCenter();
}
var options = {
media: './node_modules/blockly/media/',
grid: {
spacing: 25,
length: 3,
colour: '#ccc'
},
move: {
scrollbars: true,
drag: true,
wheel: true,
},
zoom: {
controls: true,
startScale: 1.0,
maxScale: 4,
minScale: 0.25,
scaleSpeed: 1.1
}
/*toolbox: document.getElementById('toolbox')*/
}
</script>
<xml xmlns="https://developers.google.com/blockly/xml" id="workspace-blocks" style="display: none">
<block type="turtle_basic"></block>
<block type="turtle_nullifier" y="120"></block>
<block type="turtle_changer" y="230"></block>
</xml>
</body>
</html>