Search improvements (#4393)

* * In search, clicking on excerpt text goes to the corresponding anchor, if there is one.
* Fix image paths in search
* Bump pagefind version

* Refine JS
This commit is contained in:
Clyde Stubbs 2024-10-28 16:12:24 +11:00 committed by GitHub
parent bac5e2ab82
commit 7ce07b9512
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 16 deletions

View File

@ -1,6 +1,6 @@
ESPHOME_PATH = ../esphome ESPHOME_PATH = ../esphome
ESPHOME_REF = 2024.10.2 ESPHOME_REF = 2024.10.2
PAGEFIND_VERSION=1.1.0 PAGEFIND_VERSION=1.1.1
PAGEFIND=pagefind PAGEFIND=pagefind
NET_PAGEFIND=../pagefindbin/pagefind NET_PAGEFIND=../pagefindbin/pagefind

View File

@ -60,7 +60,7 @@
if (!inpel) if (!inpel)
return; return;
var mobileWidth = getComputedStyle(document.body).getPropertyValue("--mobile-width-stop"); const mobileWidth = getComputedStyle(document.body).getPropertyValue("--mobile-width-stop");
function isMobile() { function isMobile() {
return window.innerWidth <= mobileWidth; return window.innerWidth <= mobileWidth;
@ -74,26 +74,42 @@
const margin = 20; const margin = 20;
const resultTemplate = (result) => { function getLink(location, anchors, url) {
let wrapper = new El("li").class("pagefind-modular-list-result"); if (!anchors || !anchors.length)
return null;
// find the closest anchor at or before the current location
const anchor = anchors.sort((a, b) => b.location - a.location).find(a => a.location <= location);
if (anchor) {
return url + "#" + anchor.id;
}
return null;
}
let thumb = new El("div").class("pagefind-modular-list-thumb").addTo(wrapper); const resultTemplate = (result) => {
if (result?.meta?.image) { const wrapper = new El("li").class("pagefind-modular-list-result");
const thumb = new El("div").class("pagefind-modular-list-thumb").addTo(wrapper);
let image = result?.meta?.image;
if (image) {
if (image.includes("/_images/"))
image = image.substring(image.indexOf("/_images/"));
new El("img").class("pagefind-modular-list-image").attrs({ new El("img").class("pagefind-modular-list-image").attrs({
src: result.meta.image, src: image,
alt: result.meta.image_alt || result.meta.title alt: result.meta.image_alt || result.meta.title
}).addTo(thumb); }).addTo(thumb);
} }
let inner = new El("div").class("pagefind-modular-list-inner").addTo(wrapper); const inner = new El("div").class("pagefind-modular-list-inner").addTo(wrapper);
let title = new El("p").class("pagefind-modular-list-title").addTo(inner); const title = new El("p").class("pagefind-modular-list-title").addTo(inner);
new El("a").class("pagefind-modular-list-link").text(result.meta?.title).attrs({ new El("a").class("pagefind-modular-list-link").text(result.meta?.title).attrs({
href: result.meta?.url || result.url href: result.meta?.url || result.url
}).addTo(title); }).addTo(title);
let excerpt = new El("p").class("pagefind-modular-list-excerpt").addTo(inner); const excerpt = new El("p").class("pagefind-modular-list-excerpt").addTo(inner);
const locations = result.weighted_locations.sort((a, b) => b.weight - a.weight);
const url = getLink(locations[0]?.location, result.anchors, result.url) || result.meta?.url || result.url;
new El("a").class("pagefind-modular-list-link").html(result.excerpt).attrs({ new El("a").class("pagefind-modular-list-link").html(result.excerpt).attrs({
href: result.meta?.url || result.url href: url
}).addTo(excerpt); }).addTo(excerpt);
return wrapper.element; return wrapper.element;
@ -102,9 +118,9 @@
function resizeTarget() { function resizeTarget() {
const searchPos = inpel.getBoundingClientRect(); const searchPos = inpel.getBoundingClientRect();
var leftPos; let leftPos;
var topPos; let topPos;
var maxWidth = 650; const maxWidth = 650;
target.style.width = "auto;" target.style.width = "auto;"
target.style.height = "fit-content"; target.style.height = "fit-content";
target.style.maxWidth = maxWidth + "px"; target.style.maxWidth = maxWidth + "px";
@ -171,7 +187,7 @@
const clickCallback = (event) => { const clickCallback = (event) => {
const path = event.composedPath(); const path = event.composedPath();
if (path.includes(target) || path.includes(inpel)) if (path.includes(inpel))
return; return;
hideTargets(); hideTargets();
}; };
@ -184,5 +200,14 @@
} }
}); });
} }
const input_field = document.getElementById("pfmod-input-0");
if (input_field) {
input_field.focus({preventScroll: true});
input_field.addEventListener("blur", () => {
requestAnimationFrame(() => {
input_field.focus({preventScroll: true});
});
})
}
}); });
</script> </script>

View File

@ -5,6 +5,6 @@ exclude_selectors:
- ".toctree-wrapper" - ".toctree-wrapper"
- ".sphinxsidebar" - ".sphinxsidebar"
- ".breadcrumbs" - ".breadcrumbs"
- "pre"
glob: "{components,cookbook,guides,projects,web-api}/**/*.html" glob: "{components,cookbook,guides,projects,web-api}/**/*.html"
root_selector: div[role=main] root_selector: div[role=main]