Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shuffle template parts #47763

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
68 changes: 47 additions & 21 deletions packages/block-library/src/template-part/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ import {
store as blockEditorStore,
__experimentalRecursionProvider as RecursionProvider,
__experimentalUseHasRecursion as useHasRecursion,
BlockControls,
} from '@wordpress/block-editor';
import { Spinner, Modal, MenuItem } from '@wordpress/components';
import {
Spinner,
Modal,
MenuItem,
ToolbarButton,
Tooltip,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { useState, createInterpolateElement } from '@wordpress/element';
Expand All @@ -33,6 +40,7 @@ import {
useAlternativeBlockPatterns,
useAlternativeTemplateParts,
useTemplatePartArea,
useShuffleTemplatePart,
} from './utils/hooks';

export default function TemplatePartEdit( {
Expand Down Expand Up @@ -92,6 +100,12 @@ export default function TemplatePartEdit( {
const isPlaceholder = ! slug;
const isEntityAvailable = ! isPlaceholder && ! isMissing && isResolved;
const TagName = tagName || areaObject.tagName;
const shuffleTemplatePart = useShuffleTemplatePart(
area,
clientId,
templatePartId,
setAttributes
);

// The `isSelected` check ensures the `BlockSettingsMenuControls` fill
// doesn't render multiple times. The block controls has similar internal check.
Expand Down Expand Up @@ -157,27 +171,39 @@ export default function TemplatePartEdit( {
</TagName>
) }
{ canReplace && (
<BlockSettingsMenuControls>
{ () => (
<MenuItem
onClick={ () => {
setIsTemplatePartSelectionOpen( true );
} }
<>
<BlockSettingsMenuControls>
{ () => (
<MenuItem
onClick={ () => {
setIsTemplatePartSelectionOpen( true );
} }
>
{ createInterpolateElement(
__( 'Replace <BlockTitle />' ),
{
BlockTitle: (
<BlockTitle
clientId={ clientId }
maximumLength={ 25 }
/>
),
}
) }
</MenuItem>
) }
</BlockSettingsMenuControls>
<BlockControls group="other">
<Tooltip
position="top center"
text={ __( 'Try a different design' ) }
>
{ createInterpolateElement(
__( 'Replace <BlockTitle />' ),
{
BlockTitle: (
<BlockTitle
clientId={ clientId }
maximumLength={ 25 }
/>
),
}
) }
</MenuItem>
) }
</BlockSettingsMenuControls>
<ToolbarButton onClick={ shuffleTemplatePart }>
{ __( 'Shuffle' ) }
</ToolbarButton>
</Tooltip>
</BlockControls>
</>
) }
{ isEntityAvailable && (
<TemplatePartInnerBlocks
Expand Down
54 changes: 52 additions & 2 deletions packages/block-library/src/template-part/edit/utils/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { kebabCase } from 'lodash';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useMemo } from '@wordpress/element';
import { useMemo, useRef } from '@wordpress/element';
import { serialize } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';

Expand Down Expand Up @@ -61,7 +61,7 @@ export function useAlternativeTemplateParts( area, excludedId ) {
templatePart.area === area )
) || []
);
}, [ templateParts, area ] );
}, [ templateParts, area, excludedId ] );

return {
templateParts: filteredTemplateParts,
Expand Down Expand Up @@ -113,6 +113,7 @@ export function useCreateTemplatePartFromBlocks( area, setAttributes ) {
// if provided value is not allowed.
area,
};

const templatePart = await saveEntityRecord(
'postType',
'wp_template_part',
Expand Down Expand Up @@ -161,3 +162,52 @@ export function useTemplatePartArea( area ) {
[ area ]
);
}

export function useShuffleTemplatePart(
area,
clientId,
templatePartId,
setAttributes
) {
const currentPatternRef = useRef( null );
const { templateParts } = useAlternativeTemplateParts(
area,
templatePartId
);
const blockPatterns = useAlternativeBlockPatterns( area, clientId );
const { replaceInnerBlocks } = useDispatch( blockEditorStore );

return async function shuffleTemplatePart() {
const list = [
...templateParts,
...blockPatterns.filter( ( pattern ) => {
const serializedPatternBlocks = serialize( pattern.blocks );
return (
pattern.name !== currentPatternRef.current &&
templateParts.every(
( templatePart ) =>
templatePart.content.raw !== serializedPatternBlocks
)
);
} ),
];
// We explicitly want the randomness here.
// eslint-disable-next-line no-restricted-syntax
const randomIndex = Math.floor( Math.random() * list.length );
const randomItem = list[ randomIndex ];

if ( randomIndex < templateParts.length ) {
const templatePart = randomItem;
setAttributes( {
slug: templatePart.slug,
theme: templatePart.theme,
area: undefined,
} );
} else {
const pattern = randomItem;
currentPatternRef.current = pattern.name;

replaceInnerBlocks( clientId, pattern.blocks );
}
};
}