From c33fb846abc513aa51ae64afcf875c6284bfc878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arno=C5=A1t=20Pleskot?= Date: Thu, 14 Sep 2023 23:15:57 +0200 Subject: [PATCH] chore: cleanup --- src/components/App.tsx | 66 ++++++++++++++-------------------- src/scene/scrollConstraints.ts | 52 ++++++++------------------- src/types.ts | 2 +- 3 files changed, 42 insertions(+), 78 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index fee2694895..fa5ff00bd8 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -2009,28 +2009,23 @@ class App extends React.Component { ); } - if ( - this.state.scrollConstraints && - this.state.scrollConstraints.isAnimating - ) { - const { shouldAnimate, animateTo } = constrainScrollState(this.state); + if (this.state.scrollConstraints?.animateOnNextUpdate) { + const newState = constrainScrollState(this.state, false); scrollConstraintsAnimationTimeout = setTimeout(() => { this.cancelInProgresAnimation?.(); - if (shouldAnimate && animateTo) { - const fromValues = { - scrollX: this.state.scrollX, - scrollY: this.state.scrollY, - zoom: this.state.zoom.value, - }; - const toValues = { - scrollX: animateTo.scrollX, - scrollY: animateTo.scrollY, - zoom: animateTo.zoom.value, - }; + const fromValues = { + scrollX: this.state.scrollX, + scrollY: this.state.scrollY, + zoom: this.state.zoom.value, + }; + const toValues = { + scrollX: newState.scrollX, + scrollY: newState.scrollY, + zoom: newState.zoom.value, + }; - this.animateToConstrainedArea(fromValues, toValues); - } + this.animateToConstrainedArea(fromValues, toValues); }, 200); } } @@ -2701,22 +2696,10 @@ class App extends React.Component { ...(this.state.scrollConstraints && { // manually reset if setState in onCancel wasn't committed yet shouldCacheIgnoreZoom: false, - scrollConstraints: { - ...this.state.scrollConstraints, - isAnimating: false, - }, }), }; - const { state, shouldAnimate } = constrainScrollState(newState); - - this.setState({ - ...state, - scrollConstraints: { - ...state.scrollConstraints!, - isAnimating: shouldAnimate, - }, - }); + this.setState(constrainScrollState(newState)); }; private animateToConstrainedArea = ( @@ -2728,7 +2711,7 @@ class App extends React.Component { shouldCacheIgnoreZoom: false, scrollConstraints: { ...state.scrollConstraints!, - isAnimating: false, + animateOnNextUpdate: false, }, })); }; @@ -2744,7 +2727,7 @@ class App extends React.Component { shouldCacheIgnoreZoom: true, scrollConstraints: { ...state.scrollConstraints!, - isAnimating: false, + animateOnNextUpdate: false, }, }; }); @@ -8266,10 +8249,13 @@ class App extends React.Component { viewModeEnabled: true, }, () => { - const { animateTo } = constrainScrollState({ - ...this.state, - scrollConstraints, - }); + const newState = constrainScrollState( + { + ...this.state, + scrollConstraints, + }, + false, + ); this.animateToConstrainedArea( { @@ -8278,9 +8264,9 @@ class App extends React.Component { zoom: this.state.zoom.value, }, { - scrollX: animateTo!.scrollX, - scrollY: animateTo!.scrollY, - zoom: animateTo!.zoom.value, + scrollX: newState.scrollX, + scrollY: newState.scrollY, + zoom: newState.zoom.value, }, ); }, diff --git a/src/scene/scrollConstraints.ts b/src/scene/scrollConstraints.ts index 402dc80c0a..a0497170d4 100644 --- a/src/scene/scrollConstraints.ts +++ b/src/scene/scrollConstraints.ts @@ -310,7 +310,7 @@ let memoizedValues: { "zoom" | "width" | "height" | "scrollConstraints" >; constraints: ReturnType; - constraintsWithoutOverscroll: ReturnType; + allowOverscroll: boolean; } | null = null; /** @@ -321,30 +321,24 @@ let memoizedValues: { */ export const constrainScrollState = ( state: AppState, -): { - state: AppState; - shouldAnimate: boolean; - animateTo: { - scrollX: AppState["scrollX"]; - scrollY: AppState["scrollY"]; - zoom: AppState["zoom"]; - } | null; -} => { + allowOverscroll = true, +): AppState => { if (!state.scrollConstraints) { - return { state, shouldAnimate: false, animateTo: null }; + return state; } const { scrollX, scrollY, width, height, scrollConstraints, zoom } = state; const canUseMemoizedValues = - memoizedValues?.previousState.scrollConstraints && // can't use memoized values if there were no scrollConstraints in memoizedValues memoizedValues && // there are memoized values + memoizedValues.previousState.scrollConstraints && // can't use memoized values if there were no scrollConstraints in memoizedValues + memoizedValues.allowOverscroll === allowOverscroll && // allowOverscroll is the same as in memoizedValues + // current scrollConstraints are the same as in memoizedValues isShallowEqual( - // current scrollConstraints are the same as in memoizedValues state.scrollConstraints, memoizedValues.previousState.scrollConstraints!, ) && + // current zoom and window dimensions are equal to those in memoizedValues isShallowEqual( - // current zoom and window dimensions are equal to those in memoizedValues { zoom: zoom.value, width, height }, { zoom: memoizedValues.previousState.zoom.value, @@ -360,17 +354,7 @@ export const constrainScrollState = ( width, height, zoom, - allowOverscroll: true, - }); - - const constraintsWithoutOverscroll = canUseMemoizedValues - ? memoizedValues!.constraintsWithoutOverscroll - : calculateConstraints({ - scrollConstraints, - width, - height, - zoom, - allowOverscroll: false, + allowOverscroll, }); const constrainedValues = constrainScrollValues({ @@ -379,12 +363,6 @@ export const constrainScrollState = ( scrollY, }); - const animateTo = constrainScrollValues({ - ...constraintsWithoutOverscroll, - scrollX, - scrollY, - }); - if (!canUseMemoizedValues) { memoizedValues = { previousState: { @@ -394,16 +372,16 @@ export const constrainScrollState = ( scrollConstraints: state.scrollConstraints, }, constraints, - constraintsWithoutOverscroll, + allowOverscroll, }; } return { - state: { - ...state, - ...constrainedValues, + ...state, + scrollConstraints: { + ...state.scrollConstraints, + animateOnNextUpdate: isViewportOutsideOfConstrainedArea(state), }, - shouldAnimate: isViewportOutsideOfConstrainedArea(state), - animateTo, + ...constrainedValues, }; }; diff --git a/src/types.ts b/src/types.ts index 1859e17e33..e269e37bdf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -661,7 +661,7 @@ export type ScrollConstraints = { y: number; width: number; height: number; - isAnimating?: boolean; + animateOnNextUpdate?: boolean; viewportZoomFactor?: number; lockZoom?: boolean; };