Skip to content

Commit 91e9402

Browse files
committed
Switch searcher to use keys instead of keyCodes
According to https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode the keyCode is deprecated (and removed). It is causing me some difficulty with the GUI tests because it can't differentiate between a slash and question mark correctly. Using keys seems to work.
1 parent c9ad6db commit 91e9402

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

src/front-end/searcher/searcher.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ window.search = window.search || {};
3333
mark_exclude = ['text'],
3434
marker = new Mark(content),
3535
URL_SEARCH_PARAM = 'search',
36-
URL_MARK_PARAM = 'highlight',
37-
38-
SEARCH_HOTKEY_KEYCODES = [83, 191], // `s` or `/`.
39-
ESCAPE_KEYCODE = 27,
40-
DOWN_KEYCODE = 40,
41-
UP_KEYCODE = 38,
42-
SELECT_KEYCODE = 13;
36+
URL_MARK_PARAM = 'highlight';
4337

4438
let current_searchterm = '',
4539
doc_urls = [],
@@ -352,7 +346,7 @@ window.search = window.search || {};
352346
return;
353347
}
354348

355-
if (e.keyCode === ESCAPE_KEYCODE) {
349+
if (e.key === 'Escape') {
356350
e.preventDefault();
357351
searchbar.classList.remove('active');
358352
setSearchUrlParameters('',
@@ -362,46 +356,46 @@ window.search = window.search || {};
362356
}
363357
showSearch(false);
364358
marker.unmark();
365-
} else if (!hasFocus() && SEARCH_HOTKEY_KEYCODES.includes(e.keyCode)) {
359+
} else if (!hasFocus() && (e.key === 'S' || e.key === '/')) {
366360
e.preventDefault();
367361
showSearch(true);
368362
window.scrollTo(0, 0);
369363
searchbar.select();
370-
} else if (hasFocus() && (e.keyCode === DOWN_KEYCODE
371-
|| e.keyCode === SELECT_KEYCODE)) {
364+
} else if (hasFocus() && (e.key === 'ArrowDown'
365+
|| e.key === 'Enter')) {
372366
e.preventDefault();
373367
const first = searchresults.firstElementChild;
374368
if (first !== null) {
375369
unfocusSearchbar();
376370
first.classList.add('focus');
377-
if (e.keyCode === SELECT_KEYCODE) {
371+
if (e.key === 'Enter') {
378372
window.location.assign(first.querySelector('a'));
379373
}
380374
}
381-
} else if (!hasFocus() && (e.keyCode === DOWN_KEYCODE
382-
|| e.keyCode === UP_KEYCODE
383-
|| e.keyCode === SELECT_KEYCODE)) {
375+
} else if (!hasFocus() && (e.key === 'ArrowDown'
376+
|| e.key === 'ArrowUp'
377+
|| e.key === 'Enter')) {
384378
// not `:focus` because browser does annoying scrolling
385379
const focused = searchresults.querySelector('li.focus');
386380
if (!focused) {
387381
return;
388382
}
389383
e.preventDefault();
390-
if (e.keyCode === DOWN_KEYCODE) {
384+
if (e.key === 'ArrowDown') {
391385
const next = focused.nextElementSibling;
392386
if (next) {
393387
focused.classList.remove('focus');
394388
next.classList.add('focus');
395389
}
396-
} else if (e.keyCode === UP_KEYCODE) {
390+
} else if (e.key === 'ArrowUp') {
397391
focused.classList.remove('focus');
398392
const prev = focused.previousElementSibling;
399393
if (prev) {
400394
prev.classList.add('focus');
401395
} else {
402396
searchbar.select();
403397
}
404-
} else { // SELECT_KEYCODE
398+
} else { // Enter
405399
window.location.assign(focused.querySelector('a'));
406400
}
407401
}

0 commit comments

Comments
 (0)