@@ -36,6 +36,7 @@ import {
3636 handlePanning ,
3737 handlePanningEnd ,
3838 handlePanningStart ,
39+ handleAlignToBounds ,
3940} from "./pan/panning.logic" ;
4041import {
4142 handlePinchStart ,
@@ -46,7 +47,6 @@ import {
4647 handleDoubleClick ,
4748 isDoubleClickAllowed ,
4849} from "./double-click/double-click.logic" ;
49- import { handleAlignToScaleBounds } from "./zoom/zoom.logic" ;
5050
5151type StartCoordsType = { x : number ; y : number } | null ;
5252
@@ -60,7 +60,7 @@ export class ZoomPanPinch {
6060
6161 public transformState : ReactZoomPanPinchState ;
6262 public setup : LibrarySetup ;
63- public observer : ResizeObserver ;
63+ public observer ? : ResizeObserver ;
6464 public onChangeCallbacks : Set < ( ctx : ReactZoomPanPinchRef ) => void > =
6565 new Set ( ) ;
6666 public onInitCallbacks : Set < ( ctx : ReactZoomPanPinchRef ) => void > = new Set ( ) ;
@@ -158,7 +158,7 @@ export class ZoomPanPinch {
158158 document . removeEventListener ( "mouseleave" , this . clearPanning , passive ) ;
159159
160160 handleCancelAnimation ( this ) ;
161- this . observer . disconnect ( ) ;
161+ this . observer ? .disconnect ( ) ;
162162 } ;
163163
164164 handleInitializeWrapperEvents = ( wrapper : HTMLDivElement ) : void => {
@@ -172,40 +172,50 @@ export class ZoomPanPinch {
172172 wrapper . addEventListener ( "touchend" , this . onTouchPanningStop , passive ) ;
173173 } ;
174174
175- handleInitialize = ( contentComponent : HTMLDivElement ) : void => {
175+ handleInitialize = (
176+ wrapper : HTMLDivElement ,
177+ contentComponent : HTMLDivElement ,
178+ ) : void => {
176179 let isCentered = false ;
177-
180+
178181 const { centerOnInit } = this . setup ;
179182
183+ const hasTarget = ( entries : ResizeObserverEntry [ ] , target : Element ) => {
184+ for ( const entry of entries ) {
185+ if ( entry . target === target ) {
186+ return true ;
187+ }
188+ }
189+
190+ return false ;
191+ } ;
192+
180193 this . applyTransformation ( ) ;
181194 this . onInitCallbacks . forEach ( ( callback ) => {
182195 callback ( getContext ( this ) ) ;
183196 } ) ;
184197
185198 this . observer = new ResizeObserver ( ( entries ) => {
186- for ( const entry of entries ) {
187- if ( entry . target === contentComponent ) {
188- if ( centerOnInit && ! isCentered ) {
189- const currentWidth = contentComponent . offsetWidth ;
190- const currentHeight = contentComponent . offsetHeight ;
191-
192- if ( currentWidth > 0 || currentHeight > 0 ) {
193- isCentered = true ;
194-
195- this . setCenter ( ) ;
196- }
197- } else {
198- const { pinchMidpoint } = this ;
199-
200- handleAlignToScaleBounds ( this , pinchMidpoint ?. x , pinchMidpoint ?. y ) ;
201- }
199+ if ( hasTarget ( entries , wrapper ) || hasTarget ( entries , contentComponent ) ) {
200+ if ( centerOnInit && ! isCentered ) {
201+ const currentWidth = contentComponent . offsetWidth ;
202+ const currentHeight = contentComponent . offsetHeight ;
202203
203- break ;
204+ if ( currentWidth > 0 || currentHeight > 0 ) {
205+ isCentered = true ;
206+
207+ this . setCenter ( ) ;
208+ }
209+ } else {
210+ handleCancelAnimation ( this ) ;
211+ handleCalculateBounds ( this , this . transformState . scale ) ;
212+ handleAlignToBounds ( this , 0 ) ;
204213 }
205214 }
206215 } ) ;
207216
208217 // Start observing the target node for configured mutations
218+ this . observer . observe ( wrapper ) ;
209219 this . observer . observe ( contentComponent ) ;
210220 } ;
211221
@@ -565,7 +575,7 @@ export class ZoomPanPinch {
565575 this . contentComponent = contentComponent ;
566576 handleCalculateBounds ( this , this . transformState . scale ) ;
567577 this . handleInitializeWrapperEvents ( wrapperComponent ) ;
568- this . handleInitialize ( contentComponent ) ;
578+ this . handleInitialize ( wrapperComponent , contentComponent ) ;
569579 this . initializeWindowEvents ( ) ;
570580 this . isInitialized = true ;
571581 const ctx = getContext ( this ) ;
0 commit comments