Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 51 additions & 30 deletions packages/block-library/src/query/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,48 @@ import { __ } from '@wordpress/i18n';
*/
import QueryContent from './query-content';
import QueryPlaceholder from './query-placeholder';
import { getTransformedBlocksFromPattern } from '../utils';
import {
useBlockNameForPatterns,
getTransformedBlocksFromPattern,
} from '../utils';

const QueryEdit = ( props ) => {
const { clientId, name, attributes } = props;
const { clientId, attributes } = props;
const [ isPatternSelectionModalOpen, setIsPatternSelectionModalOpen ] =
useState( false );
const { replaceBlock, selectBlock } = useDispatch( blockEditorStore );
const hasInnerBlocks = useSelect(
( select ) =>
!! select( blockEditorStore ).getBlocks( clientId ).length,
[ clientId ]
);
const Component = hasInnerBlocks ? QueryContent : QueryPlaceholder;
return (
<>
<Component
{ ...props }
openPatternSelectionModal={ () =>
setIsPatternSelectionModalOpen( true )
}
/>
{ isPatternSelectionModalOpen && (
<PatternSelectionModal
clientId={ clientId }
attributes={ attributes }
setIsPatternSelectionModalOpen={
setIsPatternSelectionModalOpen
}
/>
) }
</>
);
};

function PatternSelectionModal( {
clientId,
attributes,
setIsPatternSelectionModalOpen,
} ) {
const { replaceBlock, selectBlock } = useDispatch( blockEditorStore );
const onBlockPatternSelect = ( blocks ) => {
const { newBlocks, queryClientIds } = getTransformedBlocksFromPattern(
blocks,
Expand All @@ -47,34 +76,26 @@ const QueryEdit = ( props ) => {
} ),
[ attributes.query.postType ]
);
const blockNameForPatterns = useBlockNameForPatterns(
clientId,
attributes
);
return (
<>
<Component
{ ...props }
openPatternSelectionModal={ () =>
setIsPatternSelectionModalOpen( true )
}
/>
{ isPatternSelectionModalOpen && (
<Modal
className="block-editor-query-pattern__selection-modal"
title={ __( 'Choose a pattern' ) }
closeLabel={ __( 'Cancel' ) }
onRequestClose={ () =>
setIsPatternSelectionModalOpen( false )
}
>
<BlockContextProvider value={ blockPreviewContext }>
<BlockPatternSetup
blockName={ name }
clientId={ clientId }
onBlockPatternSelect={ onBlockPatternSelect }
/>
</BlockContextProvider>
</Modal>
) }
</>
<Modal
className="block-editor-query-pattern__selection-modal"
title={ __( 'Choose a pattern' ) }
closeLabel={ __( 'Cancel' ) }
onRequestClose={ () => setIsPatternSelectionModalOpen( false ) }
>
<BlockContextProvider value={ blockPreviewContext }>
<BlockPatternSetup
blockName={ blockNameForPatterns }
clientId={ clientId }
onBlockPatternSelect={ onBlockPatternSelect }
/>
</BlockContextProvider>
</Modal>
);
};
}

export default QueryEdit;
41 changes: 41 additions & 0 deletions packages/block-library/src/query/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { get } from 'lodash';
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { decodeEntities } from '@wordpress/html-entities';
import { cloneBlock, store as blocksStore } from '@wordpress/blocks';

Expand Down Expand Up @@ -214,3 +215,43 @@ export const getTransformedBlocksFromPattern = (
}
return { newBlocks: clonedBlocks, queryClientIds };
};

/**
* Helper hook that determines if there is an active variation of the block
* and if there are available specific patterns for this variation.
* If there are, they are going to be suggested to the user in setup and
* replace flow.
*
* If there are no patterns, the default ones for Query Loop are going to be suggested.
*
* @param {string} clientId The block's client ID.
* @param {Object} attributes The block's attributes.
* @return {string} The block name to be used in the patterns suggestions.
*/
export function useBlockNameForPatterns( clientId, attributes ) {
const activeVariationName = useSelect(
( select ) =>
select( blocksStore ).getActiveBlockVariation(
queryLoopName,
attributes
)?.name,

[ attributes ]
);
const blockName = `${ queryLoopName }/${ activeVariationName }`;
const activeVariationPatterns = useSelect(
( select ) => {
const {
getBlockRootClientId,
__experimentalGetPatternsByBlockTypes,
} = select( blockEditorStore );
const rootClientId = getBlockRootClientId( clientId );
return __experimentalGetPatternsByBlockTypes(
blockName,
rootClientId
);
},
[ clientId, blockName ]
);
return activeVariationPatterns?.length ? blockName : queryLoopName;
}