Skip to content

Commit

Permalink
feat: switch to readthedocs search addon
Browse files Browse the repository at this point in the history
The extension is no longer supported.
  • Loading branch information
thephez committed Oct 8, 2024
1 parent 898b5ab commit d03d47b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
59 changes: 42 additions & 17 deletions _static/js/pydata-search-close.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Script to allow use of readthedocs-sphinx-search extension with the pydata
// theme
// Script to allow use of readthedocs-sphinx-search extension with the pydata theme
//
// Based in part on:
// https://github.com/pydata/pydata-sphinx-theme/blob/v0.13.3/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js#L167-L272
Expand Down Expand Up @@ -28,9 +27,8 @@ var findSearchInput = () => {
}
};

/** Function to hide the search field */
var hideSearchField = () => {

// Hide Pydata theme's search
var hidePydataSearch = () => {
let input = findSearchInput();
let searchPopupWrapper = document.querySelector(".search-button__wrapper");
let hiddenInput = searchPopupWrapper.querySelector("input");
Expand All @@ -44,38 +42,65 @@ var hideSearchField = () => {
}
};

/** Add an event listener for hideSearchField() for Escape*/
// Hide the ReadTheDocs search (addon version)
function hideRtdSearch() {
// Grab the search element from the DOM
const searchElement = document.querySelector('readthedocs-search');
searchElement.closeModal();
}

// Hide any open search screens
function hideSearch() {
hidePydataSearch();
hideRtdSearch();
}

// Show the ReadTheDocs search (addon version)
function showRtDSearch() {
const searchElement = document.querySelector('readthedocs-search');
searchElement.showModal();

// If we're displaying ReadTheDocs search, make sure to hide the Pydata theme's search
hidePydataSearch();
}

// Add event listeners for key strokes
var addEventListenerForSearchKeyboard = () => {
window.addEventListener(
"keydown",
(event) => {
// Allow Escape key to hide the search field
if (event.code == "Escape") {
hideSearchField();
hidePydataSearch();
}

// Open the ReadTheDocs search modal when Ctrl+K is pressed
if (event.ctrlKey && event.key === 'k') {
event.preventDefault(); // Prevent default behavior of Ctrl+K
showRtDSearch()
}
},
true
);
};

/** Activate callbacks for search button popup */
// Activate callbacks for search button popup
var setupSearchButtons = () => {
addEventListenerForSearchKeyboard();

// Add event listeners to elements with class "search-button__button"
const searchButtons = document.querySelectorAll('.search-button__button');
searchButtons.forEach(button => {
button.addEventListener('click', showRtDSearch);
});
};

// Custom code to manage closing the RtD search dialog properly
$(document).ready(function(){
$(".search__cross").click(function(){
hideSearchField();
});
$(".search__outer__wrapper.search__backdrop").click(function(){
hideSearchField();
});
$(".search-button__overlay").click(function(){
// Shouldn't be necessary since it's currently hidden by CSS, but just in
// case
// Shouldn't be necessary since it's currently hidden by CSS, but just in case
console.log("Close by search-button__overlay");
hideSearchField();
hidePydataSearch();
});
});

Expand Down
1 change: 0 additions & 1 deletion conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
'sphinx.ext.autodoc',
'sphinx_copybutton',
'sphinx_design',
'sphinx_search.extension',
'sphinx.ext.intersphinx',
]

Expand Down

0 comments on commit d03d47b

Please sign in to comment.