prettier/website/playground/index.js

62 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-04-12 03:01:36 +03:00
import "codemirror-graphql/mode";
2018-04-12 00:22:03 +03:00
import React from "react";
import ReactDOM from "react-dom";
2018-04-12 00:22:03 +03:00
import Playground from "./Playground";
2018-04-17 22:42:03 +03:00
import VersionLink from "./VersionLink";
2018-04-17 22:09:37 +03:00
import WorkerApi from "./WorkerApi";
2018-04-20 17:13:09 +03:00
import { fixPrettierVersion } from "./util";
2018-04-17 22:42:03 +03:00
class App extends React.Component {
constructor() {
super();
this.state = { loaded: false };
this.worker = new WorkerApi("/worker.js");
}
componentDidMount() {
2018-04-19 20:46:29 +03:00
this.worker.getMetadata().then(({ supportInfo, version }) => {
this.setState({
loaded: true,
2018-04-20 17:13:09 +03:00
availableOptions: supportInfo.options.map(augmentOption),
2018-04-19 20:46:29 +03:00
version: fixPrettierVersion(version)
2018-04-17 22:42:03 +03:00
});
2018-04-19 20:46:29 +03:00
});
2018-04-17 22:42:03 +03:00
}
render() {
const { loaded, availableOptions, version } = this.state;
2018-04-17 23:29:44 +03:00
if (!loaded) {
return "Loading...";
}
2018-04-17 22:42:03 +03:00
return (
<React.Fragment>
<VersionLink version={version} />
<Playground
worker={this.worker}
availableOptions={availableOptions}
version={version}
/>
</React.Fragment>
);
}
}
2018-04-20 17:13:09 +03:00
function augmentOption(option) {
2018-04-20 17:50:36 +03:00
if (option.type === "boolean" && option.default === true) {
option.inverted = true;
}
2018-04-20 17:13:09 +03:00
option.cliName =
"--" +
(option.inverted ? "no-" : "") +
option.name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
return option;
}
2018-04-17 22:42:03 +03:00
ReactDOM.render(<App />, document.getElementById("root"));