Skip to content

Commit

Permalink
Add a unique id to inserter items
Browse files Browse the repository at this point in the history
This lets the inserter do faster string comparison between inserter
items rather than deep object comparison.
  • Loading branch information
noisysocks committed Jan 17, 2018
1 parent 6759f55 commit c442f0e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
18 changes: 12 additions & 6 deletions editor/components/inserter/group.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isEqual, find } from 'lodash';
import { isEqual } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -29,27 +29,33 @@ export default class InserterGroup extends Component {
componentWillReceiveProps( nextProps ) {
if ( ! isEqual( this.props.items, nextProps.items ) ) {
this.activeItems = deriveActiveItems( nextProps.items );

// Try and preserve any still valid selected state.
const current = find( this.activeItems, ( item ) => isEqual( item, this.state.current ) );
if ( ! current ) {
const currentIsStillActive = this.state.current && this.activeItems.some( item =>
item.id === this.state.current.id
);

if ( ! currentIsStillActive ) {
this.setState( {
current: this.activeItems.length > 0 ? this.activeItems[ 0 ] : null,
} );
}
}
}

renderItem( item, index ) {
renderItem( item ) {
const { current } = this.state;
const { onSelectItem } = this.props;

const isCurrent = current && current.id === item.id;

return (
<button
role="menuitem"
key={ index }
key={ item.id }
className="editor-inserter__block"
onClick={ () => onSelectItem( item ) }
tabIndex={ isEqual( current, item ) || item.isDisabled ? null : '-1' }
tabIndex={ isCurrent || item.isDisabled ? null : '-1' }
disabled={ item.isDisabled }
>
<BlockIcon icon={ item.icon } />
Expand Down
7 changes: 7 additions & 0 deletions editor/components/inserter/test/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { noop } from 'lodash';
import { InserterMenu, searchItems } from '../menu';

const textItem = {
id: 'core/text-block',
name: 'core/text-block',
initialAttributes: {},
title: 'Text',
Expand All @@ -18,6 +19,7 @@ const textItem = {
};

const advancedTextItem = {
id: 'core/advanced-text-block',
name: 'core/advanced-text-block',
initialAttributes: {},
title: 'Advanced Text',
Expand All @@ -26,6 +28,7 @@ const advancedTextItem = {
};

const someOtherItem = {
id: 'core/some-other-block',
name: 'core/some-other-block',
initialAttributes: {},
title: 'Some Other Block',
Expand All @@ -34,6 +37,7 @@ const someOtherItem = {
};

const moreItem = {
id: 'core/more-block',
name: 'core/more-block',
initialAttributes: {},
title: 'More',
Expand All @@ -42,6 +46,7 @@ const moreItem = {
};

const youtubeItem = {
id: 'core-embed/youtube',
name: 'core-embed/youtube',
initialAttributes: {},
title: 'YouTube',
Expand All @@ -51,6 +56,7 @@ const youtubeItem = {
};

const textEmbedItem = {
id: 'core-embed/a-text-embed',
name: 'core-embed/a-text-embed',
initialAttributes: {},
title: 'A Text Embed',
Expand All @@ -59,6 +65,7 @@ const textEmbedItem = {
};

const reusableItem = {
id: 'core/block/123',
name: 'core/block',
initialAttributes: { ref: 123 },
title: 'My reusable block',
Expand Down
3 changes: 3 additions & 0 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,7 @@ export function getNotices( state ) {
* block. Inserter items encapsulate both regular blocks and reusable blocks.
*
* @typedef {Object} Editor.InserterItem
* @property {string} id Unique identifier for the item.
* @property {string} name The type of block to create.
* @property {Object} initialAttributes Attributes to pass to the newly created block.
* @property {string} title Title of the item, as it appears in the inserter.
Expand Down Expand Up @@ -1147,6 +1148,7 @@ function buildInserterItemFromBlockType( state, enabledBlockTypes, blockType ) {
}

return {
id: blockType.name,
name: blockType.name,
initialAttributes: {},
title: blockType.title,
Expand Down Expand Up @@ -1180,6 +1182,7 @@ function buildInserterItemFromReusableBlock( enabledBlockTypes, reusableBlock )
}

return {
id: `core/block/${ reusableBlock.id }`,
name: 'core/block',
initialAttributes: { ref: reusableBlock.id },
title: reusableBlock.title,
Expand Down
2 changes: 2 additions & 0 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,7 @@ describe( 'selectors', () => {

expect( getInserterItems( state, [ 'core/test-block' ] ) ).toEqual( [
{
id: 'core/test-block',
name: 'core/test-block',
initialAttributes: {},
title: 'test block',
Expand Down Expand Up @@ -2336,6 +2337,7 @@ describe( 'selectors', () => {

expect( getInserterItems( state, [ 'core/block' ] ) ).toEqual( [
{
id: 'core/block/123',
name: 'core/block',
initialAttributes: { ref: 123 },
title: 'My reusable block',
Expand Down

0 comments on commit c442f0e

Please sign in to comment.