Website: usability improvements (#2118)
* feat(website): use worker thread for formatting * Create index.html Remove step=5master
parent
e69d0fe5fb
commit
40c41f232f
|
@ -1,5 +1,5 @@
|
|||
!/*.js
|
||||
/docs/
|
||||
/docs/lib/
|
||||
/tests/**/*.js
|
||||
!/tests/**/jsfmt.spec.js
|
||||
!/**/.eslintrc.js
|
||||
|
|
530
docs/index.html
530
docs/index.html
|
@ -1,150 +1,178 @@
|
|||
<meta charset="utf-8">
|
||||
<title>Prettier</title>
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Prettier</title>
|
||||
|
||||
<script>
|
||||
// "Polyfills" in order for all the code to run
|
||||
global = window;
|
||||
Buffer = {isBuffer: function() { return false; }};
|
||||
fs = module$1 = module = path = os = crypto = {};
|
||||
process = {argv: [], env: {}}
|
||||
assert = {ok: function() {}, strictEqual: function() {}};
|
||||
function require(path) { return window[path.replace(/.+-/, '')]; }
|
||||
</script>
|
||||
<!-- CodeMirror -->
|
||||
<link rel="stylesheet" href="https://codemirror.net/lib/codemirror.css">
|
||||
<link rel="stylesheet" href="https://codemirror.net/theme/neat.css">
|
||||
<script src="https://codemirror.net/lib/codemirror.js"></script>
|
||||
<script src="https://codemirror.net/mode/javascript/javascript.js"></script>
|
||||
<script src="https://codemirror.net/addon/display/rulers.js"></script>
|
||||
<script src="https://codemirror.net/addon/search/searchcursor.js"></script>
|
||||
<script src="https://codemirror.net/addon/edit/matchbrackets.js"></script>
|
||||
<script src="https://codemirror.net/addon/edit/closebrackets.js"></script>
|
||||
<script src="https://codemirror.net/addon/comment/comment.js"></script>
|
||||
<script src="https://codemirror.net/addon/wrap/hardwrap.js"></script>
|
||||
<script src="https://codemirror.net/keymap/sublime.js"></script>
|
||||
|
||||
<script src="index.js"></script>
|
||||
<script>prettier = index;</script>
|
||||
<style type="text/css">
|
||||
html,
|
||||
body {
|
||||
font-family: "Helvetica Neue", "Open Sans", sans-serif;
|
||||
font-size: 11.7px;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: #FAFAFA;
|
||||
color: #6A6A6A;
|
||||
}
|
||||
|
||||
<link rel="stylesheet" href="https://codemirror.net/lib/codemirror.css">
|
||||
<link rel="stylesheet" href="https://codemirror.net/theme/neat.css">
|
||||
<script src="https://codemirror.net/lib/codemirror.js"></script>
|
||||
<script src="https://codemirror.net/mode/javascript/javascript.js"></script>
|
||||
<script src="https://codemirror.net/addon/display/rulers.js"></script>
|
||||
<script src="https://codemirror.net/addon/search/searchcursor.js"></script>
|
||||
<script src="https://codemirror.net/addon/edit/matchbrackets.js"></script>
|
||||
<script src="https://codemirror.net/addon/edit/closebrackets.js"></script>
|
||||
<script src="https://codemirror.net/addon/comment/comment.js"></script>
|
||||
<script src="https://codemirror.net/addon/wrap/hardwrap.js"></script>
|
||||
<script src="https://codemirror.net/keymap/sublime.js"></script>
|
||||
body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
<style type="text/css">
|
||||
html, body {
|
||||
font-family: "Helvetica Neue", "Open Sans", sans-serif;
|
||||
font-size: 11.7px;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: #FAFAFA;
|
||||
color: #6A6A6A;
|
||||
}
|
||||
.options-summary {
|
||||
font-size: 1.2em;
|
||||
margin-top: 8px;
|
||||
text-align: center;
|
||||
}
|
||||
header {
|
||||
height: 64px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
background-color: #1A2B34;
|
||||
color: #e0e0e0;
|
||||
}
|
||||
@media (max-width: 500px) {
|
||||
.tagline { display: none; }
|
||||
}
|
||||
header a, header a:visited {
|
||||
color: #e0e0e0;
|
||||
}
|
||||
header a:focus, header a:active, header a:hover {
|
||||
color: #70a0a0;
|
||||
}
|
||||
.logo {
|
||||
height: 100px;
|
||||
}
|
||||
.tagline {
|
||||
flex: 2;
|
||||
font-weight: 200;
|
||||
}
|
||||
.links {
|
||||
font-size: 1.3em;
|
||||
margin-right: 30px;
|
||||
}
|
||||
.editors-container {
|
||||
display: flex;
|
||||
height: calc(100vh - 120px);
|
||||
}
|
||||
.editors {
|
||||
display: flex;
|
||||
flex-flow: row wrap;
|
||||
flex: 1;
|
||||
}
|
||||
.editor {
|
||||
min-width: 300px;
|
||||
display: flex;
|
||||
flex: 1;
|
||||
position: relative;
|
||||
}
|
||||
.arrow {
|
||||
align-self: center;
|
||||
font-size: 1.2em;
|
||||
}
|
||||
.CodeMirror {
|
||||
height: auto;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
font-family: Menlo, monospace;
|
||||
font-size: 11.05px;
|
||||
line-height: 17.68px;
|
||||
}
|
||||
.options-container {
|
||||
display: flex;
|
||||
flex-flow: row wrap;
|
||||
justify-content: space-around;
|
||||
}
|
||||
.options-summary {
|
||||
font-size: 1.2em;
|
||||
margin-top: 8px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.options {
|
||||
padding: 0 10px;
|
||||
display: flex;
|
||||
flex-flow: column wrap;
|
||||
flex-grow: 1;
|
||||
justify-content: space-around;
|
||||
margin: 5px;
|
||||
margin-bottom: 0;
|
||||
min-width: 150px;
|
||||
}
|
||||
header {
|
||||
height: 64px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
background-color: #1A2B34;
|
||||
color: #e0e0e0;
|
||||
}
|
||||
|
||||
label {
|
||||
font-family: "Consolas", "Courier New", Courier, monospace;
|
||||
margin: 0 10px;
|
||||
}
|
||||
input[type="number"] {
|
||||
max-width: 40px;
|
||||
}
|
||||
footer {
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
@media (max-width: 500px) {
|
||||
.tagline {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
<header>
|
||||
<a href=".">
|
||||
<img class="logo" src="https://cloud.githubusercontent.com/assets/311752/24730511/e6690c38-1a18-11e7-95a9-2af91bb60440.png">
|
||||
</a>
|
||||
<h2 class="tagline"><em>Opinionated</em> code formatter</h2>
|
||||
<span class="links">
|
||||
<a
|
||||
class="github-button"
|
||||
href="https://github.com/prettier/prettier"
|
||||
data-show-count="true"
|
||||
aria-label="Star prettier on GitHub">
|
||||
GitHub
|
||||
header a,
|
||||
header a:visited {
|
||||
color: #e0e0e0;
|
||||
}
|
||||
|
||||
header a:focus,
|
||||
header a:active,
|
||||
header a:hover {
|
||||
color: #70a0a0;
|
||||
}
|
||||
|
||||
.logo {
|
||||
height: 100px;
|
||||
}
|
||||
|
||||
.tagline {
|
||||
flex: 2;
|
||||
font-weight: 200;
|
||||
}
|
||||
|
||||
.links {
|
||||
font-size: 1.3em;
|
||||
margin-right: 30px;
|
||||
}
|
||||
|
||||
.editors-container {
|
||||
display: flex;
|
||||
flex-grow: 1;
|
||||
}
|
||||
|
||||
.editors {
|
||||
display: flex;
|
||||
flex-flow: row wrap;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.editor {
|
||||
min-width: 400px;
|
||||
display: flex;
|
||||
flex: 1;
|
||||
position: relative;
|
||||
border-top: 1px solid #1A2B34;
|
||||
}
|
||||
|
||||
.arrow {
|
||||
align-self: center;
|
||||
font-size: 1.2em;
|
||||
}
|
||||
|
||||
.CodeMirror {
|
||||
height: auto;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
font-family: Menlo, monospace;
|
||||
font-size: 11.05px;
|
||||
line-height: 17.68px;
|
||||
}
|
||||
|
||||
.options-container {
|
||||
display: flex;
|
||||
flex-flow: row wrap;
|
||||
justify-content: space-around;
|
||||
}
|
||||
|
||||
.options {
|
||||
padding: 0 10px;
|
||||
display: flex;
|
||||
flex-flow: column wrap;
|
||||
flex-grow: 1;
|
||||
justify-content: space-around;
|
||||
margin: 5px;
|
||||
margin-bottom: 0;
|
||||
min-width: 150px;
|
||||
}
|
||||
|
||||
label {
|
||||
font-family: "Consolas", "Courier New", Courier, monospace;
|
||||
margin: 0 10px;
|
||||
}
|
||||
|
||||
@media (max-width: 400px) {
|
||||
label {
|
||||
padding: 8px;
|
||||
}
|
||||
}
|
||||
|
||||
input[type="number"] {
|
||||
max-width: 40px;
|
||||
}
|
||||
|
||||
footer {
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<a href=".">
|
||||
<img class="logo" src="https://cloud.githubusercontent.com/assets/311752/24730511/e6690c38-1a18-11e7-95a9-2af91bb60440.png">
|
||||
</a>
|
||||
</span>
|
||||
</header>
|
||||
<h2 class="tagline"><em>Opinionated</em> code formatter</h2>
|
||||
<span class="links">
|
||||
<a
|
||||
class="github-button"
|
||||
href="https://github.com/prettier/prettier"
|
||||
data-show-count="true"
|
||||
aria-label="Star prettier on GitHub">
|
||||
GitHub
|
||||
</a>
|
||||
</span>
|
||||
</header>
|
||||
|
||||
<section>
|
||||
<div class="editors-container">
|
||||
<section class="editors-container">
|
||||
<div class="editors">
|
||||
<div class="editor input">
|
||||
<textarea id="input-editor"></textarea>
|
||||
|
@ -152,201 +180,51 @@ function require(path) { return window[path.replace(/.+-/, '')]; }
|
|||
<div class="editor doc">
|
||||
<textarea id="doc-editor"></textarea>
|
||||
</div>
|
||||
<span class="arrow">→</span>
|
||||
<div class="editor output">
|
||||
<textarea id="output-editor"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<details>
|
||||
<summary class="options-summary">Options</summary>
|
||||
<section>
|
||||
<details>
|
||||
<summary class="options-summary">Options</summary>
|
||||
|
||||
<div class="options-container">
|
||||
<div class="options">
|
||||
<label>--print-width <input type="number" value="80" id="printWidth"></input> </label>
|
||||
<label>--tab-width <input type="number" value="2" id="tabWidth"></input> </label>
|
||||
<div class="options-container">
|
||||
<div class="options">
|
||||
<label>--print-width <input type="number" value="80" min="0" id="printWidth"></input> </label>
|
||||
<label>--tab-width <input type="number" min="0" value="2" id="tabWidth"></input> </label>
|
||||
</div>
|
||||
<div class="options">
|
||||
<label><input type="checkbox" id="useTabs"></input> --use-tabs</label>
|
||||
<label><input type="checkbox" data-inverted id="semi"></input> --no-semi</label>
|
||||
<label><input type="checkbox" id="singleQuote"></input> --single-quote</label>
|
||||
<label><input type="checkbox" data-inverted id="bracketSpacing"></input> --no-bracket-spacing</label>
|
||||
<label><input type="checkbox" id="jsxBracketSameLine"></input> --jsx-bracket-same-line</label>
|
||||
</div>
|
||||
<div class="options">
|
||||
<label>--trailing-comma <select id="trailingComma"><option value="none">none</option><option value="es5">es5</option><option value="all">all</option></select></label>
|
||||
<label>--parser <select id="parser"><option value="babylon">babylon</option><option value="flow">flow</option><option value="typescript">typescript</option><option value="postcss">postcss</option></select></label>
|
||||
<span style="flex: 0.3"></span>
|
||||
<label><input type="checkbox" id="doc"></input> show doc (debug)</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="options">
|
||||
<label><input type="checkbox" id="useTabs"></input> --use-tabs</label>
|
||||
<label><input type="checkbox" data-inverted id="semi"></input> --no-semi</label>
|
||||
<label><input type="checkbox" id="singleQuote"></input> --single-quote</label>
|
||||
<label><input type="checkbox" data-inverted id="bracketSpacing"></input> --no-bracket-spacing</label>
|
||||
<label><input type="checkbox" id="jsxBracketSameLine"></input> --jsx-bracket-same-line</label>
|
||||
</div>
|
||||
<div class="options">
|
||||
<label>--trailing-comma <select id="trailingComma"><option value="none">none</option><option value="es5">es5</option><option value="all">all</option></select></label>
|
||||
<label>--parser <select id="parser"><option value="babylon">babylon</option><option value="flow">flow</option><option value="typescript">typescript</option><option value="postcss">postcss</option></select></label>
|
||||
<span style="flex: 0.3"></span>
|
||||
<label><input type="checkbox" id="doc"></input> show doc (debug)</label>
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
</section>
|
||||
</details>
|
||||
</section>
|
||||
|
||||
<footer>
|
||||
<p class="version-link">
|
||||
<a href="https://github.com/prettier/prettier">
|
||||
prettier version
|
||||
<span class="version"></span>
|
||||
(master)
|
||||
</a>
|
||||
</p>
|
||||
</footer>
|
||||
<footer>
|
||||
<p class="version-link">
|
||||
<a href="https://github.com/prettier/prettier">
|
||||
prettier version
|
||||
<span class="version"></span>
|
||||
(master)
|
||||
</a>
|
||||
</p>
|
||||
</footer>
|
||||
|
||||
<script id="code">
|
||||
(function () {
|
||||
var OPTIONS = ['printWidth', 'tabWidth', 'singleQuote', 'trailingComma', 'bracketSpacing', 'jsxBracketSameLine', 'parser', 'semi', 'useTabs', 'doc'];
|
||||
function setOptions(options) {
|
||||
OPTIONS.forEach(function(option) {
|
||||
var elem = document.getElementById(option);
|
||||
if (elem.tagName === 'SELECT') {
|
||||
elem.value = options[option];
|
||||
} else if (elem.type === 'number') {
|
||||
elem.value = options[option];
|
||||
} else {
|
||||
var isInverted = elem.hasAttribute("data-inverted");
|
||||
elem.checked = isInverted ? !options[option] : options[option];
|
||||
}
|
||||
});
|
||||
}
|
||||
<script src="lib/prettier-version.js"></script>
|
||||
<script src="playground.js"></script>
|
||||
|
||||
function getOptions() {
|
||||
var options = {};
|
||||
OPTIONS.forEach(function(option) {
|
||||
var elem = document.getElementById(option);
|
||||
if (elem.tagName === 'SELECT') {
|
||||
options[option] = elem.value;
|
||||
} else if (elem.type === 'number') {
|
||||
options[option] = Number(elem.value);
|
||||
} else {
|
||||
var isInverted = elem.hasAttribute("data-inverted");
|
||||
options[option] = isInverted ? !elem.checked : elem.checked;
|
||||
}
|
||||
});
|
||||
return options;
|
||||
}
|
||||
|
||||
function omitNonFormatterOptions(options) {
|
||||
var optionsClone = Object.assign({}, options);
|
||||
delete optionsClone.doc;
|
||||
return optionsClone;
|
||||
}
|
||||
|
||||
function replaceHash(hash) {
|
||||
if (
|
||||
typeof URL === "function" &&
|
||||
typeof history === "object" &&
|
||||
typeof history.replaceState === "function"
|
||||
) {
|
||||
var url = new URL(location);
|
||||
url.hash = hash;
|
||||
history.replaceState(null, null, url);
|
||||
} else {
|
||||
location.hash = hash;
|
||||
}
|
||||
}
|
||||
|
||||
function onceParserLoaded(parser, cb) {
|
||||
if (window[parser]) {
|
||||
cb();
|
||||
return;
|
||||
}
|
||||
|
||||
// We can't synchronously insert a script, so we first make sure that the
|
||||
// parser is loaded before calling format that will require it
|
||||
var s = document.createElement('script');
|
||||
s.src = 'parser-' + parser + '.js';
|
||||
s.type = "text/javascript";
|
||||
s.onload = cb;
|
||||
document.getElementsByTagName('head')[0].appendChild(s);
|
||||
}
|
||||
|
||||
function format() {
|
||||
var options = getOptions();
|
||||
onceParserLoaded(options.parser || 'babylon', function() {
|
||||
[docEditor, outputEditor].forEach(function(editor) {
|
||||
editor.setOption(
|
||||
'rulers',
|
||||
[{column: options.printWidth, color: '#444444'}]
|
||||
);
|
||||
});
|
||||
document.getElementsByClassName('doc')[0].style.display = options.doc ? 'flex' : 'none';
|
||||
|
||||
var value = encodeURIComponent(
|
||||
JSON.stringify(
|
||||
Object.assign({content: inputEditor.getValue(), options: options})
|
||||
)
|
||||
);
|
||||
replaceHash(value);
|
||||
var formatterOptions = omitNonFormatterOptions(options);
|
||||
var res;
|
||||
try {
|
||||
res = prettier.format(inputEditor.getValue(), formatterOptions);
|
||||
} catch (e) {
|
||||
res = e.toString();
|
||||
}
|
||||
outputEditor.setValue(res);
|
||||
|
||||
if (options.doc) {
|
||||
var debug;
|
||||
try {
|
||||
var doc = prettier.__debug.printToDoc(inputEditor.getValue(), formatterOptions);
|
||||
debug = prettier.__debug.formatDoc(doc, formatterOptions);
|
||||
} catch (e) {
|
||||
debug = e.toString();
|
||||
}
|
||||
docEditor.setValue(debug);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
document.querySelector('.options-container').onchange = format;
|
||||
|
||||
var editorOptions = {
|
||||
lineNumbers: true,
|
||||
keyMap: "sublime",
|
||||
autoCloseBrackets: true,
|
||||
matchBrackets: true,
|
||||
showCursorWhenSelecting: true,
|
||||
theme: 'neat',
|
||||
tabWidth: 2
|
||||
};
|
||||
var inputEditor = CodeMirror.fromTextArea(
|
||||
document.getElementById('input-editor'),
|
||||
editorOptions
|
||||
);
|
||||
inputEditor.on('change', format);
|
||||
|
||||
var docEditor = CodeMirror.fromTextArea(
|
||||
document.getElementById('doc-editor'),
|
||||
{readOnly: true, lineNumbers: false, theme: 'neat'}
|
||||
);
|
||||
|
||||
var outputEditor = CodeMirror.fromTextArea(
|
||||
document.getElementById('output-editor'),
|
||||
{readOnly: true, lineNumbers: true, theme: 'neat'}
|
||||
);
|
||||
|
||||
document.getElementsByClassName('version')[0].innerText = prettier.version;
|
||||
|
||||
try {
|
||||
var json = JSON.parse(decodeURIComponent(location.hash.slice(1)));
|
||||
setOptions(json.options);
|
||||
inputEditor.setValue(json.content);
|
||||
} catch (e) {
|
||||
inputEditor.setValue(
|
||||
'hello ( "world"\n);\n\n' +
|
||||
'[ "lorem", "ipsum", \'dolor\', sit("amet"), consectetur[ \'adipiscing\' ] + "elit" ].reduce(\n (first, second) => first + second,\n "")\n\n' +
|
||||
'const Foo = ({ bar, baz, things }) => {\n' +
|
||||
' return <div style={{\ncolor: "papayawhip"}}>\n' +
|
||||
' <br/>{things.map(thing => reallyLongPleaseDontPutOnOneLine(thing) ? <p>{ok}</p> : <Quax bar={bar} baz={ baz } {...thing}></Quax>)\n' +
|
||||
' }</div>}'
|
||||
);
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
|
||||
<script async defer src="https://buttons.github.io/buttons.js"></script>
|
||||
<script async defer src="https://buttons.github.io/buttons.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -21,11 +21,11 @@ var babylon = function () {
|
|||
var e = index,
|
||||
s = { sourceType: "module", allowImportExportEverywhere: !1, allowReturnOutsideFunction: !0, plugins: ["jsx", "flow", "doExpressions", "objectRestSpread", "decorators", "classProperties", "exportExtensions", "asyncGenerators", "functionBind", "functionSent", "dynamicImport"] };var i = void 0;try {
|
||||
i = e.parse(t, s);
|
||||
} catch (i) {
|
||||
} catch (r) {
|
||||
try {
|
||||
return e.parse(t, Object.assign({}, s, { strictMode: !1 }));
|
||||
i = e.parse(t, Object.assign({}, s, { strictMode: !1 }));
|
||||
} catch (t) {
|
||||
throw createError(i.message.replace(/ \(.*\)/, ""), { start: { line: i.loc.line, column: i.loc.column + 1 } });
|
||||
throw createError(r.message.replace(/ \(.*\)/, ""), { start: { line: r.loc.line, column: r.loc.column + 1 } });
|
||||
}
|
||||
}return delete i.tokens, i;
|
||||
}var parserCreateError = createError$1,
|
File diff suppressed because one or more lines are too long
|
@ -232,9 +232,9 @@ var process = {
|
|||
uptime: uptime
|
||||
};
|
||||
|
||||
var __dirname = '/Users/vjeux/random/prettier/dist';
|
||||
var __dirname = '/Users/azz/code/prettier/dist';
|
||||
|
||||
var __filename = '/Users/vjeux/random/prettier/dist/parser-typescript.js';
|
||||
var __filename = '/Users/azz/code/prettier/dist/parser-typescript.js';
|
||||
|
||||
var browser$1 = true;
|
||||
|
|
@ -0,0 +1 @@
|
|||
prettierVersion = "1.4.4";
|
|
@ -0,0 +1,143 @@
|
|||
/* eslint-env browser */
|
||||
/* eslint no-var: off, strict: off, prefer-arrow-callback: off */
|
||||
/* global CodeMirror prettierVersion */
|
||||
|
||||
(function() {
|
||||
var OPTIONS = [
|
||||
"printWidth",
|
||||
"tabWidth",
|
||||
"singleQuote",
|
||||
"trailingComma",
|
||||
"bracketSpacing",
|
||||
"jsxBracketSameLine",
|
||||
"parser",
|
||||
"semi",
|
||||
"useTabs",
|
||||
"doc"
|
||||
];
|
||||
function setOptions(options) {
|
||||
OPTIONS.forEach(function(option) {
|
||||
var elem = document.getElementById(option);
|
||||
if (elem.tagName === "SELECT") {
|
||||
elem.value = options[option];
|
||||
} else if (elem.type === "number") {
|
||||
elem.value = options[option];
|
||||
} else {
|
||||
var isInverted = elem.hasAttribute("data-inverted");
|
||||
elem.checked = isInverted ? !options[option] : options[option];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getOptions() {
|
||||
var options = {};
|
||||
OPTIONS.forEach(function(option) {
|
||||
var elem = document.getElementById(option);
|
||||
if (elem.tagName === "SELECT") {
|
||||
options[option] = elem.value;
|
||||
} else if (elem.type === "number") {
|
||||
options[option] = Number(elem.value);
|
||||
} else {
|
||||
var isInverted = elem.hasAttribute("data-inverted");
|
||||
options[option] = isInverted ? !elem.checked : elem.checked;
|
||||
}
|
||||
});
|
||||
return options;
|
||||
}
|
||||
|
||||
function omitNonFormatterOptions(options) {
|
||||
var optionsClone = Object.assign({}, options);
|
||||
delete optionsClone.doc;
|
||||
return optionsClone;
|
||||
}
|
||||
|
||||
function replaceHash(hash) {
|
||||
if (
|
||||
typeof URL === "function" &&
|
||||
typeof history === "object" &&
|
||||
typeof history.replaceState === "function"
|
||||
) {
|
||||
var url = new URL(location);
|
||||
url.hash = hash;
|
||||
history.replaceState(null, null, url);
|
||||
} else {
|
||||
location.hash = hash;
|
||||
}
|
||||
}
|
||||
|
||||
function formatAsync() {
|
||||
var options = getOptions();
|
||||
|
||||
[docEditor, outputEditor].forEach(function(editor) {
|
||||
editor.setOption("rulers", [
|
||||
{ column: options.printWidth, color: "#444444" }
|
||||
]);
|
||||
});
|
||||
document.getElementsByClassName("doc")[0].style.display = options.doc
|
||||
? "flex"
|
||||
: "none";
|
||||
|
||||
var value = encodeURIComponent(
|
||||
JSON.stringify(
|
||||
Object.assign({ content: inputEditor.getValue(), options: options })
|
||||
)
|
||||
);
|
||||
replaceHash(value);
|
||||
var formatterOptions = omitNonFormatterOptions(options);
|
||||
worker.postMessage({
|
||||
text: inputEditor.getValue(),
|
||||
options: formatterOptions,
|
||||
doc: options.doc
|
||||
});
|
||||
}
|
||||
|
||||
document.querySelector(".options-container").onchange = formatAsync;
|
||||
|
||||
var editorOptions = {
|
||||
lineNumbers: true,
|
||||
keyMap: "sublime",
|
||||
autoCloseBrackets: true,
|
||||
matchBrackets: true,
|
||||
showCursorWhenSelecting: true,
|
||||
theme: "neat",
|
||||
tabWidth: 2
|
||||
};
|
||||
var inputEditor = CodeMirror.fromTextArea(
|
||||
document.getElementById("input-editor"),
|
||||
editorOptions
|
||||
);
|
||||
inputEditor.on("change", formatAsync);
|
||||
|
||||
var docEditor = CodeMirror.fromTextArea(
|
||||
document.getElementById("doc-editor"),
|
||||
{ readOnly: true, lineNumbers: false, theme: "neat" }
|
||||
);
|
||||
|
||||
var outputEditor = CodeMirror.fromTextArea(
|
||||
document.getElementById("output-editor"),
|
||||
{ readOnly: true, lineNumbers: true, theme: "neat" }
|
||||
);
|
||||
|
||||
var worker = new Worker("./worker.js");
|
||||
worker.onmessage = function(message) {
|
||||
outputEditor.setValue(message.data.formatted);
|
||||
docEditor.setValue(message.data.doc || "");
|
||||
};
|
||||
|
||||
document.getElementsByClassName("version")[0].innerText = prettierVersion;
|
||||
|
||||
try {
|
||||
var json = JSON.parse(decodeURIComponent(location.hash.slice(1)));
|
||||
setOptions(json.options);
|
||||
inputEditor.setValue(json.content);
|
||||
} catch (e) {
|
||||
inputEditor.setValue(
|
||||
'hello ( "world"\n);\n\n' +
|
||||
'[ "lorem", "ipsum", \'dolor\', sit("amet"), consectetur[ \'adipiscing\' ] + "elit" ].reduce(\n (first, second) => first + second,\n "")\n\n' +
|
||||
"const Foo = ({ bar, baz, things }) => {\n" +
|
||||
' return <div style={{\ncolor: "papayawhip"}}>\n' +
|
||||
" <br/>{things.map(thing => reallyLongPleaseDontPutOnOneLine(thing) ? <p>{ok}</p> : <Quax bar={bar} baz={ baz } {...thing}></Quax>)\n" +
|
||||
" }</div>}"
|
||||
);
|
||||
}
|
||||
})();
|
|
@ -0,0 +1,53 @@
|
|||
/* eslint-env worker */
|
||||
/* eslint no-var: off, strict: off */
|
||||
|
||||
// "Polyfills" in order for all the code to run
|
||||
self.global = self;
|
||||
self.Buffer = {
|
||||
isBuffer: function() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
// eslint-disable-next-line
|
||||
fs = module$1 = module = path = os = crypto = {};
|
||||
self.process = { argv: [], env: {} };
|
||||
self.assert = { ok: function() {}, strictEqual: function() {} };
|
||||
self.require = function require(path) {
|
||||
return self[path.replace(/.+-/, "")];
|
||||
};
|
||||
|
||||
importScripts("lib/index.js");
|
||||
var prettier = index; // eslint-disable-line
|
||||
|
||||
var parsersLoaded = {};
|
||||
|
||||
self.onmessage = function(message) {
|
||||
lazyLoadParser(message.data.options.parser);
|
||||
|
||||
var formatted, doc;
|
||||
try {
|
||||
formatted = prettier.format(message.data.text, message.data.options);
|
||||
} catch (e) {
|
||||
formatted = e.toString();
|
||||
}
|
||||
|
||||
if (message.data.doc) {
|
||||
try {
|
||||
doc = prettier.__debug.formatDoc(
|
||||
prettier.__debug.printToDoc(message.data.text, message.data.options),
|
||||
{}
|
||||
);
|
||||
} catch (e) {
|
||||
doc = e.toString();
|
||||
}
|
||||
}
|
||||
|
||||
self.postMessage({ formatted: formatted, doc: doc });
|
||||
};
|
||||
|
||||
function lazyLoadParser(parser) {
|
||||
if (!parsersLoaded[parser]) {
|
||||
importScripts("lib/parser-" + parser + ".js");
|
||||
parsersLoaded[parser] = true;
|
||||
}
|
||||
}
|
|
@ -3,8 +3,8 @@ set -e
|
|||
cd "$(dirname "$0")";
|
||||
cd ../..;
|
||||
|
||||
rm -Rf dist/
|
||||
rm -f docs/*.js
|
||||
rm -Rf dist/ docs/lib/
|
||||
mkdir -p docs/lib/
|
||||
|
||||
## --- Lib ---
|
||||
|
||||
|
@ -43,12 +43,12 @@ echo;
|
|||
## --- Docs ---
|
||||
|
||||
echo 'Bundling docs index...';
|
||||
cp dist/index.js docs/index.js
|
||||
node_modules/babel-cli/bin/babel.js dist/index.js --out-file docs/index.js --presets=es2015
|
||||
cp dist/index.js docs/lib/index.js
|
||||
node_modules/babel-cli/bin/babel.js dist/index.js --out-file docs/lib/index.js --presets=es2015
|
||||
|
||||
echo 'Bundling docs babylon...';
|
||||
node_modules/.bin/rollup -c scripts/build/rollup.docs.config.js --environment filepath:parser-babylon.js
|
||||
node_modules/babel-cli/bin/babel.js docs/parser-babylon.js --out-file docs/parser-babylon.js --presets=es2015
|
||||
node_modules/babel-cli/bin/babel.js docs/lib/parser-babylon.js --out-file docs/lib/parser-babylon.js --presets=es2015
|
||||
|
||||
echo 'Bundling docs flow...';
|
||||
node_modules/.bin/rollup -c scripts/build/rollup.docs.config.js --environment filepath:parser-flow.js
|
||||
|
@ -72,6 +72,9 @@ echo;
|
|||
echo 'Remove eval'
|
||||
sed -i '' -e 's/eval("require")/require/g' dist/index.js dist/bin/prettier.js
|
||||
|
||||
echo 'Create prettier-version.js'
|
||||
node -p '`prettierVersion = "${require(".").version}";`' > docs/lib/prettier-version.js
|
||||
|
||||
echo 'Copy package.json'
|
||||
cp package.json dist/
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ const basename = filename.replace(/\..+/, '');
|
|||
|
||||
export default Object.assign(baseConfig, {
|
||||
entry: 'dist/' + filepath,
|
||||
dest: 'docs/' + filename,
|
||||
dest: 'docs/lib/' + filename,
|
||||
format: 'iife',
|
||||
plugins: [json(), resolve({ preferBuiltins: true }), commonjs(), globals()],
|
||||
useStrict: false,
|
||||
|
|
Loading…
Reference in New Issue