Skip to content

Commit

Permalink
Separate fetch from static patterns setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jan 24, 2024
1 parent 297228b commit 7bdf71f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
13 changes: 8 additions & 5 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ export const getAllPatterns = createRegistrySelector( ( select ) =>
// This setting is left for back compat.
const {
__experimentalBlockPatterns = [],
__experimentalFetchBlockPatterns,
__experimentalUserPatternCategories = [],
__experimentalReusableBlocks = [],
} = state.settings;
Expand All @@ -321,10 +320,14 @@ export const getAllPatterns = createRegistrySelector( ( select ) =>
};
}
);
const blockPatterns = __experimentalFetchBlockPatterns
? unlock( select( store ) ).getFetchedPatterns()
: __experimentalBlockPatterns;
return [ ...userPatterns, ...blockPatterns ];
return [
...userPatterns,
...__experimentalBlockPatterns,
...unlock( select( store ) ).getFetchedPatterns(),
].filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
);
}, getAllPatternsDependants )
);

Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/store/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const getFetchedPatterns =
async ( { dispatch, select } ) => {
const { __experimentalFetchBlockPatterns } = select.getSettings();
if ( ! __experimentalFetchBlockPatterns ) {
return;
return [];
}
const patterns = await __experimentalFetchBlockPatterns();
dispatch( { type: 'RECEIVE_BLOCK_PATTERNS', patterns } );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ function useBlockEditorSettings( settings, postType, postId ) {
settings.__experimentalAdditionalBlockPatternCategories ?? // WP 6.0
settings.__experimentalBlockPatternCategories; // WP 5.9

const blockPatterns = useMemo(
() =>
[ ...( settingsBlockPatterns || [] ) ].filter(
( { postTypes } ) => {
return (
! postTypes ||
( Array.isArray( postTypes ) &&
postTypes.includes( postType ) )
);
}
),
[ settingsBlockPatterns, postType ]
);

const blockPatternCategories = useMemo(
() =>
[
Expand Down Expand Up @@ -232,24 +246,17 @@ function useBlockEditorSettings( settings, postType, postId ) {
isDistractionFree,
keepCaretInsideBlock,
mediaUpload: hasUploadPermissions ? mediaUpload : undefined,
__experimentalBlockPatterns: blockPatterns,
__experimentalFetchBlockPatterns: async () => {
const restPatterns = await fetchBlockPatterns();
return [
...( settingsBlockPatterns || [] ),
...( restPatterns || [] ),
]
.filter(
( x, index, arr ) =>
index ===
arr.findIndex( ( y ) => x.name === y.name )
)
.filter( ( { postTypes } ) => {
return ( await fetchBlockPatterns() ).filter(
( { postTypes } ) => {
return (
! postTypes ||
( Array.isArray( postTypes ) &&
postTypes.includes( postType ) )
);
} );
}
);
},
__experimentalReusableBlocks: reusableBlocks,
__experimentalBlockPatternCategories: blockPatternCategories,
Expand Down Expand Up @@ -292,7 +299,7 @@ function useBlockEditorSettings( settings, postType, postId ) {
hasUploadPermissions,
reusableBlocks,
userPatternCategories,
settingsBlockPatterns,
blockPatterns,
blockPatternCategories,
canUseUnfilteredHTML,
undo,
Expand Down

0 comments on commit 7bdf71f

Please sign in to comment.