22 * The timeline runner that runs the animation when previewing.
33 */
44import InteractEditorRunner from './class-runner'
5- import { useRef , useMemo } from '@wordpress/element'
5+ import {
6+ useRef , useState , useEffect ,
7+ } from '@wordpress/element'
68import { cloneDeep } from 'lodash'
79import { getBlockClientId } from './with-tracked-anchors'
810import { doAction } from '@wordpress/hooks'
11+ import { select } from '@wordpress/data'
912
1013// Create the runner.
1114const runner = new InteractEditorRunner ( )
@@ -36,9 +39,13 @@ doAction( 'interact.interaction.types.loaded' )
3639
3740export const useTimelineRunnerRef = ( interaction , actions , timelineIndex ) => {
3841 const runnerRef = useRef ( null )
42+ const [ initialStyles , setInitialStyles ] = useState ( '' )
43+
44+ const renderingMode = select ( 'core/editor' ) . getRenderingMode ( )
45+ const prevRenderingMode = useRef ( renderingMode )
3946
4047 // Initialize the runner
41- const initialStyles = useMemo ( ( ) => {
48+ useEffect ( ( ) => {
4249 // We will need to spawn a new runner for each timeline. Spawning will
4350 // create a new running with the same configuration.
4451 if ( ! runnerRef . current ) {
@@ -52,16 +59,26 @@ export const useTimelineRunnerRef = ( interaction, actions, timelineIndex ) => {
5259 actions : cloneDeep ( actions ) ,
5360 } ]
5461
55- // This replaces all block anchors with the block's ID. The editor
56- // doesn't show block anchors, since it's always in the format of
57- // "block-clientId"
58- replaceBlockAnchorsForEditor ( isolatedInteraction )
62+ const initRunner = ( ) => {
63+ // This replaces all block anchors with the block's ID. The editor
64+ // doesn't show block anchors, since it's always in the format of
65+ // "block-clientId"
66+ replaceBlockAnchorsForEditor ( isolatedInteraction )
67+ runnerRef . current ?. configure ( [ isolatedInteraction ] )
68+ runnerRef . current ?. init ( )
69+ setInitialStyles ( runnerRef . current ?. getStartingActionStyles ( ) || '' )
70+ }
5971
60- runnerRef . current ?. configure ( [ isolatedInteraction ] )
61- runnerRef . current ?. init ( )
72+ // If the rendering mode changed, we need to delay the init to
73+ // avoid race condition tracking the anchors.
74+ if ( prevRenderingMode . current !== renderingMode ) {
75+ requestAnimationFrame ( initRunner )
76+ } else {
77+ initRunner ( )
78+ }
6279
63- return runnerRef . current ?. getStartingActionStyles ( ) || ''
64- } , [ interaction , timelineIndex , actions ] )
80+ prevRenderingMode . current = renderingMode
81+ } , [ interaction , timelineIndex , actions , renderingMode ] )
6582
6683 return [ runnerRef , initialStyles ]
6784}
0 commit comments