From 660e46ed7eda07d236e437007cc437821c02f09b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?= Date: Fri, 9 Nov 2018 18:21:30 +0100 Subject: [PATCH] Avoid unnecessary re-renders when navigating between blocks (#11495) * Performance: Avoid unnecessary re-renders when navigating between blocks with keyboard * Small tweak per review --- .../editor/src/components/block-list/block.js | 23 ++++++++++++++++++- .../editor/src/components/block-list/index.js | 20 ---------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/editor/src/components/block-list/block.js b/packages/editor/src/components/block-list/block.js index bd156cb3f79ec..cf505245b9f5a 100644 --- a/packages/editor/src/components/block-list/block.js +++ b/packages/editor/src/components/block-list/block.js @@ -70,6 +70,7 @@ export class BlockListBlock extends Component { this.onDragStart = this.onDragStart.bind( this ); this.onDragEnd = this.onDragEnd.bind( this ); this.selectOnOpen = this.selectOnOpen.bind( this ); + this.onShiftSelection = this.onShiftSelection.bind( this ); this.hadTouchStart = false; this.state = { @@ -285,7 +286,7 @@ export class BlockListBlock extends Component { if ( event.shiftKey ) { if ( ! this.props.isSelected ) { - this.props.onShiftSelection( this.props.clientId ); + this.onShiftSelection(); event.preventDefault(); } } else { @@ -357,6 +358,20 @@ export class BlockListBlock extends Component { } } + onShiftSelection() { + if ( ! this.props.isSelectionEnabled ) { + return; + } + + const { getBlockSelectionStart, onMultiSelect, onSelect } = this.props; + + if ( getBlockSelectionStart() ) { + onMultiSelect( getBlockSelectionStart(), this.props.clientId ); + } else { + onSelect( this.props.clientId ); + } + } + render() { return ( @@ -601,6 +616,7 @@ const applyWithSelect = withSelect( ( select, { clientId, rootClientId, isLargeV getEditorSettings, hasSelectedInnerBlock, getTemplateLock, + getBlockSelectionStart, } = select( 'core/editor' ); const isSelected = isBlockSelected( clientId ); const { hasFixedToolbar, focusMode } = getEditorSettings(); @@ -634,6 +650,9 @@ const applyWithSelect = withSelect( ( select, { clientId, rootClientId, isLargeV block, isSelected, isParentOfSelectedBlock, + // We only care about this value when the shift key is pressed. + // We call it dynamically in the event handler to avoid unnecessary re-renders. + getBlockSelectionStart, }; } ); @@ -641,6 +660,7 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps ) => { const { updateBlockAttributes, selectBlock, + multiSelect, insertBlocks, insertDefaultBlock, removeBlock, @@ -657,6 +677,7 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps ) => { onSelect( clientId = ownProps.clientId, initialPosition ) { selectBlock( clientId, initialPosition ); }, + onMultiSelect: multiSelect, onInsertBlocks( blocks, index ) { const { rootClientId } = ownProps; insertBlocks( blocks, index, rootClientId ); diff --git a/packages/editor/src/components/block-list/index.js b/packages/editor/src/components/block-list/index.js index 021a3758dbe80..7d589a329fca3 100644 --- a/packages/editor/src/components/block-list/index.js +++ b/packages/editor/src/components/block-list/index.js @@ -29,7 +29,6 @@ class BlockList extends Component { this.onSelectionStart = this.onSelectionStart.bind( this ); this.onSelectionEnd = this.onSelectionEnd.bind( this ); - this.onShiftSelection = this.onShiftSelection.bind( this ); this.setBlockRef = this.setBlockRef.bind( this ); this.setLastClientY = this.setLastClientY.bind( this ); this.onPointerMove = throttle( this.onPointerMove.bind( this ), 100 ); @@ -170,20 +169,6 @@ class BlockList extends Component { } } - onShiftSelection( clientId ) { - if ( ! this.props.isSelectionEnabled ) { - return; - } - - const { selectionStartClientId, onMultiSelect, onSelect } = this.props; - - if ( selectionStartClientId ) { - onMultiSelect( selectionStartClientId, clientId ); - } else { - onSelect( clientId ); - } - } - render() { const { blockClientIds, @@ -200,7 +185,6 @@ class BlockList extends Component { clientId={ clientId } blockRef={ this.setBlockRef } onSelectionStart={ this.onSelectionStart } - onShiftSelection={ this.onShiftSelection } rootClientId={ rootClientId } isFirst={ blockIndex === 0 } isLast={ blockIndex === blockClientIds.length - 1 } @@ -221,7 +205,6 @@ export default compose( [ isMultiSelecting, getMultiSelectedBlocksStartClientId, getMultiSelectedBlocksEndClientId, - getBlockSelectionStart, } = select( 'core/editor' ); const { rootClientId } = ownProps; @@ -229,7 +212,6 @@ export default compose( [ blockClientIds: getBlockOrder( rootClientId ), selectionStart: getMultiSelectedBlocksStartClientId(), selectionEnd: getMultiSelectedBlocksEndClientId(), - selectionStartClientId: isSelectionEnabled() && getBlockSelectionStart(), isSelectionEnabled: isSelectionEnabled(), isMultiSelecting: isMultiSelecting(), }; @@ -239,14 +221,12 @@ export default compose( [ startMultiSelect, stopMultiSelect, multiSelect, - selectBlock, } = dispatch( 'core/editor' ); return { onStartMultiSelect: startMultiSelect, onStopMultiSelect: stopMultiSelect, onMultiSelect: multiSelect, - onSelect: selectBlock, }; } ), ] )( BlockList );