Update web/templates/entry_view.html

This commit is contained in:
Joshua Laymon 2025-08-23 16:04:04 +00:00
parent 37d2531364
commit fd12a809de

View File

@ -467,37 +467,10 @@ function showToast(message, duration = 3000) {
}
</script>
<!-- Highlighter: uses exact DB field names -->
<!-- Highlighter: robust triggers (DOMContentLoaded + pageshow + readyState) -->
<script>
document.addEventListener("DOMContentLoaded", async () => {
// Respect per-user toggle
let enabled = true;
try {
const res = await fetch("/api/get-prefs/");
const prefs = await res.json();
if (prefs && typeof prefs.highlight_search_hits !== "undefined") {
enabled = !!prefs.highlight_search_hits;
}
} catch (_) {}
if (!enabled) return;
// Read last search (prefer JS-literal fallback, then JSON blob)
let q = (window.__lastSearchQ || "").trim();
let fields = Array.isArray(window.__lastSearchFields) ? window.__lastSearchFields : [];
if ((!q || !fields.length)) {
const dataEl = document.getElementById("last-search-data");
if (dataEl) {
try {
const payload = JSON.parse(dataEl.textContent || "{}");
q = (payload.q || "").trim();
fields = Array.isArray(payload.fields) ? payload.fields : [];
} catch (_) {}
}
}
if (!q || !fields.length) return;
// Exact field -> selector mapping for your template IDs
(function(){
// EXACT field -> selector mapping for your template IDs
const fieldToSelector = {
subject: "#subject-list",
illustration: "#illustration-text",
@ -508,17 +481,55 @@ document.addEventListener("DOMContentLoaded", async () => {
talk_number: "#talk_title-text",
};
// Tokenize like your search (keep quoted phrases, strip * and ?)
const tokens = tokenize(q).map(t => t.replaceAll("*","").replaceAll("?","")).filter(Boolean);
if (!tokens.length) return;
// Kickoff with multiple safety nets so it also runs after bfcache restores
if (document.readyState === "complete" || document.readyState === "interactive") {
run();
} else {
document.addEventListener("DOMContentLoaded", run, { once: true });
}
// pageshow fires on bfcache restores and some soft navigations
window.addEventListener("pageshow", run);
// Highlight only within fields that were searched
for (const f of fields) {
const sel = fieldToSelector[f];
if (!sel) continue; // ignore fields not shown on page
const container = document.querySelector(sel);
if (!container) continue;
for (const tok of tokens) highlightAll(container, tok);
async function run() {
// Respect per-user toggle
let enabled = true;
try {
const res = await fetch("/api/get-prefs/");
const prefs = await res.json();
if (prefs && typeof prefs.highlight_search_hits !== "undefined") {
enabled = !!prefs.highlight_search_hits;
}
} catch (_) {}
if (!enabled) return;
// Read last search (prefer JS-literal fallback, then JSON blob)
let q = (window.__lastSearchQ || "").trim();
let fields = Array.isArray(window.__lastSearchFields) ? window.__lastSearchFields : [];
if ((!q || !fields.length)) {
const dataEl = document.getElementById("last-search-data");
if (dataEl) {
try {
const payload = JSON.parse(dataEl.textContent || "{}");
q = (payload.q || "").trim();
fields = Array.isArray(payload.fields) ? payload.fields : [];
} catch (_) {}
}
}
if (!q || !fields.length) return;
// Tokenize like your search (keep quoted phrases, strip * and ?)
const tokens = tokenize(q).map(t => t.replaceAll("*","").replaceAll("?","")).filter(Boolean);
if (!tokens.length) return;
// Highlight only within fields that were searched
for (const f of fields) {
const sel = fieldToSelector[f];
if (!sel) continue;
const container = document.querySelector(sel);
if (!container) continue;
for (const tok of tokens) highlightAll(container, tok);
}
}
// ---- helpers ----
@ -572,7 +583,8 @@ document.addEventListener("DOMContentLoaded", async () => {
while (walker.nextNode()) nodes.push(walker.currentNode);
nodes.forEach(cb);
}
});
})();
</script>
{% endblock %}