This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
66 lines (53 loc) · 1.41 KB
/
script.js
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
var click = document.getElementById("click")
var blocklyArea = document.getElementById('blocklyArea');
var workspace = Blockly.inject('blocklyDiv',
{
zoom:
{controls: true,
wheel: true,
startScale: 1.0,
maxScale: 3,
minScale: 0.3,
scaleSpeed: 1.2,
pinch: true},
grid:
{spacing: 30,
length: 3,
colour: '#ccc',
snap: true},
toolbox: document.getElementById('toolbox')
});
//code-gen & runing
var code = Blockly.JavaScript.workspaceToCode(workspace);
function myUpdateFunction(event) {
code = Blockly.JavaScript.workspaceToCode(workspace);
document.getElementById('text').value = code;
}
workspace.addChangeListener(myUpdateFunction);
$("#text").hide()
$("#play").click(function() {
click.play()
try {
eval(code);
} catch (error) {
throw new Error("Something went badly wrong!");
alert(error);
}
})
$("#code").click(function() {
click.play()
$("#text").toggle()
})
$("#save").click(function() {
click.play()
var xml = Blockly.Xml.workspaceToDom(workspace);
var xml_text = Blockly.Xml.domToText(xml);
var name = window.prompt("name");
store.setItem(name,xml_text);
})
$("#load").click(function() {
click.play()
var name = window.prompt("name");
var xml = Blockly.Xml.textToDom(store.getItem(name));
Blockly.Xml.domToWorkspace(xml, workspace);
})