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

Inserter: Prevent unnecessary re-renders when selecting a different block #7724

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ exports[`DefaultBlockAppender should append a default block when input focused 1
value="Write your story"
/>
<WithSelect(WithDispatch(InserterWithShortcuts)) />
<WithSelect(WithDispatch(Inserter))
<WithSelect(Inserter)
position="top right"
>
<WithSafeTimeout(WithSelect(WithDispatch(DotTip)))
id="core/editor.inserter"
>
Welcome to the wonderful world of blocks! Click the “+” (“Add block”) button to add a new block. There are blocks available for all kind of content: you can insert text, headings, images, lists, and lots more!
</WithSafeTimeout(WithSelect(WithDispatch(DotTip)))>
</WithSelect(WithDispatch(Inserter))>
</WithSelect(Inserter)>
</div>
`;

Expand All @@ -66,15 +66,15 @@ exports[`DefaultBlockAppender should match snapshot 1`] = `
value="Write your story"
/>
<WithSelect(WithDispatch(InserterWithShortcuts)) />
<WithSelect(WithDispatch(Inserter))
<WithSelect(Inserter)
position="top right"
>
<WithSafeTimeout(WithSelect(WithDispatch(DotTip)))
id="core/editor.inserter"
>
Welcome to the wonderful world of blocks! Click the “+” (“Add block”) button to add a new block. There are blocks available for all kind of content: you can insert text, headings, images, lists, and lots more!
</WithSafeTimeout(WithSelect(WithDispatch(DotTip)))>
</WithSelect(WithDispatch(Inserter))>
</WithSelect(Inserter)>
</div>
`;

Expand All @@ -96,14 +96,14 @@ exports[`DefaultBlockAppender should optionally show without prompt 1`] = `
value=""
/>
<WithSelect(WithDispatch(InserterWithShortcuts)) />
<WithSelect(WithDispatch(Inserter))
<WithSelect(Inserter)
position="top right"
>
<WithSafeTimeout(WithSelect(WithDispatch(DotTip)))
id="core/editor.inserter"
>
Welcome to the wonderful world of blocks! Click the “+” (“Add block”) button to add a new block. There are blocks available for all kind of content: you can insert text, headings, images, lists, and lots more!
</WithSafeTimeout(WithSelect(WithDispatch(DotTip)))>
</WithSelect(WithDispatch(Inserter))>
</WithSelect(Inserter)>
</div>
`;
44 changes: 8 additions & 36 deletions editor/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
*/
import { __ } from '@wordpress/i18n';
import { Dropdown, IconButton } from '@wordpress/components';
import { createBlock, isUnmodifiedDefaultBlock } from '@wordpress/blocks';
import { Component, compose } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { withSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import InserterMenu from './menu';

export { default as InserterResultsPortal } from './results-portal';

class Inserter extends Component {
constructor() {
super( ...arguments );
Expand All @@ -32,15 +29,13 @@ class Inserter extends Component {

render() {
const {
items,
hasBlockTypes,
position,
title,
children,
onInsertBlock,
rootUID,
} = this.props;

if ( items.length === 0 ) {
if ( ! hasBlockTypes ) {
return null;
}

Expand All @@ -65,13 +60,7 @@ class Inserter extends Component {
</IconButton>
) }
renderContent={ ( { onClose } ) => {
const onSelect = ( item ) => {
onInsertBlock( item );

onClose();
};

return <InserterMenu items={ items } onSelect={ onSelect } rootUID={ rootUID } />;
Copy link
Member

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.

return <InserterMenu onClose={ onClose } />;
} }
/>
);
Expand All @@ -80,32 +69,15 @@ class Inserter extends Component {

export default compose( [
withSelect( ( select ) => {
const {
getBlockTypes,
} = select( 'core/blocks' );
const {
getEditedPostAttribute,
getBlockInsertionPoint,
getSelectedBlock,
getInserterItems,
} = select( 'core/editor' );
const insertionPoint = getBlockInsertionPoint();
const { rootUID } = insertionPoint;
return {
hasBlockTypes: getBlockTypes().length > 0,
title: getEditedPostAttribute( 'title' ),
insertionPoint,
selectedBlock: getSelectedBlock(),
items: getInserterItems( rootUID ),
rootUID,
};
} ),
withDispatch( ( dispatch, ownProps ) => ( {
onInsertBlock: ( item ) => {
const { insertionPoint, selectedBlock } = ownProps;
const { index, rootUID, layout } = insertionPoint;
const { name, initialAttributes } = item;
const insertedBlock = createBlock( name, { ...initialAttributes, layout } );
if ( selectedBlock && isUnmodifiedDefaultBlock( selectedBlock ) ) {
return dispatch( 'core/editor' ).replaceBlocks( selectedBlock.uid, insertedBlock );
}
return dispatch( 'core/editor' ).insertBlock( insertedBlock, index, rootUID );
},
} ) ),
] )( Inserter );
37 changes: 34 additions & 3 deletions editor/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -276,22 +281,48 @@ 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 ) ) {
dispatch( 'core/editor' ).replaceBlocks( selectedBlock.uid, insertedBlock );
} else {
dispatch( 'core/editor' ).insertBlock( insertedBlock, index, rootUID );
}

if ( onClose ) {
onClose();
}
},
} ) ),
withSpokenMessages,
withInstanceId,
Expand Down
2 changes: 1 addition & 1 deletion editor/components/rich-text/tokens/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { withSelect } from '@wordpress/data';
* Internal dependencies
*/
import './style.scss';
import { InserterResultsPortal } from '../../../inserter';
import InserterResultsPortal from '../../../inserter/results-portal';

class TokenUI extends Component {
constructor() {
Expand Down