Skip to content

Commit 38403ea

Browse files
marxinjakobandersen
andcommitted
Implement new shortcuts:
- "/" for Focus search bar - "ESC" - clear highlighted text Fixes #691. Co-authored-by: Jakob Lykke Andersen <jakobandersen@users.noreply.github.com>
1 parent fc428ad commit 38403ea

File tree

5 files changed

+70
-24
lines changed

5 files changed

+70
-24
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Features added
4949
references for readability with standalone readers
5050
* #9822 (and #9062), add new Intersphinx role :rst:role:`external` for explict
5151
lookup in the external projects, without resolving to the local project.
52+
* #9337: HTML theme, add option ``enable_search_shortcuts`` that enables :kbd:'/' as
53+
a Quick search shortcut and :kbd:`Esc` shortcut that
54+
removes search highlighting.
5255

5356
Bugs fixed
5457
----------

doc/usage/theming.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,18 @@ These themes are:
158158
dimension string such as '70em' or '50%'. Use 'none' if you don't
159159
want a width limit. Defaults may depend on the theme (often 800px).
160160

161-
- **navigation_with_keys** (true or false): Allow navigating to the
162-
previous/next page using the keyboard's left and right arrows. Defaults to
163-
``False``.
161+
- **navigation_with_keys** (true or false): Allow navigating
162+
with the following keyboard shortcuts:
163+
164+
- :kbd:`Left arrow`: previous page
165+
- :kbd:`Right arrow`: next page
166+
167+
Defaults to ``False``.
168+
169+
- **enable_search_shortcuts** (true or false): Allow jumping to the search box
170+
with :kbd:`/` and allow removal of search highlighting with :kbd:`Esc`.
171+
172+
Defaults to ``True``.
164173

165174
- **globaltoc_collapse** (true or false): Only expand subsections
166175
of the current document in ``globaltoc.html``

sphinx/themes/basic/static/doctools.js

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ var Documentation = {
154154
this.fixFirefoxAnchorBug();
155155
this.highlightSearchWords();
156156
this.initIndexTable();
157-
if (DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) {
158-
this.initOnKeyListeners();
159-
}
157+
this.initOnKeyListeners();
160158
},
161159

162160
/**
@@ -269,6 +267,13 @@ var Documentation = {
269267
window.history.replaceState({}, '', url);
270268
},
271269

270+
/**
271+
* helper function to focus on search bar
272+
*/
273+
focusSearchBar : function() {
274+
$('input[name=q]').first().focus();
275+
},
276+
272277
/**
273278
* make the url absolute
274279
*/
@@ -291,27 +296,54 @@ var Documentation = {
291296
},
292297

293298
initOnKeyListeners: function() {
299+
// only install a listener if it is really needed
300+
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS &&
301+
!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS)
302+
return;
303+
294304
$(document).keydown(function(event) {
295305
var activeElementType = document.activeElement.tagName;
296306
// don't navigate when in search box, textarea, dropdown or button
297307
if (activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT'
298-
&& activeElementType !== 'BUTTON' && !event.altKey && !event.ctrlKey && !event.metaKey
299-
&& !event.shiftKey) {
300-
switch (event.keyCode) {
301-
case 37: // left
302-
var prevHref = $('link[rel="prev"]').prop('href');
303-
if (prevHref) {
304-
window.location.href = prevHref;
305-
return false;
306-
}
307-
break;
308-
case 39: // right
309-
var nextHref = $('link[rel="next"]').prop('href');
310-
if (nextHref) {
311-
window.location.href = nextHref;
312-
return false;
313-
}
314-
break;
308+
&& activeElementType !== 'BUTTON') {
309+
if (event.altKey || event.ctrlKey || event.metaKey)
310+
return;
311+
312+
if (!event.shiftKey) {
313+
switch (event.key) {
314+
case 'ArrowLeft':
315+
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS)
316+
break;
317+
var prevHref = $('link[rel="prev"]').prop('href');
318+
if (prevHref) {
319+
window.location.href = prevHref;
320+
return false;
321+
}
322+
break;
323+
case 'ArrowRight':
324+
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS)
325+
break;
326+
var nextHref = $('link[rel="next"]').prop('href');
327+
if (nextHref) {
328+
window.location.href = nextHref;
329+
return false;
330+
}
331+
break;
332+
case 'Escape':
333+
if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS)
334+
break;
335+
Documentation.hideSearchWords();
336+
return false;
337+
}
338+
}
339+
340+
// some keyboard layouts may need Shift to get /
341+
switch (event.key) {
342+
case '/':
343+
if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS)
344+
break;
345+
Documentation.focusSearchBar();
346+
return false;
315347
}
316348
}
317349
});

sphinx/themes/basic/static/documentation_options.js_t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ var DOCUMENTATION_OPTIONS = {
88
LINK_SUFFIX: '{{ link_suffix }}',
99
HAS_SOURCE: {{ has_source|lower }},
1010
SOURCELINK_SUFFIX: '{{ sourcelink_suffix }}',
11-
NAVIGATION_WITH_KEYS: {{ 'true' if theme_navigation_with_keys|tobool else 'false'}}
11+
NAVIGATION_WITH_KEYS: {{ 'true' if theme_navigation_with_keys|tobool else 'false'}},
12+
ENABLE_SEARCH_SHORTCUTS: {{ 'true' if enable_search_shortcuts|tobool else 'true'}},
1213
};

sphinx/themes/basic/theme.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ sidebarwidth = 230
1010
body_min_width = 450
1111
body_max_width = 800
1212
navigation_with_keys = False
13+
enable_search_shortcuts = True
1314
globaltoc_collapse = true
1415
globaltoc_includehidden = false
1516
globaltoc_maxdepth =

0 commit comments

Comments
 (0)