-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
191 lines (174 loc) · 5.15 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/spritejs@2/dist/spritejs.min.js"></script>
<script src="/blockly/blockly_node_javascript_cn.js"></script>
<script src="/dist/spritly.js"></script>
<style>
html, body {
width: 100%;
min-width: 600px;
height: 100%;
padding: 0;
margin: 0;
}
#main {
width: 100%;
height: 100%;
display: flex;
justify-content: space-between;
}
#controls {
position: absolute;
right: 10px;
top: 10px;
z-index: 9999;
}
#controls button {
cursor: pointer;
}
#blocklyDiv {
height: 100%;
width: 62%;
border: none;
}
#displayDiv {
width: 38%;
margin-left: 20px;
}
#stage {
height: 70%;
background: url(res/img/grid.jpg) repeat;
}
#stage iframe {
width: 100%;
height: 100%;
}
#links {
padding: 30px;
display: flex;
}
#links a {
margin-right: 20px;
}
</style>
</head>
<body>
<div id="controls">
<button onclick="showXml()">Blocks</button>
<button onclick="showCode()">Code</button>
<button onclick="window.frames[0].location.href = `sandbox.html?t=${Date.now()}`">Run</button>
</div>
<div id="main">
<div id="blocklyDiv"></div>
<div id="displayDiv">
<div id="stage">
<iframe frameborder="no"></iframe>
</div>
<div id="links">
<a href="#index">index</a>
<a href="#bird_fly0">bird_fly demo 0</a>
</div>
</div>
</div>
<script>
/* global spritly */
(async function () {
const {Blockly, Application} = spritly;
function debounce(action, delay = 300) {
let timer = null;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
action.apply(this, args);
}, delay);
};
}
const update = debounce(() => {
window.frames[0].location.href = `sandbox.html?t=${Date.now()}`;
});
const app = new Application('blocklyDiv');
async function loadStartBlocks() {
const hash = window.location.hash || '#';
const blockFile = hash.slice(1) || 'index';
const workspace = await app.initWorkspace({
media: 'res/media/',
config: blockFile,
grid: {spacing: 25,
length: 3,
colour: '#ccc',
snap: true,
},
zoom: {
controls: true,
wheel: true,
},
});
if(workspace.getAllBlocks().length <= 0) {
update();
}
workspace.addChangeListener((event) => {
if(event.type === Blockly.Events.CHANGE && event.element !== 'collapsed'
|| event.type === Blockly.Events.CREATE
|| event.type === Blockly.Events.DELETE
|| event.type === Blockly.Events.MOVE && event.oldParentId !== event.newParentId) {
update();
}
});
window.workspace = workspace;
}
function showXml() {
const wnd = window.open('', '__blank', '');
wnd.document.write(`<pre class="prettyprint" lang-xml>${app.xml.replace(/</g, '<').replace(/>/g, '>')}</pre>`);
wnd.document.write('<script>var sel = document.createElement("script"); sel.src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"; window.onload = function(){document.body.appendChild(sel)};<\/script>'); // eslint-disable-line
wnd.document.close();
}
function showCode() {
// Generate JavaScript code and display it.
const wnd = window.open('', '__blank', '');
wnd.document.write(`<pre class="prettyprint" lang-js>${app.code.replace(/</g, '<').replace(/>/g, '>')}</pre>`);
wnd.document.write('<script>var sel = document.createElement("script"); sel.src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"; window.onload = function(){document.body.appendChild(sel)};<\/script>'); // eslint-disable-line
wnd.document.close();
}
function runCode() {
try {
window.frames[0].exec(app.code);
} catch (e) {
console.error(e);
// alert(e);
}
}
window.runCode = runCode;
window.showCode = showCode;
window.showXml = showXml;
await loadStartBlocks();
update();
const links = document.getElementById('links');
links.addEventListener('click', async (evt) => {
evt.preventDefault();
if(evt.target.tagName === 'A') {
const link = evt.target;
window.location.href = link.href;
await loadStartBlocks();
}
});
document.addEventListener('keydown', (evt) => {
const sendbox = window.frames[0];
if(sendbox.spritly) {
sendbox.spritly.runtime.dispatchEvent('KEYDOWN', document, evt);
}
});
document.addEventListener('keyup', (evt) => {
const sendbox = window.frames[0];
if(sendbox.spritly) {
sendbox.spritly.runtime.dispatchEvent('KEYUP', document, evt);
}
});
}());
</script>
</body>
</html>