Making sure we only try to parse options after resources has been loaded (#3085)
- Making sure we only attempt to do config parsing after window.onload since lz-string dependency needs to be available to the browser - Setting up some sane default options in case something goes wrongmaster
parent
878412c680
commit
693d1fe01b
|
@ -26,77 +26,80 @@ var OPTIONS = [
|
||||||
|
|
||||||
var IDEMPOTENT_MESSAGE = "✓ Second format is unchanged.";
|
var IDEMPOTENT_MESSAGE = "✓ Second format is unchanged.";
|
||||||
|
|
||||||
var state = (function loadState(hash) {
|
|
||||||
var parsed;
|
|
||||||
try {
|
|
||||||
// providing backwards support for old json encoded URIComponent
|
|
||||||
if (hash.indexOf("%7B%22") !== -1) {
|
|
||||||
parsed = JSON.parse(decodeURIComponent(hash));
|
|
||||||
} else {
|
|
||||||
parsed = LZString.decompressFromEncodedURIComponent(hash);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
return {
|
|
||||||
options: undefined,
|
|
||||||
content: [
|
|
||||||
'function HelloWorld({greeting = "hello", greeted = \'"World"\', silent = false, onMouseOver,}) {',
|
|
||||||
"",
|
|
||||||
" if(!greeting){return null};",
|
|
||||||
"",
|
|
||||||
" // TODO: Don't use random in render",
|
|
||||||
' let num = Math.floor (Math.random() * 1E+7).toString().replace(/\\.\\d+/ig, "")',
|
|
||||||
"",
|
|
||||||
" return <div className='HelloWorld' title={`You are visitor number ${ num }`} onMouseOver={onMouseOver}>",
|
|
||||||
"",
|
|
||||||
" <strong>{ greeting.slice( 0, 1 ).toUpperCase() + greeting.slice(1).toLowerCase() }</strong>",
|
|
||||||
' {greeting.endsWith(",") ? " " : <span style={{color: \'\\grey\'}}>", "</span> }',
|
|
||||||
" <em>",
|
|
||||||
"\t{ greeted }",
|
|
||||||
"\t</em>",
|
|
||||||
" { (silent)",
|
|
||||||
' ? "."',
|
|
||||||
' : "!"}',
|
|
||||||
"",
|
|
||||||
" </div>;",
|
|
||||||
"",
|
|
||||||
"}"
|
|
||||||
].join("\n")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
// Support old links with the deprecated "postcss" value for the parser option.
|
|
||||||
if (parsed && parsed.options && parsed.options.parser === "postcss") {
|
|
||||||
parsed.options.parser = "css";
|
|
||||||
}
|
|
||||||
return parsed;
|
|
||||||
})(location.hash.slice(1));
|
|
||||||
|
|
||||||
var worker = new Worker("/worker.js");
|
var worker = new Worker("/worker.js");
|
||||||
|
|
||||||
worker.onmessage = function(message) {
|
const DEFAULT_OPTIONS = {
|
||||||
if (prettierVersion === "?") {
|
options: undefined,
|
||||||
prettierVersion = message.data.version;
|
content: [
|
||||||
document.getElementById("version").textContent = prettierVersion;
|
'function HelloWorld({greeting = "hello", greeted = \'"World"\', silent = false, onMouseOver,}) {',
|
||||||
}
|
"",
|
||||||
if (outputEditor && docEditor && astEditor) {
|
" if(!greeting){return null};",
|
||||||
outputEditor.setValue(message.data.formatted);
|
"",
|
||||||
docEditor.setValue(message.data.doc || "");
|
" // TODO: Don't use random in render",
|
||||||
astEditor.setValue(message.data.ast || "");
|
' let num = Math.floor (Math.random() * 1E+7).toString().replace(/\\.\\d+/ig, "")',
|
||||||
output2Editor.setValue(
|
"",
|
||||||
message.data.formatted === ""
|
" return <div className='HelloWorld' title={`You are visitor number ${ num }`} onMouseOver={onMouseOver}>",
|
||||||
? ""
|
"",
|
||||||
: message.data.formatted2 === message.data.formatted
|
" <strong>{ greeting.slice( 0, 1 ).toUpperCase() + greeting.slice(1).toLowerCase() }</strong>",
|
||||||
? IDEMPOTENT_MESSAGE
|
' {greeting.endsWith(",") ? " " : <span style={{color: \'\\grey\'}}>", "</span> }',
|
||||||
: message.data.formatted2 || ""
|
" <em>",
|
||||||
);
|
"\t{ greeted }",
|
||||||
document.getElementById("button-report-issue").search =
|
"\t</em>",
|
||||||
"body=" + LZString.compressToEncodedURIComponent(createMarkdown(true));
|
" { (silent)",
|
||||||
}
|
' ? "."',
|
||||||
|
' : "!"}',
|
||||||
|
"",
|
||||||
|
" </div>;",
|
||||||
|
"",
|
||||||
|
"}"
|
||||||
|
].join("\n")
|
||||||
};
|
};
|
||||||
|
|
||||||
// Warm up the worker (load the current parser while CodeMirror loads)
|
|
||||||
worker.postMessage({ text: "", options: state.options });
|
|
||||||
|
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
|
var state = (function loadState(hash) {
|
||||||
|
var parsed;
|
||||||
|
try {
|
||||||
|
// providing backwards support for old json encoded URIComponent
|
||||||
|
if (hash.indexOf("%7B%22") !== -1) {
|
||||||
|
parsed = JSON.parse(decodeURIComponent(hash));
|
||||||
|
} else {
|
||||||
|
parsed = JSON.parse(LZString.decompressFromEncodedURIComponent(hash));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return DEFAULT_OPTIONS;
|
||||||
|
}
|
||||||
|
// Support old links with the deprecated "postcss" value for the parser option.
|
||||||
|
if (parsed && parsed.options && parsed.options.parser === "postcss") {
|
||||||
|
parsed.options.parser = "css";
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsed || DEFAULT_OPTIONS;
|
||||||
|
})(location.hash.slice(1));
|
||||||
|
|
||||||
|
worker.onmessage = function(message) {
|
||||||
|
if (prettierVersion === "?") {
|
||||||
|
prettierVersion = message.data.version;
|
||||||
|
document.getElementById("version").textContent = prettierVersion;
|
||||||
|
}
|
||||||
|
if (outputEditor && docEditor && astEditor) {
|
||||||
|
outputEditor.setValue(message.data.formatted);
|
||||||
|
docEditor.setValue(message.data.doc || "");
|
||||||
|
astEditor.setValue(message.data.ast || "");
|
||||||
|
output2Editor.setValue(
|
||||||
|
message.data.formatted === ""
|
||||||
|
? ""
|
||||||
|
: message.data.formatted2 === message.data.formatted
|
||||||
|
? IDEMPOTENT_MESSAGE
|
||||||
|
: message.data.formatted2 || ""
|
||||||
|
);
|
||||||
|
document.getElementById("button-report-issue").search =
|
||||||
|
"body=" + encodeURIComponent(createMarkdown(true));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Warm up the worker (load the current parser while CodeMirror loads)
|
||||||
|
worker.postMessage({ text: "", options: state.options });
|
||||||
|
|
||||||
state.options && setOptions(state.options);
|
state.options && setOptions(state.options);
|
||||||
|
|
||||||
var editorOptions = {
|
var editorOptions = {
|
||||||
|
|
Loading…
Reference in New Issue