Skip to content

Commit

Permalink
fetch patterns not template parts
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Oct 11, 2023
1 parent 3f3da4b commit 4aa4272
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ function injectThemeAttributeInBlockTemplateContent(
return block;
}

function preparePatterns( patterns, template, currentThemeStylesheet ) {
// TODO - describe what this does?
function filterPatterns( patterns, template ) {
// Filter out duplicates.
const filterOutDuplicatesByName = ( currentItem, index, items ) =>
index === items.findIndex( ( item ) => currentItem.name === item.name );
Expand All @@ -45,27 +46,31 @@ function preparePatterns( patterns, template, currentThemeStylesheet ) {

// Filter only the patterns that are compatible with the current template.
const filterCompatiblePatterns = ( pattern ) =>
pattern.templateTypes?.includes( template.slug );
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
)
.map( ( pattern ) => ( {
...pattern,
keywords: pattern.keywords || [],
type: PATTERN_TYPES.theme,
blocks: parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} ).map( ( block ) =>
injectThemeAttributeInBlockTemplateContent(
block,
currentThemeStylesheet
)
),
} ) );
return patterns.filter(
filterOutCorePatterns &&
filterOutDuplicatesByName &&
filterCompatiblePatterns
);
}

// TODO - describe what this does?
function preparePatterns( patterns, currentThemeStylesheet ) {
return patterns.map( ( pattern ) => ( {
...pattern,
keywords: pattern.keywords || [],
type: PATTERN_TYPES.theme,
blocks: parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} ).map( ( block ) =>
injectThemeAttributeInBlockTemplateContent(
block,
currentThemeStylesheet
)
),
} ) );
}

export function useAvailablePatterns( template ) {
Expand All @@ -89,11 +94,8 @@ export function useAvailablePatterns( template ) {
...( blockPatterns || [] ),
...( restBlockPatterns || [] ),
];
return preparePatterns(
mergedPatterns,
template,
currentThemeStylesheet
);
const filteredPatterns = filterPatterns( mergedPatterns, template );
return preparePatterns( filteredPatterns, currentThemeStylesheet );
}, [ blockPatterns, restBlockPatterns, template, currentThemeStylesheet ] );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import LastRevision from './last-revision';
import SidebarCard from '../sidebar-card';
import PatternCategories from './pattern-categories';
import { PATTERN_TYPES } from '../../../utils/constants';
import { useAvailableTemplateParts } from './hooks';
import { useAvailablePatterns } from './hooks';

const CARD_ICONS = {
wp_block: symbol,
Expand Down Expand Up @@ -69,7 +69,7 @@ export default function TemplatePanel() {
[]
);

const availableTemplateParts = useAvailableTemplateParts( record );
const availablePatterns = useAvailablePatterns( record );

if ( ! title && ! description ) {
return null;
Expand All @@ -92,7 +92,7 @@ export default function TemplatePanel() {
) }
</p>
<TemplatesList
availableTemplates={ availableTemplateParts }
availableTemplates={ availablePatterns }
onSelect={ () => {} }
/>
<LastRevision />
Expand Down

0 comments on commit 4aa4272

Please sign in to comment.