2018-04-12 00:22:03 +03:00
|
|
|
import CodeMirror from "codemirror";
|
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
class CodeMirrorPanel extends React.Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this._textareaRef = React.createRef();
|
|
|
|
this._codeMirror = null;
|
2018-04-17 20:50:53 +03:00
|
|
|
this._cached = "";
|
2018-04-12 00:22:03 +03:00
|
|
|
this.handleChange = this.handleChange.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-04-17 20:50:53 +03:00
|
|
|
const { mode, rulerColumn: column, rulerColor: color, value } = this.props;
|
2018-04-12 00:22:03 +03:00
|
|
|
this._codeMirror = CodeMirror.fromTextArea(
|
|
|
|
this._textareaRef.current,
|
2018-04-12 02:55:37 +03:00
|
|
|
Object.assign({ mode, rulers: [{ column, color }] }, this.props.options)
|
2018-04-12 00:22:03 +03:00
|
|
|
);
|
|
|
|
this._codeMirror.on("change", this.handleChange);
|
2018-04-17 20:50:53 +03:00
|
|
|
|
|
|
|
this.updateValue(value || "");
|
2018-04-12 00:22:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._codeMirror && this._codeMirror.toTextArea();
|
|
|
|
}
|
|
|
|
|
2018-04-12 02:55:37 +03:00
|
|
|
componentDidUpdate(prevProps) {
|
2018-04-17 20:50:53 +03:00
|
|
|
if (this.props.options.readOnly && this.props.value !== this._cached) {
|
|
|
|
this.updateValue(this.props.value);
|
2018-04-12 00:22:03 +03:00
|
|
|
}
|
2018-04-12 02:55:37 +03:00
|
|
|
if (this.props.mode !== prevProps.mode) {
|
|
|
|
this._codeMirror.setOption("mode", this.props.mode);
|
|
|
|
}
|
|
|
|
if (this.props.rulerColumn !== prevProps.rulerColumn) {
|
|
|
|
const { rulerColumn: column, rulerColor: color } = this.props;
|
|
|
|
this._codeMirror.setOption("rulers", [{ column, color }]);
|
|
|
|
}
|
2018-04-12 00:22:03 +03:00
|
|
|
}
|
|
|
|
|
2018-04-17 20:50:53 +03:00
|
|
|
updateValue(value) {
|
|
|
|
this._cached = value;
|
|
|
|
this._codeMirror.setValue(value);
|
|
|
|
}
|
|
|
|
|
2018-04-12 00:22:03 +03:00
|
|
|
handleChange(doc, change) {
|
|
|
|
if (change.origin !== "setValue") {
|
2018-04-17 20:50:53 +03:00
|
|
|
this._cached = doc.getValue();
|
|
|
|
this.props.onChange(this._cached);
|
2018-04-12 00:22:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="editor input">
|
|
|
|
<textarea ref={this._textareaRef} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 02:55:37 +03:00
|
|
|
export function InputPanel({ mode, rulerColumn, value, onChange }) {
|
2018-04-12 00:22:03 +03:00
|
|
|
return (
|
|
|
|
<CodeMirrorPanel
|
|
|
|
options={{
|
|
|
|
lineNumbers: true,
|
|
|
|
keyMap: "sublime",
|
|
|
|
autoCloseBrackets: true,
|
|
|
|
matchBrackets: true,
|
|
|
|
showCursorWhenSelecting: true,
|
2018-04-12 02:55:37 +03:00
|
|
|
tabWidth: 2
|
2018-04-12 00:22:03 +03:00
|
|
|
}}
|
2018-04-12 02:55:37 +03:00
|
|
|
mode={mode}
|
|
|
|
rulerColumn={rulerColumn}
|
|
|
|
rulerColor="#eeeeee"
|
2018-04-12 00:22:03 +03:00
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-12 02:55:37 +03:00
|
|
|
export function OutputPanel({ mode, rulerColumn, value }) {
|
2018-04-12 00:22:03 +03:00
|
|
|
return (
|
|
|
|
<CodeMirrorPanel
|
|
|
|
options={{
|
|
|
|
readOnly: true,
|
2018-04-12 02:55:37 +03:00
|
|
|
lineNumbers: true
|
2018-04-12 00:22:03 +03:00
|
|
|
}}
|
2018-04-12 02:55:37 +03:00
|
|
|
mode={mode}
|
|
|
|
rulerColumn={rulerColumn}
|
|
|
|
rulerColor="#444444"
|
2018-04-12 00:22:03 +03:00
|
|
|
value={value}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function DebugPanel({ value }) {
|
|
|
|
return (
|
|
|
|
<CodeMirrorPanel
|
|
|
|
options={{ readOnly: true, lineNumbers: false, mode: "jsx" }}
|
|
|
|
value={value}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|