fix: centralize zoom state and guard auto-fit during pinch#62
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The component maintained two separate scale values —
visualScale(for intermediate wheel/pinch steps) andscale(committed value) — that could diverge and cause elements to be positioned or measured with the wrong scale factor. Additionally,onViewportScrollwould 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 modulesrc/utils/zoom.tsandtests/utils/zoom.spec.ts.applyScaleToDocsis now fully absorbed bysyncScaleStateinsidePDFElements, which is the single authoritative place that updatespagesScale.fix: centralize zoom state and guard auto-fit during pinchpendingZoomScale: accumulates fast scroll-wheel ticks between RAF frames so scale always converges correctly under rapid input.syncScaleState(): single method that normalises, clamps, updatespagesScaleon every document, and invalidates the measurement cache. All zoom paths (wheel, pinch,adjustZoomToFit, externalscaleprop mutation) now funnel through it.isSyncingScaleflag: prevents thescalewatcher from re-enteringsyncScaleStatewhen the method itself writes back tothis.scale.onViewportScrollnow checks!isPinchingbefore triggering auto-fit on resize, preventing concurrent auto-fit from fighting an in-progress pinch gesture.adjustZoomToFitacceptsforce = falseso callers withautoFitZoomdisabled can still trigger a one-shot fit.test: add regression tests for zoom state sync and touch resizeRegression tests that prove the bugs exist on the old code and pass on the new code:
pagesScalestays in syncscaleprop mutation triggerssyncScaleStatevia watcherisDraggingElementisPinching; resumes after pinch endsautoFitZoomprop is disabled