p5.js-web-editor/client/modules/IDE/components/Editor.js

65 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-06-27 20:03:22 +00:00
import React, { PropTypes } from 'react';
2016-06-23 22:29:55 +00:00
import CodeMirror from 'codemirror';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/addon/selection/active-line';
class Editor extends React.Component {
componentDidMount() {
this._cm = CodeMirror(this.refs.container, { // eslint-disable-line
theme: 'p5-widget',
value: this.props.file.content,
2016-06-23 22:29:55 +00:00
lineNumbers: true,
styleActiveLine: true,
2016-07-12 19:58:11 +00:00
mode: 'javascript',
lineWrapping: true
2016-06-23 22:29:55 +00:00
});
this._cm.on('change', () => { // eslint-disable-line
// this.props.updateFileContent('sketch.js', this._cm.getValue());
this.props.updateFileContent(this.props.file.name, this._cm.getValue());
2016-06-23 22:29:55 +00:00
});
this._cm.getWrapperElement().style['font-size'] = `${this.props.fontSize}px`;
2016-07-11 03:11:06 +00:00
this._cm.setOption('indentWithTabs', this.props.isTabIndent);
2016-07-11 13:00:44 +00:00
this._cm.setOption('tabSize', this.props.indentationAmount);
2016-06-23 22:29:55 +00:00
}
componentDidUpdate(prevProps) {
if (this.props.file.content !== prevProps.file.content &&
this.props.file.content !== this._cm.getValue()) {
this._cm.setValue(this.props.file.content); // eslint-disable-line no-underscore-dangle
2016-06-23 22:29:55 +00:00
}
if (this.props.fontSize !== prevProps.fontSize) {
this._cm.getWrapperElement().style['font-size'] = `${this.props.fontSize}px`;
}
2016-07-06 15:27:39 +00:00
if (this.props.indentationAmount !== prevProps.indentationAmount) {
this._cm.setOption('tabSize', this.props.indentationAmount);
}
2016-07-11 03:11:06 +00:00
if (this.props.isTabIndent !== prevProps.isTabIndent) {
this._cm.setOption('indentWithTabs', this.props.isTabIndent);
}
2016-06-23 22:29:55 +00:00
}
componentWillUnmount() {
this._cm = null;
}
_cm: CodeMirror.Editor
render() {
return <div ref="container" className="editor-holder"></div>;
}
}
2016-06-27 20:03:22 +00:00
Editor.propTypes = {
2016-07-11 02:52:48 +00:00
indentationAmount: PropTypes.number.isRequired,
2016-07-12 03:40:30 +00:00
isTabIndent: PropTypes.bool.isRequired,
updateFileContent: PropTypes.func.isRequired,
fontSize: PropTypes.number.isRequired,
file: PropTypes.shape({
name: PropTypes.string.isRequired,
content: PropTypes.string.isRequired
2016-07-12 03:40:30 +00:00
})
2016-06-27 20:03:22 +00:00
};
2016-06-23 22:29:55 +00:00
export default Editor;