Skip to content

Commit 94f9a9c

Browse files
authored
Merge pull request #2553 from GuillaumeGomez/load-on-need
Only load searchindex when needed
2 parents 1b046e5 + dc6b0a6 commit 94f9a9c

File tree

5 files changed

+113
-18
lines changed

5 files changed

+113
-18
lines changed

src/front-end/css/chrome.css

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,34 @@ mark.fade-out {
344344
max-width: var(--content-max-width);
345345
}
346346

347+
#searchbar-outer.searching #searchbar {
348+
padding-right: 30px;
349+
}
350+
#searchbar-outer .spinner-wrapper {
351+
display: none;
352+
}
353+
#searchbar-outer.searching .spinner-wrapper {
354+
display: block;
355+
}
356+
357+
.search-wrapper {
358+
position: relative;
359+
}
360+
361+
.spinner-wrapper {
362+
--spinner-margin: 2px;
363+
position: absolute;
364+
margin-block-start: calc(var(--searchbar-margin-block-start) + var(--spinner-margin));
365+
right: var(--spinner-margin);
366+
top: 0;
367+
bottom: var(--spinner-margin);
368+
padding: 6px;
369+
background-color: var(--bg);
370+
}
371+
347372
#searchbar {
348373
width: 100%;
349-
margin-block-start: 5px;
374+
margin-block-start: var(--searchbar-margin-block-start);
350375
margin-block-end: 0;
351376
margin-inline-start: auto;
352377
margin-inline-end: auto;

src/front-end/css/variables.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
--menu-bar-height: 50px;
1212
--mono-font: "Source Code Pro", Consolas, "Ubuntu Mono", Menlo, "DejaVu Sans Mono", monospace, monospace;
1313
--code-font-size: 0.875em; /* please adjust the ace font size accordingly in editor.js */
14+
--searchbar-margin-block-start: 5px;
1415
}
1516

1617
/* Themes */

src/front-end/searcher/searcher.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ window.search = window.search || {};
2222
}
2323

2424
const search_wrap = document.getElementById('search-wrapper'),
25+
searchbar_outer = document.getElementById('searchbar-outer'),
2526
searchbar = document.getElementById('searchbar'),
2627
searchresults = document.getElementById('searchresults'),
2728
searchresults_outer = document.getElementById('searchresults-outer'),
@@ -262,6 +263,18 @@ window.search = window.search || {};
262263
doc_urls = config.doc_urls;
263264
searchindex = elasticlunr.Index.load(config.index);
264265

266+
searchbar_outer.classList.remove('searching');
267+
268+
searchbar.focus();
269+
270+
const searchterm = searchbar.value.trim();
271+
if (searchterm !== '') {
272+
searchbar.classList.add('active');
273+
doSearch(searchterm);
274+
}
275+
}
276+
277+
function initSearchInteractions(config) {
265278
// Set up events
266279
searchicon.addEventListener('click', () => {
267280
searchIconClickHandler();
@@ -288,6 +301,8 @@ window.search = window.search || {};
288301
config.hasFocus = hasFocus;
289302
}
290303

304+
initSearchInteractions(window.search);
305+
291306
function unfocusSearchbar() {
292307
// hacky, but just focusing a div only works once
293308
const tmp = document.createElement('input');
@@ -401,8 +416,25 @@ window.search = window.search || {};
401416
}
402417
}
403418

419+
function loadSearchScript(url, id) {
420+
if (document.getElementById(id)) {
421+
return;
422+
}
423+
searchbar_outer.classList.add('searching');
424+
425+
const script = document.createElement('script');
426+
script.src = url;
427+
script.id = id;
428+
script.onload = () => init(window.search);
429+
script.onerror = error => {
430+
console.error(`Failed to load \`${url}\`: ${error}`);
431+
};
432+
document.head.append(script);
433+
}
434+
404435
function showSearch(yes) {
405436
if (yes) {
437+
loadSearchScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
406438
search_wrap.classList.remove('hidden');
407439
searchicon.setAttribute('aria-expanded', 'true');
408440
} else {
@@ -485,14 +517,14 @@ window.search = window.search || {};
485517
// Don't search the same twice
486518
if (current_searchterm === searchterm) {
487519
return;
488-
} else {
489-
current_searchterm = searchterm;
490520
}
491-
521+
searchbar_outer.classList.add('searching');
492522
if (searchindex === null) {
493523
return;
494524
}
495525

526+
current_searchterm = searchterm;
527+
496528
// Do the actual search
497529
const results = searchindex.search(searchterm, search_options);
498530
const resultcount = Math.min(results.length, results_options.limit_results);
@@ -511,19 +543,9 @@ window.search = window.search || {};
511543

512544
// Display results
513545
showResults(true);
546+
searchbar_outer.classList.remove('searching');
514547
}
515548

516-
function loadScript(url, id) {
517-
const script = document.createElement('script');
518-
script.src = url;
519-
script.id = id;
520-
script.onload = () => init(window.search);
521-
script.onerror = error => {
522-
console.error(`Failed to load \`${url}\`: ${error}`);
523-
};
524-
document.head.append(script);
525-
}
526-
527-
loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');
528-
549+
// Exported functions
550+
search.hasFocus = hasFocus;
529551
})(window.search);

src/front-end/templates/index.hbs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@
186186
{{#if search_enabled}}
187187
<div id="search-wrapper" class="hidden">
188188
<form id="searchbar-outer" class="searchbar-outer">
189-
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
189+
<div class="search-wrapper">
190+
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
191+
<div class="spinner-wrapper">
192+
<i class="fa fa-spinner fa-spin"></i>
193+
</div>
194+
</div>
190195
</form>
191196
<div id="searchresults-outer" class="searchresults-outer hidden">
192197
<div id="searchresults-header" class="searchresults-header"></div>

tests/gui/search.goml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,45 @@ assert-text: ("#searchresults-header", "")
2828
call-function: ("open-search", {})
2929
write: "strikethrough"
3030
wait-for-text: ("#searchresults-header", "2 search results for 'strikethrough':")
31+
32+
// Now we test search shortcuts and more page changes.
33+
go-to: |DOC_PATH| + "index.html"
34+
35+
// This check is to ensure that the search bar is inside the search wrapper.
36+
assert: "#search-wrapper #searchbar"
37+
assert-css: ("#search-wrapper", {"display": "none"})
38+
39+
// Now we make sure the search input appear with the `S` shortcut.
40+
press-key: 's'
41+
wait-for-css-false: ("#search-wrapper", {"display": "none"})
42+
// We ensure the search bar has the focus.
43+
assert: "#searchbar:focus"
44+
45+
// Now we press `Escape` to ensure that the search input disappears again.
46+
press-key: 'Escape'
47+
wait-for-css: ("#search-wrapper", {"display": "none"})
48+
49+
// Making it appear by clicking on the search button.
50+
click: "#search-toggle"
51+
wait-for-css: ("#search-wrapper", {"display": "block"})
52+
// We ensure the search bar has the focus.
53+
assert: "#searchbar:focus"
54+
55+
// We input "test".
56+
write: "test"
57+
// The results should now appear.
58+
wait-for-text: ("#searchresults-header", "search results for 'test':", ENDS_WITH)
59+
assert: "#searchresults"
60+
// Ensure that the URL was updated as well.
61+
assert-document-property: ({"URL": "?search=test"}, ENDS_WITH)
62+
63+
// Now we ensure that when we land on the page with a "search in progress", the search results are
64+
// loaded and that the search input has focus.
65+
go-to: |DOC_PATH| + "index.html?search=test"
66+
wait-for-text: ("#searchresults-header", "search results for 'test':", ENDS_WITH)
67+
assert: "#searchbar:focus"
68+
assert: "#searchresults"
69+
70+
// And now we press `Escape` to close everything.
71+
press-key: 'Escape'
72+
wait-for-css: ("#search-wrapper", {"display": "none"})

0 commit comments

Comments
 (0)