From 160df9a31bdf9d7ce8028aef7ca5fd5b1855b57b Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Sun, 16 Jan 2022 11:04:16 +0100 Subject: [PATCH] Change theme value to enable_search_shortcuts. And rework javascript shortcut handling. --- CHANGES | 7 +- doc/usage/theming.rst | 9 +-- sphinx/themes/basic/static/doctools.js | 69 ++++++++++--------- .../basic/static/documentation_options.js_t | 3 +- sphinx/themes/basic/theme.conf | 3 +- 5 files changed, 43 insertions(+), 48 deletions(-) diff --git a/CHANGES b/CHANGES index 09ac63b453..d6b9124faa 100644 --- a/CHANGES +++ b/CHANGES @@ -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 ---------- diff --git a/doc/usage/theming.rst b/doc/usage/theming.rst index 0bc9ab64e0..5f54170615 100644 --- a/doc/usage/theming.rst +++ b/doc/usage/theming.rst @@ -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``. diff --git a/sphinx/themes/basic/static/doctools.js b/sphinx/themes/basic/static/doctools.js index 6d3913ada8..ab91382808 100644 --- a/sphinx/themes/basic/static/doctools.js +++ b/sphinx/themes/basic/static/doctools.js @@ -153,6 +153,7 @@ var Documentation = { init : function() { this.fixFirefoxAnchorBug(); this.highlightSearchWords(); + this.initIndexTable(); this.initOnKeyListeners(); }, @@ -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; } } }); diff --git a/sphinx/themes/basic/static/documentation_options.js_t b/sphinx/themes/basic/static/documentation_options.js_t index 1e42ec6842..e40c788c74 100644 --- a/sphinx/themes/basic/static/documentation_options.js_t +++ b/sphinx/themes/basic/static/documentation_options.js_t @@ -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'}}, }; diff --git a/sphinx/themes/basic/theme.conf b/sphinx/themes/basic/theme.conf index ef516f01e6..45834f000f 100644 --- a/sphinx/themes/basic/theme.conf +++ b/sphinx/themes/basic/theme.conf @@ -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 =