Skip to content

Commit

Permalink
Merge pull request #31 from readthedocs/hotkey-for-search-modal
Browse files Browse the repository at this point in the history
Add forward slash as hotkey for search UI
  • Loading branch information
ericholscher committed Jul 17, 2019
2 parents 2c43687 + a786a0c commit ba93b77
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
18 changes: 18 additions & 0 deletions sphinx_search/_static/js/rtd_sphinx_search.js
Expand Up @@ -2,6 +2,11 @@ const MAX_SUGGESTIONS = 50;
const MAX_SECTION_RESULTS = 3;
const MAX_SUBSTRING_LIMIT = 100;

// Possible states of search modal
const SEARCH_MODAL_OPENED = "opened";
const SEARCH_MODAL_CLOSED = "closed";

let SEARCH_MODAL_STATE = SEARCH_MODAL_CLOSED;
let TOTAL_PAGE_RESULTS = 0;
let SEARCH_QUERY = "";

Expand Down Expand Up @@ -603,6 +608,8 @@ const showSearchModal = custom_query => {
// removes previous results (if there are any).
removeResults();

SEARCH_MODAL_STATE = SEARCH_MODAL_OPENED;

// removes the focus from the initial input field
// which as already present in the docs.
let search_bar = getInputField();
Expand Down Expand Up @@ -634,6 +641,8 @@ const removeSearchModal = () => {
// removes previous results before closing
removeResults();

SEARCH_MODAL_STATE = SEARCH_MODAL_CLOSED;

// sets the value of input field to empty string and remove the focus.
let search_outer_input = document.querySelector(".search__outer__input");
if (search_outer_input !== null) {
Expand Down Expand Up @@ -786,6 +795,15 @@ window.addEventListener("DOMContentLoaded", evt => {
}
});

// open search modal if "forward slash" button is pressed
document.addEventListener("keydown", e => {
if (e.keyCode === 191) {
if (SEARCH_MODAL_STATE !== SEARCH_MODAL_OPENED) {
showSearchModal();
}
}
});

// if "rtd_search" is present in URL parameters,
// then open the search modal and show the results
// for the value of "rtd_search"
Expand Down
2 changes: 1 addition & 1 deletion sphinx_search/_static/js/rtd_sphinx_search.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ba93b77

Please sign in to comment.