From 6f368769290059cbd9f2b56944a831649d40c4df Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Mon, 14 Jun 2021 09:37:40 +0200 Subject: [PATCH] Implement new shortcuts: - "s" and "/" for Focus search bar - "h" - clear highlighted text Fixes #691. --- doc/usage/theming.rst | 5 +++-- sphinx/themes/basic/static/doctools.js | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/doc/usage/theming.rst b/doc/usage/theming.rst index 60bc20e24f..f076dd01dc 100644 --- a/doc/usage/theming.rst +++ b/doc/usage/theming.rst @@ -155,8 +155,9 @@ These themes are: want a width limit. Defaults may depend on the theme (often 800px). - **navigation_with_keys** (true or false): Allow navigating to the - previous/next page using the keyboard's left and right arrows. Defaults to - ``False``. + previous/next page using the keyboard's left and right arrows. + Apart from that, 's' and '/' jumps to Quick search and 'h' + hides search matches. Defaults to ``False``. - **globaltoc_collapse** (true or false): Only expand subsections of the current document in ``globaltoc.html`` diff --git a/sphinx/themes/basic/static/doctools.js b/sphinx/themes/basic/static/doctools.js index 8cbf1b161a..b80362d3e0 100644 --- a/sphinx/themes/basic/static/doctools.js +++ b/sphinx/themes/basic/static/doctools.js @@ -264,6 +264,8 @@ var Documentation = { hideSearchWords : function() { $('#searchbox .highlight-link').fadeOut(300); $('span.highlighted').removeClass('highlighted'); + newurl = window.location.href.replace(new RegExp('\\?highlight=[^#]*'), ''); + history.pushState({}, null, newurl); }, /** @@ -294,21 +296,26 @@ var Documentation = { if (activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT' && activeElementType !== 'BUTTON' && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) { - switch (event.keyCode) { - case 37: // left + switch (event.key) { + case 'ArrowLeft': var prevHref = $('link[rel="prev"]').prop('href'); if (prevHref) { window.location.href = prevHref; return false; } - break; - case 39: // right + case 'ArrowRight': var nextHref = $('link[rel="next"]').prop('href'); if (nextHref) { window.location.href = nextHref; return false; } - break; + case '/': + case 's': + $('input[name=q]').focus(); + return false; + case 'h': + Documentation.hideSearchWords(); + return false; } } });