This commit is contained in:
Andrew Pimenov 2026-09-04 10:13:51 +03:00 • committed by GitHub
commit 34cac462ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -44,57 +44,55 @@ export function parseResultQuery(
return { op: m[1], threshold: v }; return { op: m[1], threshold: v };
} }
export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] { function searchNumeric(deps: SearchDeps, docs: SearchDoc[], rq: { op: string; threshold: Value }): SearchHit[] {
const q = query.trim(); if (rq.threshold.kind !== "quantity") return [];
if (!q) return []; const th = rq.threshold.value;
const docs = deps.docs(); const hits: SearchHit[] = [];
const rq = parseResultQuery(deps.engine, q); for (const doc of docs) {
if (rq && rq.threshold.kind === "quantity") { const results = deps.workspace.getCachedResults(doc);
const th = rq.threshold.value; let lines: string[] | undefined;
const hits: SearchHit[] = []; for (let i = 0; i < results.length; i++) {
for (const doc of docs) { const r = results[i];
const results = deps.workspace.getCachedResults(doc); if (!r.value || r.value.kind !== "quantity") continue;
let lines: string[] | undefined; const v = r.value.value;
for (let i = 0; i < results.length; i++) { let match = false;
const r = results[i]; switch (rq.op) {
if (!r.value || r.value.kind !== "quantity") continue; case ">":
const v = r.value.value; match = v.gt(th);
let match = false; break;
switch (rq.op) { case ">=":
case ">": match = v.gte(th);
match = v.gt(th); break;
break; case "<":
case ">=": match = v.lt(th);
match = v.gte(th); break;
break; case "<=":
case "<": match = v.lte(th);
match = v.lt(th); break;
break; case "=":
case "<=": match = v.eq(th);
match = v.lte(th); break;
break; case "~":
case "=": match = !th.isZero() && v.minus(th).abs().div(th.abs()).lte(0.01);
match = v.eq(th); break;
break; }
case "~": if (match) {
match = !th.isZero() && v.minus(th).abs().div(th.abs()).lte(0.01); if (!lines) lines = doc.text.split("\n");
break; hits.push({
} docId: doc.id,
if (match) { docTitle: doc.title,
if (!lines) lines = doc.text.split("\n"); line: i + 1,
hits.push({ text: lines[i] ?? "",
docId: doc.id, result: r.text ?? undefined,
docTitle: doc.title, });
line: i + 1, if (hits.length >= 200) return hits;
text: lines[i] ?? "",
result: r.text ?? undefined,
});
if (hits.length >= 200) return hits;
}
} }
} }
return hits;
} }
return hits;
}
function searchText(docs: SearchDoc[], q: string): SearchHit[] {
const ql = q.toLowerCase(); const ql = q.toLowerCase();
const hits: SearchHit[] = []; const hits: SearchHit[] = [];
for (const doc of docs) { for (const doc of docs) {
@ -114,6 +112,17 @@ export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
return hits; return hits;
} }
export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
const q = query.trim();
if (!q) return [];
const docs = deps.docs();
const rq = parseResultQuery(deps.engine, q);
if (rq && rq.threshold.kind === "quantity") {
return searchNumeric(deps, docs, rq);
}
return searchText(docs, q);
}
export function initSearch(deps: SearchDeps): SearchController { export function initSearch(deps: SearchDeps): SearchController {
const overlay = $("#search-overlay"); const overlay = $("#search-overlay");
const input = $<HTMLInputElement>("#search-input"); const input = $<HTMLInputElement>("#search-input");