make sure all pre-create hooks finished

This commit is contained in:
tophf 2017-12-07 20:26:41 +03:00
parent a71d2fa226
commit 6a838e9d5e
2 changed files with 36 additions and 33 deletions

View File

@ -5,7 +5,9 @@ global save toggleStyle setupAutocomplete makeSectionVisible getSectionForChild
*/ */
'use strict'; 'use strict';
onDOMready().then(() => { addEventListener('init:allDone', function _() {
removeEventListener('init:allDone', _);
CodeMirror.defaults.lint = linterConfig.getForCodeMirror(); CodeMirror.defaults.lint = linterConfig.getForCodeMirror();
const COMMANDS = { const COMMANDS = {

View File

@ -26,47 +26,23 @@ const CssToProperty = {'url': 'urls', 'url-prefix': 'urlPrefixes', 'domain': 'do
let editor; let editor;
preinit();
window.onbeforeunload = beforeUnload; window.onbeforeunload = beforeUnload;
chrome.runtime.onMessage.addListener(onRuntimeMessage); chrome.runtime.onMessage.addListener(onRuntimeMessage);
preinit();
Promise.all([ Promise.all([
initStyleData().then(style => { initStyleData(),
styleId = style.id;
sessionStorage.justEditedStyleId = styleId;
// we set "usercss" class on <html> when <body> is empty
// so there'll be no flickering of the elements that depend on it
if (isUsercss(style)) {
document.documentElement.classList.add('usercss');
}
// strip URL parameters when invoked for a non-existent id
if (!styleId) {
history.replaceState({}, document.title, location.pathname);
}
return style;
}),
onDOMready(), onDOMready(),
onBackgroundReady(),
]) ])
.then(([style]) => Promise.all([ .then(([style]) => Promise.all([
style, style,
initColorpicker(), initColorpicker(),
initCollapsibles(), initCollapsibles(),
initHooksCommon(), initHooksCommon(),
dispatchEvent(new Event('init:allDone')),
])) ]))
.then(([style]) => { .then(createEditor);
const usercss = isUsercss(style);
$('#heading').textContent = t(styleId ? 'editStyleHeading' : 'addStyleTitle');
$('#name').placeholder = t(usercss ? 'usercssEditorNamePlaceholder' : 'styleMissingName');
$('#name').title = usercss ? t('usercssReplaceTemplateName') : '';
$('#lint').addEventListener('scroll', hideLintHeaderOnScroll, {passive: true});
if (usercss) {
editor = createSourceEditor(style);
} else {
initWithSectionStyle({style});
document.addEventListener('wheel', scrollEntirePageOnCtrlShift);
}
});
function preinit() { function preinit() {
// make querySelectorAll enumeration code readable // make querySelectorAll enumeration code readable
@ -178,6 +154,20 @@ function preinit() {
}); });
} }
function createEditor([style]) {
const usercss = isUsercss(style);
$('#heading').textContent = t(styleId ? 'editStyleHeading' : 'addStyleTitle');
$('#name').placeholder = t(usercss ? 'usercssEditorNamePlaceholder' : 'styleMissingName');
$('#name').title = usercss ? t('usercssReplaceTemplateName') : '';
$('#lint').addEventListener('scroll', hideLintHeaderOnScroll, {passive: true});
if (usercss) {
editor = createSourceEditor(style);
} else {
initWithSectionStyle({style});
document.addEventListener('wheel', scrollEntirePageOnCtrlShift);
}
}
function onRuntimeMessage(request) { function onRuntimeMessage(request) {
switch (request.method) { switch (request.method) {
case 'styleUpdated': case 'styleUpdated':
@ -258,9 +248,20 @@ function initStyleData() {
) )
], ],
}); });
return !id ? return getStylesSafe({id: id || -1})
Promise.resolve(createEmptyStyle()) : .then(([style = createEmptyStyle()]) => {
getStylesSafe({id}).then(([style]) => style || createEmptyStyle()); styleId = sessionStorage.justEditedStyleId = style.id;
// we set "usercss" class on <html> when <body> is empty
// so there'll be no flickering of the elements that depend on it
if (isUsercss(style)) {
document.documentElement.classList.add('usercss');
}
// strip URL parameters when invoked for a non-existent id
if (!styleId) {
history.replaceState({}, document.title, location.pathname);
}
return style;
});
} }
function initHooks() { function initHooks() {