Skip to content

Commit

Permalink
Change theme value to enable_search_shortcuts.
Browse files Browse the repository at this point in the history
And rework javascript shortcut handling.
  • Loading branch information
marxin committed Jan 16, 2022
1 parent c18be2e commit 160df9a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 48 deletions.
7 changes: 3 additions & 4 deletions CHANGES
Expand Up @@ -49,10 +49,9 @@ Features added
references for readability with standalone readers
* #9822 (and #9062), add new Intersphinx role :rst:role:`external` for explict
lookup in the external projects, without resolving to the local project.
* #9337: HTML theme, add option ``search_with_keys`` that enables :kbd:'/' as
a Quick search shortcut.
Add option ``remove_highlight_with_keys`` that enables :kbd:`Esc` shortcut
that removes search highlighting.
* #9337: HTML theme, add option ``enable_search_shortcuts`` that enables :kbd:'/' as
a Quick search shortcut and :kbd:`Esc` shortcut that
removes search highlighting.

Bugs fixed
----------
Expand Down
9 changes: 2 additions & 7 deletions doc/usage/theming.rst
Expand Up @@ -166,13 +166,8 @@ These themes are:

Defaults to ``False``.

- **search_with_keys** (true or false): Allow jumping to the search box
with :kbd:`/`.

Defaults to ``True``.

- **remove_highlight_with_keys** (true or false): Allow removal of search
highlighting with :kbd:`Esc`.
- **enable_search_shortcuts** (true or false): Allow jumping to the search box
with :kbd:`/` and allow removal of search highlighting with :kbd:`Esc`.

Defaults to ``True``.

Expand Down
69 changes: 36 additions & 33 deletions sphinx/themes/basic/static/doctools.js
Expand Up @@ -153,6 +153,7 @@ var Documentation = {
init : function() {
this.fixFirefoxAnchorBug();
this.highlightSearchWords();
this.initIndexTable();
this.initOnKeyListeners();
},

Expand Down Expand Up @@ -290,50 +291,52 @@ var Documentation = {
initOnKeyListeners: function() {
// only install a listener if it is really needed
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS &&
!DOCUMENTATION_OPTIONS.SEARCH_WITH_KEYS &&
!DOCUMENTATION_OPTIONS.REMOVE_HIGHLIGHT_WITH_KEYS)
!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS)
return;

$(document).keydown(function(event) {
var activeElementType = document.activeElement.tagName;
// don't navigate when in search box, textarea, dropdown or button
if (activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT'
&& activeElementType !== 'BUTTON') {
if (!event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
switch (event.key) {
case 'ArrowLeft':
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS)
if (event.altKey || event.ctrlKey || event.metaKey)
return;

if (!event.shiftKey) {
switch (event.key) {
case 'ArrowLeft':
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS)
break;
var prevHref = $('link[rel="prev"]').prop('href');
if (prevHref) {
window.location.href = prevHref;
return false;
}
break;
var prevHref = $('link[rel="prev"]').prop('href');
if (prevHref) {
window.location.href = prevHref;
return false;
}
break;
case 'ArrowRight':
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS)
case 'ArrowRight':
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS)
break;
var nextHref = $('link[rel="next"]').prop('href');
if (nextHref) {
window.location.href = nextHref;
return false;
}
break;
var nextHref = $('link[rel="next"]').prop('href');
if (nextHref) {
window.location.href = nextHref;
case 'Escape':
if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS)
break;
Documentation.hideSearchWords();
return false;
}
break;
case 'Escape':
if (!DOCUMENTATION_OPTIONS.REMOVE_HIGHLIGHT_WITH_KEYS)
break;
Documentation.hideSearchWords();
return false;
}
}
if (!event.altKey && !event.ctrlKey && !event.metaKey) {
// some keyboard layouts need Shift to get /
switch (event.key) {
case '/':
if (!DOCUMENTATION_OPTIONS.SEARCH_WITH_KEYS)
break;
$('input[name=q]').first().focus();
return false;
}

// some keyboard layouts may need Shift to get /
switch (event.key) {
case '/':
if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS)
break;
$('input[name=q]').first().focus();
return false;
}
}
});
Expand Down
3 changes: 1 addition & 2 deletions sphinx/themes/basic/static/documentation_options.js_t
Expand Up @@ -9,6 +9,5 @@ var DOCUMENTATION_OPTIONS = {
HAS_SOURCE: {{ has_source|lower }},
SOURCELINK_SUFFIX: '{{ sourcelink_suffix }}',
NAVIGATION_WITH_KEYS: {{ 'true' if theme_navigation_with_keys|tobool else 'false'}},
SEARCH_WITH_KEYS: {{ 'true' if theme_search_with_keys|tobool else 'true'}},
REMOVE_HIGHLIGHT_WITH_KEYS: {{ 'true' if theme_remove_highlight_with_keys|tobool else 'true'}}
ENABLE_SEARCH_SHORTCUTS: {{ 'true' if enable_search_shortcuts|tobool else 'true'}},
};
3 changes: 1 addition & 2 deletions sphinx/themes/basic/theme.conf
Expand Up @@ -10,8 +10,7 @@ sidebarwidth = 230
body_min_width = 450
body_max_width = 800
navigation_with_keys = False
search_with_keys = True
remove_highlight_with_keys = True
enable_search_shortcuts = True
globaltoc_collapse = true
globaltoc_includehidden = false
globaltoc_maxdepth =

0 comments on commit 160df9a

Please sign in to comment.