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

Add Shuffle option to sections via pattern category #59251

Merged
merged 4 commits into from
Feb 27, 2024
Merged
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
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { store as blockEditorStore } from '../../store';
import __unstableBlockNameContext from './block-name-context';
import NavigableToolbar from '../navigable-toolbar';
import { useHasAnyBlockControls } from '../block-controls/use-has-block-controls';
import Shuffle from './shuffle';

/**
* Renders the block toolbar.
Expand Down Expand Up @@ -185,6 +186,7 @@ export function PrivateBlockToolbar( {
</ToolbarGroup>
</div>
) }
<Shuffle clientId={ blockClientId } />
{ shouldShowVisualToolbar && isMultiToolbar && (
<BlockGroupToolbar />
) }
Expand Down
86 changes: 86 additions & 0 deletions packages/block-editor/src/components/block-toolbar/shuffle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* WordPress dependencies
*/
import { shuffle } from '@wordpress/icons';
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';

const EMPTY_ARRAY = [];

export default function Shuffle( { clientId } ) {
const { categories, patterns } = useSelect(
( select ) => {
const {
getBlockAttributes,
getBlockRootClientId,
__experimentalGetAllowedPatterns,
} = select( blockEditorStore );
const attributes = getBlockAttributes( clientId );
const _categories = attributes?.metadata?.categories || EMPTY_ARRAY;
const rootBlock = getBlockRootClientId( clientId );
const _patterns = __experimentalGetAllowedPatterns( rootBlock );
return {
categories: _categories,
patterns: _patterns,
};
},
[ clientId ]
);
const { replaceBlocks } = useDispatch( blockEditorStore );
const sameCategoryPatternsWithSingleWrapper = useMemo( () => {
if (
! categories ||
categories.length === 0 ||
! patterns ||
patterns.length === 0
) {
return EMPTY_ARRAY;
}
return patterns.filter( ( pattern ) => {
return (
// Check if the pattern has only one top level block,
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved
// otherwise we may shuffle to pattern that will not allow to continue shuffling.
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved
pattern.blocks.length === 1 &&
pattern.categories.some( ( category ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hiya! I think we have to check whether the pattern.categories is available or not. Otherwise, it will throw an error if the pattern doens't have the category.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #60070

return categories.includes( category );
} )
);
} );
}, [ categories, patterns ] );
if ( sameCategoryPatternsWithSingleWrapper.length === 0 ) {
return null;
}
return (
<ToolbarGroup>
<ToolbarButton
label={ __( 'Shuffle' ) }
icon={ shuffle }
onClick={ () => {
const randomPattern =
sameCategoryPatternsWithSingleWrapper[
Math.floor(
// eslint-disable-next-line no-restricted-syntax
Math.random() *
sameCategoryPatternsWithSingleWrapper.length
)
];
randomPattern.blocks[ 0 ].attributes = {
...randomPattern.blocks[ 0 ].attributes,
metadata: {
...randomPattern.blocks[ 0 ].attributes.metadata,
categories,
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved
},
};
replaceBlocks( clientId, randomPattern.blocks );
} }
/>
</ToolbarGroup>
);
}
16 changes: 13 additions & 3 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2278,11 +2278,21 @@ export const __experimentalGetParsedPattern = createRegistrySelector(
if ( ! pattern ) {
return null;
}
const blocks = parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} );
if ( blocks.length === 1 ) {
blocks[ 0 ].attributes = {
...blocks[ 0 ].attributes,
metadata: {
...( blocks[ 0 ].attributes.metadata || {} ),
categories: pattern.categories,
},
};
}
return {
...pattern,
blocks: parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} ),
blocks,
};
}, getAllPatternsDependants( select ) )
);
Expand Down
14 changes: 14 additions & 0 deletions packages/block-library/src/pattern/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ const PatternEdit = ( { attributes, clientId } ) => {
injectThemeAttributeInBlockTemplateContent( block )
)
);
// If the pattern has a single block and categories, we should add the
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved
// categories of the pattern to the block's metadata.
if (
clonedBlocks.length === 1 &&
selectedPattern.categories?.length > 0
) {
clonedBlocks[ 0 ].attributes = {
...clonedBlocks[ 0 ].attributes,
metadata: {
...clonedBlocks[ 0 ].attributes.metadata,
categories: selectedPattern.categories,
},
};
}
const rootEditingMode = getBlockEditingMode( rootClientId );
registry.batch( () => {
// Temporarily set the root block to default mode to allow replacing the pattern.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<p>Dummy text</p>
<!-- /wp:paragraph -->

<!-- wp:social-links {"customIconColor":"#ffffff","iconColorValue":"#ffffff","customIconBackgroundColor":"#3962e3","iconBackgroundColorValue":"#3962e3","className":"has-icon-color"} -->
<!-- wp:social-links {"customIconColor":"#ffffff","iconColorValue":"#ffffff","customIconBackgroundColor":"#3962e3","iconBackgroundColorValue":"#3962e3","metadata":{"categories":["call-to-action"]},"className":"has-icon-color"} -->
<ul class="wp-block-social-links has-icon-color has-icon-background-color"><!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"#","service":"chain"} /-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<p>Dummy text</p>
<!-- /wp:paragraph -->

<!-- wp:social-links {"customIconColor":"#ffffff","iconColorValue":"#ffffff","customIconBackgroundColor":"#3962e3","iconBackgroundColorValue":"#3962e3","className":"has-icon-color"} -->
<!-- wp:social-links {"customIconColor":"#ffffff","iconColorValue":"#ffffff","customIconBackgroundColor":"#3962e3","iconBackgroundColorValue":"#3962e3","metadata":{"categories":["call-to-action"]},"className":"has-icon-color"} -->
<ul class="wp-block-social-links has-icon-color has-icon-background-color"><!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /-->

<!-- wp:social-link {"url":"#","service":"chain"} /-->
Expand Down
13 changes: 10 additions & 3 deletions test/e2e/specs/editor/various/patterns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,16 @@ test.describe( 'Unsynced pattern', () => {
.click();
await page.getByLabel( 'My unsynced pattern' ).click();

await expect
.poll( editor.getBlocks )
.toEqual( [ ...before, ...before ] );
await expect.poll( editor.getBlocks ).toEqual( [
...before,
{
...before[ 0 ],
attributes: {
...before[ 0 ].attributes,
metadata: { categories: [ 'contact-details' ] },
},
},
] );
} );
} );

Expand Down
Loading