From ff91cb06bb2eb8f3114e343fbc736e2d2941e039 Mon Sep 17 00:00:00 2001 From: Jerry Jones Date: Thu, 18 Jul 2024 10:43:14 -0500 Subject: [PATCH] Reduce number of select calls by lifting useSelect calls to ZoomOutModeInserters --- .../zoom-out-mode-inserter-button.js | 61 +------------ .../block-tools/zoom-out-mode-inserters.js | 91 ++++++++++++------- 2 files changed, 61 insertions(+), 91 deletions(-) diff --git a/packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js b/packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js index f382ec1ec3d6ed..8ea80a53830135 100644 --- a/packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js +++ b/packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js @@ -6,57 +6,12 @@ import clsx from 'clsx'; /** * WordPress dependencies */ -import { useSelect } from '@wordpress/data'; import { useState } from '@wordpress/element'; import { Button } from '@wordpress/components'; import { plus } from '@wordpress/icons'; import { _x } from '@wordpress/i18n'; -/** - * Internal dependencies - */ -import { store as blockEditorStore } from '../../store'; -import { unlock } from '../../lock-unlock'; - -function ZoomOutModeInserterButton( { - previousClientId, - nextClientId, - index, -} ) { - const { - sectionRootClientId, - setInserterIsOpened, - hasSelection, - selectedBlockClientId, - hoveredBlockClientId, - } = useSelect( ( select ) => { - const { - getSettings, - getSelectionStart, - getSelectedBlockClientId, - getHoveredBlockClientId, - } = select( blockEditorStore ); - const { sectionRootClientId: root } = unlock( getSettings() ); - - return { - hasSelection: !! getSelectionStart().clientId, - sectionRootClientId: root, - setInserterIsOpened: - getSettings().__experimentalSetIsInserterOpened, - selectedBlockClientId: getSelectedBlockClientId(), - hoveredBlockClientId: getHoveredBlockClientId(), - }; - }, [] ); - - const isSelected = - hasSelection && - ( selectedBlockClientId === previousClientId || - selectedBlockClientId === nextClientId ); - - const isHovered = - hoveredBlockClientId === previousClientId || - hoveredBlockClientId === nextClientId; - +function ZoomOutModeInserterButton( { isVisible, onClick } ) { const [ zoomOutModeInserterButtonHovered, setZoomOutModeInserterButtonHovered, @@ -71,20 +26,10 @@ function ZoomOutModeInserterButton( { 'block-editor-button-pattern-inserter__button', 'block-editor-block-tools__zoom-out-mode-inserter-button', { - 'is-visible': - isHovered || - isSelected || - zoomOutModeInserterButtonHovered, + 'is-visible': isVisible || zoomOutModeInserterButtonHovered, } ) } - onClick={ () => { - setInserterIsOpened( { - rootClientId: sectionRootClientId, - insertionIndex: index, - tab: 'patterns', - category: 'all', - } ); - } } + onClick={ onClick } onMouseOver={ () => { setZoomOutModeInserterButtonHovered( true ); } } diff --git a/packages/block-editor/src/components/block-tools/zoom-out-mode-inserters.js b/packages/block-editor/src/components/block-tools/zoom-out-mode-inserters.js index df30db00a36aaf..5f5b0401b512e0 100644 --- a/packages/block-editor/src/components/block-tools/zoom-out-mode-inserters.js +++ b/packages/block-editor/src/components/block-tools/zoom-out-mode-inserters.js @@ -14,34 +14,41 @@ import { unlock } from '../../lock-unlock'; function ZoomOutModeInserters() { const [ isReady, setIsReady ] = useState( false ); - const { blockOrder, insertionPoint, setInserterIsOpened } = useSelect( - ( select ) => { - const { - getSettings, - getBlockOrder, - getSelectionStart, - getSelectedBlockClientId, - } = select( blockEditorStore ); - const { sectionRootClientId: root } = unlock( getSettings() ); - // To do: move ZoomOutModeInserters to core/editor. - // Or we perhaps we should move the insertion point state to the - // block-editor store. I'm not sure what it was ever moved to the editor - // store, because all the inserter components all live in the - // block-editor package. - // eslint-disable-next-line @wordpress/data-no-store-string-literals - const editor = select( 'core/editor' ); - return { - hasSelection: !! getSelectionStart().clientId, - blockOrder: getBlockOrder( root ), - insertionPoint: unlock( editor ).getInsertionPoint(), - sectionRootClientId: root, - setInserterIsOpened: - getSettings().__experimentalSetIsInserterOpened, - selectedBlockClientId: getSelectedBlockClientId(), - }; - }, - [] - ); + const { + hasSelection, + blockOrder, + insertionPoint, + setInserterIsOpened, + sectionRootClientId, + selectedBlockClientId, + hoveredBlockClientId, + } = useSelect( ( select ) => { + const { + getSettings, + getBlockOrder, + getSelectionStart, + getSelectedBlockClientId, + getHoveredBlockClientId, + } = select( blockEditorStore ); + const { sectionRootClientId: root } = unlock( getSettings() ); + // To do: move ZoomOutModeInserters to core/editor. + // Or we perhaps we should move the insertion point state to the + // block-editor store. I'm not sure what it was ever moved to the editor + // store, because all the inserter components all live in the + // block-editor package. + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const editor = select( 'core/editor' ); + return { + hasSelection: !! getSelectionStart().clientId, + blockOrder: getBlockOrder( root ), + insertionPoint: unlock( editor ).getInsertionPoint(), + sectionRootClientId: root, + setInserterIsOpened: + getSettings().__experimentalSetIsInserterOpened, + selectedBlockClientId: getSelectedBlockClientId(), + hoveredBlockClientId: getHoveredBlockClientId(), + }; + }, [] ); const isMounted = useRef( false ); @@ -78,11 +85,23 @@ function ZoomOutModeInserters() { return null; } + const previousClientId = clientId; + const nextClientId = blockOrder[ index ]; + + const isSelected = + hasSelection && + ( selectedBlockClientId === previousClientId || + selectedBlockClientId === nextClientId ); + + const isHovered = + hoveredBlockClientId === previousClientId || + hoveredBlockClientId === nextClientId; + return ( { shouldRenderInsertionPoint && (
{ + setInserterIsOpened( { + rootClientId: sectionRootClientId, + insertionIndex: index, + tab: 'patterns', + category: 'all', + } ); + } } /> ) }