Skip to content

Commit 11a7ec1

Browse files
Add possibility to focus on search input using keyboard
1 parent f7db895 commit 11a7ec1

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

util/gh-pages/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ <h1>Clippy Lints</h1>
541541
<div class="col-12 col-md-5 search-control">
542542
<div class="input-group">
543543
<label class="input-group-addon" id="filter-label" for="search-input">Filter:</label>
544-
<input type="text" class="form-control filter-input" placeholder="Keywords or search string" id="search-input"
544+
<input type="text" class="form-control filter-input" placeholder="Keywords or search string (`S` or `/` to focus)" id="search-input"
545545
ng-model="search" ng-blur="updatePath()" ng-keyup="$event.keyCode == 13 && updatePath()"
546546
ng-model-options="{debounce: 50}" />
547547
<span class="input-group-btn">

util/gh-pages/script.js

+34
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,40 @@ function setTheme(theme, store) {
579579
}
580580
}
581581

582+
function getVirtualKey(ev) {
583+
if ("key" in ev && typeof ev.key !== "undefined") {
584+
return ev.key;
585+
}
586+
587+
const c = ev.charCode || ev.keyCode;
588+
if (c === 27) {
589+
return "Escape";
590+
}
591+
return String.fromCharCode(c);
592+
}
593+
594+
function handleShortcut(ev) {
595+
if (document.activeElement.tagName === "INPUT") {
596+
if (getVirtualKey(ev) === "Escape") {
597+
document.activeElement.blur();
598+
}
599+
} else {
600+
switch (getVirtualKey(ev)) {
601+
case "s":
602+
case "S":
603+
case "/":
604+
ev.preventDefault(); // To prevent the kep to be put into the input.
605+
document.getElementById("search-input").focus();
606+
break;
607+
default:
608+
break;
609+
}
610+
}
611+
}
612+
613+
document.addEventListener("keypress", handleShortcut);
614+
document.addEventListener("keydown", handleShortcut);
615+
582616
// loading the theme after the initial load
583617
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
584618
const theme = localStorage.getItem('clippy-lint-list-theme');

0 commit comments

Comments
 (0)