diff --git a/components/ConversionPanel.js b/components/ConversionPanel.js index b624bae..55a3659 100644 --- a/components/ConversionPanel.js +++ b/components/ConversionPanel.js @@ -1,22 +1,22 @@ -import React, { PureComponent } from 'react' -import Router from 'next/router' -import isBrowser from 'is-in-browser' -import copy from 'copy-text-to-clipboard' +import React, { PureComponent } from "react"; +import Router from "next/router"; +import isBrowser from "is-in-browser"; +import copy from "copy-text-to-clipboard"; -const theme = 'tomorrow' +const theme = "tomorrow"; -let brace -let AceEditor +let brace; +let AceEditor; if (isBrowser) { - brace = require('brace').default - AceEditor = require('react-ace').default + brace = require("brace").default; + AceEditor = require("react-ace").default; - require('brace/mode/javascript') - require(`brace/theme/${theme}`) - require('brace/mode/json') - require('brace/mode/typescript') - require('brace/mode/css') - require('brace/mode/html') + require("brace/mode/javascript"); + require(`brace/theme/${theme}`); + require("brace/mode/json"); + require("brace/mode/typescript"); + require("brace/mode/css"); + require("brace/mode/html"); } // const prettierParsers = { @@ -27,11 +27,11 @@ if (isBrowser) { // } const prettifyMap = { - css: 'css_beautify', - json: 'js_beautify', - html: 'html_beautify', - typescript: 'js_beautify' -} + css: "css_beautify", + json: "js_beautify", + html: "html_beautify", + typescript: "js_beautify" +}; type Props = { leftMode: ?string, @@ -43,112 +43,123 @@ type Props = { initialCheckboxValue: boolean, leftTitle: string, rightTitle: string -} +}; export default class ConversionPanel extends PureComponent { - props : Props + props: Props; static defaultProps = { - leftMode: 'javascript', - rightMode: 'javascript', - pathname: '/' - } + leftMode: "javascript", + rightMode: "javascript", + pathname: "/" + }; - constructor (props) { - super(props) + constructor(props) { + super(props); this.state = { - resultValue: '', - value: '', - info: '', - infoType: '' - } + resultValue: "", + value: "", + info: "", + infoType: "" + }; } - componentDidMount () { - const code = this.props.url.query.code || this.props.defaultText + componentDidMount() { + const code = this.props.url.query.code || this.props.defaultText; this.setState({ value: code, resultValue: this.props.getTransformedValue(code) - }) + }); } onChange = (newValue: string) => { try { - const code = this.props.getTransformedValue(newValue) + const code = this.props.getTransformedValue(newValue); this.setState({ resultValue: code, - info: '', - infoType: '' - }) + info: "", + infoType: "" + }); - this.setCodeInUrl(newValue) + this.setCodeInUrl(newValue); } catch (e) { this.setState({ info: e.message, - infoType: 'error' - }) + infoType: "error" + }); } this.setState({ value: newValue - }) - } + }); + }; - setCodeInUrl = (code) => { + setCodeInUrl = code => { Router.replace({ pathname: this.props.url.pathname, query: { code } - }) - } + }); + }; toggleCheckbox = (e: MouseEvent) => { - const checked = e.currentTarget.checked + const checked = e.currentTarget.checked; if (this.props.onCheckboxChange) { this.props.onCheckboxChange(checked, () => { this.setState({ resultValue: this.props.getTransformedValue(this.state.value) - }) - }) + }); + }); } - } + }; setResult = (result: string) => { - this.setState({ - value: result - }, () => { - this.setCodeInUrl(result) - }) - } + this.setState( + { + value: result + }, + () => { + this.setCodeInUrl(result); + } + ); + }; prettifyCode = () => { - debugger - const { leftMode } = this.props - const { value } = this.state + debugger; + const { leftMode } = this.props; + const { value } = this.state; - this.setResult(window[prettifyMap[leftMode]](value, {indent_size: 2})) - } + this.setResult(window[prettifyMap[leftMode]](value, { indent_size: 2 })); + }; copyCode = () => { - copy(this.state.resultValue) + copy(this.state.resultValue); this.setState({ - info: 'Code copied to clipboard.', - infoType: 'success' - }) - } + info: "Code copied to clipboard.", + infoType: "success" + }); + }; - render () { - const { leftMode, rightMode, onCheckboxChange, checkboxText, initialCheckboxValue, leftTitle, rightTitle } = this.props - const {infoType, resultValue, value, info} = this.state + render() { + const { + leftMode, + rightMode, + onCheckboxChange, + checkboxText, + initialCheckboxValue, + leftTitle, + rightTitle + } = this.props; + const { infoType, resultValue, value, info } = this.state; return (