-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
80 lines (64 loc) · 2.22 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Blockly from '../../node-blockly/browser';
var editor;
var code = document.getElementById('startBlocks');
function render(element, toolbox, newCode) {
var dom = code;
if (editor) {
editor.removeChangeListener(updateCode);
if (!newCode) {
dom = Blockly.Xml.workspaceToDom(editor)
}
editor.dispose()
}
editor = Blockly.inject(element, {
toolbox: document.getElementById(toolbox)
})
Blockly.Xml.domToWorkspace(dom, editor);
editor.addChangeListener(updateCode);
return editor
}
function updateCode() {
document.getElementById('js').childNodes[0].innerText = Blockly.JavaScript.workspaceToCode(editor)
document.getElementById('php').childNodes[0].innerText = Blockly.PHP.workspaceToCode(editor)
document.getElementById('lua').childNodes[0].innerText = Blockly.Lua.workspaceToCode(editor)
document.getElementById('dart').childNodes[0].innerText = Blockly.Dart.workspaceToCode(editor)
document.getElementById('python').childNodes[0].innerText = Blockly.Python.workspaceToCode(editor)
document.getElementById('xml').childNodes[0].innerText = Blockly.Xml.domToPrettyText(Blockly.Xml.workspaceToDom(editor))
document.querySelectorAll('pre code').forEach((block) => {
hljs.highlightBlock(block);
});
}
editor = render('editor', 'toolbox');
updateCode();
document.getElementById('locale').onchange = (e) => {
import('node-blockly/lib/i18n/' + e.target.value).then((locale) => {
Blockly.setLocale(locale);
render('editor', 'toolbox');
})
}
$("#locale").select2({
allowClear: false,
width: 200,
});
$("#import").click(() => {
var xml = prompt('Insert xml');
if (!xml) return;
try {
code.innerHTML = xml;
if (code.childNodes[0].nodeType === 1 && code.childNodes[0].tagName.toLowerCase() === 'xml') {
code.innerHTML = code.childNodes[0].innerHTML;
} else {
code.innerHTML = `
<block type="text_print" inline="false">
<value name="TEXT">
<block type="text">
<field name="TEXT">${code.innerHTML}</field>
</block>
</value>
</block>`
}
render('editor', 'toolbox', true);
} catch (e) {
alert('Invalid xml: ' + e.toString())
}
});