diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 733f3418f98d42..9036b27a77f972 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2,6 +2,7 @@ * WordPress dependencies */ import { + getActiveBlockVariation, getBlockType, getBlockTypes, getBlockVariations, @@ -1650,10 +1651,26 @@ const isBlockVisibleInTheInserter = ( let current = rootClientId; let hasParent = false; do { - if ( parents.includes( getBlockName( state, current ) ) ) { + const ancestorBlockName = getBlockName( state, current ); + if ( parents.includes( ancestorBlockName ) ) { hasParent = true; break; } + const ancestorAttributes = getBlockAttributes( state, current ); + + const variation = getActiveBlockVariation( + ancestorBlockName, + ancestorAttributes, + 'inserter' + ); + + if ( variation ) { + // TODO: Compare block variation name to parent/ancestor list. + // Problem: For template parts that have been persisted to the DB, + // variation.name is prefixed with `instance_` (e.g. `instance_header`). + console.log( variation ); + } + current = state.blocks.parents.get( current ); } while ( current ); diff --git a/packages/blocks/src/api/index.js b/packages/blocks/src/api/index.js index 88ebf0036ced32..b571f9d0b904a7 100644 --- a/packages/blocks/src/api/index.js +++ b/packages/blocks/src/api/index.js @@ -131,6 +131,7 @@ export { getBlockSupport, hasBlockSupport, getBlockVariations, + getActiveBlockVariation, isReusableBlock, isTemplatePart, getChildBlockNames, diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index 42fba15241200a..cc15bcf33c791c 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -707,6 +707,26 @@ export const getBlockVariations = ( blockName, scope ) => { return select( blocksStore ).getBlockVariations( blockName, scope ); }; +/** + * Returns the active block variation for a given block based on its attributes. + * Ignored from documentation as the recommended usage is via useSelect from @wordpress/data. + * + * @ignore + * + * @param {string} blockName Name of block (example: “core/columns”). + * @param {Object} attributes Block attributes used to determine active variation. + * @param {WPBlockVariationScope} [scope] Block variation scope name. + * + * @return {(WPBlockVariation|undefined)} Active block variation. + */ +export const getActiveBlockVariation = ( blockName, attributes, scope ) => { + return select( blocksStore ).getActiveBlockVariation( + blockName, + attributes, + scope + ); +}; + /** * Registers a new block variation for the given block type. *