import React, { PureComponent } from "react"; import cn from "classnames"; import Router from "next/router"; import isBrowser from "is-in-browser"; import copy from "copy-text-to-clipboard"; import unfetch from "unfetch"; const theme = "tomorrow"; let CodeMirror; if (isBrowser) { CodeMirror = require("react-codemirror2").default; require("codemirror-graphql/mode"); require("codemirror/mode/javascript/javascript"); require("codemirror/mode/xml/xml"); require("codemirror/mode/jsx/jsx"); require("codemirror/mode/css/css"); require("codemirror/mode/rust/rust"); require("codemirror/mode/sql/sql"); require("codemirror/mode/clike/clike"); require("codemirror/mode/go/go"); } const prettierParsers = { css: 'postcss', javascript: 'babylon', jsx: 'babylon', graphql: 'graphql', json: 'json', typescript: 'typescript', flow: 'flow' } const modeMapping = { javascript: { name: "javascript" }, json: { name: "javascript", json: true }, typescript: { name: "javascript", typescript: true }, html: { name: "xml", htmlMode: true }, flow: { name: 'javascript', typescript: true }, mysql: "text/x-mysql", scala: "text/x-scala" }; type Props = { leftMode: ?string, rightMode: ?string, resultValue: string, onCheckboxChange: Function, checkboxText: ?string, pathname: string, initialCheckboxValue: ?boolean, leftTitle: string, rightTitle: string, prettifyRightPanel: ?boolean, splitLeft: ?boolean, splitTitle: ?string, splitValue: ?string, splitMode: ?string, showFetchButton: boolean, fetchButtonText: ?string }; export default class ConversionPanel extends PureComponent { props: Props; static defaultProps = { leftMode: "javascript", rightMode: "javascript", pathname: "/", prettifyRightPanel: true, splitLeft: false, showFetchButton: true, fetchButtonText: 'Fetch JSON from URL' }; state = { resultValue: "", value: "", splitValue: "", info: "", infoType: "" }; componentDidMount() { const { defaultText, splitValue } = this.props; const code = defaultText; const sValue = splitValue; this.setState({ value: code, splitValue: sValue, resultValue: this.props.getTransformedValue(code, sValue) }); } fetchJSON = async () => { const url = window.prompt('Enter URL to fetch JSON from a remote server.') if (!url) return const res = await unfetch(url) const json = await res.json() this.onChange(JSON.stringify(json, null, 2)) } onChange = (newValue, leftSplitValue) => { const nValue = newValue || this.state.value; const splitValue = leftSplitValue && typeof leftSplitValue === "string" ? leftSplitValue : this.state.splitValue; try { const code = this.props.getTransformedValue(nValue, splitValue); this.setState({ resultValue: code, info: "", infoType: "" }); } catch (e) { this.setState({ info: e.message, infoType: "error" }); } this.setState({ value: nValue, splitValue: splitValue }); }; toggleCheckbox = e => { const checked = e.currentTarget.checked; if (this.props.onCheckboxChange) { this.props.onCheckboxChange(checked, () => { this.setState({ resultValue: this.props.getTransformedValue(this.state.value) }); }); } }; setResult = result => { this.setState({ value: result }); }; setSplitValue = splitValue => this.setState({splitValue}) prettifyCode = () => { const { leftMode } = this.props; const { value } = this.state; this.setResult(prettier.format(value, {parser: prettierParsers[leftMode]})); }; prettifySplitCode = () => { const { splitMode } = this.props; const { splitValue } = this.state; this.setSplitValue(prettier.format(splitValue, {parser: prettierParsers[splitMode]})); } copyCode = () => { const { rightMode } = this.props; const { resultValue } = this.state; copy( rightMode !== "rust" ? js_beautify(resultValue, { e4x: true }) : resultValue ); this.setState({ info: "Code copied to clipboard.", infoType: "success" }); }; render() { const { leftMode, rightMode, onCheckboxChange, checkboxText, initialCheckboxValue, leftTitle, rightTitle, prettifyRightPanel, splitLeft, splitTitle, splitMode, showFetchButton, fetchButtonText } = this.props; const { infoType, resultValue, value, info, splitValue } = this.state; const leftClass = cn("section left", { split: splitLeft }); const codeMirrorOptions = { lineNumbers: true, theme: "chrome-devtools", lineWrapping: true, scrollbarStyle: null }; return (