Update web/templates/entry_view.html
This commit is contained in:
@@ -467,37 +467,10 @@ function showToast(message, duration = 3000) {
|
|||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Highlighter: uses exact DB field names -->
|
<!-- Highlighter: robust triggers (DOMContentLoaded + pageshow + readyState) -->
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener("DOMContentLoaded", async () => {
|
(function(){
|
||||||
// Respect per-user toggle
|
// EXACT field -> selector mapping for your template IDs
|
||||||
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
|
|
||||||
const fieldToSelector = {
|
const fieldToSelector = {
|
||||||
subject: "#subject-list",
|
subject: "#subject-list",
|
||||||
illustration: "#illustration-text",
|
illustration: "#illustration-text",
|
||||||
@@ -508,17 +481,55 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||||||
talk_number: "#talk_title-text",
|
talk_number: "#talk_title-text",
|
||||||
};
|
};
|
||||||
|
|
||||||
// Tokenize like your search (keep quoted phrases, strip * and ?)
|
// Kickoff with multiple safety nets so it also runs after bfcache restores
|
||||||
const tokens = tokenize(q).map(t => t.replaceAll("*","").replaceAll("?","")).filter(Boolean);
|
if (document.readyState === "complete" || document.readyState === "interactive") {
|
||||||
if (!tokens.length) return;
|
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
|
async function run() {
|
||||||
for (const f of fields) {
|
// Respect per-user toggle
|
||||||
const sel = fieldToSelector[f];
|
let enabled = true;
|
||||||
if (!sel) continue; // ignore fields not shown on page
|
try {
|
||||||
const container = document.querySelector(sel);
|
const res = await fetch("/api/get-prefs/");
|
||||||
if (!container) continue;
|
const prefs = await res.json();
|
||||||
for (const tok of tokens) highlightAll(container, tok);
|
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 ----
|
// ---- helpers ----
|
||||||
@@ -572,7 +583,8 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||||||
while (walker.nextNode()) nodes.push(walker.currentNode);
|
while (walker.nextNode()) nodes.push(walker.currentNode);
|
||||||
nodes.forEach(cb);
|
nodes.forEach(cb);
|
||||||
}
|
}
|
||||||
});
|
})();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
Reference in New Issue
Block a user