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

Editor: Flatten Inserter mapSelectToProps to optimize rendering #11028

Merged
merged 2 commits into from
Oct 26, 2018
Merged
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
28 changes: 16 additions & 12 deletions packages/editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,30 @@ export default compose( [
getBlockInsertionPoint,
getSelectedBlock,
getInserterItems,
getBlockOrder,
} = select( 'core/editor' );
const insertionPoint = getBlockInsertionPoint();
const parentId = rootClientId || insertionPoint.rootClientId;

let index;
Copy link
Contributor

@youknowriad youknowriad Oct 26, 2018

Choose a reason for hiding this comment

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

I think this should be initiailized something like getBlockOrder( rootClientId ).length?

Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I can tell, it's not needed, for reasons described in the comment a few lines below (maybe more valuable to pull up?)

// Otherwise, the default behavior for an undefined index is to
// append block to the end of the rootClientId context.

Copy link
Member Author

Choose a reason for hiding this comment

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

Related:

const { index = subState.length } = action;
return {
...state,
...mappedBlocks,
[ rootClientId ]: insertAt( subState, mappedBlocks[ rootClientId ], index ),
};

if ( rootClientId === undefined ) {
// Unless explicitly provided, the default insertion point provided
// by the store occurs immediately following the selected block.
// Otherwise, the default behavior for an undefined index is to
// append block to the end of the rootClientId context.
const insertionPoint = getBlockInsertionPoint();
( { rootClientId, layout, index } = insertionPoint );
}

return {
title: getEditedPostAttribute( 'title' ),
insertionPoint: {
rootClientId: parentId,
layout: rootClientId ? layout : insertionPoint.layout,
index: rootClientId ? getBlockOrder( rootClientId ).length : insertionPoint.index,
},
selectedBlock: getSelectedBlock(),
items: getInserterItems( parentId ),
rootClientId: parentId,
items: getInserterItems( rootClientId ),
layout,
index,
rootClientId,
};
} ),
withDispatch( ( dispatch, ownProps ) => ( {
onInsertBlock: ( item ) => {
const { selectedBlock, insertionPoint } = ownProps;
const { index, rootClientId, layout } = insertionPoint;
const { selectedBlock, index, rootClientId, layout } = ownProps;
const { name, initialAttributes } = item;
const insertedBlock = createBlock( name, { ...initialAttributes, layout } );
if ( selectedBlock && isUnmodifiedDefaultBlock( selectedBlock ) ) {
Expand Down