-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Shuffle option to sections via pattern category
- Loading branch information
1 parent
b7e936b
commit 049fe36
Showing
4 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/block-editor/src/components/block-toolbar/shuffle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
// otherwise we may shuffle to pattern that will not allow to continue shuffling. | ||
pattern.blocks.length === 1 && | ||
pattern.categories.some( ( category ) => { | ||
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, | ||
}, | ||
}; | ||
replaceBlocks( clientId, randomPattern.blocks ); | ||
} } | ||
/> | ||
</ToolbarGroup> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters