-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Iterate zoom out shuffle into a more visual control #66194
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce51ab1
Iterate zoom out shuffle into a more visual control
ntsekouras 95d620a
fix from rebase
ntsekouras e681dae
change label case
ntsekouras c928926
remove unnecessary isActive
richtabor ce8309c
tweak sizing to line up with button
richtabor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
packages/block-editor/src/components/block-toolbar/change-design.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,133 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
ToolbarButton, | ||
ToolbarGroup, | ||
Dropdown, | ||
__experimentalDropdownContentWrapper as DropdownContentWrapper, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { cloneBlock } from '@wordpress/blocks'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { useAsyncList } from '@wordpress/compose'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
import BlockPatternsList from '../block-patterns-list'; | ||
|
||
const EMPTY_ARRAY = []; | ||
const MAX_PATTERNS_TO_SHOW = 6; | ||
const POPOVER_PROPS = { | ||
placement: 'bottom-start', | ||
}; | ||
|
||
export default function ChangeDesign( { clientId } ) { | ||
const { categories, currentPatternName, patterns } = useSelect( | ||
( select ) => { | ||
const { | ||
getBlockAttributes, | ||
getBlockRootClientId, | ||
__experimentalGetAllowedPatterns, | ||
} = select( blockEditorStore ); | ||
const attributes = getBlockAttributes( clientId ); | ||
const _categories = attributes?.metadata?.categories || EMPTY_ARRAY; | ||
const rootBlock = getBlockRootClientId( clientId ); | ||
|
||
// Calling `__experimentalGetAllowedPatterns` is expensive. | ||
// Checking if the block can be changed prevents unnecessary selector calls. | ||
// See: https://github.com/WordPress/gutenberg/pull/64736. | ||
const _patterns = | ||
_categories.length > 0 | ||
? __experimentalGetAllowedPatterns( rootBlock ) | ||
: EMPTY_ARRAY; | ||
return { | ||
categories: _categories, | ||
currentPatternName: attributes?.metadata?.patternName, | ||
patterns: _patterns, | ||
}; | ||
}, | ||
[ clientId ] | ||
); | ||
const { replaceBlocks } = useDispatch( blockEditorStore ); | ||
const sameCategoryPatternsWithSingleWrapper = useMemo( () => { | ||
if ( categories.length === 0 || ! patterns || patterns.length === 0 ) { | ||
return EMPTY_ARRAY; | ||
} | ||
return patterns | ||
.filter( ( pattern ) => { | ||
const isCorePattern = | ||
pattern.source === 'core' || | ||
( pattern.source?.startsWith( 'pattern-directory' ) && | ||
pattern.source !== 'pattern-directory/theme' ); | ||
return ( | ||
// Check if the pattern has only one top level block, | ||
// otherwise we may switch to a pattern that doesn't have replacement suggestions. | ||
pattern.blocks.length === 1 && | ||
// We exclude the core patterns and pattern directory patterns that are not theme patterns. | ||
! isCorePattern && | ||
// Exclude current pattern. | ||
currentPatternName !== pattern.name && | ||
pattern.categories?.some( ( category ) => { | ||
return categories.includes( category ); | ||
} ) && | ||
// Check if the pattern is not a synced pattern. | ||
( pattern.syncStatus === 'unsynced' || ! pattern.id ) | ||
); | ||
} ) | ||
.slice( 0, MAX_PATTERNS_TO_SHOW ); | ||
}, [ categories, currentPatternName, patterns ] ); | ||
|
||
const currentShownPatterns = useAsyncList( | ||
sameCategoryPatternsWithSingleWrapper | ||
); | ||
|
||
if ( sameCategoryPatternsWithSingleWrapper.length < 2 ) { | ||
return null; | ||
} | ||
|
||
const onClickPattern = ( pattern ) => { | ||
const newBlocks = ( pattern.blocks ?? [] ).map( ( block ) => { | ||
return cloneBlock( block ); | ||
} ); | ||
newBlocks[ 0 ].attributes.metadata = { | ||
...newBlocks[ 0 ].attributes.metadata, | ||
categories, | ||
}; | ||
replaceBlocks( clientId, newBlocks ); | ||
}; | ||
|
||
return ( | ||
<Dropdown | ||
popoverProps={ POPOVER_PROPS } | ||
renderToggle={ ( { onToggle, isOpen } ) => { | ||
return ( | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
onClick={ () => onToggle( ! isOpen ) } | ||
aria-expanded={ isOpen } | ||
> | ||
{ __( 'Change design' ) } | ||
</ToolbarButton> | ||
</ToolbarGroup> | ||
); | ||
} } | ||
renderContent={ () => ( | ||
<DropdownContentWrapper | ||
className="block-editor-block-toolbar-change-design-content-wrapper" | ||
paddingSize="none" | ||
> | ||
<BlockPatternsList | ||
shownPatterns={ currentShownPatterns } | ||
blockPatterns={ sameCategoryPatternsWithSingleWrapper } | ||
onClickPattern={ onClickPattern } | ||
showTitle={ false } | ||
/> | ||
</DropdownContentWrapper> | ||
) } | ||
/> | ||
); | ||
} |
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
111 changes: 0 additions & 111 deletions
111
packages/block-editor/src/components/block-toolbar/shuffle.js
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's your current thoughts about absorbing this into the component with
variant="grid"
or something. How feasible...?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's doable (regarding the current usages and wrappers etc..). It's on my list to do this and see to consolidate the similar replace lists.