Skip to content

Commit

Permalink
Block editor: pass patterns selector as setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Feb 5, 2024
1 parent 2f44afa commit 50eedab
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 56 deletions.
2 changes: 0 additions & 2 deletions packages/block-editor/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import reducer from './reducer';
import * as selectors from './selectors';
import * as privateActions from './private-actions';
import * as privateSelectors from './private-selectors';
import * as resolvers from './resolvers';
import * as actions from './actions';
import { STORE_NAME } from './constants';
import { unlock } from '../lock-unlock';
Expand All @@ -23,7 +22,6 @@ import { unlock } from '../lock-unlock';
export const storeConfig = {
reducer,
selectors,
resolvers,
actions,
};

Expand Down
11 changes: 4 additions & 7 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,6 @@ export const getInserterMediaCategories = createSelector(
]
);

export function getFetchedPatterns( state ) {
return state.blockPatterns;
}

/**
* Returns whether there is at least one allowed pattern for inner blocks children.
* This is useful for deferring the parsing of all patterns until needed.
Expand Down Expand Up @@ -285,7 +281,7 @@ export const hasAllowedPatterns = createRegistrySelector( ( select ) =>
} );
},
( state, rootClientId ) => [
getAllPatternsDependants( state ),
getAllPatternsDependants( select )( state ),
state.settings.allowedBlockTypes,
state.settings.templateLock,
state.blockListSettings[ rootClientId ],
Expand All @@ -301,6 +297,7 @@ export const getAllPatterns = createRegistrySelector( ( select ) =>
__experimentalBlockPatterns = [],
__experimentalUserPatternCategories = [],
__experimentalReusableBlocks = [],
__experimentalSelectBlockPatterns,
} = state.settings;
const userPatterns = ( __experimentalReusableBlocks ?? [] ).map(
( userPattern ) => {
Expand All @@ -325,12 +322,12 @@ export const getAllPatterns = createRegistrySelector( ( select ) =>
return [
...userPatterns,
...__experimentalBlockPatterns,
...unlock( select( STORE_NAME ) ).getFetchedPatterns(),
...( __experimentalSelectBlockPatterns( select ) ?? [] ),
].filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
);
}, getAllPatternsDependants )
}, getAllPatternsDependants( select ) )
);

/**
Expand Down
10 changes: 0 additions & 10 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2058,15 +2058,6 @@ function blockBindingsSources( state = {}, action ) {
return state;
}

function blockPatterns( state = [], action ) {
switch ( action.type ) {
case 'RECEIVE_BLOCK_PATTERNS':
return action.patterns;
}

return state;
}

const combinedReducers = combineReducers( {
blocks,
isDragging,
Expand Down Expand Up @@ -2099,7 +2090,6 @@ const combinedReducers = combineReducers( {
openedBlockSettingsMenu,
registeredInserterMediaCategories,
blockBindingsSources,
blockPatterns,
} );

function withAutomaticChangeReset( reducer ) {
Expand Down
17 changes: 0 additions & 17 deletions packages/block-editor/src/store/resolvers.js

This file was deleted.

12 changes: 6 additions & 6 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2299,12 +2299,12 @@ export const __experimentalGetParsedPattern = createRegistrySelector(
__unstableSkipMigrationLogs: true,
} ),
};
}, getAllPatternsDependants )
}, getAllPatternsDependants( select ) )
);

const getAllowedPatternsDependants = ( state, rootClientId ) => {
const getAllowedPatternsDependants = ( select ) => ( state, rootClientId ) => {
return [
...getAllPatternsDependants( state ),
...getAllPatternsDependants( select )( state ),
state.settings.allowedBlockTypes,
state.settings.templateLock,
state.blockListSettings[ rootClientId ],
Expand Down Expand Up @@ -2345,7 +2345,7 @@ export const __experimentalGetAllowedPatterns = createRegistrySelector(
);

return patternsAllowed;
}, getAllowedPatternsDependants );
}, getAllowedPatternsDependants( select ) );
}
);

Expand Down Expand Up @@ -2384,7 +2384,7 @@ export const getPatternsByBlockTypes = createRegistrySelector( ( select ) =>
return filteredPatterns;
},
( state, blockNames, rootClientId ) =>
getAllowedPatternsDependants( state, rootClientId )
getAllowedPatternsDependants( select )( state, rootClientId )
)
);

Expand Down Expand Up @@ -2458,7 +2458,7 @@ export const __experimentalGetPatternTransformItems = createRegistrySelector(
);
},
( state, blocks, rootClientId ) =>
getAllowedPatternsDependants( state, rootClientId )
getAllowedPatternsDependants( select )( state, rootClientId )
)
);

Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/store/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export const checkAllowListRecursive = ( blocks, allowedBlockTypes ) => {
return true;
};

export const getAllPatternsDependants = ( state ) => {
export const getAllPatternsDependants = ( select ) => ( state ) => {
return [
state.settings.__experimentalBlockPatterns,
state.settings.__experimentalUserPatternCategories,
state.settings.__experimentalReusableBlocks,
state.settings.__experimentalFetchBlockPatterns,
state.settings.__experimentalSelectBlockPatterns?.( select ),
state.blockPatterns,
];
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
store as coreStore,
__experimentalFetchLinkSuggestions as fetchLinkSuggestions,
__experimentalFetchUrlData as fetchUrlData,
fetchBlockPatterns,
} from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
import { store as preferencesStore } from '@wordpress/preferences';
Expand Down Expand Up @@ -87,6 +86,7 @@ const BLOCK_EDITOR_SETTINGS = [
*/
function useBlockEditorSettings( settings, postType, postId ) {
const isLargeViewport = useViewportMatch( 'medium' );
const { getBlockPatterns } = useSelect( coreStore );
const {
allowRightClickOverrides,
blockTypes,
Expand Down Expand Up @@ -247,17 +247,7 @@ function useBlockEditorSettings( settings, postType, postId ) {
keepCaretInsideBlock,
mediaUpload: hasUploadPermissions ? mediaUpload : undefined,
__experimentalBlockPatterns: blockPatterns,
__experimentalFetchBlockPatterns: async () => {
return ( await fetchBlockPatterns() ).filter(
( { postTypes } ) => {
return (
! postTypes ||
( Array.isArray( postTypes ) &&
postTypes.includes( postType ) )
);
}
);
},
__experimentalSelectBlockPatterns: getBlockPatterns,
__experimentalReusableBlocks: reusableBlocks,
__experimentalBlockPatternCategories: blockPatternCategories,
__experimentalUserPatternCategories: userPatternCategories,
Expand Down Expand Up @@ -300,6 +290,7 @@ function useBlockEditorSettings( settings, postType, postId ) {
reusableBlocks,
userPatternCategories,
blockPatterns,
getBlockPatterns,
blockPatternCategories,
canUseUnfilteredHTML,
undo,
Expand Down

0 comments on commit 50eedab

Please sign in to comment.