Skip to content

Commit 6a7f83c

Browse files
ellatrixjsnajdrMamaduka
authored
Block editor: pass patterns selector as setting (#58661)
Co-authored-by: ellatrix <ellatrix@git.wordpress.org> Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
1 parent 99c7e35 commit 6a7f83c

10 files changed

Lines changed: 53 additions & 56 deletions

File tree

‎packages/block-editor/src/private-apis.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { usesContextKey } from './components/rich-text/format-edit';
2626
import { ExperimentalBlockCanvas } from './components/block-canvas';
2727
import { getDuotoneFilter } from './components/duotone/utils';
2828
import { useFlashEditableBlocks } from './components/use-flash-editable-blocks';
29+
import { selectBlockPatternsKey } from './store/private-keys';
2930

3031
/**
3132
* Private @wordpress/block-editor APIs.
@@ -56,4 +57,5 @@ lock( privateApis, {
5657
useReusableBlocksRenameHint,
5758
usesContextKey,
5859
useFlashEditableBlocks,
60+
selectBlockPatternsKey,
5961
} );

‎packages/block-editor/src/store/index.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import reducer from './reducer';
1010
import * as selectors from './selectors';
1111
import * as privateActions from './private-actions';
1212
import * as privateSelectors from './private-selectors';
13-
import * as resolvers from './resolvers';
1413
import * as actions from './actions';
1514
import { STORE_NAME } from './constants';
1615
import { unlock } from '../lock-unlock';
@@ -23,7 +22,6 @@ import { unlock } from '../lock-unlock';
2322
export const storeConfig = {
2423
reducer,
2524
selectors,
26-
resolvers,
2725
actions,
2826
};
2927

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const selectBlockPatternsKey = Symbol( 'selectBlockPatternsKey' );

‎packages/block-editor/src/store/private-selectors.js‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { checkAllowListRecursive, getAllPatternsDependants } from './utils';
2222
import { INSERTER_PATTERN_TYPES } from '../components/inserter/block-patterns-tab/utils';
2323
import { STORE_NAME } from './constants';
2424
import { unlock } from '../lock-unlock';
25+
import { selectBlockPatternsKey } from './private-keys';
2526

2627
export { getBlockSettings } from './get-block-settings';
2728

@@ -250,10 +251,6 @@ export const getInserterMediaCategories = createSelector(
250251
]
251252
);
252253

253-
export function getFetchedPatterns( state ) {
254-
return state.blockPatterns;
255-
}
256-
257254
/**
258255
* Returns whether there is at least one allowed pattern for inner blocks children.
259256
* This is useful for deferring the parsing of all patterns until needed.
@@ -285,7 +282,7 @@ export const hasAllowedPatterns = createRegistrySelector( ( select ) =>
285282
} );
286283
},
287284
( state, rootClientId ) => [
288-
getAllPatternsDependants( state ),
285+
getAllPatternsDependants( select )( state ),
289286
state.settings.allowedBlockTypes,
290287
state.settings.templateLock,
291288
state.blockListSettings[ rootClientId ],
@@ -325,12 +322,12 @@ export const getAllPatterns = createRegistrySelector( ( select ) =>
325322
return [
326323
...userPatterns,
327324
...__experimentalBlockPatterns,
328-
...unlock( select( STORE_NAME ) ).getFetchedPatterns(),
325+
...( state.settings[ selectBlockPatternsKey ]?.( select ) ?? [] ),
329326
].filter(
330327
( x, index, arr ) =>
331328
index === arr.findIndex( ( y ) => x.name === y.name )
332329
);
333-
}, getAllPatternsDependants )
330+
}, getAllPatternsDependants( select ) )
334331
);
335332

336333
/**

‎packages/block-editor/src/store/reducer.js‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,15 +2064,6 @@ function blockBindingsSources( state = {}, action ) {
20642064
return state;
20652065
}
20662066

2067-
function blockPatterns( state = [], action ) {
2068-
switch ( action.type ) {
2069-
case 'RECEIVE_BLOCK_PATTERNS':
2070-
return action.patterns;
2071-
}
2072-
2073-
return state;
2074-
}
2075-
20762067
const combinedReducers = combineReducers( {
20772068
blocks,
20782069
isDragging,
@@ -2105,7 +2096,6 @@ const combinedReducers = combineReducers( {
21052096
openedBlockSettingsMenu,
21062097
registeredInserterMediaCategories,
21072098
blockBindingsSources,
2108-
blockPatterns,
21092099
} );
21102100

21112101
function withAutomaticChangeReset( reducer ) {

‎packages/block-editor/src/store/resolvers.js‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎packages/block-editor/src/store/selectors.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,12 +2299,12 @@ export const __experimentalGetParsedPattern = createRegistrySelector(
22992299
__unstableSkipMigrationLogs: true,
23002300
} ),
23012301
};
2302-
}, getAllPatternsDependants )
2302+
}, getAllPatternsDependants( select ) )
23032303
);
23042304

2305-
const getAllowedPatternsDependants = ( state, rootClientId ) => {
2305+
const getAllowedPatternsDependants = ( select ) => ( state, rootClientId ) => {
23062306
return [
2307-
...getAllPatternsDependants( state ),
2307+
...getAllPatternsDependants( select )( state ),
23082308
state.settings.allowedBlockTypes,
23092309
state.settings.templateLock,
23102310
state.blockListSettings[ rootClientId ],
@@ -2345,7 +2345,7 @@ export const __experimentalGetAllowedPatterns = createRegistrySelector(
23452345
);
23462346

23472347
return patternsAllowed;
2348-
}, getAllowedPatternsDependants );
2348+
}, getAllowedPatternsDependants( select ) );
23492349
}
23502350
);
23512351

@@ -2384,7 +2384,7 @@ export const getPatternsByBlockTypes = createRegistrySelector( ( select ) =>
23842384
return filteredPatterns;
23852385
},
23862386
( state, blockNames, rootClientId ) =>
2387-
getAllowedPatternsDependants( state, rootClientId )
2387+
getAllowedPatternsDependants( select )( state, rootClientId )
23882388
)
23892389
);
23902390

@@ -2458,7 +2458,7 @@ export const __experimentalGetPatternTransformItems = createRegistrySelector(
24582458
);
24592459
},
24602460
( state, blocks, rootClientId ) =>
2461-
getAllowedPatternsDependants( state, rootClientId )
2461+
getAllowedPatternsDependants( select )( state, rootClientId )
24622462
)
24632463
);
24642464

‎packages/block-editor/src/store/utils.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import { selectBlockPatternsKey } from './private-keys';
5+
16
export const checkAllowList = ( list, item, defaultResult = null ) => {
27
if ( typeof list === 'boolean' ) {
38
return list;
@@ -40,12 +45,12 @@ export const checkAllowListRecursive = ( blocks, allowedBlockTypes ) => {
4045
return true;
4146
};
4247

43-
export const getAllPatternsDependants = ( state ) => {
48+
export const getAllPatternsDependants = ( select ) => ( state ) => {
4449
return [
4550
state.settings.__experimentalBlockPatterns,
4651
state.settings.__experimentalUserPatternCategories,
4752
state.settings.__experimentalReusableBlocks,
48-
state.settings.__experimentalFetchBlockPatterns,
53+
state.settings[ selectBlockPatternsKey ]?.( select ),
4954
state.blockPatterns,
5055
];
5156
};

‎packages/core-data/src/private-selectors.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import createSelector from 'rememo';
5+
6+
/**
7+
* WordPress dependencies
8+
*/
9+
import { createRegistrySelector } from '@wordpress/data';
10+
111
/**
212
* Internal dependencies
313
*/
414
import type { State } from './selectors';
15+
import { STORE_NAME } from './name';
516

617
type EntityRecordKey = string | number;
718

@@ -28,3 +39,19 @@ export function getNavigationFallbackId(
2839
): EntityRecordKey | undefined {
2940
return state.navigationFallbackId;
3041
}
42+
43+
export const getBlockPatternsForPostType = createRegistrySelector(
44+
( select: any ) =>
45+
createSelector(
46+
( state, postType ) =>
47+
select( STORE_NAME )
48+
.getBlockPatterns()
49+
.filter(
50+
( { postTypes } ) =>
51+
! postTypes ||
52+
( Array.isArray( postTypes ) &&
53+
postTypes.includes( postType ) )
54+
),
55+
() => [ select( STORE_NAME ).getBlockPatterns() ]
56+
)
57+
);

‎packages/editor/src/components/provider/use-block-editor-settings.js‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import {
77
store as coreStore,
88
__experimentalFetchLinkSuggestions as fetchLinkSuggestions,
99
__experimentalFetchUrlData as fetchUrlData,
10-
fetchBlockPatterns,
1110
} from '@wordpress/core-data';
1211
import { __ } from '@wordpress/i18n';
1312
import { store as preferencesStore } from '@wordpress/preferences';
1413
import { useViewportMatch } from '@wordpress/compose';
1514
import { store as blocksStore } from '@wordpress/blocks';
15+
import { privateApis } from '@wordpress/block-editor';
1616

1717
/**
1818
* Internal dependencies
1919
*/
2020
import inserterMediaCategories from '../media-categories';
2121
import { mediaUpload } from '../../utils';
2222
import { store as editorStore } from '../../store';
23+
import { unlock } from '../../lock-unlock';
2324

2425
const EMPTY_BLOCKS_LIST = [];
2526

@@ -244,17 +245,10 @@ function useBlockEditorSettings( settings, postType, postId ) {
244245
keepCaretInsideBlock,
245246
mediaUpload: hasUploadPermissions ? mediaUpload : undefined,
246247
__experimentalBlockPatterns: blockPatterns,
247-
__experimentalFetchBlockPatterns: async () => {
248-
return ( await fetchBlockPatterns() ).filter(
249-
( { postTypes } ) => {
250-
return (
251-
! postTypes ||
252-
( Array.isArray( postTypes ) &&
253-
postTypes.includes( postType ) )
254-
);
255-
}
256-
);
257-
},
248+
[ unlock( privateApis ).selectBlockPatternsKey ]: ( select ) =>
249+
unlock( select( coreStore ) ).getBlockPatternsForPostType(
250+
postType
251+
),
258252
__experimentalReusableBlocks: reusableBlocks,
259253
__experimentalBlockPatternCategories: blockPatternCategories,
260254
__experimentalUserPatternCategories: userPatternCategories,

0 commit comments

Comments
 (0)