1- // Script to allow use of readthedocs-sphinx-search extension with the pydata
2- // theme
1+ // Script to allow use of readthedocs-sphinx-search extension with the pydata theme
32//
43// Based in part on:
54// https://github.com/pydata/pydata-sphinx-theme/blob/v0.13.3/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js#L167-L272
@@ -28,9 +27,8 @@ var findSearchInput = () => {
2827 }
2928} ;
3029
31- /** Function to hide the search field */
32- var hideSearchField = ( ) => {
33-
30+ // Hide Pydata theme's search
31+ var hidePydataSearch = ( ) => {
3432 let input = findSearchInput ( ) ;
3533 let searchPopupWrapper = document . querySelector ( ".search-button__wrapper" ) ;
3634 let hiddenInput = searchPopupWrapper . querySelector ( "input" ) ;
@@ -44,38 +42,65 @@ var hideSearchField = () => {
4442 }
4543} ;
4644
47- /** Add an event listener for hideSearchField() for Escape*/
45+ // Hide the ReadTheDocs search (addon version)
46+ function hideRtdSearch ( ) {
47+ // Grab the search element from the DOM
48+ const searchElement = document . querySelector ( 'readthedocs-search' ) ;
49+ searchElement . closeModal ( ) ;
50+ }
51+
52+ // Hide any open search screens
53+ function hideSearch ( ) {
54+ hidePydataSearch ( ) ;
55+ hideRtdSearch ( ) ;
56+ }
57+
58+ // Show the ReadTheDocs search (addon version)
59+ function showRtDSearch ( ) {
60+ const searchElement = document . querySelector ( 'readthedocs-search' ) ;
61+ searchElement . showModal ( ) ;
62+
63+ // If we're displaying ReadTheDocs search, make sure to hide the Pydata theme's search
64+ hidePydataSearch ( ) ;
65+ }
66+
67+ // Add event listeners for key strokes
4868var addEventListenerForSearchKeyboard = ( ) => {
4969 window . addEventListener (
5070 "keydown" ,
5171 ( event ) => {
5272 // Allow Escape key to hide the search field
5373 if ( event . code == "Escape" ) {
54- hideSearchField ( ) ;
74+ hidePydataSearch ( ) ;
75+ }
76+
77+ // Open the ReadTheDocs search modal when Ctrl+K is pressed
78+ if ( event . ctrlKey && event . key === 'k' ) {
79+ event . preventDefault ( ) ; // Prevent default behavior of Ctrl+K
80+ showRtDSearch ( )
5581 }
5682 } ,
5783 true
5884 ) ;
5985} ;
6086
61- /** Activate callbacks for search button popup */
87+ // Activate callbacks for search button popup
6288var setupSearchButtons = ( ) => {
6389 addEventListenerForSearchKeyboard ( ) ;
90+
91+ // Add event listeners to elements with class "search-button__button"
92+ const searchButtons = document . querySelectorAll ( '.search-button__button' ) ;
93+ searchButtons . forEach ( button => {
94+ button . addEventListener ( 'click' , showRtDSearch ) ;
95+ } ) ;
6496} ;
6597
6698// Custom code to manage closing the RtD search dialog properly
6799$ ( document ) . ready ( function ( ) {
68- $ ( ".search__cross" ) . click ( function ( ) {
69- hideSearchField ( ) ;
70- } ) ;
71- $ ( ".search__outer__wrapper.search__backdrop" ) . click ( function ( ) {
72- hideSearchField ( ) ;
73- } ) ;
74100 $ ( ".search-button__overlay" ) . click ( function ( ) {
75- // Shouldn't be necessary since it's currently hidden by CSS, but just in
76- // case
101+ // Shouldn't be necessary since it's currently hidden by CSS, but just in case
77102 console . log ( "Close by search-button__overlay" ) ;
78- hideSearchField ( ) ;
103+ hidePydataSearch ( ) ;
79104 } ) ;
80105} ) ;
81106
0 commit comments