Skip to content

Commit

Permalink
enhacements
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jul 23, 2024
1 parent 2a3af4d commit 1f64df8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 54 deletions.
50 changes: 36 additions & 14 deletions packages/edit-site/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
* WordPress dependencies
*/
import { store as coreDataStore } from '@wordpress/core-data';
import { createRegistrySelector } from '@wordpress/data';
import { createRegistrySelector, createSelector } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import { Platform } from '@wordpress/element';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as editorStore } from '@wordpress/editor';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';
import { TEMPLATE_PART_POST_TYPE } from '../utils/constants';
import getFilteredTemplatePartBlocks from '../utils/get-filtered-template-parts';

/**
* @typedef {'template'|'template_type'} TemplateType Template type.
Expand Down Expand Up @@ -240,6 +243,20 @@ export function isSaveViewOpened( state ) {
return state.saveViewPanel;
}

function getBlocksAndTemplateParts( select ) {
const templateParts = select( coreDataStore ).getEntityRecords(
'postType',
TEMPLATE_PART_POST_TYPE,
{ per_page: -1 }
);

const { getBlocksByName, getBlocksByClientId } = select( blockEditorStore );

const clientIds = getBlocksByName( 'core/template-part' );
const blocks = getBlocksByClientId( clientIds );
return [ blocks, templateParts ];
}

/**
* Returns the template parts and their blocks for the current edited template.
*
Expand All @@ -248,19 +265,24 @@ export function isSaveViewOpened( state ) {
* @return {Array} Template parts and their blocks in an array.
*/
export const getCurrentTemplateTemplateParts = createRegistrySelector(
( select ) => () => {
deprecated(
`select( 'core/edit-site' ).getCurrentTemplateTemplateParts()`,
{
since: '6.7',
version: '6.9',
alternative: `select( 'core/block-editor' ).getBlocksByName( 'core/template-part' )`,
}
);
return unlock(
select( editorStore )
).getCurrentTemplateTemplateParts();
}
( select ) =>
createSelector(
() => {
deprecated(
`select( 'core/edit-site' ).getCurrentTemplateTemplateParts()`,
{
since: '6.7',
version: '6.9',
alternative: `select( 'core/block-editor' ).getBlocksByName( 'core/template-part' )`,
}
);

return getFilteredTemplatePartBlocks(
...getBlocksAndTemplateParts( select )
);
},
() => getBlocksAndTemplateParts( select )
)
);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import memoize from 'memize';

/**
* WordPress dependencies
*/
Expand All @@ -20,7 +15,10 @@ const EMPTY_ARRAY = [];
* @param {?Array} templateParts Available template parts.
* @return {Array} An array of template parts and their blocks.
*/
function getFilteredTemplatePartBlocks( blocks = EMPTY_ARRAY, templateParts ) {
export default function getFilteredTemplatePartBlocks(
blocks = EMPTY_ARRAY,
templateParts
) {
const templatePartsById = templateParts
? // Key template parts by their ID.
templateParts.reduce(
Expand Down Expand Up @@ -61,9 +59,3 @@ function getFilteredTemplatePartBlocks( blocks = EMPTY_ARRAY, templateParts ) {

return result;
}

const memoizedGetFilteredTemplatePartBlocks = memoize(
getFilteredTemplatePartBlocks
);

export { memoizedGetFilteredTemplatePartBlocks as getFilteredTemplatePartBlocks };
File renamed without changes.
28 changes: 0 additions & 28 deletions packages/editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
__experimentalGetDefaultTemplatePartAreas,
} from './selectors';
import { getEntityActions as _getEntityActions } from '../dataviews/store/private-selectors';
import { getFilteredTemplatePartBlocks } from './utils/get-filtered-template-parts';
import { TEMPLATE_PART_POST_TYPE } from './constants';

const EMPTY_INSERTION_POINT = {
Expand Down Expand Up @@ -162,30 +161,3 @@ export const hasPostMetaChanges = createRegistrySelector(
export function getEntityActions( state, ...args ) {
return _getEntityActions( state.dataviews, ...args );
}

/**
* Returns the template parts and their blocks for the current edited template.
* This selector is deprecated and not used on the codebase, was just kept because
* it is called inside select( 'core/edit-site' ).getCurrentTemplateTemplateParts()
* which is also deprecated.
*
* @deprecated
* @param {Object} state Global application state.
* @return {Array} Template parts and their blocks in an array.
*/
export const getCurrentTemplateTemplateParts = createRegistrySelector(
( select ) => () => {
const templateParts = select( coreStore ).getEntityRecords(
'postType',
TEMPLATE_PART_POST_TYPE,
{ per_page: -1 }
);

const clientIds =
select( blockEditorStore ).getBlocksByName( 'core/template-part' );
const blocks =
select( blockEditorStore ).getBlocksByClientId( clientIds );

return getFilteredTemplatePartBlocks( blocks, templateParts );
}
);

0 comments on commit 1f64df8

Please sign in to comment.