From 0ca6ff691e66280d633c29b8b18f2c494c1bc64b Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Tue, 17 Apr 2018 16:42:03 -0300 Subject: [PATCH] simplify some stuff --- website/playground/Playground.js | 293 ++++++++------------- website/playground/index.js | 91 ++++++- website/static/separate-css/playground.css | 1 - 3 files changed, 197 insertions(+), 188 deletions(-) diff --git a/website/playground/Playground.js b/website/playground/Playground.js index 195351a49..3c0ef6d04 100644 --- a/website/playground/Playground.js +++ b/website/playground/Playground.js @@ -4,7 +4,6 @@ import { Button, ClipboardButton, LinkButton } from "./buttons"; import EditorState from "./EditorState"; import { DebugPanel, InputPanel, OutputPanel } from "./panels"; import PrettierFormat from "./PrettierFormat"; -import VersionLink from "./VersionLink"; import { shallowEqual } from "./helpers"; import * as urlHash from "./urlHash"; import formatMarkdown from "./markdown"; @@ -16,62 +15,24 @@ import { Checkbox } from "./sidebar/inputs"; const CATEGORIES_ORDER = ["Global", "JavaScript", "Markdown", "Special"]; -const ENABLED_OPTIONS = [ - "parser", - "printWidth", - "tabWidth", - "useTabs", - { name: "semi", inverted: true }, - "singleQuote", - { name: "bracketSpacing", inverted: true }, - "jsxBracketSameLine", - "arrowParens", - "trailingComma", - "proseWrap", - "insertPragma", - "requirePragma" -].map(option => (typeof option === "string" ? { name: option } : option)); - class Playground extends React.Component { - constructor() { + constructor(props) { super(); - this.state = Object.assign( - { - content: "", - loaded: false, - options: null - }, - urlHash.read() - ); + const { content, options } = urlHash.read(); + + const defaultOptions = getDefaultOptions(props.availableOptions); + + this.state = { + content: content || "", + options: Object.assign(defaultOptions, options) + }; + this.handleOptionValueChange = this.handleOptionValueChange.bind(this); this.setContent = content => this.setState({ content }); this.clearContent = this.setContent.bind(this, ""); - this.resetOptions = () => - this.setState(state => ({ - options: getDefaultOptions(state.availableOptions) - })); - this.getCurrentUrl = () => window.location.href; - } - - componentDidMount() { - this.props.worker - .postMessage({ type: "meta" }) - .then(({ supportInfo, version }) => { - const availableOptions = parsePrettierOptions(supportInfo); - const options = Object.assign( - getDefaultOptions(availableOptions), - this.state.options - ); - - this.setState({ - loaded: true, - availableOptions, - options, - version: fixPrettierVersion(version) - }); - }); + this.resetOptions = () => this.setState({ options: defaultOptions }); } componentDidUpdate(_, prevState) { @@ -91,7 +52,9 @@ class Playground extends React.Component { } getMarkdown(formatted, reformatted, full) { - const { availableOptions, content, options, version } = this.state; + const { content, options } = this.state; + const { availableOptions, version } = this.props; + return formatMarkdown( content, formatted, @@ -105,116 +68,112 @@ class Playground extends React.Component { } render() { - const { availableOptions, content, loaded, options } = this.state; - - if (!loaded) return "Loading..."; + const { availableOptions } = this.props; + const { content, options } = this.state; return ( - - - - {editorState => ( - - {({ formatted, debugAst, debugDoc, reformatted }) => ( - -
- - + {editorState => ( + + {({ formatted, debugAst, debugDoc, reformatted }) => ( + +
+ + + + - - - - - -
- -
-
-
- - {editorState.showAst ? ( - - ) : null} - {editorState.showDoc ? ( - - ) : null} + + +
+ +
+ +
+ + {editorState.showAst ? ( + + ) : null} + {editorState.showDoc ? ( + + ) : null} + + {editorState.showSecondFormat ? ( - {editorState.showSecondFormat ? ( - - ) : null} -
+ ) : null}
-
-
- - -
-
- - Copy link - - this.getMarkdown(formatted, reformatted)} - > - Copy markdown - - - Report issue - -
+
+
+
+ +
- - )} - - )} - - +
+ window.location.href}> + Copy link + + this.getMarkdown(formatted, reformatted)} + > + Copy markdown + + + Report issue + +
+
+ + )} + + )} + ); } } @@ -250,14 +209,6 @@ function getSecondFormat(formatted, reformatted) { : reformatted; } -function fixPrettierVersion(version) { - const match = version.match(/^\d+\.\d+\.\d+-pr.(\d+)$/); - if (match) { - return `pr-${match[1]}`; - } - return version; -} - function getDefaultOptions(availableOptions) { return availableOptions.reduce((acc, option) => { acc[option.name] = option.default; @@ -280,28 +231,4 @@ function getCodemirrorMode(parser) { } } -function parsePrettierOptions(supportInfo) { - const supportedOptions = supportInfo.options.reduce((acc, option) => { - acc[option.name] = option; - return acc; - }, {}); - - return ENABLED_OPTIONS.reduce((optionsList, optionConfig) => { - if (!supportedOptions[optionConfig.name]) return optionsList; - - const option = Object.assign( - {}, - optionConfig, - supportedOptions[optionConfig.name] - ); - option.cliName = - "--" + - (option.inverted ? "no-" : "") + - option.name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(); - - optionsList.push(option); - return optionsList; - }, []); -} - export default Playground; diff --git a/website/playground/index.js b/website/playground/index.js index 0ad806a6a..871f387c4 100644 --- a/website/playground/index.js +++ b/website/playground/index.js @@ -4,9 +4,92 @@ import React from "react"; import ReactDOM from "react-dom"; import Playground from "./Playground"; +import VersionLink from "./VersionLink"; import WorkerApi from "./WorkerApi"; -ReactDOM.render( - , - document.getElementById("root") -); +const ENABLED_OPTIONS = [ + "parser", + "printWidth", + "tabWidth", + "useTabs", + { name: "semi", inverted: true }, + "singleQuote", + { name: "bracketSpacing", inverted: true }, + "jsxBracketSameLine", + "arrowParens", + "trailingComma", + "proseWrap", + "insertPragma", + "requirePragma" +].map(option => (typeof option === "string" ? { name: option } : option)); + +class App extends React.Component { + constructor() { + super(); + this.state = { loaded: false }; + this.worker = new WorkerApi("/worker.js"); + } + + componentDidMount() { + this.worker + .postMessage({ type: "meta" }) + .then(({ supportInfo, version }) => { + this.setState({ + loaded: true, + availableOptions: parsePrettierOptions(supportInfo), + version: fixPrettierVersion(version) + }); + }); + } + + render() { + const { loaded, availableOptions, version } = this.state; + + if (!loaded) return "Loading..."; + + return ( + + + + + ); + } +} + +ReactDOM.render(, document.getElementById("root")); + +function parsePrettierOptions(supportInfo) { + const supportedOptions = supportInfo.options.reduce((acc, option) => { + acc[option.name] = option; + return acc; + }, {}); + + return ENABLED_OPTIONS.reduce((optionsList, optionConfig) => { + if (!supportedOptions[optionConfig.name]) return optionsList; + + const option = Object.assign( + {}, + optionConfig, + supportedOptions[optionConfig.name] + ); + option.cliName = + "--" + + (option.inverted ? "no-" : "") + + option.name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(); + + optionsList.push(option); + return optionsList; + }, []); +} + +function fixPrettierVersion(version) { + const match = version.match(/^\d+\.\d+\.\d+-pr.(\d+)$/); + if (match) { + return `pr-${match[1]}`; + } + return version; +} diff --git a/website/static/separate-css/playground.css b/website/static/separate-css/playground.css index 2ae216467..31f7a690b 100644 --- a/website/static/separate-css/playground.css +++ b/website/static/separate-css/playground.css @@ -15,7 +15,6 @@ body { margin: 0; } -#root, .playground-container { height: 100%; display: flex;