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: pass patterns selector as setting #58661

Merged
merged 5 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { usesContextKey } from './components/rich-text/format-edit';
import { ExperimentalBlockCanvas } from './components/block-canvas';
import { getDuotoneFilter } from './components/duotone/utils';
import { useFlashEditableBlocks } from './components/use-flash-editable-blocks';
import { selectBlockPatternsKey } from './store/private-keys';

/**
* Private @wordpress/block-editor APIs.
Expand Down Expand Up @@ -56,4 +57,5 @@ lock( privateApis, {
useReusableBlocksRenameHint,
usesContextKey,
useFlashEditableBlocks,
selectBlockPatternsKey,
} );
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
1 change: 1 addition & 0 deletions packages/block-editor/src/store/private-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const selectBlockPatternsKey = Symbol( 'selectBlockPatternsKey' );
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 @@ -22,6 +22,7 @@ import { checkAllowListRecursive, getAllPatternsDependants } from './utils';
import { INSERTER_PATTERN_TYPES } from '../components/inserter/block-patterns-tab/utils';
import { STORE_NAME } from './constants';
import { unlock } from '../lock-unlock';
import { selectBlockPatternsKey } from './private-keys';

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

Expand Down Expand Up @@ -250,10 +251,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 +282,7 @@ export const hasAllowedPatterns = createRegistrySelector( ( select ) =>
} );
},
( state, rootClientId ) => [
getAllPatternsDependants( state ),
getAllPatternsDependants( select )( state ),
state.settings.allowedBlockTypes,
state.settings.templateLock,
state.blockListSettings[ rootClientId ],
Expand Down Expand Up @@ -325,12 +322,12 @@ export const getAllPatterns = createRegistrySelector( ( select ) =>
return [
...userPatterns,
...__experimentalBlockPatterns,
...unlock( select( STORE_NAME ) ).getFetchedPatterns(),
...( state.settings[ selectBlockPatternsKey ]?.( 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 @@ -2064,15 +2064,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 @@ -2105,7 +2096,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 @@ -2307,12 +2307,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 @@ -2353,7 +2353,7 @@ export const __experimentalGetAllowedPatterns = createRegistrySelector(
);

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

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

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

Expand Down
9 changes: 7 additions & 2 deletions packages/block-editor/src/store/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import { selectBlockPatternsKey } from './private-keys';

export const checkAllowList = ( list, item, defaultResult = null ) => {
if ( typeof list === 'boolean' ) {
return list;
Expand Down Expand Up @@ -40,12 +45,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[ selectBlockPatternsKey ]?.( select ),
state.blockPatterns,
];
};
27 changes: 27 additions & 0 deletions packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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() ]
)
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ 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';
import { useViewportMatch } from '@wordpress/compose';
import { store as blocksStore } from '@wordpress/blocks';
import { privateApis } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import inserterMediaCategories from '../media-categories';
import { mediaUpload } from '../../utils';
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';

const EMPTY_BLOCKS_LIST = [];

Expand Down Expand Up @@ -247,17 +248,10 @@ 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 ) )
);
}
);
},
ellatrix marked this conversation as resolved.
Show resolved Hide resolved
[ unlock( privateApis ).selectBlockPatternsKey ]: ( select ) =>
unlock( select( coreStore ) ).getBlockPatternsForPostType(
postType
),
__experimentalReusableBlocks: reusableBlocks,
__experimentalBlockPatternCategories: blockPatternCategories,
__experimentalUserPatternCategories: userPatternCategories,
Expand Down
Loading