prettier/website/playground/Playground.js

309 lines
10 KiB
JavaScript
Raw Normal View History

2018-04-12 00:22:03 +03:00
import React from "react";
import { Button, ClipboardButton } 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-20 17:13:09 +03:00
import Option from "./sidebar/options";
2018-04-12 18:16:16 +03:00
import { Checkbox } from "./sidebar/inputs";
const CATEGORIES_ORDER = [
"Global",
"Common",
"JavaScript",
"Markdown",
"HTML",
"Special"
];
2018-04-20 17:13:09 +03:00
const ENABLED_OPTIONS = [
"parser",
"printWidth",
"tabWidth",
"useTabs",
"semi",
"singleQuote",
"bracketSpacing",
"jsxSingleQuote",
2018-04-20 17:13:09 +03:00
"jsxBracketSameLine",
"quoteProps",
2018-04-20 17:13:09 +03:00
"arrowParens",
"trailingComma",
"proseWrap",
"htmlWhitespaceSensitivity",
2018-04-20 17:13:09 +03:00
"insertPragma",
"requirePragma",
"vueIndentScriptAndStyle"
2018-04-20 17:13:09 +03:00
];
const ISSUES_URL = "https://github.com/prettier/prettier/issues/new?body=";
const MAX_LENGTH = 8000 - ISSUES_URL.length; // it seems that GitHub limit is 8195
const COPY_MESSAGE =
"<!-- The issue body has been saved to the clipboard. Please paste it after this line! 👇 -->\n";
2018-04-12 18:16:16 +03:00
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
const original = urlHash.read();
2018-04-17 22:42:03 +03:00
2018-04-20 17:13:09 +03:00
const defaultOptions = util.getDefaults(
props.availableOptions,
ENABLED_OPTIONS
);
2018-04-17 22:42:03 +03:00
const options = Object.assign(defaultOptions, original.options);
const content = original.content || getCodeSample(options.parser);
this.state = { content, options };
2018-04-17 22:42:03 +03:00
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-20 17:13:09 +03:00
2018-04-20 17:50:36 +03:00
this.enabledOptions = orderOptions(props.availableOptions, ENABLED_OPTIONS);
2018-04-20 17:13:09 +03:00
this.rangeStartOption = props.availableOptions.find(
opt => opt.name === "rangeStart"
);
this.rangeEndOption = props.availableOptions.find(
opt => opt.name === "rangeEnd"
);
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) {
2018-04-19 20:46:29 +03:00
this.setState(state => {
2018-04-20 17:13:09 +03:00
const options = Object.assign({}, state.options);
2018-04-20 17:13:09 +03:00
if (option.type === "int" && isNaN(value)) {
delete options[option.name];
} else {
options[option.name] = value;
}
const content =
state.content === "" ||
state.content === getCodeSample(state.options.parser)
? getCodeSample(options.parser)
: state.content;
return { options, content };
2018-04-19 20:46:29 +03:00
});
2018-04-12 00:22:03 +03:00
}
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,
2018-04-17 19:40:55 +03:00
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-20 17:50:36 +03:00
const { 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}
code={content}
2018-04-17 22:42:03 +03:00
options={options}
debugAst={editorState.showAst}
debugDoc={editorState.showDoc}
2018-04-20 15:54:15 +03:00
reformat={editorState.showSecondFormat}
2018-04-17 22:42:03 +03:00
>
{({ formatted, debug }) => {
const fullReport = this.getMarkdown(
formatted,
debug.reformatted,
true
);
const showFullReport =
encodeURIComponent(fullReport).length < MAX_LENGTH;
return (
<React.Fragment>
<div className="editors-container">
<Sidebar visible={editorState.showSidebar}>
<SidebarOptions
categories={CATEGORIES_ORDER}
availableOptions={this.enabledOptions}
optionValues={options}
onOptionValueChange={this.handleOptionValueChange}
2018-04-20 17:13:09 +03:00
/>
<SidebarCategory title="Range">
<label>
The selected range will be highlighted in yellow in
the input editor
</label>
<Option
option={this.rangeStartOption}
value={options.rangeStart}
onChange={this.handleOptionValueChange}
/>
<Option
option={this.rangeEndOption}
value={options.rangeEnd}
overrideMax={content.length}
onChange={this.handleOptionValueChange}
/>
</SidebarCategory>
<SidebarCategory title="Debug">
<Checkbox
label="show AST"
checked={editorState.showAst}
onChange={editorState.toggleAst}
/>
<Checkbox
label="show doc"
checked={editorState.showDoc}
onChange={editorState.toggleDoc}
/>
<Checkbox
label="show second format"
checked={editorState.showSecondFormat}
onChange={editorState.toggleSecondFormat}
/>
</SidebarCategory>
<div className="sub-options">
<Button onClick={this.resetOptions}>
Reset to defaults
</Button>
</div>
</Sidebar>
<div className="editors">
<InputPanel
mode={util.getCodemirrorMode(options.parser)}
ruler={options.printWidth}
value={content}
codeSample={getCodeSample(options.parser)}
overlayStart={options.rangeStart}
overlayEnd={options.rangeEnd}
onChange={this.setContent}
2018-04-12 21:09:04 +03:00
/>
{editorState.showAst ? (
<DebugPanel value={debug.ast || ""} />
) : null}
{editorState.showDoc ? (
<DebugPanel value={debug.doc || ""} />
) : null}
2018-04-12 21:09:04 +03:00
<OutputPanel
2018-04-19 20:26:18 +03:00
mode={util.getCodemirrorMode(options.parser)}
value={formatted}
2018-04-20 00:57:11 +03:00
ruler={options.printWidth}
2018-04-12 21:09:04 +03:00
/>
{editorState.showSecondFormat ? (
<OutputPanel
mode={util.getCodemirrorMode(options.parser)}
value={getSecondFormat(formatted, debug.reformatted)}
ruler={options.printWidth}
/>
) : null}
</div>
2018-04-17 16:28:35 +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>
<ClipboardButton
copy={JSON.stringify(
// Remove `parser` since people usually paste this
// into their .prettierrc and specifying a toplevel
// parser there is an anti-pattern. Note:
// `JSON.stringify` omits keys whose values are
// `undefined`.
Object.assign({}, options, { parser: undefined }),
null,
2
)}
>
Copy config JSON
</ClipboardButton>
</div>
<div className="bottom-bar-buttons bottom-bar-buttons-right">
<ClipboardButton copy={window.location.href}>
Copy link
</ClipboardButton>
<ClipboardButton
copy={() =>
this.getMarkdown(formatted, debug.reformatted)
}
>
Copy markdown
</ClipboardButton>
<a
href={getReportLink(
showFullReport ? fullReport : COPY_MESSAGE
)}
target="_blank"
rel="noopener noreferrer"
>
<ClipboardButton
copy={() => (showFullReport ? "" : fullReport)}
>
Report issue
</ClipboardButton>
</a>
</div>
2018-04-17 22:42:03 +03:00
</div>
</React.Fragment>
);
}}
2018-04-17 22:42:03 +03:00
</PrettierFormat>
)}
</EditorState>
2018-04-12 00:22:03 +03:00
);
}
}
2018-04-20 17:50:36 +03:00
function orderOptions(availableOptions, order) {
const optionsByName = {};
for (const option of availableOptions) {
optionsByName[option.name] = option;
}
return order.map(name => optionsByName[name]);
}
2018-04-17 19:40:55 +03:00
function getReportLink(reportBody) {
return `${ISSUES_URL}${encodeURIComponent(reportBody)}`;
2018-04-17 19:40:55 +03:00
}
function getSecondFormat(formatted, reformatted) {
return formatted === ""
? ""
: formatted === reformatted
2018-11-07 04:12:25 +03:00
? "✓ Second format is unchanged."
: reformatted;
2018-04-17 19:40:55 +03:00
}
2018-04-12 00:22:03 +03:00
export default Playground;