forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (101 loc) · 3.26 KB
/
Copy pathscript.js
File metadata and controls
116 lines (101 loc) · 3.26 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
/* For demo purposes only, component to track its own (and parents) amount of renderings */
var RenderCounter=React.createClass({_c:0,render:function(){return React.createElement("div", {className:"render-counter "+(++this._c%2?'odd':'even')}, this._c)}});
// Save the original source before pretty printer is fired
function getCodeFromTA(elem) {
var cm = $(elem).data('cm');
if (!cm) {
console.log("Code editors not yet loaded");
return "";
}
var code = cm.getDoc().getValue();
return code;
}
function runCodeHelper(code) {
// some global vars..
window.observable = mobx.observable;
window.autorun = mobx.autorun;
window.computed = mobx.computed;
window.observer = mobxReact.observer;
var globalEval = eval; // global scope trick, See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
code = Babel.transform(code, { presets: ['react', 'es2015-no-commonjs', 'stage-1'], plugins: [
'transform-decorators-legacy'
] }).code.replace(/"use strict"/g,"");
try {
globalEval(code);
} catch (e) {
console.error(e);
}
}
function runCode(ids) {
$(ids.join(",")).each(function(i, elem) {
clearConsole();
var code = getCodeFromTA(elem);
runCodeHelper(code);
});
};
var runLineHandle = null;
var runLineIndex = 0;
var lineMarker;
function runCodePerLine() {
if (typeof observableTodoStore === 'undefined') {
runCode(['#code1', '#code3', '#code4', '#react1'])
}
function runNext() {
var cm = $("#play1").data('cm');
var code = cm.getDoc().getValue();
var lines = code.split("\n");
var idx = runLineIndex % lines.length;
var line = lines[idx];
if (lineMarker)
lineMarker.clear();
lineMarker = cm.getDoc().markText({ line: idx, ch: 0}, { line: idx, ch: line.length }, { css: 'background-color:#ff9955;' });
runCodeHelper(line);
runLineIndex++;
};
if (!runLineHandle) {
$("#runline-btn").text("Pause");
runNext();
runLineHandle = setInterval(runNext, 2000);
}
else {
clearInterval(runLineHandle);
runLineHandle = null;
$("#runline-btn").text("Continue");
}
}
$console = $('#consoleout');
var baseLog = console.log, baseError = console.error;
console.log = function(arg) {
baseLog.apply(console, arguments);
$console.html($console.html() + "<div>" + escapeHtml(arg).replace('\n','<br/>\n') + "</div>");
};
console.error = function(arg) {
baseError.apply(console, arguments);
$console.html($console.html() + "<div style='color:red'>" + escapeHtml(arg).replace(/\n/,'<br/>') + "</div>\n");
};
function clearConsole() {
$('#consoleout').text('')
}
var entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
function escapeHtml(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
}
$(function() {
$("textarea").each(function(i, t) {
var cm = CodeMirror.fromTextArea(t, {
lineNumbers: false,
mode: "javascript",
theme: 'xq-light'
});
$(t).data('cm', cm);
});
});