-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor.jsx
99 lines (96 loc) · 3.52 KB
/
editor.jsx
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
var Components = Components || {};
Components.Editor = React.createClass({
getDefaultProps: function() {
return {
theme: "ace/theme/chrome",
waitForChange: 2000,
name: "editor",
defaultFile: "fib.js",
};
},
getInitialState: function () {
return {
interpreterStatus: "idle"
};
},
getInterpreterMessage: function () {
if (this.state.interpreterStatus === "idle") {
return "Idle......";
} else if (this.state.interpreterStatus === "running") {
return "Running......";
} else {
return "Error: " + this.state.interpreterError.toString();
}
},
componentDidMount: function () {
this.editor = ace.edit(this.props.name);
this.editor.setTheme(this.props.theme);
this.editor.getSession().setMode("ace/mode/javascript");
this.editor.getSession().on("change", this.onTextChange);
this.openFile(this.props.defaultFile);
this.waitForChange = Number(this.props.waitForChange);
this.interpreterTimer = null;
if (window.Worker) {
this.worker = new Worker('./interpreterWorker.js');
this.worker.onmessage = this.handleMessage;
}
},
handleMessage: function (e) {
console.log('onmesssage back');
if (e.data.err) {
console.log('worker error:', e.data.err);
this.setState({interpreterStatus: 'error', interpreterError: e.data.err});
} else {
this.setState({interpreterStatus: 'idle'});
this.analyzer = e.data.analyzer;
this.present();
}
},
present: function () {
console.log('value', this.refs.pickAnimator.value);
React.unmountComponentAtNode(document.getElementById('presenter'));
React.render(<Components.Presenter analyzer={this.analyzer} animator={animators[this.refs.pickAnimator.value]}/>, document.getElementById('presenter'));
},
render: function () {
return (
<div>
<Components.FileList onClick={this.openFile} defaultValue={this.props.defaultFile} > </Components.FileList>
<div id={this.props.name}> </div>
<code>
Interperter Status: {this.getInterpreterMessage()}
</code>
<div style={{"padding-top":"14px"}} >
<p>
<h4><b>Pick a Animator: <Components.SelectDropdown options={Object.keys(animators)} ref="pickAnimator" defaultValue="topo" onChange={this.present} /> </b></h4>
</p>
</div>
</div>
);
},
openFile: function (fileTitle) {
var data = null;
Data.files.forEach(function (file) {
if (file.title === fileTitle) {
data = file;
}
});
if (data) {
this.editor.setValue(data.content);
} else {
// TODO: handle error
}
},
onTextChange: function (e) {
console.log('changed');
if (this.interpreterTimer) {
clearTimeout(this.interpreterTimer);
}
this.interpreterTimer = setTimeout(this.issueInterpreter, this.waitForChange);
},
issueInterpreter: function () {
this.interpreterTimer = null;
this.setState({interpreterStatus: 'running'});
this.worker.postMessage(this.editor.getValue());
},
});
React.render(<Components.Editor defaultFile="CombinationCalculator.js" name="editor" />, document.getElementById('editor-wrap'))