prettier/website/playground/Playground.js

198 lines
6.7 KiB
JavaScript
Raw Normal View History

2018-04-12 00:22:03 +03:00
import React from "react";
2018-04-12 21:09:04 +03:00
import { Button, ClipboardButton, LinkButton } from "./buttons";
2018-04-12 00:22:03 +03:00
import EditorState from "./EditorState";
import { DebugPanel, InputPanel, OutputPanel } from "./panels";
2018-04-12 04:28:50 +03:00
import PrettierFormat from "./PrettierFormat";
2018-04-12 19:27:34 +03:00
import { shallowEqual } from "./helpers";
import * as urlHash from "./urlHash";
import formatMarkdown from "./markdown";
2018-04-19 20:26:18 +03:00
import * as util from "./util";
import getCodeSample from "./codeSamples";
2018-04-12 18:16:16 +03:00
import { Sidebar, SidebarCategory } from "./sidebar/components";
2018-04-13 00:09:37 +03:00
import SidebarOptions from "./sidebar/SidebarOptions";
2018-04-12 18:16:16 +03:00
import { Checkbox } from "./sidebar/inputs";
const CATEGORIES_ORDER = ["Global", "JavaScript", "Markdown", "Special"];
2018-04-12 00:22:03 +03:00
class Playground extends React.Component {
2018-04-17 22:42:03 +03:00
constructor(props) {
2018-04-12 00:22:03 +03:00
super();
2018-04-12 19:27:34 +03:00
2018-04-17 22:42:03 +03:00
const { content, options } = urlHash.read();
2018-04-19 20:26:18 +03:00
const defaultOptions = util.getDefaults(props.availableOptions);
2018-04-17 22:42:03 +03:00
this.state = {
content: content || "",
options: Object.assign(defaultOptions, options)
};
2018-04-12 00:22:03 +03:00
this.handleOptionValueChange = this.handleOptionValueChange.bind(this);
this.setContent = content => this.setState({ content });
2018-04-12 17:51:49 +03:00
this.clearContent = this.setContent.bind(this, "");
2018-04-17 22:42:03 +03:00
this.resetOptions = () => this.setState({ options: defaultOptions });
2018-04-12 00:22:03 +03:00
}
2018-04-12 19:27:34 +03:00
componentDidUpdate(_, prevState) {
const { content, options } = this.state;
if (
!shallowEqual(prevState.options, this.state.options) ||
prevState.content !== content
) {
urlHash.replace({ content, options });
}
}
2018-04-12 00:22:03 +03:00
handleOptionValueChange(option, value) {
this.setState(state => ({
options: Object.assign({}, state.options, { [option.name]: value })
}));
}
2018-04-17 19:40:55 +03:00
getMarkdown(formatted, reformatted, full) {
2018-04-17 22:42:03 +03:00
const { content, options } = this.state;
const { availableOptions, version } = this.props;
2018-04-17 19:40:55 +03:00
return formatMarkdown(
content,
formatted,
reformatted || "",
version,
window.location.href,
options,
2018-04-19 20:26:18 +03:00
util.buildCliArgs(availableOptions, options),
2018-04-17 19:40:55 +03:00
full
);
}
2018-04-12 00:22:03 +03:00
render() {
2018-04-17 23:29:44 +03:00
const { availableOptions, worker } = this.props;
2018-04-17 22:42:03 +03:00
const { content, options } = this.state;
2018-04-12 00:22:03 +03:00
return (
2018-04-17 22:42:03 +03:00
<EditorState>
{editorState => (
<PrettierFormat
2018-04-17 23:29:44 +03:00
worker={worker}
2018-04-19 20:26:18 +03:00
code={content || getCodeSample(options.parser)}
2018-04-17 22:42:03 +03:00
options={options}
debugAst={editorState.showAst}
debugDoc={editorState.showDoc}
secondFormat={editorState.showSecondFormat}
>
{({ formatted, debugAst, debugDoc, reformatted }) => (
<React.Fragment>
<div className="editors-container">
<Sidebar visible={editorState.showSidebar}>
<SidebarOptions
categories={CATEGORIES_ORDER}
availableOptions={availableOptions}
optionValues={options}
onOptionValueChange={this.handleOptionValueChange}
/>
<SidebarCategory title="Debug">
<Checkbox
label="show AST"
checked={editorState.showAst}
onChange={editorState.toggleAst}
2018-04-17 16:28:35 +03:00
/>
2018-04-17 22:42:03 +03:00
<Checkbox
label="show doc"
checked={editorState.showDoc}
onChange={editorState.toggleDoc}
/>
<Checkbox
label="show second format"
checked={editorState.showSecondFormat}
onChange={editorState.toggleSecondFormat}
2018-04-12 21:09:04 +03:00
/>
2018-04-17 22:42:03 +03:00
</SidebarCategory>
<div className="sub-options">
<Button onClick={this.resetOptions}>
Reset to defaults
</Button>
</div>
</Sidebar>
<div className="editors">
<InputPanel
2018-04-19 20:26:18 +03:00
mode={util.getCodemirrorMode(options.parser)}
2018-04-17 22:42:03 +03:00
rulerColumn={options.printWidth}
value={content}
2018-04-19 20:26:18 +03:00
placeholder={getCodeSample(options.parser)}
2018-04-17 22:42:03 +03:00
onChange={this.setContent}
/>
{editorState.showAst ? (
<DebugPanel value={debugAst || ""} />
) : null}
{editorState.showDoc ? (
<DebugPanel value={debugDoc || ""} />
) : null}
<OutputPanel
2018-04-19 20:26:18 +03:00
mode={util.getCodemirrorMode(options.parser)}
2018-04-17 22:42:03 +03:00
value={formatted}
rulerColumn={options.printWidth}
/>
{editorState.showSecondFormat ? (
2018-04-12 21:09:04 +03:00
<OutputPanel
2018-04-19 20:26:18 +03:00
mode={util.getCodemirrorMode(options.parser)}
2018-04-17 22:42:03 +03:00
value={getSecondFormat(formatted, reformatted)}
2018-04-12 21:09:04 +03:00
rulerColumn={options.printWidth}
/>
2018-04-17 22:42:03 +03:00
) : null}
2018-04-17 16:28:35 +03:00
</div>
2018-04-17 22:42:03 +03:00
</div>
<div className="bottom-bar">
<div className="bottom-bar-buttons">
<Button onClick={editorState.toggleSidebar}>
{editorState.showSidebar ? "Hide" : "Show"} options
</Button>
<Button onClick={this.clearContent}>Clear</Button>
2018-04-17 16:28:35 +03:00
</div>
2018-04-17 22:42:03 +03:00
<div className="bottom-bar-buttons bottom-bar-buttons-right">
2018-04-17 23:29:44 +03:00
<ClipboardButton copy={window.location.href}>
2018-04-17 22:42:03 +03:00
Copy link
</ClipboardButton>
<ClipboardButton
copy={() => this.getMarkdown(formatted, reformatted)}
>
Copy markdown
</ClipboardButton>
<LinkButton
href={getReportLink(
this.getMarkdown(formatted, reformatted, true)
)}
target="_blank"
rel="noopener"
>
Report issue
</LinkButton>
</div>
</div>
</React.Fragment>
)}
</PrettierFormat>
)}
</EditorState>
2018-04-12 00:22:03 +03:00
);
}
}
2018-04-17 19:40:55 +03:00
function getReportLink(reportBody) {
return `https://github.com/prettier/prettier/issues/new?body=${encodeURIComponent(
reportBody
)}`;
}
function getSecondFormat(formatted, reformatted) {
return formatted === ""
? ""
: formatted === reformatted
? "✓ Second format is unchanged."
: reformatted;
}
2018-04-12 00:22:03 +03:00
export default Playground;