diff --git a/src/hooks/use-block-context.js b/src/hooks/use-block-context.js index bdb2246e7..ae33c2b97 100644 --- a/src/hooks/use-block-context.js +++ b/src/hooks/use-block-context.js @@ -20,7 +20,7 @@ import { * WordPress dependencies */ import { - subscribe, select, createReduxStore, register, dispatch, useSelect, + select, createReduxStore, register, useSelect, } from '@wordpress/data' import { useBlockEditContext } from '@wordpress/block-editor' @@ -140,44 +140,6 @@ register( createReduxStore( 'stackable/block-context', { selectors: STORE_SELECTORS, } ) ) -// This holds the current tree of client ids, we check against this if the -// block/content structure has changed. -let prevClientIds = null -// let oldAllClientIds = null - -// Subscribe to all editor changes, so we can listen in to block structure -// changes. -subscribe( () => { - const tree = select( 'stackable/block-editor' )?.getClientTree() - if ( tree && ! prevClientIds ) { - prevClientIds = tree - const blocks = fixReusableInnerBlocks( tree ) - dispatch( 'stackable/block-context' ).setBlockTree( blocks ) - return - } - - // We can do a direct comparison here since the object being returned - // isn't changed unless the client id tree is changed, so this holds up - // even when blocks are edited. - if ( tree && tree !== prevClientIds ) { - prevClientIds = tree - const blocks = fixReusableInnerBlocks( tree ) - dispatch( 'stackable/block-context' ).setBlockTree( blocks ) - } -} ) - -// Use to correct the blocks returned from getBlocks. -// Applies only core/block (reusable blocks) - Adds missing innerBlocks -const fixReusableInnerBlocks = blocks => { - return ( blocks || [] ).map( block => { - return { - ...block, - innerBlocks: fixReusableInnerBlocks( block.innerBlocks ), - name: select( 'core/block-editor' ).getBlockName( block.clientId ), - } - } ) -} - // The default context if none is found. This can be true when the block is // being previewed as an example. const DEFAULT_CONTEXT = { diff --git a/src/plugins/get-client-id-tree/store.js b/src/plugins/get-client-id-tree/store.js index b777618f4..b788434e6 100644 --- a/src/plugins/get-client-id-tree/store.js +++ b/src/plugins/get-client-id-tree/store.js @@ -4,7 +4,7 @@ import { register, createReduxStore, - select, + select, dispatch, } from '@wordpress/data' // Include all the stored state. @@ -49,10 +49,26 @@ const getUnmemoizedClientTree = rootClientId => { return result } +// Use to correct the blocks returned from getBlocks. +// Applies only core/block (reusable blocks) - Adds missing innerBlocks +const fixReusableInnerBlocks = blocks => { + return ( blocks || [] ).map( block => { + return { + ...block, + innerBlocks: fixReusableInnerBlocks( block.innerBlocks ), + name: select( 'core/block-editor' ).getBlockName( block.clientId ), + } + } ) +} + const STORE_REDUCER = ( state = DEFAULT_STATE, action ) => { switch ( action.type ) { case 'UPDATE_CLIENT_TREE': { const tree = getUnmemoizedClientTree() + if ( tree ) { + const blocks = fixReusableInnerBlocks( tree ) + dispatch( 'stackable/block-context' ).setBlockTree( blocks ) + } return { ...state,