Skip to content

Commit 7aaf5a6

Browse files
Yeray Diaz Diazwaylan
Yeray Diaz Diaz
authored andcommitted
Prevent pressing Enter in search box from reloading the page
1 parent 24d61ce commit 7aaf5a6

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

mkdocs/contrib/search/templates/search/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ if (!window.Worker) {
7070
$.getScript(base_url + "/search/worker.js").done(function () {
7171
console.log('Loaded worker');
7272
init();
73-
window.postMessage = initSearch;
73+
window.postMessage = function (msg) {
74+
onWorkerMessage({data: msg});
75+
};
7476
}).fail(function (jqxhr, settings, exception) {
7577
console.error('Could not load worker.js');
7678
});

mkdocs/contrib/search/templates/search/worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function onScriptsLoaded () {
8484
console.log('Lunr index built, search ready');
8585
}
8686
allowSearch = true;
87-
postMessage({allowSearch: true});
87+
postMessage({allowSearch: allowSearch});
8888
}
8989

9090
function init () {

mkdocs/themes/readthedocs/css/theme_extra.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ pre .cs, pre .c {
128128
* Additions specific to the search functionality provided by MkDocs
129129
*/
130130

131-
.search-results article {
131+
.search-results {
132132
margin-top: 23px;
133+
}
134+
135+
.search-results article {
133136
border-top: 1px solid #E1E4E5;
134137
padding-top: 24px;
135138
}

mkdocs/themes/readthedocs/js/theme.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,27 @@ $( document ).ready(function() {
1313

1414
// Keyboard navigation
1515
document.addEventListener("keydown", function(e) {
16-
if ($(e.target).is(':input')) return true;
17-
var key = e.which || e.keyCode || window.event && window.event.keyCode;
18-
var page;
19-
switch (key) {
20-
case 78: // n
21-
page = $('[role="navigation"] a:contains(Next):first').prop('href');
22-
break;
23-
case 80: // p
24-
page = $('[role="navigation"] a:contains(Previous):first').prop('href');
25-
break;
26-
default: break;
27-
}
28-
if (page) window.location.href = page;
16+
var key = e.which || e.keyCode || window.event && window.event.keyCode;
17+
var page;
18+
switch (key) {
19+
case 78: // n
20+
page = $('[role="navigation"] a:contains(Next):first').prop('href');
21+
break;
22+
case 80: // p
23+
page = $('[role="navigation"] a:contains(Previous):first').prop('href');
24+
break;
25+
case 13: // enter
26+
if (e.target === document.getElementById('mkdocs-search-query')) {
27+
e.preventDefault();
28+
}
29+
break;
30+
default: break;
31+
}
32+
if ($(e.target).is(':input')) {
33+
return true;
34+
} else if (page) {
35+
window.location.href = page;
36+
}
2937
});
3038

3139
$(document).on('click', "[data-toggle='rst-current-version']", function() {

0 commit comments

Comments
 (0)