prettier/website/playground/panels.js

97 lines
2.3 KiB
JavaScript
Raw Normal View History

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;
this.handleChange = this.handleChange.bind(this);
}
componentDidMount() {
2018-04-12 02:55:37 +03:00
const { mode, rulerColumn: column, rulerColor: color } = 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);
this._codeMirror.setValue(this.props.value || "");
}
componentWillUnmount() {
this._codeMirror && this._codeMirror.toTextArea();
}
2018-04-12 02:55:37 +03:00
componentDidUpdate(prevProps) {
2018-04-12 00:22:03 +03:00
if (this.props.value !== this._codeMirror.getValue()) {
this._codeMirror.setValue(this.props.value);
}
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
}
handleChange(doc, change) {
if (change.origin !== "setValue") {
this.props.onChange(doc.getValue());
}
}
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}
/>
);
}