From 2fa13cf4e038e595b745ff3822364f895ad17fb9 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 21 Feb 2025 14:25:58 +0100 Subject: [PATCH] Add a spinner when search is in progress --- src/front-end/css/chrome.css | 27 ++++++++++++++++++++++++++- src/front-end/css/variables.css | 1 + src/front-end/searcher/searcher.js | 20 +++++++++++++------- src/front-end/templates/index.hbs | 7 ++++++- tests/gui/search.goml | 5 +---- 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/front-end/css/chrome.css b/src/front-end/css/chrome.css index 360a6537..69db06a1 100644 --- a/src/front-end/css/chrome.css +++ b/src/front-end/css/chrome.css @@ -344,9 +344,34 @@ mark.fade-out { max-width: var(--content-max-width); } +#searchbar-outer.searching #searchbar { + padding-right: 30px; +} +#searchbar-outer .spinner-wrapper { + display: none; +} +#searchbar-outer.searching .spinner-wrapper { + display: block; +} + +.search-wrapper { + position: relative; +} + +.spinner-wrapper { + --spinner-margin: 2px; + position: absolute; + margin-block-start: calc(var(--searchbar-margin-block-start) + var(--spinner-margin)); + right: var(--spinner-margin); + top: 0; + bottom: var(--spinner-margin); + padding: 6px; + background-color: var(--bg); +} + #searchbar { width: 100%; - margin-block-start: 5px; + margin-block-start: var(--searchbar-margin-block-start); margin-block-end: 0; margin-inline-start: auto; margin-inline-end: auto; diff --git a/src/front-end/css/variables.css b/src/front-end/css/variables.css index 5742d241..3854b3a7 100644 --- a/src/front-end/css/variables.css +++ b/src/front-end/css/variables.css @@ -11,6 +11,7 @@ --menu-bar-height: 50px; --mono-font: "Source Code Pro", Consolas, "Ubuntu Mono", Menlo, "DejaVu Sans Mono", monospace, monospace; --code-font-size: 0.875em; /* please adjust the ace font size accordingly in editor.js */ + --searchbar-margin-block-start: 5px; } /* Themes */ diff --git a/src/front-end/searcher/searcher.js b/src/front-end/searcher/searcher.js index 5e4de4be..e92c1c85 100644 --- a/src/front-end/searcher/searcher.js +++ b/src/front-end/searcher/searcher.js @@ -22,6 +22,7 @@ window.search = window.search || {}; } const search_wrap = document.getElementById('search-wrapper'), + searchbar_outer = document.getElementById('searchbar-outer'), searchbar = document.getElementById('searchbar'), searchresults = document.getElementById('searchresults'), searchresults_outer = document.getElementById('searchresults-outer'), @@ -262,12 +263,13 @@ window.search = window.search || {}; doc_urls = config.doc_urls; searchindex = elasticlunr.Index.load(config.index); - searchbar.removeAttribute("disabled"); + searchbar_outer.classList.remove('searching'); + searchbar.focus(); const searchterm = searchbar.value.trim(); - if (searchterm !== "") { - searchbar.classList.add("active"); + if (searchterm !== '') { + searchbar.classList.add('active'); doSearch(searchterm); } } @@ -414,10 +416,12 @@ window.search = window.search || {}; } } - function loadScript(url, id) { + function loadSearchScript(url, id) { if (document.getElementById(id)) { return; } + searchbar_outer.classList.add('searching'); + const script = document.createElement('script'); script.src = url; script.id = id; @@ -430,7 +434,7 @@ window.search = window.search || {}; function showSearch(yes) { if (yes) { - loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index'); + loadSearchScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index'); search_wrap.classList.remove('hidden'); searchicon.setAttribute('aria-expanded', 'true'); } else { @@ -511,10 +515,11 @@ window.search = window.search || {}; function doSearch(searchterm) { // Don't search the same twice - if (current_searchterm == searchterm) { + if (current_searchterm === searchterm) { return; } - if (searchindex == null) { + searchbar_outer.classList.add('searching'); + if (searchindex === null) { return; } @@ -538,6 +543,7 @@ window.search = window.search || {}; // Display results showResults(true); + searchbar_outer.classList.remove('searching'); } // Exported functions diff --git a/src/front-end/templates/index.hbs b/src/front-end/templates/index.hbs index 00d9d199..13950326 100644 --- a/src/front-end/templates/index.hbs +++ b/src/front-end/templates/index.hbs @@ -186,7 +186,12 @@ {{#if search_enabled}}