diff --git a/edit/codemirror-editing-hooks.js b/edit/codemirror-editing-hooks.js index 46550b8e..7cb66a69 100644 --- a/edit/codemirror-editing-hooks.js +++ b/edit/codemirror-editing-hooks.js @@ -5,7 +5,9 @@ global save toggleStyle setupAutocomplete makeSectionVisible getSectionForChild */ 'use strict'; -onDOMready().then(() => { +addEventListener('init:allDone', function _() { + removeEventListener('init:allDone', _); + CodeMirror.defaults.lint = linterConfig.getForCodeMirror(); const COMMANDS = { diff --git a/edit/edit.js b/edit/edit.js index 0e95c192..255cc975 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -26,47 +26,23 @@ const CssToProperty = {'url': 'urls', 'url-prefix': 'urlPrefixes', 'domain': 'do let editor; -preinit(); window.onbeforeunload = beforeUnload; chrome.runtime.onMessage.addListener(onRuntimeMessage); +preinit(); + Promise.all([ - initStyleData().then(style => { - styleId = style.id; - sessionStorage.justEditedStyleId = styleId; - // we set "usercss" class on when 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; - }), + initStyleData(), onDOMready(), - onBackgroundReady(), ]) .then(([style]) => Promise.all([ style, initColorpicker(), initCollapsibles(), initHooksCommon(), + dispatchEvent(new Event('init:allDone')), ])) -.then(([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); - } -}); +.then(createEditor); function preinit() { // 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) { switch (request.method) { case 'styleUpdated': @@ -258,9 +248,20 @@ function initStyleData() { ) ], }); - return !id ? - Promise.resolve(createEmptyStyle()) : - getStylesSafe({id}).then(([style]) => style || createEmptyStyle()); + return getStylesSafe({id: id || -1}) + .then(([style = createEmptyStyle()]) => { + styleId = sessionStorage.justEditedStyleId = style.id; + // we set "usercss" class on when 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() {