Skip to content

Commit 63ae0d5

Browse files
authored
Merge pull request #2698 from traviscross/TC/search-ergonomics
Improve ergonomics of search
2 parents d24c0ca + 52e406b commit 63ae0d5

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

guide/src/guide/reading.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ Tapping the menu bar will scroll the page to the top.
4242
## Search
4343

4444
Each book has a built-in search system.
45-
Pressing the search icon (<i class="fa fa-search"></i>) in the menu bar, or pressing the `S` key on the keyboard will open an input box for entering search terms.
45+
Pressing the search icon (<i class="fa fa-search"></i>) in the menu bar, or pressing the <kbd>/</kbd> or <kbd>S</kbd> key on the keyboard will open an input box for entering search terms.
4646
Typing some terms will show matching chapters and sections in real time.
4747

4848
Clicking any of the results will jump to that section.
4949
The up and down arrow keys can be used to navigate the results, and enter will open the highlighted section.
5050

5151
After loading a search result, the matching search terms will be highlighted in the text.
52-
Clicking a highlighted word or pressing the `Esc` key will remove the highlighting.
52+
Clicking a highlighted word or pressing the <kbd>Escape</kbd> key will remove the highlighting.
5353

5454
## Code blocks
5555

src/front-end/searcher/searcher.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ window.search = window.search || {};
3535
URL_SEARCH_PARAM = 'search',
3636
URL_MARK_PARAM = 'highlight',
3737

38-
SEARCH_HOTKEY_KEYCODE = 83,
38+
SEARCH_HOTKEY_KEYCODES = [83, 191], // `s` or `/`.
3939
ESCAPE_KEYCODE = 27,
4040
DOWN_KEYCODE = 40,
4141
UP_KEYCODE = 38,
@@ -362,15 +362,22 @@ window.search = window.search || {};
362362
}
363363
showSearch(false);
364364
marker.unmark();
365-
} else if (!hasFocus() && e.keyCode === SEARCH_HOTKEY_KEYCODE) {
365+
} else if (!hasFocus() && SEARCH_HOTKEY_KEYCODES.includes(e.keyCode)) {
366366
e.preventDefault();
367367
showSearch(true);
368368
window.scrollTo(0, 0);
369369
searchbar.select();
370-
} else if (hasFocus() && e.keyCode === DOWN_KEYCODE) {
370+
} else if (hasFocus() && (e.keyCode === DOWN_KEYCODE
371+
|| e.keyCode === SELECT_KEYCODE)) {
371372
e.preventDefault();
372-
unfocusSearchbar();
373-
searchresults.firstElementChild.classList.add('focus');
373+
const first = searchresults.firstElementChild;
374+
if (first !== null) {
375+
unfocusSearchbar();
376+
first.classList.add('focus');
377+
if (e.keyCode === SELECT_KEYCODE) {
378+
window.location.assign(first.querySelector('a'));
379+
}
380+
}
374381
} else if (!hasFocus() && (e.keyCode === DOWN_KEYCODE
375382
|| e.keyCode === UP_KEYCODE
376383
|| e.keyCode === SELECT_KEYCODE)) {

src/front-end/templates/index.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
143143
</ul>
144144
{{#if search_enabled}}
145-
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
145+
<button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar">
146146
<i class="fa fa-search"></i>
147147
</button>
148148
{{/if}}

0 commit comments

Comments
 (0)