Skip to content

Commit 8ee964c

Browse files
committed
simplify rerouteHotkeys to avoid enabling it twice
1 parent 24a0077 commit 8ee964c

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

edit/codemirror-factory.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,7 @@
2121

2222
create(place, options) {
2323
const cm = CodeMirror(place, options);
24-
const {wrapper} = cm.display;
2524
cm.lastActive = 0;
26-
cm.on('blur', () => {
27-
rerouteHotkeys(true);
28-
setTimeout(() => {
29-
wrapper.classList.toggle('CodeMirror-active', wrapper.contains(document.activeElement));
30-
});
31-
});
32-
cm.on('focus', () => {
33-
rerouteHotkeys(false);
34-
wrapper.classList.add('CodeMirror-active');
35-
cm.lastActive = Date.now();
36-
});
3725
cms.add(cm);
3826
return cm;
3927
},
@@ -52,6 +40,23 @@
5240
},
5341
};
5442

43+
const onCmFocus = cm => {
44+
rerouteHotkeys.toggle(false);
45+
cm.display.wrapper.classList.add('CodeMirror-active');
46+
cm.lastActive = Date.now();
47+
};
48+
const onCmBlur = cm => {
49+
rerouteHotkeys.toggle(true);
50+
setTimeout(() => {
51+
const {wrapper} = cm.display;
52+
wrapper.classList.toggle('CodeMirror-active', wrapper.contains(document.activeElement));
53+
});
54+
};
55+
CodeMirror.defineInitHook(cm => {
56+
cm.on('focus', onCmFocus);
57+
cm.on('blur', onCmBlur);
58+
});
59+
5560
const handledPrefs = {
5661
'editor.colorpicker'() {}, // handled in colorpicker-helper.js
5762
async 'editor.theme'(key, value) {

edit/linter-dialogs.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global $ $create $createLink messageBoxProxy */// dom.js
22
/* global chromeSync */// storage-util.js
33
/* global editor */
4-
/* global helpPopup rerouteHotkeys showCodeMirrorPopup */// util.js
4+
/* global helpPopup showCodeMirrorPopup */// util.js
55
/* global linterMan */
66
/* global t */// localization.js
77
/* global tryJSONparse */// toolbox.js
@@ -62,7 +62,6 @@
6262
cm.focus();
6363
cm.on('changes', updateConfigButtons);
6464
updateConfigButtons();
65-
rerouteHotkeys(false);
6665
window.on('closeHelp', onConfigClose, {once: true});
6766
};
6867

@@ -160,7 +159,6 @@
160159
}
161160

162161
function onConfigClose() {
163-
rerouteHotkeys(true);
164162
cm = null;
165163
}
166164

edit/sections-editor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function SectionsEditor() {
2323
let headerOffset; // in compact mode the header is at the top so it reduces the available height
2424

2525
updateHeader();
26+
rerouteHotkeys.toggle(true); // enabled initially because we don't always focus a CodeMirror
2627
editor.livePreview.init(null, style.id);
2728
container.classList.add('section-editor');
2829
$('#to-mozilla').on('click', showMozillaFormat);
@@ -53,7 +54,8 @@ function SectionsEditor() {
5354
},
5455

5556
getSearchableInputs(cm) {
56-
return sections.find(s => s.cm === cm).appliesTo.map(a => a.valueEl).filter(Boolean);
57+
const sec = sections.find(s => s.cm === cm);
58+
return sec ? sec.appliesTo.map(a => a.valueEl).filter(Boolean) : [];
5759
},
5860

5961
jumpToEditor(i) {
@@ -488,7 +490,6 @@ function SectionsEditor() {
488490
scrollTo(0, si.scrollY);
489491
// only restore focus if it's the first CM to avoid derpy quirks
490492
focusOn = si.cms[0].focus && 0;
491-
rerouteHotkeys(true);
492493
} else {
493494
si = null;
494495
}

edit/util.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* global $ $create getEventKeyName messageBoxProxy moveFocus */// dom.js
22
/* global CodeMirror */
3-
/* global debounce */// toolbox.js
43
/* global editor */
54
/* global prefs */
65
/* global t */// localization.js
@@ -59,7 +58,7 @@ const helpPopup = {
5958
};
6059

6160
// reroute handling to nearest editor when keypress resolves to one of these commands
62-
Object.assign(rerouteHotkeys, {
61+
const rerouteHotkeys = {
6362
commands: [
6463
'beautify',
6564
'colorpicker',
@@ -97,7 +96,7 @@ Object.assign(rerouteHotkeys, {
9796
event.stopPropagation();
9897
}
9998
},
100-
});
99+
};
101100

102101
function clipString(str, limit = 100) {
103102
return str.length <= limit ? str : str.substr(0, limit) + '...';
@@ -149,14 +148,6 @@ function createHotkeyInput(prefId, onDone = () => {}) {
149148
});
150149
}
151150

152-
function rerouteHotkeys(enable, immediately) {
153-
if (immediately) {
154-
rerouteHotkeys.toggle(enable);
155-
} else {
156-
debounce(rerouteHotkeys.toggle, 0, enable);
157-
}
158-
}
159-
160151
/* exported showCodeMirrorPopup */
161152
function showCodeMirrorPopup(title, html, options) {
162153
const popup = helpPopup.show(title, html);
@@ -174,7 +165,6 @@ function showCodeMirrorPopup(title, html, options) {
174165
keyMap: prefs.get('editor.keyMap'),
175166
}, options));
176167
cm.focus();
177-
rerouteHotkeys(false);
178168

179169
document.documentElement.style.pointerEvents = 'none';
180170
popup.style.pointerEvents = 'auto';
@@ -192,7 +182,6 @@ function showCodeMirrorPopup(title, html, options) {
192182
window.on('closeHelp', () => {
193183
window.off('keydown', onKeyDown, true);
194184
document.documentElement.style.removeProperty('pointer-events');
195-
rerouteHotkeys(true);
196185
cm = popup.codebox = null;
197186
}, {once: true});
198187

0 commit comments

Comments
 (0)