-
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
Lodash: Refactor block editor away from _.map()
#47214
Conversation
Size Change: -34 B (0%) Total Size: 1.33 MB
ℹ️ View Unchanged
|
( block ) => block.name | ||
), | ||
selectedBlocks: getBlocksByClientId( ids ) | ||
.filter( Boolean ) |
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.
Always an array since we're calling it on a result of Array.prototype.filter()
.
@@ -113,7 +113,7 @@ export function BlockTypesTab( { | |||
</InserterPanel> | |||
) } | |||
|
|||
{ map( currentlyRenderedCategories, ( category ) => { | |||
{ currentlyRenderedCategories.map( ( category ) => { |
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.
Always an array:
const [ current, setCurrent ] = useState( [] as T[] ); |
@@ -148,8 +148,7 @@ export function BlockTypesTab( { | |||
</InserterPanel> | |||
) } | |||
|
|||
{ map( | |||
currentlyRenderedCollections, | |||
{ currentlyRenderedCollections.map( |
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.
Same as above.
@@ -41,7 +36,7 @@ const usePatternsState = ( onInsert, rootClientId ) => { | |||
const { createSuccessNotice } = useDispatch( noticesStore ); | |||
const onClickPattern = useCallback( ( pattern, blocks ) => { | |||
onInsert( | |||
map( blocks, ( block ) => cloneBlock( block ) ), | |||
( blocks ?? [] ).map( ( block ) => cloneBlock( block ) ), |
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.
Just in case the click handler gets called with a nullish value.
@@ -260,7 +255,7 @@ const ImageURLInputUI = ( { | |||
additionalControls={ | |||
! linkEditorValue && ( | |||
<NavigableMenu> | |||
{ map( getLinkDestinations(), ( link ) => ( | |||
{ getLinkDestinations().map( ( link ) => ( |
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.
Always an array:
gutenberg/packages/block-editor/src/components/url-popover/image-url-input-ui.js
Line 161 in d343bd5
const linkDestinations = [ |
@@ -314,13 +313,11 @@ export const __experimentalGetGlobalBlocksByName = createSelector( | |||
*/ | |||
export const getBlocksByClientId = createSelector( | |||
( state, clientIds ) => | |||
map( | |||
Array.isArray( clientIds ) ? clientIds : [ clientIds ], | |||
( Array.isArray( clientIds ) ? clientIds : [ clientIds ] ).map( |
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.
Always an array as guaranteed by the ternary.
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.
Offhand comment: fundamental selectors like this one really shouldn't be doing this kind of magic; they get called so often that they should be as simple and do as little work as possible (including instancing new things). Out of scope for this PR, though, your change looks good 👍
( clientId ) => getBlock( state, clientId ) | ||
), | ||
( state, clientIds ) => | ||
map( | ||
Array.isArray( clientIds ) ? clientIds : [ clientIds ], | ||
( Array.isArray( clientIds ) ? clientIds : [ clientIds ] ).map( |
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.
Always an array as guaranteed by the ternary.
@@ -507,18 +504,18 @@ export const getBlockParents = createSelector( | |||
export const getBlockParentsByBlockName = createSelector( | |||
( state, clientId, blockName, ascending = false ) => { | |||
const parents = getBlockParents( state, clientId, ascending ); | |||
return map( | |||
map( parents, ( id ) => ( { | |||
return parents |
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.
Always an array:
gutenberg/packages/block-editor/src/store/selectors.js
Lines 481 to 488 in 71a3344
const parents = []; | |
let current = clientId; | |
while ( !! state.blocks.parents.get( current ) ) { | |
current = state.blocks.parents.get( current ); | |
parents.push( current ); | |
} | |
return ascending ? parents : parents.reverse(); |
( { id } ) => id | ||
); | ||
} ) | ||
.map( ( { id } ) => id ); |
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.
Already an array as guaranteed by the previous calls.
@@ -23,7 +18,7 @@ import wrap from './transforms/wrap'; | |||
* @return {Array} converted rules. | |||
*/ | |||
const transformStyles = ( styles, wrapperClassName = '' ) => { | |||
return map( styles, ( { css, baseURL } ) => { | |||
return Object.values( styles ?? [] ).map( ( { css, baseURL } ) => { |
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.
Can be nullish, which is why the nullish coalescing:
return select( blockEditorStore ).getSettings()?.styles; |
And we're doing the Object.values()
because there are instances where we pass an object:
const activeSupports = { |
although most of the rest of the usages pass an array of styles. Object.values()
will correctly yield us the values for arrays, so we're good with that part, as well.
Flaky tests detected in 328e39c. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3939050354
|
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, thank you @tyxla! 👍
@@ -314,13 +313,11 @@ export const __experimentalGetGlobalBlocksByName = createSelector( | |||
*/ | |||
export const getBlocksByClientId = createSelector( | |||
( state, clientIds ) => | |||
map( | |||
Array.isArray( clientIds ) ? clientIds : [ clientIds ], | |||
( Array.isArray( clientIds ) ? clientIds : [ clientIds ] ).map( |
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.
Offhand comment: fundamental selectors like this one really shouldn't be doing this kind of magic; they get called so often that they should be as simple and do as little work as possible (including instancing new things). Out of scope for this PR, though, your change looks good 👍
@@ -23,7 +18,7 @@ import wrap from './transforms/wrap'; | |||
* @return {Array} converted rules. | |||
*/ | |||
const transformStyles = ( styles, wrapperClassName = '' ) => { | |||
return map( styles, ( { css, baseURL } ) => { | |||
return Object.values( styles ?? [] ).map( ( { css, baseURL } ) => { |
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'm curious, why did you go for []
instead of {}
? They both work fine, in hindsight, I just found it surprising at first.
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.
Because various usages of transformStyles()
across the codebase work with either arrays or objects of styles, and I had to pick one of them 😉 Since we're doing a transformation to an array anyway, it felt better to just default to an array, regardless of the fact that we're doing an Object.values()
on it. Also, the function is already documented to expect an array of styles, so we're matching that. Regardless, I think the fact that it works with both objects and arrays is a bit vague and we could definitely improve that in the future.
Thanks @sgomes 🙌 |
What?
This PR removes Lodash's
_.map()
from the block editor package. There are just a few usages and conversion is pretty straightforward.Why?
Lodash is known to unnecessarily inflate the bundle size of packages, and in most cases, it can be replaced with native language functionality. See these for more information and rationale:
@wordpress/api-fetch
package haslodash
as a dependency #39495How?
We're using
Array.prototype.map()
instead. We're using nullish coalescing where necessary to cover nullish values, andObject.values()
where necessary to convert from an object.Testing Instructions