forked from Nekomajin42/spreadsheet-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
369 lines (339 loc) · 9.7 KB
/
script.js
File metadata and controls
369 lines (339 loc) · 9.7 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"use strict";
/**
* Config object for UI locale, colors, etc.
*/
var spreadsheetBlocks = {
lang: undefined,
settings: undefined,
colors: {
grey: {
c1: "#424242",
c2: "#616161",
c3: "#757575"
},
green: {
c1: "#1B5E20",
c2: "#2E7D32",
c3: "#388E3C",
c4: "#43A047",
c5: "#4CAF50"
},
blue: {
c1: "#0D47A1",
c2: "#1565C0",
c3: "#1976D2",
c4: "#1976D2",
c5: "#2196F3"
},
bluegrey: {
c1: "#263238",
c2: "#37474F",
c3: "#455A64",
c4: "#546E7A",
c5: "#607D8B"
},
orange: {
c1: "#BF360C",
c2: "#D84315",
c3: "#E64A19",
c4: "#F4511E",
c5: "#FF5722"
},
red: {
c1: "#B71C1C",
c2: "#C62828",
c3: "#D32F2F",
c4: "#E53935",
c5: "#F44336"
},
teal: {
c1: "#004D40",
c2: "#00695C",
c3: "#00796B",
c4: "#00897B",
c5: "#009688"
}
}
};
/**
* Stuff to do on page load:
* - Load UI locale strings
* - Inject and config Blockly
* - Implement auto backup and restore
* - Make the Create button work
* - Make the menu work
*/
window.addEventListener("DOMContentLoaded", function()
{
// load settings and UI strings
spreadsheetBlocks.lang = getBrowserLocale();
spreadsheetBlocks.settings = (window.localStorage.settings) ? JSON.parse(window.localStorage.settings) : {lang: spreadsheetBlocks.lang, software: "mse"};
setUILocale(spreadsheetBlocks.lang);
// config the workspace
var workspace = Blockly.inject("blockly",
{
toolbox: document.getElementById("toolbox").textContent,
collapse: false,
comments: false,
horizontalLayout: true,
media: "blockly/media/",
scrollbars: true,
sounds: false,
trashcan: true,
zoom:
{
controls: true,
startScale: 1.0,
wheel: false
}
});
Blockly.BlockSvg.START_HAT = true;
Blockly.Flyout.prototype.autoClose = false;
Blockly.Tooltip.LIMIT = 500;
Blockly.Tooltip.HOVER_MS = 200;
// restore
if (window.localStorage.autoSave == null) // no session found, create START block
{
var xml_text = "<xml><block type='spreadsheet_start' id='spreadsheet_start' x='0' y='100'></block></xml>";
var xml = Blockly.Xml.textToDom(xml_text);
Blockly.Xml.domToWorkspace(xml, workspace);
}
else // restore previous session
{
Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(window.localStorage.autoSave), workspace);
}
// auto backup
var save = window.setInterval(function()
{
window.localStorage.autoSave = Blockly.Xml.domToText(Blockly.Xml.workspaceToDom(workspace));
}, 10000);
// make the Create button work
document.getElementById("create-button").addEventListener("click", function()
{
// get the code (only inside the start block)
var code = Blockly.JavaScript.blockToCode(workspace.getBlockById("spreadsheet_start"));
// fill the output field
var output = document.getElementById("output");
output.value = (code === "=") ? "" : code; // clear the output if the start block is empty
output.select();
if (code !== "=" && document.execCommand("copy")) // the start block is not empty AND clipboard write is supported
{
window.getSelection().removeAllRanges();
output.blur();
// animate output
document.querySelector("label[for='output']").classList.add("copy");
window.setTimeout(function(output)
{
document.querySelector("label[for='output']").classList.remove("copy");
}, 1000);
}
});
// make the menu work
document.getElementById("menu-button").addEventListener("click", function()
{
document.getElementById("menu").classList.add("open");
});
document.body.addEventListener("click", function(e)
{
if (e.target.id !== "menu-button")
{
document.getElementById("menu").classList.remove("open");
}
});
// save the workspace to XML
document.getElementById("save").addEventListener("click", function()
{
var xml = Blockly.Xml.workspaceToDom(workspace);
var xml_text = Blockly.Xml.domToPrettyText(xml);
try
{
var file = new Blob([xml_text], {type: "text/xml;charset=utf-8"});
saveAs(file, "code.sb", true);
}
catch (e)
{
console.error(e);
window.alert(getErrorLocale("error-feature"));
}
});
// open the workspace from XML
document.getElementById("open").addEventListener("click", function()
{
try
{
// create input and load file(s)
var input = document.createElement("input");
input.type = "file";
input.accept = ".sb";
input.multiple = false;
input.click();
input.addEventListener("change", function(e)
{
var reader = new FileReader();
reader.onload = function(e)
{
Blockly.mainWorkspace.clear(); // clear the workspace
var xml_text = e.target.result; // load new code
var xml = Blockly.Xml.textToDom(xml_text);
Blockly.Xml.domToWorkspace(xml, workspace);
}
reader.readAsText(input.files[0], "utf-8");
});
}
catch (e)
{
console.error(e);
window.alert(getErrorLocale("error-feature"));
}
});
// clear the workspace
document.getElementById("clear").addEventListener("click", function()
{
Blockly.mainWorkspace.clear(); // clear the workspace
var xml_text = "<xml><block type='spreadsheet_start' id='spreadsheet_start' x='0' y='100'></block></xml>";
var xml = Blockly.Xml.textToDom(xml_text);
Blockly.Xml.domToWorkspace(xml, workspace);
});
// create screenshot
document.getElementById("screenshot").addEventListener("click", function()
{
try
{
saveSvgAsPng(document.querySelector(".blocklySvg"), "spreadsheet_blocks.png", {encoderOptions: 1});
}
catch (e)
{
console.error(e);
window.alert(getErrorLocale("error-feature"));
}
});
// open and manage Settings
document.getElementById("sets").addEventListener("click", function()
{
// load saved settings
document.getElementById("settings-lang").value = spreadsheetBlocks.settings.lang || spreadsheetBlocks.lang;
document.getElementById("settings-software").value = spreadsheetBlocks.settings.software || "mse";
document.getElementById("settings-window").classList.add("open");
});
document.getElementById("settings-cancel").addEventListener("click", function(e)
{
document.getElementById("settings-window").classList.remove("open");
});
document.getElementById("settings-ok").addEventListener("click", function(e)
{
spreadsheetBlocks.settings.lang = document.getElementById("settings-lang").value;
spreadsheetBlocks.settings.software = document.getElementById("settings-software").value;
window.localStorage.settings = JSON.stringify(spreadsheetBlocks.settings);
location.reload();
});
// open Help
document.getElementById("help").addEventListener("click", function()
{
window.open("https://github.com/Nekomajin42/spreadsheet-blocks/tree/master/help");
});
});
/**
* These functions return or set the localized strings of UI elements, blocks and generators
* @default: The language of the browser
* @optional: The language selected by the user
* @fallback: English (en)
*/
function getBrowserLocale()
{
if (window.localStorage.settings) // load from saved settings
{
for (var key in lang_data.meta.supported)
{
if (key === JSON.parse(window.localStorage.settings).lang)
{
return key;
}
}
}
for (var key in lang_data.meta.supported) // find browser locale
{
if (lang_data.meta.supported[key].indexOf(navigator.language) !== -1)
{
return key;
}
}
// default
return "en";
}
function setUILocale(lang)
{
// append Blockly locale file to HEAD
if (document.getElementById("blockly-lang"))
{
document.getElementById("blockly-lang").src = "blockly/msg/js/" + spreadsheetBlocks.lang + ".js";
}
else
{
var script = document.createElement("script");
script.src = "blockly/msg/js/" + spreadsheetBlocks.lang + ".js";
script.type = "text/javascript";
script.id = "blockly-lang";
document.head.appendChild(script);
}
// controls, menu, settings
var elements = document.querySelectorAll("input[data-locale]");
for (var i=0; i<elements.length; i++)
{
elements[i].value = lang_data.gui[elements[i].dataset.locale][spreadsheetBlocks.lang].value;
elements[i].title = lang_data.gui[elements[i].dataset.locale][spreadsheetBlocks.lang].title;
}
elements = document.querySelectorAll("h1[data-locale], label[data-locale]");
for (var i=0; i<elements.length; i++)
{
elements[i].innerText = lang_data.gui[elements[i].dataset.locale][spreadsheetBlocks.lang].label;
}
elements = document.querySelectorAll("select[data-locale]");
for (var i=0; i<elements.length; i++)
{
for (var j=0; j<elements[i].options.length; j++)
{
elements[i].options[j].innerText = lang_data.gui[elements[i].dataset.locale][spreadsheetBlocks.lang][elements[i].options[j].value];
}
}
// Blockly toolbox
var toolbox = new DOMParser().parseFromString(document.getElementById("toolbox").textContent, "application/xml");
elements = toolbox.querySelectorAll("category");
for (var i=0; i<elements.length; i++)
{
elements[i].setAttribute("name", lang_data.gui[elements[i].getAttribute("locale")][spreadsheetBlocks.lang].name);
}
document.getElementById("toolbox").textContent = new XMLSerializer().serializeToString(toolbox);
}
function getErrorLocale(error)
{
return lang_data.gui[error][spreadsheetBlocks.lang].message;
}
function getBlockLocale(block)
{
return lang_data.blocks[block][spreadsheetBlocks.lang];
}
function getFunctionName(block, name)
{
if (name === undefined)
{
return lang_data.blocks[block][spreadsheetBlocks.lang].name;
}
else
{
for (var i=0; i<lang_data.blocks[block][spreadsheetBlocks.lang].name.length; i++)
{
if (lang_data.blocks[block][spreadsheetBlocks.lang].name[i][1] === name)
{
return lang_data.blocks[block][spreadsheetBlocks.lang].name[i][0];
}
}
}
}
function getSeparatorLocale()
{
if (window.localStorage.settings && JSON.parse(window.localStorage.settings).software === "loc") // LO style
{
return ";";
}
return lang_data.meta.separator[spreadsheetBlocks.lang]; // MS style
}