Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Fix stale dependencies of selectors depending on editorTool preference #66833

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getClientIdsWithDescendants,
isNavigationMode,
getBlockRootClientId,
__unstableGetEditorMode,
} from './selectors';
import {
checkAllowListRecursive,
Expand Down Expand Up @@ -116,7 +117,7 @@ export const getEnabledClientIdsTree = createSelector(
state.blockEditingModes,
state.settings.templateLock,
state.blockListSettings,
state.editorMode,
__unstableGetEditorMode( state ),
Copy link
Member

@ellatrix ellatrix Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems wrong still, shouldn't it use select( store )?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a commit, does that look better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like some tests need to be updated now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should now be fixed too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes, @talldan! This was an end-of-day PR for me, and I didn't wait for the tests...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, thought I'd help out on the night shift, as it looked fairly urgent 😄

state.zoomLevel,
getSectionRootClientId( state ),
]
Expand Down Expand Up @@ -317,7 +318,7 @@ export const hasAllowedPatterns = createRegistrySelector( ( select ) =>
},
( state, rootClientId ) => [
...getAllPatternsDependants( select )( state ),
...getInsertBlockTypeDependants( state, rootClientId ),
...getInsertBlockTypeDependants( select )( state, rootClientId ),
]
)
);
Expand Down
95 changes: 52 additions & 43 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1794,10 +1794,12 @@ const canInsertBlockTypeUnmemoized = (
*
* @return {boolean} Whether the given block type is allowed to be inserted.
*/
export const canInsertBlockType = createSelector(
canInsertBlockTypeUnmemoized,
( state, blockName, rootClientId ) =>
getInsertBlockTypeDependants( state, rootClientId )
export const canInsertBlockType = createRegistrySelector( ( select ) =>
createSelector(
canInsertBlockTypeUnmemoized,
( state, blockName, rootClientId ) =>
getInsertBlockTypeDependants( select )( state, rootClientId )
)
);

/**
Expand Down Expand Up @@ -2224,7 +2226,7 @@ export const getInserterItems = createRegistrySelector( ( select ) =>
unlock( select( STORE_NAME ) ).getReusableBlocks(),
state.blocks.order,
state.preferences.insertUsage,
...getInsertBlockTypeDependants( state, rootClientId ),
...getInsertBlockTypeDependants( select )( state, rootClientId ),
]
)
);
Expand Down Expand Up @@ -2255,44 +2257,51 @@ export const getInserterItems = createRegistrySelector( ( select ) =>
* this item.
* @property {number} frecency Heuristic that combines frequency and recency.
*/
export const getBlockTransformItems = createSelector(
( state, blocks, rootClientId = null ) => {
const normalizedBlocks = Array.isArray( blocks ) ? blocks : [ blocks ];
const buildBlockTypeTransformItem = buildBlockTypeItem( state, {
buildScope: 'transform',
} );
const blockTypeTransformItems = getBlockTypes()
.filter( ( blockType ) =>
canIncludeBlockTypeInInserter( state, blockType, rootClientId )
)
.map( buildBlockTypeTransformItem );
export const getBlockTransformItems = createRegistrySelector( ( select ) =>
createSelector(
( state, blocks, rootClientId = null ) => {
const normalizedBlocks = Array.isArray( blocks )
? blocks
: [ blocks ];
const buildBlockTypeTransformItem = buildBlockTypeItem( state, {
buildScope: 'transform',
} );
const blockTypeTransformItems = getBlockTypes()
.filter( ( blockType ) =>
canIncludeBlockTypeInInserter(
state,
blockType,
rootClientId
)
)
.map( buildBlockTypeTransformItem );

const itemsByName = Object.fromEntries(
Object.entries( blockTypeTransformItems ).map( ( [ , value ] ) => [
value.name,
value,
] )
);
const itemsByName = Object.fromEntries(
Object.entries( blockTypeTransformItems ).map(
( [ , value ] ) => [ value.name, value ]
)
);

const possibleTransforms = getPossibleBlockTransformations(
normalizedBlocks
).reduce( ( accumulator, block ) => {
if ( itemsByName[ block?.name ] ) {
accumulator.push( itemsByName[ block.name ] );
}
return accumulator;
}, [] );
return orderBy(
possibleTransforms,
( block ) => itemsByName[ block.name ].frecency,
'desc'
);
},
( state, blocks, rootClientId ) => [
getBlockTypes(),
state.preferences.insertUsage,
...getInsertBlockTypeDependants( state, rootClientId ),
]
const possibleTransforms = getPossibleBlockTransformations(
normalizedBlocks
).reduce( ( accumulator, block ) => {
if ( itemsByName[ block?.name ] ) {
accumulator.push( itemsByName[ block.name ] );
}
return accumulator;
}, [] );
return orderBy(
possibleTransforms,
( block ) => itemsByName[ block.name ].frecency,
'desc'
);
},
( state, blocks, rootClientId ) => [
getBlockTypes(),
state.preferences.insertUsage,
...getInsertBlockTypeDependants( select )( state, rootClientId ),
]
)
);

/**
Expand Down Expand Up @@ -2360,7 +2369,7 @@ export const getAllowedBlocks = createRegistrySelector( ( select ) =>
( state, rootClientId ) => [
getBlockTypes(),
unlock( select( STORE_NAME ) ).getReusableBlocks(),
...getInsertBlockTypeDependants( state, rootClientId ),
...getInsertBlockTypeDependants( select )( state, rootClientId ),
]
)
);
Expand Down Expand Up @@ -2435,7 +2444,7 @@ export const __experimentalGetParsedPattern = createRegistrySelector(

const getAllowedPatternsDependants = ( select ) => ( state, rootClientId ) => [
...getAllPatternsDependants( select )( state ),
...getInsertBlockTypeDependants( state, rootClientId ),
...getInsertBlockTypeDependants( select )( state, rootClientId ),
];

const patternsWithParsedBlocks = new WeakMap();
Expand Down
23 changes: 12 additions & 11 deletions packages/block-editor/src/store/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,15 @@ export const getAllPatternsDependants = ( select ) => ( state ) => {
];
};

export function getInsertBlockTypeDependants( state, rootClientId ) {
return [
state.blockListSettings[ rootClientId ],
state.blocks.byClientId.get( rootClientId ),
state.settings.allowedBlockTypes,
state.settings.templateLock,
state.blockEditingModes,
state.editorMode,
getSectionRootClientId( state ),
];
}
export const getInsertBlockTypeDependants =
( select ) => ( state, rootClientId ) => {
return [
state.blockListSettings[ rootClientId ],
state.blocks.byClientId.get( rootClientId ),
state.settings.allowedBlockTypes,
state.settings.templateLock,
state.blockEditingModes,
select( STORE_NAME ).__unstableGetEditorMode( state ),
getSectionRootClientId( state ),
];
};
Loading