2024-05-28 07:32:26 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-06-18 13:07:56 +00:00
|
|
|
import { capitalize } from 'lodash';
|
2024-05-28 07:32:26 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2024-07-01 11:56:41 +00:00
|
|
|
import { Button, Path, SVG, ToolbarGroup } from '@wordpress/components';
|
2024-05-28 07:32:26 +00:00
|
|
|
import {
|
|
|
|
store as blockEditorStore,
|
|
|
|
// @ts-expect-error missing type
|
|
|
|
} from '@wordpress/block-editor';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { PatternWithBlocks } from '~/customize-store/types/pattern';
|
2024-06-18 13:07:56 +00:00
|
|
|
import { PATTERN_CATEGORIES } from '../sidebar/pattern-screen/categories';
|
2024-07-04 13:46:18 +00:00
|
|
|
import { usePatternsByCategory } from '../hooks/use-patterns';
|
2024-05-28 07:32:26 +00:00
|
|
|
|
|
|
|
// This is the icon that is used in the Shuffle button. Currently we are using an outdated version of @wordpress/icons.
|
|
|
|
// import { shuffle } from '@wordpress/icons';
|
|
|
|
|
|
|
|
// Copied-pasted from a recent version of @wordpress/icons
|
|
|
|
const shuffleIcon = (
|
|
|
|
<SVG viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/SVG">
|
|
|
|
<Path d="M17.192 6.75L15.47 5.03l1.06-1.06 3.537 3.53-3.537 3.53-1.06-1.06 1.723-1.72h-3.19c-.602 0-.993.202-1.28.498-.309.319-.538.792-.695 1.383-.13.488-.222 1.023-.296 1.508-.034.664-.116 1.413-.303 2.117-.193.721-.513 1.467-1.068 2.04-.575.594-1.359.954-2.357.954H4v-1.5h4.003c.601 0 .993-.202 1.28-.498.308-.319.538-.792.695-1.383.149-.557.216-1.093.288-1.662l.039-.31a9.653 9.653 0 0 1 .272-1.653c.193-.722.513-1.467 1.067-2.04.576-.594 1.36-.954 2.358-.954h3.19zM8.004 6.75c.8 0 1.46.23 1.988.628a6.24 6.24 0 0 0-.684 1.396 1.725 1.725 0 0 0-.024-.026c-.287-.296-.679-.498-1.28-.498H4v-1.5h4.003zM12.699 14.726c-.161.459-.38.94-.684 1.396.527.397 1.188.628 1.988.628h3.19l-1.722 1.72 1.06 1.06L20.067 16l-3.537-3.53-1.06 1.06 1.723 1.72h-3.19c-.602 0-.993-.202-1.28-.498a1.96 1.96 0 0 1-.024-.026z" />
|
|
|
|
</SVG>
|
|
|
|
);
|
|
|
|
|
2024-06-18 13:07:56 +00:00
|
|
|
const getCategoryLabelFromCategories = ( categories: string[] ) => {
|
|
|
|
for ( const category in PATTERN_CATEGORIES ) {
|
|
|
|
if ( categories.includes( category ) ) {
|
|
|
|
return PATTERN_CATEGORIES[
|
|
|
|
category as keyof typeof PATTERN_CATEGORIES
|
|
|
|
].label;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-07-04 13:46:18 +00:00
|
|
|
/**
|
|
|
|
* Selects a random pattern from the provided array that is not the current pattern.
|
|
|
|
* If the randomly selected pattern is the same as the current, it attempts to select the next pattern in the array.
|
|
|
|
* If the current pattern is the last in the array, it selects the first pattern.
|
|
|
|
* If there's only one pattern in the array, it will return that pattern.
|
|
|
|
*/
|
|
|
|
const getNextPattern = (
|
|
|
|
patterns: PatternWithBlocks[],
|
|
|
|
patternName: string
|
|
|
|
) => {
|
|
|
|
const numberOfPatterns = patterns.length;
|
|
|
|
const currentPatternIndex = patterns.findIndex(
|
|
|
|
( { name } ) => name === patternName
|
|
|
|
);
|
|
|
|
const nextPatternIndex = Math.floor( Math.random() * numberOfPatterns );
|
|
|
|
|
|
|
|
if ( nextPatternIndex !== currentPatternIndex ) {
|
|
|
|
return patterns[ nextPatternIndex ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currentPatternIndex === nextPatternIndex ) {
|
|
|
|
if ( nextPatternIndex === 0 ) {
|
|
|
|
return patterns[ 1 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( nextPatternIndex === numberOfPatterns ) {
|
|
|
|
return patterns[ 0 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return patterns[ nextPatternIndex - 1 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return patterns[ 0 ];
|
|
|
|
};
|
|
|
|
|
2024-05-28 07:32:26 +00:00
|
|
|
export default function Shuffle( { clientId }: { clientId: string } ) {
|
|
|
|
const {
|
2024-07-04 13:46:18 +00:00
|
|
|
category,
|
2024-05-28 07:32:26 +00:00
|
|
|
patternName,
|
|
|
|
}: {
|
2024-07-04 13:46:18 +00:00
|
|
|
category: string;
|
2024-05-28 07:32:26 +00:00
|
|
|
patternName: string;
|
|
|
|
} = useSelect(
|
|
|
|
( select ) => {
|
|
|
|
// @ts-expect-error missing type
|
2024-07-04 13:46:18 +00:00
|
|
|
const { getBlockAttributes } = select( blockEditorStore );
|
2024-05-28 07:32:26 +00:00
|
|
|
const attributes = getBlockAttributes( clientId );
|
2024-07-04 13:46:18 +00:00
|
|
|
const categories = attributes?.metadata?.categories;
|
|
|
|
// We know that the category is one of the keys of PATTERN_CATEGORIES.
|
|
|
|
const _category = Object.keys( PATTERN_CATEGORIES ).find( ( cat ) =>
|
|
|
|
categories?.includes( cat )
|
|
|
|
) as string;
|
|
|
|
|
2024-05-28 07:32:26 +00:00
|
|
|
const _patternName = attributes?.metadata?.patternName;
|
|
|
|
|
|
|
|
return {
|
2024-07-04 13:46:18 +00:00
|
|
|
category: _category,
|
2024-05-28 07:32:26 +00:00
|
|
|
patternName: _patternName,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
[ clientId ]
|
|
|
|
);
|
2024-07-04 13:46:18 +00:00
|
|
|
|
|
|
|
const { patterns } = usePatternsByCategory( category );
|
|
|
|
|
2024-05-28 07:32:26 +00:00
|
|
|
// @ts-expect-error missing type
|
|
|
|
const { replaceBlocks } = useDispatch( blockEditorStore );
|
|
|
|
|
2024-07-04 13:46:18 +00:00
|
|
|
if ( patterns.length === 0 ) {
|
|
|
|
return null;
|
2024-05-28 07:32:26 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 13:46:18 +00:00
|
|
|
const categoryLabel = getCategoryLabelFromCategories( [ category ] );
|
2024-06-18 13:07:56 +00:00
|
|
|
|
2024-05-28 07:32:26 +00:00
|
|
|
return (
|
2024-06-18 13:07:56 +00:00
|
|
|
<ToolbarGroup className="woocommerce-customize-your-store-toolbar-shuffle-container">
|
2024-07-01 11:56:41 +00:00
|
|
|
<Button
|
2024-06-14 08:21:44 +00:00
|
|
|
icon={ shuffleIcon }
|
2024-07-01 11:56:41 +00:00
|
|
|
label={ __( 'Shuffle', 'woocommerce' ) }
|
2024-06-14 08:21:44 +00:00
|
|
|
onClick={ () => {
|
2024-07-04 13:46:18 +00:00
|
|
|
const nextPattern = getNextPattern( patterns, patternName );
|
2024-06-14 08:21:44 +00:00
|
|
|
// @ts-expect-error - attributes is marked as readonly.
|
|
|
|
nextPattern.blocks[ 0 ].attributes = {
|
|
|
|
...nextPattern.blocks[ 0 ].attributes,
|
|
|
|
metadata: {
|
|
|
|
...nextPattern.blocks[ 0 ].attributes.metadata,
|
2024-07-04 13:46:18 +00:00
|
|
|
categories: [ category ],
|
|
|
|
patternName: nextPattern.name,
|
2024-06-14 08:21:44 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
replaceBlocks( clientId, nextPattern.blocks );
|
|
|
|
} }
|
2024-07-01 11:56:41 +00:00
|
|
|
>
|
|
|
|
{ categoryLabel && (
|
|
|
|
<span>{ capitalize( categoryLabel ) }</span>
|
|
|
|
) }
|
|
|
|
</Button>
|
2024-05-28 08:16:25 +00:00
|
|
|
</ToolbarGroup>
|
2024-05-28 07:32:26 +00:00
|
|
|
);
|
|
|
|
}
|