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

36 lines
818 B
React
Raw Normal View History

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