diff --git a/src/renderer/src/components/file-annotator/index.tsx b/src/renderer/src/components/file-annotator/index.tsx index 3301b478..efb80a1e 100644 --- a/src/renderer/src/components/file-annotator/index.tsx +++ b/src/renderer/src/components/file-annotator/index.tsx @@ -170,13 +170,23 @@ export default function FileAnnotator({ file, isAnnotable = false }: Props) { if (!activeSearchMatchId) return; const timer = window.setTimeout(() => { - const element = document.querySelector( - `[data-search-match-id="${activeSearchMatchId}"]`, - ); - element?.scrollIntoView({ + const container = fileRef.current; + const element = Array.from( + container?.querySelectorAll("[data-search-match-id]") ?? [], + ).find((el) => el.getAttribute("data-search-match-id") === activeSearchMatchId); + if (!element || !container) return; + + const containerRect = container.getBoundingClientRect(); + const elementRect = element.getBoundingClientRect(); + const scrollOffset = + elementRect.top - + containerRect.top - + containerRect.height / 2 + + elementRect.height / 2; + + container.scrollBy({ + top: scrollOffset, behavior: "smooth", - block: "center", - inline: "nearest", }); }, 50);