Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Oct 11, 2023
1 parent aaae875 commit e07cd80
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { parse } from '@wordpress/blocks';
* Internal dependencies
*/
import { store as editSiteStore } from '../../../store';
import { PATTERN_CORE_SOURCES, PATTERN_TYPES } from '../../../utils/constants';
import { useExistingTemplateParts } from '../../../utils/template-part-create';
import { PATTERN_TYPES } from '../../../utils/constants';
import { unlock } from '../../../lock-unlock';

function injectThemeAttributeInBlockTemplateContent(
Expand All @@ -34,29 +33,35 @@ function injectThemeAttributeInBlockTemplateContent(
return block;
}

// TODO - describe what this does?
/**
* Filter all patterns and return only the ones that are compatible with the current template.
*
* @param {Array} patterns An array of patterns.
* @param {Object} template The current template.
* @return {Array} Array of patterns that are compatible with the current template.
*/
function filterPatterns( patterns, template ) {
// Filter out duplicates.
const filterOutDuplicatesByName = ( currentItem, index, items ) =>
index === items.findIndex( ( item ) => currentItem.name === item.name );

// Filter out core patterns.
const filterOutCorePatterns = ( pattern ) =>
! PATTERN_CORE_SOURCES.includes( pattern.source );

// Filter only the patterns that are compatible with the current template.
const filterCompatiblePatterns = ( pattern ) =>
pattern.templateTypes?.includes( template.slug ) ||
pattern.blockTypes?.includes( 'core/template-part/' + template.area ); // TODO - get this working for templates.

return patterns.filter(
filterOutCorePatterns &&
filterOutDuplicatesByName &&
filterCompatiblePatterns
filterOutDuplicatesByName && filterCompatiblePatterns
);
}

// TODO - describe what this does?
/**
* Map the array of patterns to a format that can be used by the Block Pattern List component.
*
* @param {Array} patterns An array of patterns.
* @param {string} currentThemeStylesheet The current theme path.
* @return {Array} Array of pattern objects that are compatible with the current template.
*/
function preparePatterns( patterns, currentThemeStylesheet ) {
return patterns.map( ( pattern ) => ( {
...pattern,
Expand Down Expand Up @@ -98,29 +103,3 @@ export function useAvailablePatterns( template ) {
return preparePatterns( filteredPatterns, currentThemeStylesheet );
}, [ blockPatterns, restBlockPatterns, template, currentThemeStylesheet ] );
}

function prepareTemplateParts( templateParts, template ) {
// Filter only the patterns that are compatible with the current template.
const filterCompatiblePatterns = ( templatePart ) =>
templatePart.area?.includes( template.area );

return templateParts
.filter( filterCompatiblePatterns )
.map( ( templatePart ) => ( {
id: templatePart.id,
area: templatePart.area,
keywords: templateParts.keywords || [],
type: PATTERN_TYPES.theme, //TODO?
blocks: parse( templatePart.content.raw, {
__unstableSkipMigrationLogs: true,
} ),
} ) );
}

export function useAvailableTemplateParts( template ) {
const existingTemplateParts = useExistingTemplateParts();

return useMemo( () => {
return prepareTemplateParts( existingTemplateParts || [], template );
}, [ existingTemplateParts, template ] );
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default function TemplatePanel() {
</SidebarCard>
<p>
{ __(
'Choose a predefined pattern to switch up the look of your footer.'
'Choose a predefined pattern to switch up the look of your template.' // TODO - make this dynamic?
) }
</p>
<TemplatesList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ import { moreVertical } from '@wordpress/icons';
*/
import { store as editSiteStore } from '../../../store';
import isTemplateRevertable from '../../../utils/is-template-revertable';
import ReplaceTemplateButton from './replace-template-button';
import { useAvailablePatterns } from './hooks';

export default function Actions( { template } ) {
const availablePatterns = useAvailablePatterns( template );
const { revertTemplate } = useDispatch( editSiteStore );
const isRevertable = isTemplateRevertable( template );

if (
! isRevertable &&
( ! availablePatterns.length || availablePatterns.length < 1 )
) {
if ( ! isRevertable ) {
return null;
}

Expand All @@ -48,11 +42,6 @@ export default function Actions( { template } ) {
{ __( 'Clear customizations' ) }
</MenuItem>
) }
<ReplaceTemplateButton
availableTemplates={ availablePatterns }
template={ template }
onClick={ onClose }
/>
</MenuGroup>
) }
</DropdownMenu>
Expand Down

0 comments on commit e07cd80

Please sign in to comment.