From 01daa62add627982684a24f13aebceb7c317fc10 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Tue, 11 May 2021 14:46:57 -0400 Subject: [PATCH 1/9] filter out current entity, apply message where necessary --- .../src/template-part/edit/index.js | 1 + .../src/template-part/edit/selection/index.js | 2 + .../edit/selection/template-part-previews.js | 48 ++++++++++++++----- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/packages/block-library/src/template-part/edit/index.js b/packages/block-library/src/template-part/edit/index.js index ca7ff8aded12a7..84508d297c6b00 100644 --- a/packages/block-library/src/template-part/edit/index.js +++ b/packages/block-library/src/template-part/edit/index.js @@ -164,6 +164,7 @@ export default function TemplatePartEdit( { setAttributes={ setAttributes } onClose={ onClose } area={ area } + templatePartId={ templatePartId } /> ) } /> diff --git a/packages/block-library/src/template-part/edit/selection/index.js b/packages/block-library/src/template-part/edit/selection/index.js index 93a39b8c3bec21..2ed5ac835f81f2 100644 --- a/packages/block-library/src/template-part/edit/selection/index.js +++ b/packages/block-library/src/template-part/edit/selection/index.js @@ -26,6 +26,7 @@ export default function TemplatePartSelection( { setAttributes, onClose, area, + templatePartId = null, } ) { const [ filterValue, setFilterValue ] = useState( '' ); return ( @@ -44,6 +45,7 @@ export default function TemplatePartSelection( { filterValue={ filterValue } onClose={ onClose } area={ area } + templatePartId={ templatePartId } /> diff --git a/packages/block-library/src/template-part/edit/selection/template-part-previews.js b/packages/block-library/src/template-part/edit/selection/template-part-previews.js index 90509ce3eda162..96ee91f057c54c 100644 --- a/packages/block-library/src/template-part/edit/selection/template-part-previews.js +++ b/packages/block-library/src/template-part/edit/selection/template-part-previews.js @@ -111,23 +111,37 @@ function TemplatePartsByArea( { area = 'uncategorized', labelsByArea, } ) { + const templatePartsToShow = + templateParts.filter( + ( templatePart ) => + 'uncategorized' === area || templatePart.area === area + ) || []; const templatePartsByArea = useMemo( () => { - return Object.values( groupBy( templateParts, 'area' ) ); + return Object.values( groupBy( templatePartsToShow, 'area' ) ); }, [ templateParts, area ] ); + const currentShownTPs = useAsyncList( templateParts ); + if ( ! templatePartsToShow.length ) { + return ( + + { __( + 'There are no other template parts of this area. If you are looking for another type of template part, try searchnig for it using the input above.' + ) } + + ); + } + return templatePartsByArea.map( ( templatePartList ) => { - // Only return corresponding area if block/entity is not uncategorized/general version. - if ( 'uncategorized' !== area && templatePartList[ 0 ].area !== area ) { - return null; - } return ( { templatePartList.map( ( templatePart ) => { return currentShownTPs.includes( templatePart ) ? ( @@ -222,7 +236,9 @@ function TemplatePartSearchResults( { return groupedResults.map( ( group ) => ( { group.map( ( templatePart ) => currentShownTPs.includes( templatePart ) ? ( @@ -246,18 +262,24 @@ export default function TemplatePartPreviews( { filterValue, onClose, area, + templatePartId, } ) { const composite = useCompositeState(); const { templateParts, labelsByArea } = useSelect( ( select ) => { - const _templateParts = + const _templateParts = ( select( coreStore ).getEntityRecords( 'postType', 'wp_template_part', { per_page: -1, } - ) || []; + ) || [] + ).filter( + ( templatePart ) => + `${ templatePart.theme }//${ templatePart.slug }` !== + templatePartId + ); const definedAreas = select( editorStore @@ -274,7 +296,11 @@ export default function TemplatePartPreviews( { }, [] ); if ( ! templateParts || ! templateParts.length ) { - return null; + return ( + + { __( 'There are no existing template parts to select.' ) } + + ); } if ( filterValue ) { From a47dad825fd8e90c13b31a2ff41da6ee9f33df51 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 13 May 2021 10:32:21 -0400 Subject: [PATCH 2/9] hide replace button when no options exist --- .../src/template-part/edit/index.js | 29 +++++++-- .../template-part/edit/placeholder/index.js | 17 ++--- .../edit/selection/template-part-previews.js | 63 ++++++++++--------- .../edit/utils/create-template-part-id.js | 10 +++ 4 files changed, 78 insertions(+), 41 deletions(-) create mode 100644 packages/block-library/src/template-part/edit/utils/create-template-part-id.js diff --git a/packages/block-library/src/template-part/edit/index.js b/packages/block-library/src/template-part/edit/index.js index 84508d297c6b00..e4767271d83cad 100644 --- a/packages/block-library/src/template-part/edit/index.js +++ b/packages/block-library/src/template-part/edit/index.js @@ -26,6 +26,7 @@ import TemplatePartPlaceholder from './placeholder'; import TemplatePartSelection from './selection'; import { TemplatePartAdvancedControls } from './advanced-controls'; import TemplatePartInnerBlocks from './inner-blocks'; +import { createTemplatePartId } from './utils/create-template-part-id'; export default function TemplatePartEdit( { attributes, @@ -33,7 +34,7 @@ export default function TemplatePartEdit( { clientId, } ) { const { slug, theme, tagName, layout = {} } = attributes; - const templatePartId = theme && slug ? theme + '//' + slug : null; + const templatePartId = createTemplatePartId( slug, theme ); const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders( templatePartId @@ -48,11 +49,14 @@ export default function TemplatePartEdit( { isMissing, defaultWrapper, area, + enableSelection, } = useSelect( ( select ) => { - const { getEditedEntityRecord, hasFinishedResolution } = select( - coreStore - ); + const { + getEditedEntityRecord, + getEntityRecords, + hasFinishedResolution, + } = select( coreStore ); const { getBlocks } = select( blockEditorStore ); const getEntityArgs = [ @@ -65,6 +69,19 @@ export default function TemplatePartEdit( { : null; const _area = entityRecord?.area || attributes.area; + // Check whether other entities exist for switching/selection. + const availableReplacementArgs = [ + 'postType', + 'wp_template_part', + _area && 'uncategorized' !== _area && { area: _area }, + ]; + const matchingReplacements = getEntityRecords( + ...availableReplacementArgs + ); + const _enableSelection = templatePartId + ? matchingReplacements?.length > 1 + : matchingReplacements?.length; + const hasResolvedEntity = templatePartId ? hasFinishedResolution( 'getEditedEntityRecord', @@ -82,6 +99,7 @@ export default function TemplatePartEdit( { isMissing: hasResolvedEntity && ! entityRecord, defaultWrapper: defaultWrapperElement || 'div', area: _area, + enableSelection: _enableSelection, }; }, [ templatePartId, clientId ] @@ -138,10 +156,11 @@ export default function TemplatePartEdit( { area={ attributes.area } clientId={ clientId } setAttributes={ setAttributes } + enableSelection={ enableSelection } /> ) } - { isEntityAvailable && ( + { isEntityAvailable && enableSelection && ( ( <> - + { enableSelection && ( + + ) } ) }