-
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
Inserter: Prevent unnecessary re-renders when selecting a different block #7724
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…lock
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,12 @@ import { | |
PanelBody, | ||
withSafeTimeout, | ||
} from '@wordpress/components'; | ||
import { getCategories, isSharedBlock } from '@wordpress/blocks'; | ||
import { | ||
createBlock, | ||
getCategories, | ||
isSharedBlock, | ||
isUnmodifiedDefaultBlock, | ||
} from '@wordpress/blocks'; | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
|
||
/** | ||
|
@@ -276,22 +281,45 @@ export class InserterMenu extends Component { | |
} | ||
|
||
export default compose( | ||
withSelect( ( select, { rootUID } ) => { | ||
withSelect( ( select ) => { | ||
const { | ||
getChildBlockNames, | ||
} = select( 'core/blocks' ); | ||
const { | ||
getBlockInsertionPoint, | ||
getBlockName, | ||
getInserterItems, | ||
getSelectedBlock, | ||
} = select( 'core/editor' ); | ||
const insertionPoint = getBlockInsertionPoint(); | ||
const { rootUID } = insertionPoint; | ||
const rootBlockName = getBlockName( rootUID ); | ||
return { | ||
insertionPoint, | ||
items: getInserterItems( rootUID ), | ||
rootChildBlocks: getChildBlockNames( rootBlockName ), | ||
rootUID, | ||
selectedBlock: getSelectedBlock(), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch ) => ( { | ||
withDispatch( ( dispatch, ownProps ) => ( { | ||
fetchSharedBlocks: dispatch( 'core/editor' ).fetchSharedBlocks, | ||
showInsertionPoint: dispatch( 'core/editor' ).showInsertionPoint, | ||
hideInsertionPoint: dispatch( 'core/editor' ).hideInsertionPoint, | ||
onSelect: ( item ) => { | ||
const { insertionPoint, onClose, selectedBlock } = ownProps; | ||
const { index, rootUID, layout } = insertionPoint; | ||
const { name, initialAttributes } = item; | ||
const insertedBlock = createBlock( name, { ...initialAttributes, layout } ); | ||
if ( selectedBlock && isUnmodifiedDefaultBlock( selectedBlock ) ) { | ||
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 think this function is a bit mushed together and could use some whitespace/breathing room around conditionals, so I've pushed a little style tweak. 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. Thanks 👍 |
||
dispatch( 'core/editor' ).replaceBlocks( selectedBlock.uid, insertedBlock ); | ||
} else { | ||
dispatch( 'core/editor' ).insertBlock( insertedBlock, index, rootUID ); | ||
} | ||
if ( onClose ) { | ||
onClose(); | ||
} | ||
}, | ||
} ) ), | ||
withSpokenMessages, | ||
withInstanceId, | ||
|
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.
This is nice to see removed; it's always a bit of a code smell to have a component that just passes props down to other components without using them.