[fix] theme/simple: broken highlightResult

From `mod-simple-strict`
This commit is contained in:
Ivan Gabaldon 2025-07-29 16:51:44 +02:00 committed by Markus Heiser
parent 7e1c7096ce
commit adc4361eb9

View File

@ -72,13 +72,13 @@ const keyBindingLayouts: Record<KeyBindingLayout, Record<string, KeyBinding>> =
default: { default: {
ArrowLeft: { ArrowLeft: {
key: "←", key: "←",
fun: () => highlightResult("up"), fun: () => highlightResult("up")(),
des: "select previous search result", des: "select previous search result",
cat: "Results" cat: "Results"
}, },
ArrowRight: { ArrowRight: {
key: "→", key: "→",
fun: () => highlightResult("down"), fun: () => highlightResult("down")(),
des: "select next search result", des: "select next search result",
cat: "Results" cat: "Results"
}, },
@ -113,13 +113,13 @@ const keyBindingLayouts: Record<KeyBindingLayout, Record<string, KeyBinding>> =
}, },
j: { j: {
key: "j", key: "j",
fun: () => highlightResult("down"), fun: () => highlightResult("down")(),
des: "select next search result", des: "select next search result",
cat: "Results" cat: "Results"
}, },
k: { k: {
key: "k", key: "k",
fun: () => highlightResult("up"), fun: () => highlightResult("up")(),
des: "select previous search result", des: "select previous search result",
cat: "Results" cat: "Results"
}, },
@ -155,7 +155,7 @@ const isElementInDetail = (element?: Element): boolean => {
return ancestor?.classList.contains("detail") ?? false; return ancestor?.classList.contains("detail") ?? false;
}; };
const getResultElement = (element?: Element): Element | undefined => { const getResultElement = (element?: HTMLElement): HTMLElement | undefined => {
return element?.closest(".result") ?? undefined; return element?.closest(".result") ?? undefined;
}; };
@ -164,10 +164,10 @@ const isImageResult = (resultElement?: Element): boolean => {
}; };
const highlightResult = const highlightResult =
(which: string | Element) => (which: string | HTMLElement) =>
(noScroll?: boolean, keepFocus?: boolean): void => { (noScroll?: boolean, keepFocus?: boolean): void => {
let current = document.querySelector<HTMLElement>(".result[data-vim-selected]");
let effectiveWhich = which; let effectiveWhich = which;
let current = document.querySelector<HTMLElement>(".result[data-vim-selected]");
if (!current) { if (!current) {
// no selection : choose the first one // no selection : choose the first one
current = document.querySelector<HTMLElement>(".result"); current = document.querySelector<HTMLElement>(".result");
@ -181,9 +181,10 @@ const highlightResult =
} }
} }
let next: Element | null | undefined = null;
const results = Array.from(document.querySelectorAll<HTMLElement>(".result")); const results = Array.from(document.querySelectorAll<HTMLElement>(".result"));
let next: HTMLElement | undefined;
if (typeof effectiveWhich !== "string") { if (typeof effectiveWhich !== "string") {
next = effectiveWhich; next = effectiveWhich;
} else { } else {
@ -192,14 +193,11 @@ const highlightResult =
const top = document.documentElement.scrollTop || document.body.scrollTop; const top = document.documentElement.scrollTop || document.body.scrollTop;
const bot = top + document.documentElement.clientHeight; const bot = top + document.documentElement.clientHeight;
for (let i = 0; i < results.length; i++) { for (const element of results) {
const element = results[i] as HTMLElement;
next = element;
const etop = element.offsetTop; const etop = element.offsetTop;
const ebot = etop + element.clientHeight; const ebot = etop + element.clientHeight;
if (ebot <= bot && etop > top) { if (ebot <= bot && etop > top) {
next = element;
break; break;
} }
} }
@ -221,15 +219,15 @@ const highlightResult =
} }
} }
if (next && current) { if (next) {
current.removeAttribute("data-vim-selected"); current.removeAttribute("data-vim-selected");
next.setAttribute("data-vim-selected", "true"); next.setAttribute("data-vim-selected", "true");
if (!keepFocus) { if (!keepFocus) {
const link = next.querySelector<HTMLElement>("h3 a") || next.querySelector<HTMLElement>("a"); const link = next.querySelector<HTMLAnchorElement>("h3 a") || next.querySelector<HTMLAnchorElement>("a");
if (link) { link?.focus();
link.focus();
}
} }
if (!noScroll) { if (!noScroll) {
scrollPageToSelected(); scrollPageToSelected();
} }
@ -415,11 +413,11 @@ const copyURLToClipboard = async (): Promise<void> => {
}; };
searxng.ready(() => { searxng.ready(() => {
searxng.listen("click", ".result", function (this: Element, event: Event) { searxng.listen("click", ".result", function (this: HTMLElement, event: Event) {
if (!isElementInDetail(event.target as Element)) { if (!isElementInDetail(event.target as HTMLElement)) {
highlightResult(this)(true, true); highlightResult(this)(true, true);
const resultElement = getResultElement(event.target as Element); const resultElement = getResultElement(event.target as HTMLElement);
if (resultElement && isImageResult(resultElement)) { if (resultElement && isImageResult(resultElement)) {
event.preventDefault(); event.preventDefault();
@ -432,8 +430,8 @@ searxng.ready(() => {
"focus", "focus",
".result a", ".result a",
(event: Event) => { (event: Event) => {
if (!isElementInDetail(event.target as Element)) { if (!isElementInDetail(event.target as HTMLElement)) {
const resultElement = getResultElement(event.target as Element); const resultElement = getResultElement(event.target as HTMLElement);
if (resultElement && !resultElement.getAttribute("data-vim-selected")) { if (resultElement && !resultElement.getAttribute("data-vim-selected")) {
highlightResult(resultElement)(true); highlightResult(resultElement)(true);