From b3826d70669fb8550d25b1b37c0e852f07f8e492 Mon Sep 17 00:00:00 2001 From: Addison Stavlo Date: Wed, 19 May 2021 15:21:58 -0400 Subject: [PATCH] Template part 'replace' flow - don't show currently used template part as option. (#31720) * filter out current entity, apply message where necessary * hide replace button when no options exist * ensure boolean for enableSelection * reverse params on createTemplatePartId function * refactor group title translation to helper function, re-add fallback we should never see * make new button primary when the only button * contextual instructions based on options * fix test * fix other test --- .../src/template-part/edit/index.js | 30 +++++-- .../template-part/edit/placeholder/index.js | 39 ++++++--- .../src/template-part/edit/selection/index.js | 2 + .../edit/selection/template-part-previews.js | 82 +++++++++++++++---- .../edit/utils/create-template-part-id.js | 10 +++ .../specs/experiments/template-part.test.js | 3 +- 6 files changed, 132 insertions(+), 34 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 ca7ff8aded12a7..a4b6a62767cff2 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( theme, slug ); 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 > 0; + 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 && ( ) } /> diff --git a/packages/block-library/src/template-part/edit/placeholder/index.js b/packages/block-library/src/template-part/edit/placeholder/index.js index b19fe0f74ea323..db5da513802590 100644 --- a/packages/block-library/src/template-part/edit/placeholder/index.js +++ b/packages/block-library/src/template-part/edit/placeholder/index.js @@ -29,6 +29,7 @@ export default function TemplatePartPlaceholder( { area, clientId, setAttributes, + enableSelection, } ) { const { saveEntityRecord } = useDispatch( coreStore ); const [ step, setStep ] = useState( PLACEHOLDER_STEPS.initial ); @@ -85,26 +86,38 @@ export default function TemplatePartPlaceholder( { ( <> + { enableSelection && ( + + ) } -