This repository was archived by the owner on Oct 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathlive_editor.js
More file actions
250 lines (217 loc) · 6.79 KB
/
live_editor.js
File metadata and controls
250 lines (217 loc) · 6.79 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
'use strict';
var IS_MOBILE = navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i);
var CodeMirrorEditor = React.createClass({
displayName: 'CodeMirrorEditor',
propTypes: {
lineNumbers: React.PropTypes.bool,
onChange: React.PropTypes.func
},
getDefaultProps: function getDefaultProps() {
return {
lineNumbers: false
};
},
componentDidMount: function componentDidMount() {
if (IS_MOBILE) return;
this.editor = CodeMirror.fromTextArea(ReactDOM.findDOMNode(this.refs.editor), {
mode: 'ruby',
lineNumbers: this.props.lineNumbers,
lineWrapping: true,
smartIndent: true, // javascript mode does bad things with jsx indents
matchBrackets: true,
theme: 'rubyblue',
readOnly: this.props.readOnly
});
this.editor.on('change', this.handleChange);
},
componentDidUpdate: function componentDidUpdate() {
if (this.props.readOnly) {
this.editor.setValue(this.props.codeText);
}
},
handleChange: function handleChange() {
if (!this.props.readOnly) {
this.props.onChange && this.props.onChange(this.editor.getValue());
}
},
render: function render() {
// wrap in a div to fully contain CodeMirror
var editor;
if (IS_MOBILE) {
editor = React.createElement(
'pre',
{ style: { overflow: 'scroll' } },
this.props.codeText
);
} else {
editor = React.createElement('textarea', { ref: 'editor', defaultValue: this.props.codeText });
}
return React.createElement(
'div',
{ style: this.props.style, className: this.props.className },
editor
);
}
});
var selfCleaningTimeout = {
componentDidUpdate: function componentDidUpdate() {
clearTimeout(this.timeoutID);
},
setTimeout: (function (_setTimeout) {
function setTimeout() {
return _setTimeout.apply(this, arguments);
}
setTimeout.toString = function () {
return _setTimeout.toString();
};
return setTimeout;
})(function () {
clearTimeout(this.timeoutID);
this.timeoutID = setTimeout.apply(null, arguments);
})
};
var ReactPlayground = React.createClass({
displayName: 'ReactPlayground',
mixins: [selfCleaningTimeout],
MODES: { JSX: 'JSX', JS: 'JS' }, //keyMirror({JSX: true, JS: true}),
propTypes: {
codeText: React.PropTypes.string.isRequired,
elementId: React.PropTypes.string.isRequired,
transformer: React.PropTypes.func,
renderCode: React.PropTypes.bool,
showCompiledJSTab: React.PropTypes.bool,
showLineNumbers: React.PropTypes.bool,
editorTabTitle: React.PropTypes.string
},
getDefaultProps: function getDefaultProps() {
return {
transformer: function transformer(code) {
var compiled_code = Opal.Opal.Compiler.$new(code).$compile();
//result = `eval(#{compiled_code})`
//puts "result = #{result}"
return compiled_code;
//return babel.transform(code).code;
},
editorTabTitle: 'Live Ruby Editor',
showCompiledJSTab: true,
showLineNumbers: false
};
},
getInitialState: function getInitialState() {
return {
mode: this.MODES.JSX,
code: this.props.codeText
};
},
handleCodeChange: function handleCodeChange(value) {
this.setState({ code: value });
this.executeCode();
},
handleCodeModeSwitch: function handleCodeModeSwitch(mode) {
this.setState({ mode: mode });
},
compileCode: function compileCode() {
return this.props.transformer(this.state.code);
},
render: function render() {
var isJS = this.state.mode === this.MODES.JS;
var compiledCode = '';
try {
compiledCode = this.compileCode();
} catch (err) {}
var JSContent = React.createElement(CodeMirrorEditor, {
key: 'js',
className: 'playgroundStage CodeMirror-readonly',
onChange: this.handleCodeChange,
codeText: compiledCode,
readOnly: true,
lineNumbers: this.props.showLineNumbers
});
var JSXContent = React.createElement(CodeMirrorEditor, {
key: 'jsx',
onChange: this.handleCodeChange,
className: 'playgroundStage',
codeText: this.state.code,
lineNumbers: this.props.showLineNumbers
});
var JSXTabClassName = 'playground-tab' + (isJS ? '' : ' playground-tab-active');
var JSTabClassName = 'playground-tab' + (isJS ? ' playground-tab-active' : '');
var JSTab = React.createElement(
'div',
{
className: JSTabClassName,
onClick: this.handleCodeModeSwitch.bind(this, this.MODES.JS) },
'Compiled JS'
);
var JSXTab = React.createElement(
'div',
{
className: JSXTabClassName,
onClick: this.handleCodeModeSwitch.bind(this, this.MODES.JSX) },
this.props.editorTabTitle
);
return React.createElement(
'div',
{ className: 'playground' },
React.createElement(
'div',
null,
JSXTab,
React.createElement(
'div',
{ className: 'playground-tab playground-tab-active target-tab' },
React.createElement(
'div',
null,
this.props.elementId
)
)
),
React.createElement(
'div',
{ className: 'playgroundCode' },
JSXContent
),
React.createElement(
'div',
{ className: 'playgroundPreview' },
React.createElement('div', { ref: 'mount', id: this.props.elementId })
)
);
},
componentDidMount: function componentDidMount() {
this.executeCode();
},
componentDidUpdate: function componentDidUpdate(prevProps, prevState) {
// execute code only when the state's not being updated by switching tab
// this avoids re-displaying the error, which comes after a certain delay
if (this.props.transformer !== prevProps.transformer || this.state.code !== prevState.code) {
this.executeCode();
}
},
executeCode: function executeCode() {
var mountNode = ReactDOM.findDOMNode(this.refs.mount);
Opal.Object.$$proto.$mount_node = function () {
return mountNode;
};
try {
React.unmountComponentAtNode(mountNode);
} catch (e) {}
try {
var compiledCode = this.compileCode();
if (this.props.renderCode) {
React.render(React.createElement(CodeMirrorEditor, { codeText: compiledCode, readOnly: true }), mountNode);
} else {
eval(compiledCode);
}
} catch (err) {
this.setTimeout(function () {
React.render(React.createElement(
'div',
{ className: 'playgroundError' },
err.toString()
), mountNode);
}, 500);
}
}
});