Skip to content

Commit 7fcd411

Browse files
committed
Clicking search results preserves the page history
1 parent ea4c9c4 commit 7fcd411

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

lib/docs/main.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@
7979
};
8080
var curNavSearch = "";
8181
var curSearchIndex = -1;
82-
var imFeelingLucky = false;
82+
// When true: load the search result indicated by `curSearchIndex`, or the first result if
83+
// `curSearchIndex` isn't valid.
84+
var loadSearchResult = false;
8385

8486
// names of modules in the same order as wasm
8587
const moduleList = [];
@@ -696,12 +698,25 @@
696698
domSearch.value = curNavSearch;
697699
}
698700
render();
699-
if (imFeelingLucky) {
700-
imFeelingLucky = false;
701+
if (loadSearchResult) {
702+
loadSearchResult = false;
701703
activateSelectedResult();
702704
}
703705
}
704706

707+
function doLoadSearchResult() {
708+
clearAsyncSearch();
709+
loadSearchResult = true;
710+
const old_hash = location.hash;
711+
location.hash = computeSearchHash();
712+
if (location.hash === old_hash) {
713+
// With certain sequences of history navigation and input, setting location.hash here
714+
// causes no change, and the enter key isn't acted on until another modification is made
715+
// to the search text. Force navigation to work around this.
716+
navigate(location.hash);
717+
}
718+
}
719+
705720
function activateSelectedResult() {
706721
if (domSectSearchResults.classList.contains("hidden")) {
707722
return;
@@ -720,19 +735,22 @@
720735
domSearch.blur();
721736
}
722737

738+
function onSearchResultClick(ev) {
739+
const liDom = ev.target.parentElement;
740+
const search_index = Array.from(domListSearchResults.children).indexOf(liDom);
741+
curSearchIndex = search_index;
742+
doLoadSearchResult();
743+
744+
ev.preventDefault();
745+
ev.stopPropagation();
746+
}
747+
723748
function onSearchKeyDown(ev) {
724749
switch (ev.code) {
725750
case "Enter":
726751
if (ev.shiftKey || ev.ctrlKey || ev.altKey) return;
727752

728-
clearAsyncSearch();
729-
imFeelingLucky = true;
730-
location.hash = computeSearchHash();
731-
// With certain sequences of history navigation and input, setting location.hash here
732-
// causes no change, and the enter key isn't acted on until another modification is made
733-
// to the search text. Force navigation to work around this.
734-
navigate(location.hash);
735-
753+
doLoadSearchResult();
736754
ev.preventDefault();
737755
ev.stopPropagation();
738756
return;
@@ -879,6 +897,7 @@
879897
const full_name = fullyQualifiedName(match);
880898
aDom.textContent = full_name;
881899
aDom.setAttribute('href', navLinkFqn(full_name));
900+
aDom.addEventListener("click", onSearchResultClick);
882901
}
883902
renderSearchCursor();
884903

0 commit comments

Comments
 (0)