-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Refactor BlockSwitcher - make function component and new specific selector getBlockTransformItems
#27674
Refactor BlockSwitcher - make function component and new specific selector getBlockTransformItems
#27674
Conversation
Size Change: +614 B (0%) Total Size: 1.28 MB
ℹ️ View Unchanged
|
df296eb
to
086b72b
Compare
getBlockTransformItems
packages/block-editor/src/components/block-switcher/block-styles-menu.js
Outdated
Show resolved
Hide resolved
packages/block-editor/src/components/block-switcher/block-styles-menu.js
Outdated
Show resolved
Hide resolved
</div> | ||
const BlockSwitcher = ( { clientIds } ) => { | ||
const { replaceBlocks } = useDispatch( 'core/block-editor' ); | ||
const { blocks, possibleBlockTransformations, hasBlockStyles } = useSelect( |
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.
Do we need access to the whole blocks
array in the component? If we only need to know the length
, then we should let the selector return this more specific information. The more specific the information, the less the selector will trigger the component to re-render.
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.
Do we need access to the whole blocks array in the component?
Yes
replaceBlocks( clientIds, switchToBlockType( blocks, name ) );
const isSelectionOfSameType = uniq( blocks.map( ( { name } ) => name ) ).length === 1;
let icon; | ||
if ( isSelectionOfSameType ) { | ||
const sourceBlockName = hoveredBlock.name; | ||
const blockType = getBlockType( sourceBlockName ); |
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.
The getBlockType
call should be in the selector. The more calls you can stuff in the selector, the better, because it's called way less than the render function. :)
Some prior art: c183c32#diff-e0a942d6bad655ae6985e5feb9410ac2c6127356a70142478746c9ef1921bc41R39
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.
The same goes for the variables above.
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.
changed here: 0a68ebd
packages/block-editor/src/components/block-switcher/block-transformations-menu.js
Show resolved
Hide resolved
state.preferences.insertUsage, | ||
state.settings.allowedBlockTypes, | ||
state.settings.templateLock, | ||
getBlockTypes(), |
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.
Does this change? I'm not sure this function will be called if it does change? The function is only called when the state
changes.
* this item. | ||
* @property {number} frecency Heuristic that combines frequency and recency. | ||
*/ | ||
export const getBlockTransformItems = createSelector( |
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.
Is this new code? I cannot see where it's removed if it's a refactoring.
Not entirely sure if this should be a selector and public API? Cc @youknowriad.
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.
Yes.
Based on this comment: #27674 (comment)
It's exported so as to be tested. Maybe we should make selective selectors to public API? Currently we expose all of them.
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.
LGTM 👍
isDisabled, | ||
frecency: calculateFrecency( time, count ), | ||
}; | ||
if ( buildScope === 'transform' ) return blockItemBase; |
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.
It is interesting to note that transforms don't support block variations, maybe it something we should consider.
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.
Yeah, this is tracked in this issue: #26962
Description
This PR makes BlockSwitcher a function component and took the opportunity to break it in smaller pieces as well, as it was quite big.
Based on this comment: #27674 (comment)
I have also created a new selector (
getBlockTransformItems
) to get the transform items. PreviouslygetInserterItems
was used and even though it shares some common logic with the new selector, the new one is lighter.There is also a small enhancement to
disable
transformations by taking into account if a block is allowedmultiple
times.Checklist: