p5.js-web-editor/shared/components/Editor/Editor.jsx

41 lines
1,011 B
React
Raw Normal View History

2016-05-03 20:13:04 +00:00
import React from 'react';
import CSSModules from 'react-css-modules';
import CodeMirror from 'codemirror';
import 'codemirror/mode/javascript/javascript';
2016-05-05 21:48:26 +00:00
import '../../../node_modules/codemirror/lib/codemirror.css';
2016-05-03 20:13:04 +00:00
import './p5-widget-codemirror-theme.css';
2016-05-05 21:48:26 +00:00
class Editor extends React.Component {
_cm: CodeMirror.Editor
componentDidMount() {
2016-05-03 20:13:04 +00:00
this._cm = CodeMirror(this.refs.container, {
theme: 'p5-widget',
2016-05-05 23:01:31 +00:00
value: this.props.content,
2016-05-05 21:48:26 +00:00
// value: "var a = 'Hello World!';",
2016-05-03 20:13:04 +00:00
lineNumbers: true,
mode: 'javascript'
});
2016-05-05 21:48:26 +00:00
this._cm.on('change', () => {
this.props.updateFile("sketch.js", this._cm.getValue());
});
}
componentDidUpdate(prevProps) {
2016-05-05 23:01:31 +00:00
if (this.props.content !== prevProps.content &&
this.props.content !== this._cm.getValue()) {
this._cm.setValue(this.props.content);
2016-05-05 21:48:26 +00:00
}
}
componentWillUnmount() {
2016-05-03 20:13:04 +00:00
this._cm = null;
2016-05-05 21:48:26 +00:00
}
render() {
2016-05-03 20:13:04 +00:00
return <div ref="container" className="editor-holder"></div>;
}
2016-05-05 21:48:26 +00:00
}
export default Editor;