Skip to content
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

Improve performance of global inserter: don't rerender on every attribute change. #6796

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions editor/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,37 @@ export default compose( [
} = select( 'core/editor' );
const { allowedBlockTypes, templateLock } = getEditorSettings();
const insertionPoint = getBlockInsertionPoint();
const { rootUID } = insertionPoint;
const supportedBlocks = getSupportedBlocks( rootUID, allowedBlockTypes );
const {
rootUID: insertionPointRootUID,
index: insertionPointIndex,
layout: insertionPointLayout,
} = insertionPoint;
const supportedBlocks = getSupportedBlocks( insertionPointRootUID, allowedBlockTypes );
const selectedBlock = getSelectedBlock();
const selectedBlockUid = selectedBlock && selectedBlock.uid;
const blockIsUnmodifiedDefaultBlock = selectedBlock && isUnmodifiedDefaultBlock( selectedBlock );
return {
title: getEditedPostAttribute( 'title' ),
insertionPoint,
selectedBlock: getSelectedBlock(),
hasSupportedBlocks: true === supportedBlocks || ! isEmpty( supportedBlocks ),
isLocked: !! templateLock,
selectedBlockUid,
blockIsUnmodifiedDefaultBlock,
insertionPointRootUID,
insertionPointIndex,
insertionPointLayout,
};
} ),
withDispatch( ( dispatch, ownProps ) => ( {
showInsertionPoint: dispatch( 'core/editor' ).showInsertionPoint,
hideInsertionPoint: dispatch( 'core/editor' ).hideInsertionPoint,
onInsertBlock: ( item ) => {
const { insertionPoint, selectedBlock } = ownProps;
const { index, rootUID, layout } = insertionPoint;
const { selectedBlockUid, blockIsUnmodifiedDefaultBlock, insertionPointIndex, insertionPointLayout, insertionPointRootUID } = ownProps;
const { name, initialAttributes } = item;
const insertedBlock = createBlock( name, { ...initialAttributes, layout } );
if ( selectedBlock && isUnmodifiedDefaultBlock( selectedBlock ) ) {
return dispatch( 'core/editor' ).replaceBlocks( selectedBlock.uid, insertedBlock );
const insertedBlock = createBlock( name, { ...initialAttributes, layout: insertionPointLayout } );
if ( blockIsUnmodifiedDefaultBlock ) {
return dispatch( 'core/editor' ).replaceBlocks( selectedBlockUid, insertedBlock );
}
return dispatch( 'core/editor' ).insertBlock( insertedBlock, index, rootUID );
return dispatch( 'core/editor' ).insertBlock( insertedBlock, insertionPointIndex, insertionPointRootUID );
Copy link
Contributor

@youknowriad youknowriad May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have memories of @aduth having a PR to refactor this using effects, but I'm not certain 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have memories of @aduth having a PR to refactor this using effects, but I'm not certain 🤷‍♂️

Refactor which? I have dreams (and local in-progress branches) of a world without explicit index and root UID arguments, though they have yet to be realized in the form of a pull request. I might have similar ones though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for clarifying this @aduth. In that case, do you think the changes here make sense or you think it is better to wait/work on bigger changes that remove the need for index and root uid arguments?

},
} ) ),
] )( Inserter );