[gi-docgen: 1/2] main.js: bind 's' key to focus on search input




commit cf5ddaa4b4439419ac88b7b707be4b43af8fb6f7
Author: Marc-André Lureau <marcandre lureau redhat com>
Date:   Mon May 3 23:18:52 2021 +0400

    main.js: bind 's' key to focus on search input
    
    Signed-off-by: Marc-André Lureau <marcandre lureau redhat com>

 gidocgen/templates/basic/base.html | 2 +-
 gidocgen/templates/basic/main.js   | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
---
diff --git a/gidocgen/templates/basic/base.html b/gidocgen/templates/basic/base.html
index 1c1404c..e353208 100644
--- a/gidocgen/templates/basic/base.html
+++ b/gidocgen/templates/basic/base.html
@@ -62,7 +62,7 @@ SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later
       {% if CONFIG.search_index %}
       <div class="search section">
         <form id="search-form" autocomplete="off">
-          <input id="search-input" type="text" name="do-not-autocomplete" placeholder="Search docs" 
autocomplete="off"/>
+          <input id="search-input" type="text" name="do-not-autocomplete" placeholder="Click, or press 's' 
to search" autocomplete="off"/>
         </form>
       </div>
       {% endif %}
diff --git a/gidocgen/templates/basic/main.js b/gidocgen/templates/basic/main.js
index e306bcc..7a3f45d 100644
--- a/gidocgen/templates/basic/main.js
+++ b/gidocgen/templates/basic/main.js
@@ -8,6 +8,7 @@ const urlMap = new Map(typeof baseURLs !== 'undefined' ? baseURLs : []);
 
 window.addEventListener("hashchange", onDidHashChange);
 window.addEventListener("load", onDidLoad, false);
+window.addEventListener("keydown", onKeyDown);
 
 function onDidLoad() {
     attachScrollHandlers()
@@ -141,6 +142,14 @@ function attachCopyHandlers() {
     })
 }
 
+function onKeyDown(event) {
+    let search_input = document.querySelector("#search-input");
+
+    if (event.code === "KeyS" && document.activeElement !== search_input) {
+        event.preventDefault();
+        search_input.focus();
+    }
+}
 
 // Helpers
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]