From 23f0b4296f141dd73789c4b8b3b757b6e03c254d Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 15 Jun 2023 17:40:02 +0300 Subject: [PATCH 1/2] Focus search box when pressing slash --- python_docs_theme/layout.html | 3 ++- python_docs_theme/static/search-focus.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 python_docs_theme/static/search-focus.js diff --git a/python_docs_theme/layout.html b/python_docs_theme/layout.html index b4502ba..82b7ef4 100644 --- a/python_docs_theme/layout.html +++ b/python_docs_theme/layout.html @@ -48,7 +48,7 @@

{{ _('Navigation') }}

{%- if builder != "htmlhelp" %} @@ -76,6 +76,7 @@

{{ _('Navigation') }}

{%- if not embedded %} + {%- endif -%} {%- endif -%} diff --git a/python_docs_theme/static/search-focus.js b/python_docs_theme/static/search-focus.js new file mode 100644 index 0000000..d6f2826 --- /dev/null +++ b/python_docs_theme/static/search-focus.js @@ -0,0 +1,10 @@ +document.addEventListener('keydown', function(event) { + if (event.key === '/') { + // Prevent "/" from being entered in the search box + event.preventDefault(); + + // Set the focus on the search box + let searchBox = document.getElementById('search-box'); + searchBox.focus(); + } +}); From 3f27d34ed682e2fce8dcc56b97bcc9557fb54bbe Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Fri, 16 Jun 2023 00:52:35 +0300 Subject: [PATCH 2/2] Allow / to be entered into search box, don't focus if edit already focused --- python_docs_theme/static/search-focus.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/python_docs_theme/static/search-focus.js b/python_docs_theme/static/search-focus.js index d6f2826..aa5b42f 100644 --- a/python_docs_theme/static/search-focus.js +++ b/python_docs_theme/static/search-focus.js @@ -1,10 +1,21 @@ +function isInputFocused() { + const activeElement = document.activeElement; + return ( + activeElement.tagName === 'INPUT' || + activeElement.tagName === 'TEXTAREA' || + activeElement.isContentEditable + ); +} + document.addEventListener('keydown', function(event) { if (event.key === '/') { - // Prevent "/" from being entered in the search box - event.preventDefault(); + if (!isInputFocused()) { + // Prevent "/" from being entered in the search box + event.preventDefault(); - // Set the focus on the search box - let searchBox = document.getElementById('search-box'); - searchBox.focus(); + // Set the focus on the search box + const searchBox = document.getElementById('search-box'); + searchBox.focus(); + } } });