forked from scratchblocks/scratchblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
281 lines (238 loc) · 6.98 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!DOCTYPE html>
<meta charset="utf8">
<meta name=viewport content="width=device-width, initial-scale=1">
<title>scratchblocks dev</title>
<meta name=description content="Use scratchblocks to write pictures of Scratch scripts in forum posts.">
<!--
<link rel=stylesheet href="https://fonts.googleapis.com/css?family=Noto+Sans:400,700">
-->
<link rel=stylesheet href="index.css">
<style>
#preview > svg {
display: block;
}
#preview > .scratch3 {
transform: scale(0.675);
transform-origin: 0 0;
}
#preview:before {
position: relative;
left: 1px;
min-width: 695px;
min-height: 842px;
z-index: 1;
transform: scale(0.66666);
transform-origin: 0 0;
}
.cmp #preview:before,
.diff #preview:before {
position: absolute;
display: block;
content: "";
background: url(screenshot.png) no-repeat -2px -3px;
}
.cmp.diff #preview:before {
-webkit-filter: invert(100%) opacity(50%);
}
.diff #preview:before {
-webkit-filter: opacity(50%);
}
.cmp img {
display: none;
}
body {
background: #dddede;
}
</style>
<!---------------------------------------------------------------------------->
<h1>
<a id=title href="/">scratchblocks dev</a>
<span>
<a target="_blank" href="http://wiki.scratch.mit.edu/wiki/Block_Plugin/Syntax">help</a>
<a href="http://github.com/blob8108/scratchblocks2">github</a>
<a href="generator/"><b>generate</b></a>
</span>
</h1>
<div id="side">
<div>
<select id="choose-lang">
<option value="">Select language…
</select>
<small id=lang-status></small>
</div>
<textarea id="editor"></textarea>
</div>
<pre id="preview" class="blocks"></pre>
<!---------------------------------------------------------------------------->
<script src="build/scratchblocks.min.js"></script>
<script src="build/translations-all.js"></script>
<script>
var editor = document.getElementById('editor');
var exportSVGLink;
var exportPNGLink;
var obj = {};
extractHash();
editor.addEventListener('input', function(e) {
obj.script = editor.value;
objUpdated();
});
var chooseLang = document.getElementById('choose-lang');
var languageCodes = Object.keys(scratchblocks.allLanguages)
languageCodes.sort()
languageCodes.forEach(function(code) {
if (code === "en") return
var option = document.createElement("option")
option.value = code
var language = scratchblocks.allLanguages[code]
option.textContent = language.name && language.altName ? language.name + " — " + language.altName : language.name || language.altName || code
chooseLang.appendChild(option)
})
var incompleteLangs = [];
var newOnlyLangs = [];
[].slice.apply(chooseLang.options).forEach(function(el) {
var text = el.textContent;
if (/\*/.test(text)) incompleteLangs.push(el.value);
if (/\†/.test(text)) newOnlyLangs.push(el.value);
});
var langStatus = document.createTextNode("");
document.getElementById('lang-status').appendChild(langStatus);
chooseLang.addEventListener('change', function(e) {
obj.lang = chooseLang.value;
console.log(obj);
objUpdated();
});
/* Extract hash from location. Returns true if changed */
function extractHash() {
var newObj = decodeHash();
if (!newObj || !newObj.script) {
newObj = {
script: "",
lang: obj.lang,
};
}
if (newObj.lang !== obj.lang || newObj.script !== obj.script) {
obj = newObj;
return true;
}
return false;
}
function decodeHash() {
var hash = location.href.split('#')[1];
if (!hash) return;
if (!/^\?/.test(hash)) {
return {
script: decodeURIComponent(hash),
};
} else {
var newObj = {};
parts = hash.slice(1).split('&');
parts.forEach(function(part) {
var match = /^(.*)=(.*)$/.exec(part);
if (!match) return;
var key = decodeURIComponent(match[1]);
var value = decodeURIComponent(match[2]);
if (key === "lang" || key === "script") {
newObj[key] = value;
}
});
return newObj;
}
}
function objUpdated() {
// set hash
if (obj.lang) {
location.hash = ('#?lang=' + encodeURIComponent(obj.lang) +
'&script='+encodeURIComponent(obj.script));
} else if (obj.script) {
location.hash = '#'+encodeURIComponent(obj.script);
} else {
if (!(location.hash == '' || location.hash == '#')) {
location.hash = '#';
}
}
// TODO: don't spam history on every keypress!
/*
// renderMatching API example
preview.innerHTML = obj.script;
scratchblocks.renderMatching('pre.blocks', {
languages: ['en', obj.lang],
render: function(doc, cb) {
doc.render(function(svg) {
var el = document.createElement('div');
el.appendChild(svg);
var link = exportSVGLink = document.createElement('a');
exportLink.href = scratchblocks.exportSVG(svg);
exportLink.setAttribute('download', 'scratchblocks.svg');
exportLink.textContent = "Export SVG";
exportLink.className = 'export-link';
var p = document.createElement('p');
p.appendChild(exportLink);
el.appendChild(p);
cb(el);
});
},
});
*/
var doc = window.doc = scratchblocks.parse(obj.script, {
languages: obj.lang ? ['en', obj.lang] : ['en'],
});
// doc.translate(scratchblocks.allLanguages['de']);
// var doc = window.doc = scratchblocks.fromJSON({
// scripts: eval(obj.script),
// });
console.log(scratchblocks.stringify(doc));
var docView3 = scratchblocks.newView(doc, {
style: "scratch3",
});
var docView2 = scratchblocks.newView(doc, {
style: "scratch2",
});
var svg3 = docView3.render();
var svg2 = docView2.render();
preview.innerHTML = "";
preview.appendChild(svg3);
preview.appendChild(svg2);
svg3.classList.add("scratch3")
// add export link
setTimeout(function() {
if (exportSVGLink) document.body.removeChild(exportSVGLink);
var link = exportSVGLink = document.createElement('a');
link.href = docView3.exportSVG();
link.setAttribute('download', 'scratchblocks.svg');
link.textContent = "Export SVG";
link.className = 'export-link';
document.body.appendChild(link);
if (exportPNGLink) document.body.removeChild(exportPNGLink);
docView3.exportPNG(function(pngDataURL) {
var link = exportPNGLink = document.createElement('a');
link.href = pngDataURL;
link.setAttribute('download', 'scratchblocks.png');
link.textContent = "Export PNG";
link.className = 'export-link';
document.body.appendChild(link);
}, 2);
}, 0);
// update language dropdown
var lang = scratchblocks.allLanguages[obj.lang]
langStatus.textContent = lang ? lang.percentTranslated + "%" : "";
}
setInterval(function() {
if (extractHash()) {
updatedFromHash();
}
}, 200);
function updatedFromHash() {
editor.value = obj.script;
objUpdated();
chooseLang.value = obj.lang || "";
}
updatedFromHash();
</script>
<script>
function showDiff(e) {
document.body.classList[e.ctrlKey ? 'add' : 'remove']('cmp');
document.body.classList[e.altKey ? 'add' : 'remove']('diff');
}
document.addEventListener('keydown', showDiff);
document.addEventListener('keyup', showDiff);
</script>