simplify rerouteHotkeys to avoid enabling it twice

This commit is contained in:
tophf 2021-02-02 22:45:47 +03:00
parent 24a0077783
commit 8ee964c045
4 changed files with 23 additions and 30 deletions

View File

@ -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) {

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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});