-
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
Lodash: Refactor block editor away from _.map()
#47214
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||
/** | ||||
* External dependencies | ||||
*/ | ||||
import { map, groupBy } from 'lodash'; | ||||
import { groupBy } from 'lodash'; | ||||
|
||||
/** | ||||
* WordPress dependencies | ||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Always an array:
|
||||
const categoryItems = itemsPerCategory[ category.slug ]; | ||||
if ( ! categoryItems || ! categoryItems.length ) { | ||||
return null; | ||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||||
( [ namespace, collection ] ) => { | ||||
const collectionItems = itemsPerCollection[ namespace ]; | ||||
if ( ! collectionItems || ! collectionItems.length ) { | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { map } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Just in case the click handler gets called with a nullish value. |
||
pattern.name | ||
); | ||
createSuccessNotice( | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,8 +1,3 @@ | ||||
/** | ||||
* External dependencies | ||||
*/ | ||||
import { map } from 'lodash'; | ||||
|
||||
/** | ||||
* WordPress dependencies | ||||
*/ | ||||
|
@@ -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 commentThe 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
|
||||
<MenuItem | ||||
key={ link.linkDestination } | ||||
icon={ link.icon } | ||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,6 @@ | ||||||||||||||||||
/** | ||||||||||||||||||
* External dependencies | ||||||||||||||||||
*/ | ||||||||||||||||||
import { map } from 'lodash'; | ||||||||||||||||||
import createSelector from 'rememo'; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
|
@@ -202,7 +201,7 @@ export const __unstableGetClientIdWithClientIdsTree = createSelector( | |||||||||||||||||
*/ | ||||||||||||||||||
export const __unstableGetClientIdsTree = createSelector( | ||||||||||||||||||
( state, rootClientId = '' ) => | ||||||||||||||||||
map( getBlockOrder( state, rootClientId ), ( clientId ) => | ||||||||||||||||||
getBlockOrder( state, rootClientId ).map( ( clientId ) => | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always an array:
|
||||||||||||||||||
__unstableGetClientIdWithClientIdsTree( state, clientId ) | ||||||||||||||||||
), | ||||||||||||||||||
( state ) => [ state.blocks.order ] | ||||||||||||||||||
|
@@ -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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Always an array as guaranteed by the ternary. |
||||||||||||||||||
( clientId ) => state.blocks.tree.get( clientId ) | ||||||||||||||||||
) | ||||||||||||||||||
); | ||||||||||||||||||
|
@@ -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 commentThe 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
|
||||||||||||||||||
.map( ( id ) => ( { | ||||||||||||||||||
id, | ||||||||||||||||||
name: getBlockName( state, id ), | ||||||||||||||||||
} ) ).filter( ( { name } ) => { | ||||||||||||||||||
} ) ) | ||||||||||||||||||
.filter( ( { name } ) => { | ||||||||||||||||||
if ( Array.isArray( blockName ) ) { | ||||||||||||||||||
return blockName.includes( name ); | ||||||||||||||||||
} | ||||||||||||||||||
return name === blockName; | ||||||||||||||||||
} ), | ||||||||||||||||||
( { id } ) => id | ||||||||||||||||||
); | ||||||||||||||||||
} ) | ||||||||||||||||||
.map( ( { id } ) => id ); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already an array as guaranteed by the previous calls. |
||||||||||||||||||
}, | ||||||||||||||||||
( state ) => [ state.blocks.parents ] | ||||||||||||||||||
); | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,8 +1,3 @@ | ||||||
/** | ||||||
* External dependencies | ||||||
*/ | ||||||
import { map } from 'lodash'; | ||||||
|
||||||
/** | ||||||
* WordPress dependencies | ||||||
*/ | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Can be nullish, which is why the nullish coalescing:
And we're doing the
although most of the rest of the usages pass an array of styles. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious, why did you go for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because various usages of |
||||||
const transforms = []; | ||||||
if ( wrapperClassName ) { | ||||||
transforms.push( wrap( wrapperClassName ) ); | ||||||
|
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()
.