fix: search results displayed in left sidebar, not content area

This commit is contained in:
Accusys
2026-05-22 10:09:32 +08:00
parent fc338a4b59
commit a2042507a3

View File

@@ -39,6 +39,7 @@ html, body { height: 100%; }
<h1>Momentry Docs</h1>
<input id="search" type="text" placeholder="搜尋模組..." style="width:100%;padding:8px;margin-bottom:12px;border:1px solid #ddd;border-radius:6px;font-size:13px;box-sizing:border-box">
<div id="module-list"></div>
<div id="search-results" style="display:none;margin-top:8px"></div>
<div class="logout">
<a id="logout-btn">Logout</a>
</div>
@@ -154,7 +155,6 @@ function highlightText(text, query) {
}
async function fulltextSearch(q) {
el.innerHTML = '<p>Searching...</p>';
var results = [];
for (var i = 0; i < MODULES.length; i++) {
var m = MODULES[i];
@@ -171,18 +171,30 @@ async function fulltextSearch(q) {
}
} catch(e) {}
}
var target = document.getElementById('search-results');
if (!target) return;
if (results.length === 0) {
el.innerHTML = '<p>No results for <strong>' + escapeHtml(q) + '</strong></p>';
target.innerHTML = '<p style="font-size:13px;color:#888">No results for <strong>' + escapeHtml(q) + '</strong></p>';
el.innerHTML = '<p>No search results</p>';
return;
}
var html = '<h2>Search: ' + escapeHtml(q) + '</h2><p>' + results.length + ' result(s)</p>';
var html = '<p style="font-size:13px;font-weight:600;color:#333;margin-bottom:8px">' + results.length + ' result(s) for <strong>' + escapeHtml(q) + '</strong></p>';
for (var r of results) {
html += '<p style="margin:4px 0"><a href="#" onclick="loadDoc(\'' + r.module + '\');return false" style="font-weight:600">'
+ r.module + ' ' + escapeHtml(r.title) + '</a> <span style="color:#888;font-size:12px">line ' + r.line + '</span><br>'
+ '<code style="background:#f5f5f5;padding:2px 6px;border-radius:3px;font-size:12px;display:inline-block;max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap">'
+ highlightText(r.text, q) + '</code></p>';
html += '<div style="margin-bottom:8px;padding:6px;border-radius:4px;background:#f9f9f9;border:1px solid #eee;cursor:pointer" onclick="showSearchResult(\'' + r.module + '\');return false">'
+ '<span style="font-size:12px;font-weight:600;color:#0066cc">' + escapeHtml(r.module) + ' ' + escapeHtml(r.title) + '</span>'
+ ' <span style="color:#888;font-size:11px">line ' + r.line + '</span><br>'
+ '<span style="font-size:11px;color:#555;display:inline-block;max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap">'
+ highlightText(r.text, q) + '</span></div>';
}
el.innerHTML = html;
target.innerHTML = html;
el.innerHTML = '<p style="color:#888">' + results.length + ' result(s) — click a result in the sidebar to open</p>';
}
function showSearchResult(module) {
document.getElementById('search').value = '';
document.getElementById('search-results').style.display = 'none';
document.getElementById('module-list').style.display = '';
loadDoc(module);
}
async function loginUser(user, pass) {
@@ -233,14 +245,25 @@ async function initApp() {
// Search — full text across all modules
var searchTimer = null;
var searchEl = document.getElementById('search');
var searchResultsEl = document.getElementById('search-results');
var moduleListEl = document.getElementById('module-list');
if (searchEl) {
searchEl.oninput = function() {
var q = this.value.toLowerCase().trim();
document.querySelectorAll('#module-list a').forEach(function(a) {
a.style.display = a.textContent.toLowerCase().indexOf(q) >= 0 ? '' : 'none';
});
if (q.length < 2) {
moduleListEl.style.display = '';
searchResultsEl.style.display = 'none';
document.querySelectorAll('#module-list a').forEach(function(a) {
a.style.display = a.textContent.toLowerCase().indexOf(q) >= 0 ? '' : 'none';
});
if (searchTimer) clearTimeout(searchTimer);
if (!q) loadDoc(location.hash.slice(1) || '01_auth');
return;
}
moduleListEl.style.display = 'none';
searchResultsEl.style.display = 'block';
searchResultsEl.innerHTML = '<p style="font-size:13px;color:#888">Searching...</p>';
if (searchTimer) clearTimeout(searchTimer);
if (q.length < 2) { loadDoc(location.hash.slice(1) || '01_auth'); return; }
searchTimer = setTimeout(function() { fulltextSearch(q); }, 300);
};
}