From 8ee964c04532f8488c8c1348e8a6bd573c44e518 Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 2 Feb 2021 22:45:47 +0300 Subject: [PATCH] simplify rerouteHotkeys to avoid enabling it twice --- edit/codemirror-factory.js | 29 +++++++++++++++++------------ edit/linter-dialogs.js | 4 +--- edit/sections-editor.js | 5 +++-- edit/util.js | 15 ++------------- 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/edit/codemirror-factory.js b/edit/codemirror-factory.js index e6264a73..165c8b6d 100644 --- a/edit/codemirror-factory.js +++ b/edit/codemirror-factory.js @@ -21,19 +21,7 @@ create(place, options) { const cm = CodeMirror(place, options); - const {wrapper} = cm.display; cm.lastActive = 0; - cm.on('blur', () => { - rerouteHotkeys(true); - setTimeout(() => { - wrapper.classList.toggle('CodeMirror-active', wrapper.contains(document.activeElement)); - }); - }); - cm.on('focus', () => { - rerouteHotkeys(false); - wrapper.classList.add('CodeMirror-active'); - cm.lastActive = Date.now(); - }); cms.add(cm); return cm; }, @@ -52,6 +40,23 @@ }, }; + const onCmFocus = cm => { + rerouteHotkeys.toggle(false); + cm.display.wrapper.classList.add('CodeMirror-active'); + cm.lastActive = Date.now(); + }; + const onCmBlur = cm => { + rerouteHotkeys.toggle(true); + setTimeout(() => { + const {wrapper} = cm.display; + wrapper.classList.toggle('CodeMirror-active', wrapper.contains(document.activeElement)); + }); + }; + CodeMirror.defineInitHook(cm => { + cm.on('focus', onCmFocus); + cm.on('blur', onCmBlur); + }); + const handledPrefs = { 'editor.colorpicker'() {}, // handled in colorpicker-helper.js async 'editor.theme'(key, value) { diff --git a/edit/linter-dialogs.js b/edit/linter-dialogs.js index b525ca2c..fa0b8d0d 100644 --- a/edit/linter-dialogs.js +++ b/edit/linter-dialogs.js @@ -1,7 +1,7 @@ /* global $ $create $createLink messageBoxProxy */// dom.js /* global chromeSync */// storage-util.js /* global editor */ -/* global helpPopup rerouteHotkeys showCodeMirrorPopup */// util.js +/* global helpPopup showCodeMirrorPopup */// util.js /* global linterMan */ /* global t */// localization.js /* global tryJSONparse */// toolbox.js @@ -62,7 +62,6 @@ cm.focus(); cm.on('changes', updateConfigButtons); updateConfigButtons(); - rerouteHotkeys(false); window.on('closeHelp', onConfigClose, {once: true}); }; @@ -160,7 +159,6 @@ } function onConfigClose() { - rerouteHotkeys(true); cm = null; } diff --git a/edit/sections-editor.js b/edit/sections-editor.js index 2b4a7442..9bdc96ed 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -23,6 +23,7 @@ function SectionsEditor() { let headerOffset; // in compact mode the header is at the top so it reduces the available height updateHeader(); + rerouteHotkeys.toggle(true); // enabled initially because we don't always focus a CodeMirror editor.livePreview.init(null, style.id); container.classList.add('section-editor'); $('#to-mozilla').on('click', showMozillaFormat); @@ -53,7 +54,8 @@ function SectionsEditor() { }, getSearchableInputs(cm) { - return sections.find(s => s.cm === cm).appliesTo.map(a => a.valueEl).filter(Boolean); + const sec = sections.find(s => s.cm === cm); + return sec ? sec.appliesTo.map(a => a.valueEl).filter(Boolean) : []; }, jumpToEditor(i) { @@ -488,7 +490,6 @@ function SectionsEditor() { scrollTo(0, si.scrollY); // only restore focus if it's the first CM to avoid derpy quirks focusOn = si.cms[0].focus && 0; - rerouteHotkeys(true); } else { si = null; } diff --git a/edit/util.js b/edit/util.js index f8fcce5e..7bcddad7 100644 --- a/edit/util.js +++ b/edit/util.js @@ -1,6 +1,5 @@ /* global $ $create getEventKeyName messageBoxProxy moveFocus */// dom.js /* global CodeMirror */ -/* global debounce */// toolbox.js /* global editor */ /* global prefs */ /* global t */// localization.js @@ -59,7 +58,7 @@ const helpPopup = { }; // reroute handling to nearest editor when keypress resolves to one of these commands -Object.assign(rerouteHotkeys, { +const rerouteHotkeys = { commands: [ 'beautify', 'colorpicker', @@ -97,7 +96,7 @@ Object.assign(rerouteHotkeys, { event.stopPropagation(); } }, -}); +}; function clipString(str, limit = 100) { return str.length <= limit ? str : str.substr(0, limit) + '...'; @@ -149,14 +148,6 @@ function createHotkeyInput(prefId, onDone = () => {}) { }); } -function rerouteHotkeys(enable, immediately) { - if (immediately) { - rerouteHotkeys.toggle(enable); - } else { - debounce(rerouteHotkeys.toggle, 0, enable); - } -} - /* exported showCodeMirrorPopup */ function showCodeMirrorPopup(title, html, options) { const popup = helpPopup.show(title, html); @@ -174,7 +165,6 @@ function showCodeMirrorPopup(title, html, options) { keyMap: prefs.get('editor.keyMap'), }, options)); cm.focus(); - rerouteHotkeys(false); document.documentElement.style.pointerEvents = 'none'; popup.style.pointerEvents = 'auto'; @@ -192,7 +182,6 @@ function showCodeMirrorPopup(title, html, options) { window.on('closeHelp', () => { window.off('keydown', onKeyDown, true); document.documentElement.style.removeProperty('pointer-events'); - rerouteHotkeys(true); cm = popup.codebox = null; }, {once: true});