Skip to content

Commit aefb091

Browse files
authored
Merge pull request #12445 from r00ster91/noresults
autodoc: better No Results Found page and other improvements
2 parents 95573db + 78bb29d commit aefb091

File tree

2 files changed

+62
-33
lines changed

2 files changed

+62
-33
lines changed

lib/docs/index.html

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@
169169
width: 100%;
170170
margin-bottom: 0.8rem;
171171
padding: 0.5rem;
172-
font-size: 1rem;
173172
font-family: var(--ui);
173+
font-size: 1rem;
174174
color: var(--tx-color);
175175
background-color: var(--search-bg-color);
176176
border-top: 0;
@@ -339,8 +339,8 @@
339339
kbd {
340340
display: inline-block;
341341
padding: 0.3em 0.2em;
342-
font-size: 1.2em;
343-
font-size: var(--mono);
342+
font-family: var(--mono);
343+
font-size: 1em;
344344
line-height: 0.8em;
345345
vertical-align: middle;
346346
color: #000;
@@ -612,7 +612,7 @@ <h2><span>Zig Version</span></h2>
612612
</div>
613613
</nav>
614614
</div>
615-
<div class="flex-right">
615+
<div id="docs" class="flex-right">
616616
<div class="wrap">
617617
<section class="docs">
618618
<div style="position: relative">
@@ -654,7 +654,13 @@ <h2>Search Results</h2>
654654
</div>
655655
<div id="sectSearchNoResults" class="hidden">
656656
<h2>No Results Found</h2>
657-
<p>Press escape to exit search and then '?' to see more options.</p>
657+
<p>Here are some things you can try:</p>
658+
<ul>
659+
<li>Check out the <a id="langRefLink">Language Reference</a> for the language itself.</li>
660+
<li>Check out the <a href="https://ziglang.org/learn/">Learn page</a> for other helpful resources for learning Zig.</li>
661+
<li>Use your search engine.</li>
662+
</ul>
663+
<p>Press <kbd>?</kbd> to see keyboard shortcuts and <kbd>Esc</kbd> to return.</p>
658664
</div>
659665
<div id="sectFields" class="hidden">
660666
<h2>Fields</h2>
@@ -716,11 +722,13 @@ <h2>Tests</h2>
716722
<div class="modal">
717723
<h1>Keyboard Shortcuts</h1>
718724
<dl><dt><kbd>?</kbd></dt><dd>Show this help modal</dd></dl>
719-
<dl><dt><kbd>Esc</kbd></dt><dd>Clear focus; close this modal</dd></dl>
720725
<dl><dt><kbd>s</kbd></dt><dd>Focus the search field</dd></dl>
721-
<dl><dt><kbd></kbd></dt><dd>Move up in search results</dd></dl>
722-
<dl><dt><kbd></kbd></dt><dd>Move down in search results</dd></dl>
723-
<dl><dt><kbd></kbd></dt><dd>Go to active search result</dd></dl>
726+
<div style="margin-left: 1em">
727+
<dl><dt><kbd></kbd></dt><dd>Move up in search results</dd></dl>
728+
<dl><dt><kbd></kbd></dt><dd>Move down in search results</dd></dl>
729+
<dl><dt><kbd></kbd></dt><dd>Go to active search result</dd></dl>
730+
</div>
731+
<dl><dt><kbd>Esc</kbd></dt><dd>Clear focus; close this modal</dd></dl>
724732
</div>
725733
</div>
726734
</div>

lib/docs/main.js

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var zigAnalysis;
4141
const domSearch = document.getElementById("search");
4242
const domSectSearchResults = document.getElementById("sectSearchResults");
4343
const domSectSearchAllResultsLink = document.getElementById("sectSearchAllResultsLink");
44-
44+
const domDocs = document.getElementById("docs");
4545
const domListSearchResults = document.getElementById("listSearchResults");
4646
const domSectSearchNoResults = document.getElementById("sectSearchNoResults");
4747
const domSectInfo = document.getElementById("sectInfo");
@@ -51,6 +51,7 @@ var zigAnalysis;
5151
const domHdrName = document.getElementById("hdrName");
5252
const domHelpModal = document.getElementById("helpModal");
5353
const domSearchPlaceholder = document.getElementById("searchPlaceholder");
54+
const domLangRefLink = document.getElementById("langRefLink");
5455

5556
let searchTimer = null;
5657
let searchTrimResults = true;
@@ -116,10 +117,10 @@ var zigAnalysis;
116117
});
117118
domSectSearchAllResultsLink.addEventListener('click', onClickSearchShowAllResults, false);
118119
function onClickSearchShowAllResults(ev) {
119-
ev.preventDefault();
120-
ev.stopPropagation();
121-
searchTrimResults = false;
122-
onHashChange();
120+
ev.preventDefault();
121+
ev.stopPropagation();
122+
searchTrimResults = false;
123+
onHashChange();
123124
}
124125

125126
domPrivDeclsBox.addEventListener(
@@ -161,6 +162,13 @@ var zigAnalysis;
161162
window.addEventListener("keydown", onWindowKeyDown, false);
162163
onHashChange();
163164

165+
let langRefVersion = zigAnalysis.params.zigVersion;
166+
if (!/^\d+\.\d+\.\d+$/.test(langRefVersion)) {
167+
// the version is probably not released yet
168+
langRefVersion = "master";
169+
}
170+
domLangRefLink.href = `https://ziglang.org/documentation/${langRefVersion}/`;
171+
164172
function renderTitle() {
165173
let list = curNav.pkgNames.concat(curNav.declNames);
166174
let suffix = " - Zig";
@@ -3140,6 +3148,23 @@ var zigAnalysis;
31403148
domSearch.blur();
31413149
}
31423150

3151+
// hide the modal if it's visible or return to the previous result page and unfocus the search
3152+
function onEscape(ev) {
3153+
if (!domHelpModal.classList.contains("hidden")) {
3154+
domHelpModal.classList.add("hidden");
3155+
ev.preventDefault();
3156+
ev.stopPropagation();
3157+
} else {
3158+
domSearch.value = "";
3159+
domSearch.blur();
3160+
domSearchPlaceholder.classList.remove("hidden");
3161+
curSearchIndex = -1;
3162+
ev.preventDefault();
3163+
ev.stopPropagation();
3164+
startSearch();
3165+
}
3166+
}
3167+
31433168
function onSearchKeyDown(ev) {
31443169
switch (getKeyString(ev)) {
31453170
case "Enter":
@@ -3156,19 +3181,15 @@ var zigAnalysis;
31563181
ev.stopPropagation();
31573182
return;
31583183
case "Esc":
3159-
domSearch.value = "";
3160-
domSearch.blur();
3161-
curSearchIndex = -1;
3162-
ev.preventDefault();
3163-
ev.stopPropagation();
3164-
startSearch();
3165-
return;
3184+
onEscape(ev);
3185+
return
31663186
case "Up":
31673187
moveSearchCursor(-1);
31683188
ev.preventDefault();
31693189
ev.stopPropagation();
31703190
return;
31713191
case "Down":
3192+
// TODO: make the page scroll down if the search cursor is out of the screen
31723193
moveSearchCursor(1);
31733194
ev.preventDefault();
31743195
ev.stopPropagation();
@@ -3237,19 +3258,18 @@ var zigAnalysis;
32373258
function onWindowKeyDown(ev) {
32383259
switch (getKeyString(ev)) {
32393260
case "Esc":
3240-
if (!domHelpModal.classList.contains("hidden")) {
3241-
domHelpModal.classList.add("hidden");
3261+
onEscape(ev);
3262+
break;
3263+
case "s":
3264+
if (domHelpModal.classList.contains("hidden")) {
3265+
domSearch.focus();
3266+
domSearch.select();
3267+
domDocs.scrollTo(0, 0);
32423268
ev.preventDefault();
32433269
ev.stopPropagation();
3270+
startAsyncSearch();
32443271
}
32453272
break;
3246-
case "s":
3247-
domSearch.focus();
3248-
domSearch.select();
3249-
ev.preventDefault();
3250-
ev.stopPropagation();
3251-
startAsyncSearch();
3252-
break;
32533273
case "?":
32543274
ev.preventDefault();
32553275
ev.stopPropagation();
@@ -3265,6 +3285,7 @@ var zigAnalysis;
32653285
domHelpModal.style.top =
32663286
window.innerHeight / 2 - domHelpModal.clientHeight / 2 + "px";
32673287
domHelpModal.focus();
3288+
domSearch.blur();
32683289
}
32693290

32703291
function clearAsyncSearch() {
@@ -3290,7 +3311,7 @@ var zigAnalysis;
32903311
list.sort();
32913312
return list;
32923313
}
3293-
3314+
32943315
function renderSearch() {
32953316
let matchedItems = [];
32963317
let ignoreCase = curNavSearch.toLowerCase() === curNavSearch;
@@ -3379,13 +3400,13 @@ var zigAnalysis;
33793400
const text = lastPkgName + "." + match.path.declNames.join(".");
33803401
const href = navLink(match.path.pkgNames, match.path.declNames);
33813402

3382-
matchedItemsHTML += "<li><a href=\""+ href +"\">"+ text + "</a></li>";
3403+
matchedItemsHTML += "<li><a href=\"" + href + "\">" + text + "</a></li>";
33833404
}
33843405

33853406
// Replace the search results using our newly constructed HTML string
33863407
domListSearchResults.innerHTML = matchedItemsHTML;
33873408
if (searchTrimmed) {
3388-
domSectSearchAllResultsLink.classList.remove("hidden");
3409+
domSectSearchAllResultsLink.classList.remove("hidden");
33893410
}
33903411
renderSearchCursor();
33913412

0 commit comments

Comments
 (0)