diff --git a/components/ConversionPanel.js b/components/ConversionPanel.js index 3f726c4..d477322 100644 --- a/components/ConversionPanel.js +++ b/components/ConversionPanel.js @@ -2,7 +2,6 @@ import React, { PureComponent } from "react"; import cn from "classnames"; import isBrowser from "is-in-browser"; import copy from "copy-text-to-clipboard"; -import { html_beautify, css_beautify, js_beautify } from "js-beautify"; import unfetch from "unfetch"; let CodeMirror; @@ -20,11 +19,14 @@ if (isBrowser) { require("codemirror/mode/go/go"); } -const prettifyMap = { - css: css_beautify, - json: js_beautify, - html: html_beautify, - typescript: js_beautify +const prettierParsers = { + css: "postcss", + javascript: "babylon", + jsx: "babylon", + graphql: "graphql", + json: "json", + typescript: "typescript", + flow: "flow" }; const modeMapping = { @@ -43,6 +45,10 @@ const modeMapping = { name: "xml", htmlMode: true }, + flow: { + name: "javascript", + typescript: true + }, mysql: "text/x-mysql", scala: "text/x-scala" }; @@ -94,6 +100,10 @@ export default class ConversionPanel extends PureComponent { this.onChange(defaultText, splitValue); } + isPrettierAvailable() { + return isBrowser && !!window.prettier; + } + fetchJSON = async () => { const url = window.prompt("Enter URL to fetch JSON from a remote server."); if (!url) return; @@ -151,19 +161,38 @@ export default class ConversionPanel extends PureComponent { }); }; + setSplitValue = splitValue => this.setState({ splitValue }); + prettifyCode = () => { + if (!this.isPrettierAvailable()) return; const { leftMode } = this.props; const { value } = this.state; - this.setResult(prettifyMap[leftMode](value, { indent_size: 2 })); + this.setResult( + window.prettier.format(value, { parser: prettierParsers[leftMode] }) + ); + }; + + prettifySplitCode = () => { + if (!this.isPrettierAvailable()) return; + const { splitMode } = this.props; + const { splitValue } = this.state; + + this.setSplitValue( + window.prettier.format(splitValue, { parser: prettierParsers[splitMode] }) + ); }; copyCode = () => { const { rightMode } = this.props; const { resultValue } = this.state; + const parser = prettierParsers[rightMode]; + copy( - rightMode !== "rust" - ? js_beautify(resultValue, { e4x: true }) + parser + ? window.prettier.format(resultValue, { + parser + }) : resultValue ); this.setState({ @@ -172,6 +201,12 @@ export default class ConversionPanel extends PureComponent { }); }; + getPrettifyClass = () => { + return cn("btn", { + disabled: !this.isPrettierAvailable() + }) + } + render() { const { leftMode, @@ -204,14 +239,19 @@ export default class ConversionPanel extends PureComponent { // hide success footer bannner after 2 seconds. Let other types be visible until fixed. clearTimeout(this.footerTimeout); if (infoType === "success") { - this.footerTimeout = setTimeout(() => this.setState({ - info: "", - infoType: "" - }), 2000); + this.footerTimeout = setTimeout( + () => + this.setState({ + info: "", + infoType: "" + }), + 2000 + ); } return (