Skip to content

fix: centralize zoom state and guard auto-fit during pinch#62

Merged
vitormattos merged 3 commits into
mainfrom
fix/zoom-state-centralization
May 21, 2026
Merged

fix: centralize zoom state and guard auto-fit during pinch#62
vitormattos merged 3 commits into
mainfrom
fix/zoom-state-centralization

Conversation

@vitormattos
Copy link
Copy Markdown
Member

@vitormattos vitormattos commented May 21, 2026

Problem

The component maintained two separate scale values — visualScale (for intermediate wheel/pinch steps) and scale (committed value) — that could diverge and cause elements to be positioned or measured with the wrong scale factor. Additionally, onViewportScroll would trigger an auto-fit recalculation while a pinch gesture was still in progress, creating a race condition between user zoom and auto-fit.

Changes

refactor: remove zoom utility module

  • Deletes src/utils/zoom.ts and tests/utils/zoom.spec.ts.
  • applyScaleToDocs is now fully absorbed by syncScaleState inside PDFElements, which is the single authoritative place that updates pagesScale.

fix: centralize zoom state and guard auto-fit during pinch

  • pendingZoomScale: accumulates fast scroll-wheel ticks between RAF frames so scale always converges correctly under rapid input.
  • syncScaleState(): single method that normalises, clamps, updates pagesScale on every document, and invalidates the measurement cache. All zoom paths (wheel, pinch, adjustZoomToFit, external scale prop mutation) now funnel through it.
  • isSyncingScale flag: prevents the scale watcher from re-entering syncScaleState when the method itself writes back to this.scale.
  • onViewportScroll now checks !isPinching before triggering auto-fit on resize, preventing concurrent auto-fit from fighting an in-progress pinch gesture.
  • adjustZoomToFit accepts force = false so callers with autoFitZoom disabled can still trigger a one-shot fit.

test: add regression tests for zoom state sync and touch resize

Regression tests that prove the bugs exist on the old code and pass on the new code:

Scenario Type
Wheel zoom accumulates and pagesScale stays in sync regression
Direct scale prop mutation triggers syncScaleState via watcher regression
Out-of-range scale value is clamped to supported bounds regression
Auto-fit runs on touch devices when container width changes regression
Auto-fit suppressed while isDraggingElement guard
Auto-fit suppressed while isPinching; resumes after pinch ends regression
Rotation-like resize (portrait → landscape) recomputes optimal scale regression
Width unchanged → auto-fit NOT triggered (negative control) guard
Forced one-shot fit works even when autoFitZoom prop is disabled feature

applyScaleToDocs was the only export of src/utils/zoom.ts.
The responsibility is now fully absorbed by syncScaleState inside
PDFElements, which is the single place that owns the canonical scale
value. Removing the separate module eliminates the indirection and
prevents callers from updating pagesScale without going through the
centralised state-sync path.

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Before this change the component maintained two separate scale values
(visualScale for intermediate wheel/pinch steps and scale as the
committed value) that could diverge and cause elements to be positioned
or measured with the wrong scale factor.  The fix introduces:

- pendingZoomScale: accumulates fast scroll-wheel ticks between RAF
  frames so scale always converges correctly even under rapid input.
- syncScaleState(): single method that normalises, clamps, updates
  pagesScale on every document, and invalidates the measurement cache.
  All zoom paths (wheel, pinch, adjustZoomToFit, external scale prop
  mutation) now funnel through it.
- isSyncingScale flag: prevents the scale watcher from re-entering
  syncScaleState when the method itself writes back to this.scale.
- isTouchDevice flag: detected once at mount; used to guard unrelated
  touch interaction paths.
  on resize, preventing a concurrent auto-fit from fighting an
  in-progress pinch gesture.
- adjustZoomToFit accepts a force=false parameter so callers that
  have autoFitZoom disabled can still trigger a one-shot fit.

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
Covers the bugs fixed in the previous commit:

- rename visualScale references to pendingZoomScale
- wheel zoom accumulates correctly and pagesScale stays in sync
- direct scale prop mutation triggers syncScaleState via watcher
- out-of-range scale value is clamped to supported bounds
- auto-fit runs on touch devices when container width changes
- auto-fit is suppressed while isDraggingElement is true
- auto-fit is suppressed while isPinching is true; resumes after
- rotation-like resize (portrait->landscape) recomputes optimal scale
- auto-fit is not triggered when width is unchanged (negative control)
- forced one-shot fit works even when autoFitZoom prop is disabled

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
@vitormattos vitormattos merged commit 69ce3c6 into main May 21, 2026
4 checks passed
@vitormattos vitormattos deleted the fix/zoom-state-centralization branch May 21, 2026 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant