@@ -37,7 +37,7 @@ const DEFAULT_CONTENT = { ...DEFAULT }
3737export const usePreviewRenderer = (
3838 props , previewSize , plan , spacingSize ,
3939 selectedTab , selectedNum , selectedData ,
40- ref , shadowRoot , setIsLoading
40+ ref , hostRef , shadowRoot , setIsLoading
4141) => {
4242 const {
4343 designId,
@@ -61,64 +61,89 @@ export const usePreviewRenderer = (
6161 const hasBackgroundTargetRef = useRef ( false )
6262 const initialRenderRef = useRef ( null )
6363 const shadowBodySizeRef = useRef ( null )
64- const prevEnableBackgroundRef = useRef ( false )
64+ const prevEnableBackgroundRef = useRef ( null )
6565 const prevSelectedTabRef = useRef ( selectedTab )
66+ const adjustAnimateFrameRef = useRef ( null )
6667
6768 const siteTitle = useSelect ( select => select ( 'core' ) . getEntityRecord ( 'root' , 'site' ) ?. title || 'InnovateCo' , [ ] )
6869 const isDesignLibraryDevMode = devMode && localStorage . getItem ( 'stk__design_library__dev_mode' ) === '1'
6970
7071 const addHasBackground = selectedTab === 'patterns'
7172
72- const adjustScale = ( ) => {
73- const shouldAdjust = ref . current && shadowRoot &&
73+ const adjustScale = ( force = true ) => {
74+ const parentDiv = ref ?. current ?. querySelector ( '.stk-block-design__design-container' )
75+ const shouldAdjust = ref . current && hostRef . current && shadowRoot && parentDiv &&
7476 ( ! selectedNum || // adjust if design is not selected
7577 prevSelectedTabRef . current !== selectedTab ) // adjust if selected tab changed even if design is selected
7678
77- if ( shouldAdjust ) {
78- const newPreviewSize = { ...previewSize }
79- const newCardHeight = { ...cardHeight }
80- const cardRect = ref . current . getBoundingClientRect ( )
79+ if ( ! shouldAdjust ) {
80+ return
81+ }
82+ const newPreviewSize = { ...previewSize }
83+ const newCardHeight = { ...cardHeight }
8184
82- const shadowBody = shadowRoot . querySelector ( 'body' )
83- if ( shadowBody ) {
84- const cardWidth = cardRect . width // Get width of the card
85- const scaleFactor = cardWidth > 0 ? cardWidth / 1300 : 1 // Divide by 1300, which is the width of preview in the shadow DOM
86- newPreviewSize . scale = scaleFactor
85+ const cardRect = ref . current . getBoundingClientRect ( )
86+ const hostRect = hostRef . current . getBoundingClientRect ( )
87+ const parentDivRect = parentDiv . getBoundingClientRect ( )
8788
88- let _bodyHeight = 1200
89- if ( selectedTab === 'patterns' ) {
90- _bodyHeight = shadowBody . offsetHeight
91- }
89+ const cardWidth = cardRect . width
90+ const hostWidth = hostRect . width
9291
93- const _height = parseFloat ( _bodyHeight ) * scaleFactor // Also adjust the height
92+ // Consider heights equal if the difference is less than 1px
93+ const isEqualHeight = Math . abs ( parentDivRect . height - hostRect . height ) < 1
9494
95- if ( Object . keys ( newPreviewSize ) . length === 1 ) {
96- newPreviewSize . heightBackground = _height
97- newPreviewSize . heightNoBackground = _height
98- } else {
99- const heightKey = enableBackground ? 'heightBackground' : 'heightNoBackground'
100- newPreviewSize [ heightKey ] = _height
101- }
95+ if ( ! force && cardWidth === hostWidth && isEqualHeight ) {
96+ if ( adjustAnimateFrameRef . current !== null ) {
97+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
98+ }
99+ adjustAnimateFrameRef . current = null
100+ return
101+ }
102102
103- setPreviewSize ( newPreviewSize )
103+ const shadowBody = shadowRoot . querySelector ( 'body' )
104+ if ( shadowBody ) {
105+ const cardWidth = cardRect . width // Get width of the card
106+ const scaleFactor = cardWidth > 0 ? cardWidth / 1300 : 1 // Divide by 1300, which is the width of preview in the shadow DOM
107+ newPreviewSize . scale = scaleFactor
104108
105- shadowBodySizeRef . current = {
106- clientHeight : shadowBody . clientHeight ,
107- scrollHeight : shadowBody . scrollHeight ,
108- maxScrollTop : shadowBody . scrollHeight - shadowBody . clientHeight ,
109- }
109+ let _bodyHeight = 1200
110+ if ( selectedTab === 'patterns' ) {
111+ _bodyHeight = shadowBody . offsetHeight
110112 }
111113
112- if ( ! Object . keys ( newCardHeight ) . length ) {
113- newCardHeight . background = cardRect . height
114- newCardHeight . noBackground = cardRect . height
114+ const _height = parseFloat ( _bodyHeight ) * scaleFactor // Also adjust the height
115+
116+ if ( Object . keys ( newPreviewSize ) . length === 1 ) {
117+ newPreviewSize . heightBackground = _height
118+ newPreviewSize . heightNoBackground = _height
115119 } else {
116- const CardHeightKey = enableBackground ? 'background' : 'noBackground'
117- newCardHeight [ CardHeightKey ] = cardRect . height
120+ const heightKey = enableBackground ? 'heightBackground' : 'heightNoBackground'
121+ newPreviewSize [ heightKey ] = _height
122+ }
123+
124+ setPreviewSize ( newPreviewSize )
125+
126+ shadowBodySizeRef . current = {
127+ clientHeight : shadowBody . clientHeight ,
128+ scrollHeight : shadowBody . scrollHeight ,
129+ maxScrollTop : shadowBody . scrollHeight - shadowBody . clientHeight ,
118130 }
131+ }
132+
133+ if ( ! Object . keys ( newCardHeight ) . length ) {
134+ newCardHeight . background = cardRect . height
135+ newCardHeight . noBackground = cardRect . height
136+ } else {
137+ const CardHeightKey = enableBackground ? 'background' : 'noBackground'
138+ newCardHeight [ CardHeightKey ] = cardRect . height
139+ }
140+
141+ setTimeout ( ( ) => setCardHeight ( newCardHeight ) , 500 )
119142
120- setTimeout ( ( ) => setCardHeight ( newCardHeight ) , 500 )
143+ if ( adjustAnimateFrameRef . current !== null ) {
144+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
121145 }
146+ adjustAnimateFrameRef . current = requestAnimationFrame ( ( ) => adjustScale ( false ) )
122147 }
123148
124149 const renderPreview = ( blockContent = content ) => {
@@ -274,7 +299,10 @@ export const usePreviewRenderer = (
274299 useEffect ( ( ) => {
275300 if ( selectedNum === 0 && content && shadowRoot ) {
276301 renderPreview ( )
277- setTimeout ( adjustScale , 100 )
302+ if ( adjustAnimateFrameRef . current !== null ) {
303+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
304+ }
305+ adjustAnimateFrameRef . current = requestAnimationFrame ( adjustScale )
278306 }
279307 } , [ selectedNum ] )
280308
@@ -285,7 +313,11 @@ export const usePreviewRenderer = (
285313
286314 if ( prevEnableBackgroundRef . current !== enableBackground ) {
287315 prevEnableBackgroundRef . current = enableBackground
288- adjustScale ( )
316+
317+ if ( adjustAnimateFrameRef . current !== null ) {
318+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
319+ }
320+ adjustAnimateFrameRef . current = requestAnimationFrame ( adjustScale )
289321 }
290322 } , [ blocks ] )
291323
@@ -294,12 +326,25 @@ export const usePreviewRenderer = (
294326 if ( ! content || ! blocks . parsed || ! blocks . serialized ) {
295327 return
296328 }
297- setTimeout ( ( ) => {
329+
330+ if ( adjustAnimateFrameRef . current !== null ) {
331+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
332+ }
333+
334+ adjustAnimateFrameRef . current = requestAnimationFrame ( ( ) => {
298335 adjustScale ( )
299336 prevSelectedTabRef . current = selectedTab
300- } , 100 )
337+ } )
301338 } , [ content ] )
302339
340+ // cleanup any pending animation on unmount
341+ useEffect ( ( ) => {
342+ return ( ) => {
343+ cancelAnimationFrame ( adjustAnimateFrameRef . current )
344+ adjustAnimateFrameRef . current = null
345+ }
346+ } , [ ] )
347+
303348 const onClickDesign = ( ) => {
304349 if ( ! isPro && plan !== 'free' ) {
305350 return
@@ -319,6 +364,6 @@ export const usePreviewRenderer = (
319364 return {
320365 blocks : blocks . serialized , enableBackground,
321366 shadowBodySizeRef, blocksForSubstitutionRef,
322- adjustScale , onClickDesign,
367+ onClickDesign,
323368 }
324369}
0 commit comments