Skip to content
Source
Sign inSign up

threadFindPosition.ts

23 lines1019 Bytes

1import type { ThreadFindStart } from "./threadFind";
2import { collectThreadFindRanges } from "./threadFindHighlights";
3​
4export function readThreadFindPosition(
5 container: HTMLElement,
6 query: string,
7 resolveEntry: (rowId: string) => string | undefined,
8 contentInsetEndAdjustment: number,
9): ThreadFindStart | undefined {
10 const viewport = container.getBoundingClientRect();
11 for (const row of container.querySelectorAll<HTMLElement>("[data-timeline-row-id]")) {
12 const rect = row.getBoundingClientRect();
13 if (rect.bottom <= viewport.top || rect.top >= viewport.bottom - contentInsetEndAdjustment)
14 continue;
15 const rowId = row.dataset.timelineRowId;
16 const entryId = rowId ? resolveEntry(rowId) : undefined;
17 if (!entryId) continue;
18 const matches = collectThreadFindRanges(row, query);
19 const next = matches.find((match) => match.range.getBoundingClientRect().bottom > viewport.top);
20 return { entryId, occurrence: next?.occurrence ?? matches.length };
21 }
22 return undefined;