From 37af468b89da013e97cafa30561c16481232a719 Mon Sep 17 00:00:00 2001 From: Ella Date: Mon, 29 Jan 2024 13:21:14 +0200 Subject: [PATCH 1/3] Block editor: selectors: avoid has() or double get() on Maps --- packages/block-editor/src/store/selectors.js | 26 ++++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 099c6b30222efc..7de1f2c94db168 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -148,21 +148,18 @@ export function getBlockAttributes( state, clientId ) { * @return {Object} Parsed block object. */ export function getBlock( state, clientId ) { - if ( ! state.blocks.byClientId.has( clientId ) ) { - return null; - } - - return state.blocks.tree.get( clientId ); + return state.blocks.tree.get( clientId ) ?? null; } export const __unstableGetBlockWithoutInnerBlocks = createSelector( ( state, clientId ) => { - if ( ! state.blocks.byClientId.has( clientId ) ) { + const block = state.blocks.byClientId.get( clientId ); + if ( ! block ) { return null; } return { - ...state.blocks.byClientId.get( clientId ), + ...block, attributes: getBlockAttributes( state, clientId ), }; }, @@ -540,9 +537,7 @@ export function getSelectedBlock( state ) { * @return {?string} Root client ID, if exists */ export function getBlockRootClientId( state, clientId ) { - return state.blocks.parents.has( clientId ) - ? state.blocks.parents.get( clientId ) - : null; + return state.blocks.parents.get( clientId ) ?? null; } /** @@ -558,8 +553,7 @@ export const getBlockParents = createSelector( ( state, clientId, ascending = false ) => { const parents = []; let current = clientId; - while ( !! state.blocks.parents.get( current ) ) { - current = state.blocks.parents.get( current ); + while ( ( current = state.blocks.parents.get( current ) ) ) { parents.push( current ); } @@ -2737,8 +2731,7 @@ export const __unstableGetContentLockingParent = createSelector( ( state, clientId ) => { let current = clientId; let result; - while ( state.blocks.parents.has( current ) ) { - current = state.blocks.parents.get( current ); + while ( ( current = current = state.blocks.parents.get( current ) ) ) { if ( ( current && getBlockName( state, current ) === 'core/block' ) || @@ -2869,8 +2862,9 @@ export function __unstableIsWithinBlockOverlay( state, clientId ) { export const getBlockEditingMode = createRegistrySelector( ( select ) => ( state, clientId = '' ) => { - if ( state.blockEditingModes.has( clientId ) ) { - return state.blockEditingModes.get( clientId ); + const blockEditingMode = state.blockEditingModes.get( clientId ); + if ( blockEditingMode ) { + return blockEditingMode; } if ( ! clientId ) { return 'default'; From fc87b2c1a51e46d34617a6b665dcc8dc3b73cb97 Mon Sep 17 00:00:00 2001 From: Ella Date: Mon, 29 Jan 2024 13:36:03 +0200 Subject: [PATCH 2/3] Fix typo --- packages/block-editor/src/store/selectors.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 7de1f2c94db168..9d4df35832985c 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2731,12 +2731,10 @@ export const __unstableGetContentLockingParent = createSelector( ( state, clientId ) => { let current = clientId; let result; - while ( ( current = current = state.blocks.parents.get( current ) ) ) { + while ( ( current = state.blocks.parents.get( current ) ) ) { if ( - ( current && - getBlockName( state, current ) === 'core/block' ) || - ( current && - getTemplateLock( state, current ) === 'contentOnly' ) + getBlockName( state, current ) === 'core/block' || + getTemplateLock( state, current ) === 'contentOnly' ) { result = current; } From 45aeb7ada187857200b0c99b0f00c6aea744e355 Mon Sep 17 00:00:00 2001 From: Ella Date: Mon, 29 Jan 2024 13:38:44 +0200 Subject: [PATCH 3/3] Revert getBlock --- packages/block-editor/src/store/selectors.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 9d4df35832985c..0f6f7c33a91fa0 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -148,7 +148,11 @@ export function getBlockAttributes( state, clientId ) { * @return {Object} Parsed block object. */ export function getBlock( state, clientId ) { - return state.blocks.tree.get( clientId ) ?? null; + if ( ! state.blocks.byClientId.has( clientId ) ) { + return null; + } + + return state.blocks.tree.get( clientId ); } export const __unstableGetBlockWithoutInnerBlocks = createSelector(