From 676d3cb6a78c4eccf6bd8f2c9850e8545eb0944e Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 5 Apr 2026 23:27:52 +0200 Subject: [PATCH] fix(pdf): prevent scroll-sync effect from hijacking page navigation The scroll-sync $effect was re-triggering on every dependency change (including currentPage), forcing the PDF back to the annotation's page when the user clicked next/prev. Fix: track prevActiveAnnotationId and only scroll when the active annotation actually changes. Co-Authored-By: Claude Sonnet 4.6 --- frontend/src/lib/components/PdfViewer.svelte | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/frontend/src/lib/components/PdfViewer.svelte b/frontend/src/lib/components/PdfViewer.svelte index f2b45011..e3984d7f 100644 --- a/frontend/src/lib/components/PdfViewer.svelte +++ b/frontend/src/lib/components/PdfViewer.svelte @@ -218,9 +218,16 @@ $effect(() => { }); // Scroll-sync: when activeAnnotationId changes, navigate to its page +let prevActiveAnnotationId: string | null = null; $effect(() => { - if (!activeAnnotationId || !pdfDoc) return; - const ann = annotations.find((a) => a.id === activeAnnotationId); + const id = activeAnnotationId; + if (!id || id === prevActiveAnnotationId || !pdfDoc) { + prevActiveAnnotationId = id; + return; + } + prevActiveAnnotationId = id; + + const ann = annotations.find((a) => a.id === id); if (!ann) return; if (ann.pageNumber !== currentPage) { @@ -228,10 +235,9 @@ $effect(() => { } // After page renders, scroll the annotation into view (double-rAF for async render) - const targetId = activeAnnotationId; requestAnimationFrame(() => { requestAnimationFrame(() => { - const el = document.querySelector(`[data-testid="annotation-${targetId}"]`); + const el = document.querySelector(`[data-testid="annotation-${id}"]`); el?.scrollIntoView({ behavior: 'smooth', block: 'center' }); }); });