MCPcopy Create free account
hub / github.com/zhongsp/TypeScript / globalKeyHandler

Function globalKeyHandler

docs/searcher.js:318–365  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

316
317 // Eventhandler for keyevents on `document`
318 function globalKeyHandler(e) {
319 if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.target.type === 'textarea' || e.target.type === 'text' || !hasFocus() && /^(?:input|select|textarea)$/i.test(e.target.nodeName)) { return; }
320
321 if (e.keyCode === ESCAPE_KEYCODE) {
322 e.preventDefault();
323 searchbar.classList.remove("active");
324 setSearchUrlParameters("",
325 (searchbar.value.trim() !== "") ? "push" : "replace");
326 if (hasFocus()) {
327 unfocusSearchbar();
328 }
329 showSearch(false);
330 marker.unmark();
331 } else if (!hasFocus() && e.keyCode === SEARCH_HOTKEY_KEYCODE) {
332 e.preventDefault();
333 showSearch(true);
334 window.scrollTo(0, 0);
335 searchbar.select();
336 } else if (hasFocus() && e.keyCode === DOWN_KEYCODE) {
337 e.preventDefault();
338 unfocusSearchbar();
339 searchresults.firstElementChild.classList.add("focus");
340 } else if (!hasFocus() && (e.keyCode === DOWN_KEYCODE
341 || e.keyCode === UP_KEYCODE
342 || e.keyCode === SELECT_KEYCODE)) {
343 // not `:focus` because browser does annoying scrolling
344 var focused = searchresults.querySelector("li.focus");
345 if (!focused) return;
346 e.preventDefault();
347 if (e.keyCode === DOWN_KEYCODE) {
348 var next = focused.nextElementSibling;
349 if (next) {
350 focused.classList.remove("focus");
351 next.classList.add("focus");
352 }
353 } else if (e.keyCode === UP_KEYCODE) {
354 focused.classList.remove("focus");
355 var prev = focused.previousElementSibling;
356 if (prev) {
357 prev.classList.add("focus");
358 } else {
359 searchbar.select();
360 }
361 } else { // SELECT_KEYCODE
362 window.location.assign(focused.querySelector('a'));
363 }
364 }
365 }
366
367 function showSearch(yes) {
368 if (yes) {

Callers 1

initFunction · 0.85

Calls 5

hasFocusFunction · 0.85
setSearchUrlParametersFunction · 0.85
unfocusSearchbarFunction · 0.85
showSearchFunction · 0.85
addMethod · 0.80

Tested by

no test coverage detected