-
Notifications
You must be signed in to change notification settings - Fork 1
/
liveCode.js
63 lines (60 loc) · 2.2 KB
/
liveCode.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
function refreshLiveCoding(e, basename) {
var src = document.getElementById( basename + '_src');
src = src || document.getElementById( basename );
var html = src.innerText;
var script = html.replace("<script type=\"text/javascript\">", "").replace("</script>", "");
console.log('script:\n' + script);
if ( script ) {
var scriptTag = document.createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.innerText = script;
document.body.appendChild(scriptTag);
}
if ( e ) {
e.preventDefault();
e.stopPropagation();
}
return false;
}
function refreshLiveCoffee(e, basename) {
var src = document.getElementById( basename + '_src');
var dst = document.getElementById( basename + '_embed');
var html = src.innerText;
var script = html.replace("<script type=\"text/coffeescript\">", "").replace("</script>", "");
console.log(script);
dst.innerHTML = '// Generated Javascript:\n' + CoffeeScript.compile(script, {bare:true});
prettyPrint();
if ( e ) {
e.preventDefault();
e.stopPropagation();
}
return false;
}
function executeLiveCoffee(e, basename) {
var src = document.getElementById(basename + '_src');
var html = src.innerText;
var script = html.replace("<script type=\"text/coffeescript\">", "").replace("</script>", "");
if ( script ) {
html = html.replace(/<script type=\"text\/coffeescript\">([^<]*)<\/script>/, '');
var scriptTag = document.createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.innerText = CoffeeScript.compile(script, {bare:true});
console.log('script: ' + scriptTag.innerText);
document.body.appendChild(scriptTag);
}
if ( e ) {
e.preventDefault();
e.stopPropagation();
}
return false;
}
// Sync a contenteditable containing html with a div containing the result.
function manageLiveCoding(basename) {
var src = document.getElementById( basename + '_src');
src = src || document.getElementById( basename );
src.addEventListener('blur', function (e) {
window.setTimeout(function () {
prettyPrint();
}, 0);
}, false);
}