From 1b78fa9b5bdaa5c61509266b8cbca667911219e7 Mon Sep 17 00:00:00 2001 From: Ella Date: Tue, 30 Jan 2024 13:18:45 +0200 Subject: [PATCH 1/2] Block editor: avoid list re-rendering on select --- .../src/components/block-list/index.js | 57 ++++++++++++------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/packages/block-editor/src/components/block-list/index.js b/packages/block-editor/src/components/block-list/index.js index 09a4712a3dd6dd..cf88a5f679b179 100644 --- a/packages/block-editor/src/components/block-list/index.js +++ b/packages/block-editor/src/components/block-list/index.js @@ -155,25 +155,42 @@ function Items( { __experimentalAppenderTagName, layout = defaultLayout, } ) { + const selected = useSelect( + ( select ) => { + const { + getBlockOrder, + getSelectedBlockClientIds, + __unstableGetVisibleBlocks, + __unstableGetTemporarilyEditingAsBlocks, + } = select( blockEditorStore ); + const order = getBlockOrder( rootClientId ); + const selectedBlocks = getSelectedBlockClientIds(); + const visibleBlocks = __unstableGetVisibleBlocks(); + let hasVisibleBlocks = false; + let hasSelectedBlocks = false; + for ( const clientId of selectedBlocks ) { + if ( visibleBlocks.has( clientId ) ) { + hasVisibleBlocks = true; + } + if ( order.includes( clientId ) ) { + hasSelectedBlocks = true; + } + } + return { + order, + // Only pass selectedBlocks and visibleBlocks if they are + // relevant to the root block. Otherwise changes in visible + // blocks or selected blocks cause all block lists to re-render. + selectedBlocks: hasSelectedBlocks ? selectedBlocks : null, + visibleBlocks: hasVisibleBlocks ? visibleBlocks : null, + temporarilyEditingAsBlocks: + __unstableGetTemporarilyEditingAsBlocks(), + }; + }, + [ rootClientId ] + ); const { order, selectedBlocks, visibleBlocks, temporarilyEditingAsBlocks } = - useSelect( - ( select ) => { - const { - getBlockOrder, - getSelectedBlockClientIds, - __unstableGetVisibleBlocks, - __unstableGetTemporarilyEditingAsBlocks, - } = select( blockEditorStore ); - return { - order: getBlockOrder( rootClientId ), - selectedBlocks: getSelectedBlockClientIds(), - visibleBlocks: __unstableGetVisibleBlocks(), - temporarilyEditingAsBlocks: - __unstableGetTemporarilyEditingAsBlocks(), - }; - }, - [ rootClientId ] - ); + selected; return ( @@ -183,8 +200,8 @@ function Items( { value={ // Only provide data asynchronously if the block is // not visible and not selected. - ! visibleBlocks.has( clientId ) && - ! selectedBlocks.includes( clientId ) + ! visibleBlocks?.has( clientId ) && + ! selectedBlocks?.includes( clientId ) } > Date: Tue, 30 Jan 2024 13:39:25 +0200 Subject: [PATCH 2/2] Fix mistake --- packages/block-editor/src/components/block-list/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/block-list/index.js b/packages/block-editor/src/components/block-list/index.js index cf88a5f679b179..26176e15af1994 100644 --- a/packages/block-editor/src/components/block-list/index.js +++ b/packages/block-editor/src/components/block-list/index.js @@ -172,7 +172,7 @@ function Items( { if ( visibleBlocks.has( clientId ) ) { hasVisibleBlocks = true; } - if ( order.includes( clientId ) ) { + if ( selectedBlocks.includes( clientId ) ) { hasSelectedBlocks = true; } }