import React, { PureComponent } from 'react' import PropTypes from 'prop-types' import isBrowser from 'is-in-browser' import store from 'store2' import {js_beautify} from 'js-beautify' import copy from 'copy-text-to-clipboard' import Layout from '../components/Layout' const theme = 'tomorrow' let brace let AceEditor if (isBrowser) { 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') } export default class ConversionPanel extends PureComponent { static propTypes = { name: PropTypes.string.isRequired, leftMode: PropTypes.string, rightMode: PropTypes.string, pathname: PropTypes.string } static defaultProps = { leftMode: 'javascript', rightMode: 'javascript', pathname: '/' } constructor (props) { super(props) this.state = { resultValue: '', value: '', info: '' } } componentDidMount() { const {name} = this.props this.setState({ value: store(`${name}_code`) || this.props.defaultText, resultValue: store(`${name}_result`) || this.props.getTransformedValue(this.props.defaultText) }) } componentWillReceiveProps(nextProps) { const {name} = nextProps this.setState({ value: store(`${name}_code`) || nextProps.defaultText, resultValue: store(`${name}_result`) || nextProps.getTransformedValue(nextProps.defaultText) }) } onChange = (newValue) => { const {name} = this.props; try { const code = this.props.getTransformedValue(newValue) this.setState({ resultValue: code, info: '' }) store(`${name}_code`, newValue) store(`${name}_result`, code) } catch (e) { this.setState({ info: e.message }) } this.setState({ value: newValue }) } prettifyCode = () => { const value = js_beautify(this.state.value) this.setState({ value }, () => { store(`${this.props.name}_code`, value) }) } copyCode = () => { copy(this.state.resultValue) this.setState({ info: 'Code copied to clipboard.' }) } render () { return (
{isBrowser && }
{isBrowser && }
{this.state.info}
) } }