Skip to content

Commit

Permalink
Block editor: pass patterns selector as setting (#58661)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
  • Loading branch information
4 people authored Feb 14, 2024
1 parent 99c7e35 commit 6a7f83c
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 56 deletions.
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 @@ -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
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 @@ -244,17 +245,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 ) )
);
}
);
},
[ unlock( privateApis ).selectBlockPatternsKey ]: ( select ) =>
unlock( select( coreStore ) ).getBlockPatternsForPostType(
postType
),
__experimentalReusableBlocks: reusableBlocks,
__experimentalBlockPatternCategories: blockPatternCategories,
__experimentalUserPatternCategories: userPatternCategories,
Expand Down

1 comment on commit 6a7f83c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 6a7f83c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7901033028
📝 Reported issues:

Please sign in to comment.