From b5ee81618b812941b32ddbd20ddf5c0df94e6069 Mon Sep 17 00:00:00 2001 From: Ella Date: Mon, 12 Feb 2024 13:08:44 +0100 Subject: [PATCH] Restore filtering for post type --- packages/core-data/src/private-selectors.ts | 27 +++++++++++++++++++ .../provider/use-block-editor-settings.js | 9 +++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/core-data/src/private-selectors.ts b/packages/core-data/src/private-selectors.ts index 94aa00e1c8de4..85dc4e3be7203 100644 --- a/packages/core-data/src/private-selectors.ts +++ b/packages/core-data/src/private-selectors.ts @@ -1,7 +1,18 @@ +/** + * External dependencies + */ +import createSelector from 'rememo'; + +/** + * WordPress dependencies + */ +import { createRegistrySelector } from '@wordpress/data'; + /** * Internal dependencies */ import type { State } from './selectors'; +import { STORE_NAME } from './name'; type EntityRecordKey = string | number; @@ -28,3 +39,19 @@ export function getNavigationFallbackId( ): EntityRecordKey | undefined { return state.navigationFallbackId; } + +export const getBlockPatternsForPostType = createRegistrySelector( + ( select: any ) => + createSelector( + ( state, postType ) => + select( STORE_NAME ) + .getBlockPatterns() + .filter( + ( { postTypes } ) => + ! postTypes || + ( Array.isArray( postTypes ) && + postTypes.includes( postType ) ) + ), + () => [ select( STORE_NAME ).getBlockPatterns() ] + ) +); diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index 0aeadbe26ecd9..d8973850111d1 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -22,9 +22,6 @@ import { mediaUpload } from '../../utils'; import { store as editorStore } from '../../store'; import { unlock } from '../../lock-unlock'; -const selectBlockPatterns = ( select ) => - select( coreStore ).getBlockPatterns(); - const EMPTY_BLOCKS_LIST = []; const BLOCK_EDITOR_SETTINGS = [ @@ -251,8 +248,10 @@ function useBlockEditorSettings( settings, postType, postId ) { keepCaretInsideBlock, mediaUpload: hasUploadPermissions ? mediaUpload : undefined, __experimentalBlockPatterns: blockPatterns, - [ unlock( privateApis ).selectBlockPatternsKey ]: - selectBlockPatterns, + [ unlock( privateApis ).selectBlockPatternsKey ]: ( select ) => + unlock( select( coreStore ) ).getBlockPatternsForPostType( + postType + ), __experimentalReusableBlocks: reusableBlocks, __experimentalBlockPatternCategories: blockPatternCategories, __experimentalUserPatternCategories: userPatternCategories,