Skip to content

Commit 6e6c8d5

Browse files
aduthyouknowriad
authored andcommitted
Editor: Optimize Inserter props generation and reconciliation (#11243)
1 parent 393f5ba commit 6e6c8d5

File tree

3 files changed

+95
-55
lines changed

3 files changed

+95
-55
lines changed

packages/editor/src/components/default-block-appender/test/__snapshots__/index.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ exports[`DefaultBlockAppender should append a default block when input focused 1
2323
value="Write your story"
2424
/>
2525
<WithSelect(WithDispatch(InserterWithShortcuts)) />
26-
<WithSelect(WithDispatch(Inserter))
26+
<WithSelect(IfCondition(Inserter))
2727
position="top right"
2828
/>
2929
</div>
@@ -45,7 +45,7 @@ exports[`DefaultBlockAppender should match snapshot 1`] = `
4545
value="Write your story"
4646
/>
4747
<WithSelect(WithDispatch(InserterWithShortcuts)) />
48-
<WithSelect(WithDispatch(Inserter))
48+
<WithSelect(IfCondition(Inserter))
4949
position="top right"
5050
/>
5151
</div>
@@ -67,7 +67,7 @@ exports[`DefaultBlockAppender should optionally show without prompt 1`] = `
6767
value=""
6868
/>
6969
<WithSelect(WithDispatch(InserterWithShortcuts)) />
70-
<WithSelect(WithDispatch(Inserter))
70+
<WithSelect(IfCondition(Inserter))
7171
position="top right"
7272
/>
7373
</div>

packages/editor/src/components/inserter/index.js

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
*/
44
import { __ } from '@wordpress/i18n';
55
import { Dropdown, IconButton } from '@wordpress/components';
6-
import { createBlock, isUnmodifiedDefaultBlock } from '@wordpress/blocks';
76
import { Component } from '@wordpress/element';
8-
import { withSelect, withDispatch } from '@wordpress/data';
9-
import { compose } from '@wordpress/compose';
7+
import { withSelect } from '@wordpress/data';
8+
import { compose, ifCondition } from '@wordpress/compose';
109

1110
/**
1211
* Internal dependencies
@@ -30,6 +29,8 @@ class Inserter extends Component {
3029
super( ...arguments );
3130

3231
this.onToggle = this.onToggle.bind( this );
32+
this.renderToggle = this.renderToggle.bind( this );
33+
this.renderContent = this.renderContent.bind( this );
3334
}
3435

3536
onToggle( isOpen ) {
@@ -41,20 +42,46 @@ class Inserter extends Component {
4142
}
4243
}
4344

44-
render() {
45+
/**
46+
* Render callback to display Dropdown toggle element.
47+
*
48+
* @param {Function} options.onToggle Callback to invoke when toggle is
49+
* pressed.
50+
* @param {boolean} options.isOpen Whether dropdown is currently open.
51+
*
52+
* @return {WPElement} Dropdown toggle element.
53+
*/
54+
renderToggle( { onToggle, isOpen } ) {
4555
const {
46-
items,
47-
position,
48-
title,
49-
onInsertBlock,
50-
rootClientId,
5156
disabled,
5257
renderToggle = defaultRenderToggle,
5358
} = this.props;
5459

55-
if ( items.length === 0 ) {
56-
return null;
57-
}
60+
return renderToggle( { onToggle, isOpen, disabled } );
61+
}
62+
63+
/**
64+
* Render callback to display Dropdown content element.
65+
*
66+
* @param {Function} options.onClose Callback to invoke when dropdown is
67+
* closed.
68+
*
69+
* @return {WPElement} Dropdown content element.
70+
*/
71+
renderContent( { onClose } ) {
72+
const { rootClientId, index } = this.props;
73+
74+
return (
75+
<InserterMenu
76+
onSelect={ onClose }
77+
rootClientId={ rootClientId }
78+
index={ index }
79+
/>
80+
);
81+
}
82+
83+
render() {
84+
const { position, title } = this.props;
5885

5986
return (
6087
<Dropdown
@@ -64,22 +91,8 @@ class Inserter extends Component {
6491
onToggle={ this.onToggle }
6592
expandOnMobile
6693
headerTitle={ title }
67-
renderToggle={ ( { onToggle, isOpen } ) => renderToggle( { onToggle, isOpen, disabled } ) }
68-
renderContent={ ( { onClose } ) => {
69-
const onSelect = ( item ) => {
70-
onInsertBlock( item );
71-
72-
onClose();
73-
};
74-
75-
return (
76-
<InserterMenu
77-
items={ items }
78-
onSelect={ onSelect }
79-
rootClientId={ rootClientId }
80-
/>
81-
);
82-
} }
94+
renderToggle={ this.renderToggle }
95+
renderContent={ this.renderContent }
8396
/>
8497
);
8598
}
@@ -90,7 +103,6 @@ export default compose( [
90103
const {
91104
getEditedPostAttribute,
92105
getBlockInsertionPoint,
93-
getSelectedBlock,
94106
getInserterItems,
95107
} = select( 'core/editor' );
96108

@@ -106,21 +118,10 @@ export default compose( [
106118

107119
return {
108120
title: getEditedPostAttribute( 'title' ),
109-
selectedBlock: getSelectedBlock(),
110-
items: getInserterItems( rootClientId ),
111-
index,
121+
hasItems: getInserterItems( rootClientId ).length > 0,
112122
rootClientId,
123+
index,
113124
};
114125
} ),
115-
withDispatch( ( dispatch, ownProps ) => ( {
116-
onInsertBlock: ( item ) => {
117-
const { selectedBlock, index, rootClientId } = ownProps;
118-
const { name, initialAttributes } = item;
119-
const insertedBlock = createBlock( name, initialAttributes );
120-
if ( selectedBlock && isUnmodifiedDefaultBlock( selectedBlock ) ) {
121-
return dispatch( 'core/editor' ).replaceBlocks( selectedBlock.clientId, insertedBlock );
122-
}
123-
return dispatch( 'core/editor' ).insertBlock( insertedBlock, index, rootClientId );
124-
},
125-
} ) ),
126+
ifCondition( ( { hasItems } ) => hasItems ),
126127
] )( Inserter );

packages/editor/src/components/inserter/menu.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ import scrollIntoView from 'dom-scroll-into-view';
2323
import { __, _n, _x, sprintf } from '@wordpress/i18n';
2424
import { Component, findDOMNode, createRef } from '@wordpress/element';
2525
import { withSpokenMessages, PanelBody } from '@wordpress/components';
26-
import { getCategories, isReusableBlock } from '@wordpress/blocks';
26+
import {
27+
getCategories,
28+
isReusableBlock,
29+
createBlock,
30+
isUnmodifiedDefaultBlock,
31+
} from '@wordpress/blocks';
2732
import { withDispatch, withSelect } from '@wordpress/data';
2833
import { withInstanceId, compose, withSafeTimeout } from '@wordpress/compose';
2934
import { LEFT, RIGHT, UP, DOWN, BACKSPACE, ENTER } from '@wordpress/keycodes';
@@ -340,21 +345,55 @@ export class InserterMenu extends Component {
340345
export default compose(
341346
withSelect( ( select, { rootClientId } ) => {
342347
const {
343-
getChildBlockNames,
344-
} = select( 'core/blocks' );
345-
const {
348+
getEditedPostAttribute,
349+
getSelectedBlock,
350+
getInserterItems,
346351
getBlockName,
347352
} = select( 'core/editor' );
353+
const {
354+
getChildBlockNames,
355+
} = select( 'core/blocks' );
356+
348357
const rootBlockName = getBlockName( rootClientId );
358+
349359
return {
360+
selectedBlock: getSelectedBlock(),
350361
rootChildBlocks: getChildBlockNames( rootBlockName ),
362+
title: getEditedPostAttribute( 'title' ),
363+
items: getInserterItems( rootClientId ),
364+
rootClientId,
365+
};
366+
} ),
367+
withDispatch( ( dispatch, ownProps ) => {
368+
const {
369+
__experimentalFetchReusableBlocks: fetchReusableBlocks,
370+
showInsertionPoint,
371+
hideInsertionPoint,
372+
} = dispatch( 'core/editor' );
373+
374+
return {
375+
fetchReusableBlocks,
376+
showInsertionPoint,
377+
hideInsertionPoint,
378+
onSelect( item ) {
379+
const {
380+
replaceBlocks,
381+
insertBlock,
382+
} = dispatch( 'core/editor' );
383+
const { selectedBlock, index, rootClientId } = ownProps;
384+
const { name, initialAttributes } = item;
385+
386+
const insertedBlock = createBlock( name, initialAttributes );
387+
if ( selectedBlock && isUnmodifiedDefaultBlock( selectedBlock ) ) {
388+
replaceBlocks( selectedBlock.clientId, insertedBlock );
389+
} else {
390+
insertBlock( insertedBlock, index, rootClientId );
391+
}
392+
393+
ownProps.onSelect();
394+
},
351395
};
352396
} ),
353-
withDispatch( ( dispatch ) => ( {
354-
fetchReusableBlocks: dispatch( 'core/editor' ).__experimentalFetchReusableBlocks,
355-
showInsertionPoint: dispatch( 'core/editor' ).showInsertionPoint,
356-
hideInsertionPoint: dispatch( 'core/editor' ).hideInsertionPoint,
357-
} ) ),
358397
withSpokenMessages,
359398
withInstanceId,
360399
withSafeTimeout

0 commit comments

Comments
 (0)