Skip to content

Commit b8566d7

Browse files
authored
Merge pull request #4497 from WordPress/update/move-logic-out-of-inserter
Move data logic out of the inserter
2 parents 64b6017 + c442f0e commit b8566d7

File tree

6 files changed

+433
-300
lines changed

6 files changed

+433
-300
lines changed

editor/components/inserter/group.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* External dependencies
33
*/
4-
import { isEqual, find } from 'lodash';
4+
import { isEqual } from 'lodash';
55

66
/**
77
* WordPress dependencies
@@ -10,8 +10,8 @@ import { Component } from '@wordpress/element';
1010
import { NavigableMenu } from '@wordpress/components';
1111
import { BlockIcon } from '@wordpress/blocks';
1212

13-
function deriveActiveBlocks( blocks ) {
14-
return blocks.filter( ( block ) => ! block.disabled );
13+
function deriveActiveItems( items ) {
14+
return items.filter( ( item ) => ! item.isDisabled );
1515
}
1616

1717
export default class InserterGroup extends Component {
@@ -20,61 +20,62 @@ export default class InserterGroup extends Component {
2020

2121
this.onNavigate = this.onNavigate.bind( this );
2222

23-
this.activeBlocks = deriveActiveBlocks( this.props.blockTypes );
23+
this.activeItems = deriveActiveItems( this.props.items );
2424
this.state = {
25-
current: this.activeBlocks.length > 0 ? this.activeBlocks[ 0 ].name : null,
25+
current: this.activeItems.length > 0 ? this.activeItems[ 0 ] : null,
2626
};
2727
}
2828

2929
componentWillReceiveProps( nextProps ) {
30-
if ( ! isEqual( this.props.blockTypes, nextProps.blockTypes ) ) {
31-
this.activeBlocks = deriveActiveBlocks( nextProps.blockTypes );
30+
if ( ! isEqual( this.props.items, nextProps.items ) ) {
31+
this.activeItems = deriveActiveItems( nextProps.items );
32+
3233
// Try and preserve any still valid selected state.
33-
const current = find( this.activeBlocks, { name: this.state.current } );
34-
if ( ! current ) {
34+
const currentIsStillActive = this.state.current && this.activeItems.some( item =>
35+
item.id === this.state.current.id
36+
);
37+
38+
if ( ! currentIsStillActive ) {
3539
this.setState( {
36-
current: this.activeBlocks.length > 0 ? this.activeBlocks[ 0 ].name : null,
40+
current: this.activeItems.length > 0 ? this.activeItems[ 0 ] : null,
3741
} );
3842
}
3943
}
4044
}
4145

42-
renderItem( block ) {
46+
renderItem( item ) {
4347
const { current } = this.state;
44-
const { selectBlock, bindReferenceNode } = this.props;
45-
const { disabled } = block;
48+
const { onSelectItem } = this.props;
49+
50+
const isCurrent = current && current.id === item.id;
4651

4752
return (
4853
<button
4954
role="menuitem"
50-
key={ block.name === 'core/block' && block.initialAttributes ?
51-
block.name + block.initialAttributes.ref :
52-
block.name
53-
}
55+
key={ item.id }
5456
className="editor-inserter__block"
55-
onClick={ selectBlock( block ) }
56-
ref={ bindReferenceNode( block.name ) }
57-
tabIndex={ current === block.name || disabled ? null : '-1' }
58-
disabled={ disabled }
57+
onClick={ () => onSelectItem( item ) }
58+
tabIndex={ isCurrent || item.isDisabled ? null : '-1' }
59+
disabled={ item.isDisabled }
5960
>
60-
<BlockIcon icon={ block.icon } />
61-
{ block.title }
61+
<BlockIcon icon={ item.icon } />
62+
{ item.title }
6263
</button>
6364
);
6465
}
6566

6667
onNavigate( index ) {
67-
const { activeBlocks } = this;
68-
const dest = activeBlocks[ index ];
68+
const { activeItems } = this;
69+
const dest = activeItems[ index ];
6970
if ( dest ) {
7071
this.setState( {
71-
current: dest.name,
72+
current: dest,
7273
} );
7374
}
7475
}
7576

7677
render() {
77-
const { labelledBy, blockTypes } = this.props;
78+
const { labelledBy, items } = this.props;
7879

7980
return (
8081
<NavigableMenu
@@ -83,7 +84,7 @@ export default class InserterGroup extends Component {
8384
aria-labelledby={ labelledBy }
8485
cycle={ false }
8586
onNavigate={ this.onNavigate }>
86-
{ blockTypes.map( this.renderItem, this ) }
87+
{ items.map( this.renderItem, this ) }
8788
</NavigableMenu>
8889
);
8990
}

editor/components/inserter/index.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,12 @@ class Inserter extends Component {
8282
</IconButton>
8383
) }
8484
renderContent={ ( { onClose } ) => {
85-
const onInsert = ( name, initialAttributes ) => {
86-
onInsertBlock(
87-
name,
88-
initialAttributes,
89-
insertionPoint
90-
);
91-
85+
const onSelect = ( item ) => {
86+
onInsertBlock( item, insertionPoint );
9287
onClose();
9388
};
9489

95-
return <InserterMenu onSelect={ onInsert } />;
90+
return <InserterMenu onSelect={ onSelect } />;
9691
} }
9792
/>
9893
);
@@ -108,9 +103,9 @@ export default compose( [
108103
};
109104
},
110105
( dispatch ) => ( {
111-
onInsertBlock( name, initialAttributes, position ) {
106+
onInsertBlock( item, position ) {
112107
dispatch( insertBlock(
113-
createBlock( name, initialAttributes ),
108+
createBlock( item.name, item.initialAttributes ),
114109
position
115110
) );
116111
},

0 commit comments

Comments
 (0)