44 *
55 * Sphinx JavaScript utilities for all documentation.
66 *
7- * :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
7+ * :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
88 * :license: BSD, see LICENSE for details.
99 *
1010 */
@@ -154,9 +154,7 @@ var Documentation = {
154154 this . fixFirefoxAnchorBug ( ) ;
155155 this . highlightSearchWords ( ) ;
156156 this . initIndexTable ( ) ;
157- if ( DOCUMENTATION_OPTIONS . NAVIGATION_WITH_KEYS ) {
158- this . initOnKeyListeners ( ) ;
159- }
157+ this . initOnKeyListeners ( ) ;
160158 } ,
161159
162160 /**
@@ -264,6 +262,16 @@ var Documentation = {
264262 hideSearchWords : function ( ) {
265263 $ ( '#searchbox .highlight-link' ) . fadeOut ( 300 ) ;
266264 $ ( 'span.highlighted' ) . removeClass ( 'highlighted' ) ;
265+ var url = new URL ( window . location ) ;
266+ url . searchParams . delete ( 'highlight' ) ;
267+ window . history . replaceState ( { } , '' , url ) ;
268+ } ,
269+
270+ /**
271+ * helper function to focus on search bar
272+ */
273+ focusSearchBar : function ( ) {
274+ $ ( 'input[name=q]' ) . first ( ) . focus ( ) ;
267275 } ,
268276
269277 /**
@@ -288,27 +296,54 @@ var Documentation = {
288296 } ,
289297
290298 initOnKeyListeners : function ( ) {
299+ // only install a listener if it is really needed
300+ if ( ! DOCUMENTATION_OPTIONS . NAVIGATION_WITH_KEYS &&
301+ ! DOCUMENTATION_OPTIONS . ENABLE_SEARCH_SHORTCUTS )
302+ return ;
303+
291304 $ ( document ) . keydown ( function ( event ) {
292305 var activeElementType = document . activeElement . tagName ;
293306 // don't navigate when in search box, textarea, dropdown or button
294307 if ( activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT'
295- && activeElementType !== 'BUTTON' && ! event . altKey && ! event . ctrlKey && ! event . metaKey
296- && ! event . shiftKey ) {
297- switch ( event . keyCode ) {
298- case 37 : // left
299- var prevHref = $ ( 'link[rel="prev"]' ) . prop ( 'href' ) ;
300- if ( prevHref ) {
301- window . location . href = prevHref ;
302- return false ;
303- }
304- break ;
305- case 39 : // right
306- var nextHref = $ ( 'link[rel="next"]' ) . prop ( 'href' ) ;
307- if ( nextHref ) {
308- window . location . href = nextHref ;
309- return false ;
310- }
311- break ;
308+ && activeElementType !== 'BUTTON' ) {
309+ if ( event . altKey || event . ctrlKey || event . metaKey )
310+ return ;
311+
312+ if ( ! event . shiftKey ) {
313+ switch ( event . key ) {
314+ case 'ArrowLeft' :
315+ if ( ! DOCUMENTATION_OPTIONS . NAVIGATION_WITH_KEYS )
316+ break ;
317+ var prevHref = $ ( 'link[rel="prev"]' ) . prop ( 'href' ) ;
318+ if ( prevHref ) {
319+ window . location . href = prevHref ;
320+ return false ;
321+ }
322+ break ;
323+ case 'ArrowRight' :
324+ if ( ! DOCUMENTATION_OPTIONS . NAVIGATION_WITH_KEYS )
325+ break ;
326+ var nextHref = $ ( 'link[rel="next"]' ) . prop ( 'href' ) ;
327+ if ( nextHref ) {
328+ window . location . href = nextHref ;
329+ return false ;
330+ }
331+ break ;
332+ case 'Escape' :
333+ if ( ! DOCUMENTATION_OPTIONS . ENABLE_SEARCH_SHORTCUTS )
334+ break ;
335+ Documentation . hideSearchWords ( ) ;
336+ return false ;
337+ }
338+ }
339+
340+ // some keyboard layouts may need Shift to get /
341+ switch ( event . key ) {
342+ case '/' :
343+ if ( ! DOCUMENTATION_OPTIONS . ENABLE_SEARCH_SHORTCUTS )
344+ break ;
345+ Documentation . focusSearchBar ( ) ;
346+ return false ;
312347 }
313348 }
314349 } ) ;
0 commit comments