Skip to content

Commit 051adf7

Browse files
authored
Merge pull request #1222 from codidact/0valt/1206/untrap-modifier-keys
Prevent keyboard events with modifier keys from triggering shortcuts
2 parents f19abf0 + 45ab601 commit 051adf7

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

app/assets/javascripts/keyboard_tools.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,27 @@ $(() => {
7777
}
7878
});
7979

80+
/**
81+
* Checks common modifier states on a given keyboard event
82+
* @param {KeyboardEvent} e
83+
* @returns {boolean}
84+
*/
85+
const getModifierState = (e) => {
86+
return !!e.altKey || !!e.ctrlKey || !!e.metaKey || !!e.shiftKey;
87+
};
88+
89+
/**
90+
* Handles the "home" keyboard state
91+
* @param {KeyboardEvent} e
92+
*/
8093
function homeMenu(e) {
81-
if (e.key === "?") {
94+
const isHelp = e.key === "?";
95+
96+
if (!isHelp && getModifierState(e)) {
97+
return;
98+
}
99+
100+
if (isHelp) {
82101
_CodidactKeyboard.dialog(
83102
'Codidact Keyboard Shortcuts\n' +
84103
'===========================\n' +
@@ -156,7 +175,15 @@ $(() => {
156175
}
157176
}
158177

178+
/**
179+
* Handles "goto" keyboard state
180+
* @param {KeyboardEvent} e
181+
*/
159182
function gotoMenu(e) {
183+
if (getModifierState(e)) {
184+
return;
185+
}
186+
160187
if (e.key === 'm') {
161188
window.location.href = '/';
162189
} else if (e.key === 'u') {
@@ -208,7 +235,15 @@ $(() => {
208235
}
209236
}
210237

238+
/**
239+
* Handles the "goto/category" keyboard state
240+
* @param {KeyboardEvent} e
241+
*/
211242
function categoryMenu(e) {
243+
if (getModifierState(e)) {
244+
return;
245+
}
246+
212247
const number = parseInt(e.key);
213248
if (!isNaN(number)) {
214249
const data = _CodidactKeyboard.categories();
@@ -219,7 +254,15 @@ $(() => {
219254
}
220255
}
221256

257+
/**
258+
* Handles the "goto/category-tags" keyboard state
259+
* @param {KeyboardEvent} e
260+
*/
222261
function categoryTagsMenu(e) {
262+
if (getModifierState(e)) {
263+
return;
264+
}
265+
223266
const number = parseInt(e.key);
224267
if (!isNaN(number)) {
225268
const data = Object.entries(_CodidactKeyboard.categories());
@@ -229,7 +272,15 @@ $(() => {
229272
}
230273
}
231274

275+
/**
276+
* Handles the "goto/category-edits" keyboard state
277+
* @param {KeyboardEvent} e
278+
*/
232279
function categorySuggestedEditsMenu(e) {
280+
if (getModifierState(e)) {
281+
return;
282+
}
283+
233284
const number = parseInt(e.key);
234285
if (!isNaN(number)) {
235286
const data = Object.entries(_CodidactKeyboard.categories());
@@ -239,7 +290,15 @@ $(() => {
239290
}
240291
}
241292

293+
/**
294+
* Handles the "tools" keyboard state
295+
* @param {KeyboardEvent} e
296+
*/
242297
function toolsMenu(e) {
298+
if (getModifierState(e)) {
299+
return;
300+
}
301+
243302
if (e.key === 'e') {
244303
window.location.href = $(_CodidactKeyboard.selectedItem).find('.tools--item i.fa.fa-pencil-alt').parent().attr("href");
245304
} else if (e.key === 'h') {
@@ -276,7 +335,15 @@ $(() => {
276335

277336
}
278337

338+
/**
339+
* Handles the "tools/vote" keyboard state
340+
* @param {KeyboardEvent} e
341+
*/
279342
function voteMenu(e) {
343+
if (getModifierState(e)) {
344+
return;
345+
}
346+
280347
if (e.key === 'u') {
281348
const cl = $(_CodidactKeyboard.selectedItem).find('.vote-button[data-vote-type="1"]');
282349
cl.click();

0 commit comments

Comments
 (0)