-
Notifications
You must be signed in to change notification settings - Fork 262
/
example.js
61 lines (57 loc) · 1.67 KB
/
example.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
var React = require('react');
var ReactDOM = require('react-dom');
var Codemirror = require('../../src/Codemirror');
const createReactClass = require('create-react-class');
require('codemirror/mode/javascript/javascript');
require('codemirror/mode/xml/xml');
require('codemirror/mode/markdown/markdown');
var defaults = {
markdown: '# Heading\n\nSome **bold** and _italic_ text\nBy [Jed Watson](https://github.com/JedWatson)',
javascript: 'var component = {\n\tname: "react-codemirror",\n\tauthor: "Jed Watson",\n\trepo: "https://github.com/JedWatson/react-codemirror"\n};'
};
var App = createReactClass({
getInitialState () {
return {
code: defaults.markdown,
readOnly: false,
mode: 'markdown',
};
},
updateCode (newCode) {
this.setState({
code: newCode
});
},
changeMode (e) {
var mode = e.target.value;
this.setState({
mode: mode,
code: defaults[mode]
});
},
toggleReadOnly () {
this.setState({
readOnly: !this.state.readOnly
}, () => this.refs.editor.focus());
},
render () {
var options = {
lineNumbers: true,
readOnly: this.state.readOnly,
mode: this.state.mode
};
return (
<div>
<Codemirror ref="editor" value={this.state.code} onChange={this.updateCode} options={options} autoFocus={true} />
<div style={{ marginTop: 10 }}>
<select onChange={this.changeMode} value={this.state.mode}>
<option value="markdown">Markdown</option>
<option value="javascript">JavaScript</option>
</select>
<button onClick={this.toggleReadOnly}>Toggle read-only mode (currently {this.state.readOnly ? 'on' : 'off'})</button>
</div>
</div>
);
}
});
ReactDOM.render(<App />, document.getElementById('app'));