@@ -46,6 +46,7 @@ import {
4646 handleDoubleClick ,
4747 isDoubleClickAllowed ,
4848} from "./double-click/double-click.logic" ;
49+ import { handleAlignToScaleBounds } from "./zoom/zoom.logic" ;
4950
5051type StartCoordsType = { x : number ; y : number } | null ;
5152
@@ -59,7 +60,7 @@ export class ZoomPanPinch {
5960
6061 public transformState : ReactZoomPanPinchState ;
6162 public setup : LibrarySetup ;
62- public observer ? : ResizeObserver ;
63+ public observer : ResizeObserver ;
6364 public onChangeCallbacks : Set < ( ctx : ReactZoomPanPinchRef ) => void > =
6465 new Set ( ) ;
6566 public onInitCallbacks : Set < ( ctx : ReactZoomPanPinchRef ) => void > = new Set ( ) ;
@@ -157,7 +158,7 @@ export class ZoomPanPinch {
157158 document . removeEventListener ( "mouseleave" , this . clearPanning , passive ) ;
158159
159160 handleCancelAnimation ( this ) ;
160- this . observer ? .disconnect ( ) ;
161+ this . observer . disconnect ( ) ;
161162 } ;
162163
163164 handleInitializeWrapperEvents = ( wrapper : HTMLDivElement ) : void => {
@@ -172,34 +173,40 @@ export class ZoomPanPinch {
172173 } ;
173174
174175 handleInitialize = ( contentComponent : HTMLDivElement ) : void => {
176+ let isCentered = false ;
177+
175178 const { centerOnInit } = this . setup ;
176- this . applyTransformation ( ) ;
177- this . onInitCallbacks . forEach ( ( callback ) => callback ( getContext ( this ) ) ) ;
178-
179- if ( centerOnInit ) {
180- this . setCenter ( ) ;
181- this . observer = new ResizeObserver ( ( ) => {
182- const currentWidth = contentComponent . offsetWidth ;
183- const currentHeight = contentComponent . offsetHeight ;
184179
185- if ( currentWidth > 0 || currentHeight > 0 ) {
186- this . onInitCallbacks . forEach ( ( callback ) =>
187- callback ( getContext ( this ) ) ,
188- ) ;
189- this . setCenter ( ) ;
190-
191- this . observer ?. disconnect ( ) ;
180+ this . applyTransformation ( ) ;
181+ this . onInitCallbacks . forEach ( ( callback ) => {
182+ callback ( getContext ( this ) ) ;
183+ } ) ;
184+
185+ 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+ }
202+
203+ break ;
192204 }
193- } ) ;
194-
195- // if nothing about the contentComponent has changed after 5 seconds, disconnect the observer
196- setTimeout ( ( ) => {
197- this . observer ?. disconnect ( ) ;
198- } , 5000 ) ;
205+ }
206+ } ) ;
199207
200- // Start observing the target node for configured mutations
201- this . observer . observe ( contentComponent ) ;
202- }
208+ // Start observing the target node for configured mutations
209+ this . observer . observe ( contentComponent ) ;
203210 } ;
204211
205212 /// ///////
0 commit comments