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

Patterns: parse nested patterns through __experimentalGetParsedPattern #59083

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 33 additions & 3 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2282,6 +2282,38 @@ export const __experimentalGetDirectInsertBlock = createSelector(
]
);

function parseBlocks( blocks, patterns ) {
for ( let i = 0; i < blocks.length; i++ ) {
const block = blocks[ i ];

// Check and parse innerBlocks if present
if ( block.innerBlocks && block.innerBlocks.length > 0 ) {
parseBlocks( block.innerBlocks, patterns ); // Recursively parse inner blocks
}

if ( block.name === 'core/pattern' ) {
const _pattern = patterns.find(
( { name } ) => name === block.attributes.slug
);
if ( _pattern ) {
const toInsert = deepParsePattern( _pattern, patterns );
blocks.splice( i, 1, ...toInsert );
i += toInsert.length - 1; // Adjust loop counter for the new blocks
}
}
}
}

function deepParsePattern( pattern, patterns ) {
const blocks = parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} );

parseBlocks( blocks, patterns ); // Start parsing with the top-level blocks

return blocks;
}

export const __experimentalGetParsedPattern = createRegistrySelector(
( select ) =>
createSelector( ( state, patternName ) => {
Expand All @@ -2295,9 +2327,7 @@ export const __experimentalGetParsedPattern = createRegistrySelector(
}
return {
...pattern,
blocks: parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} ),
blocks: deepParsePattern( pattern, patterns ),
};
}, getAllPatternsDependants( select ) )
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,38 @@ function injectThemeAttributeInBlockTemplateContent(
return block;
}

function parseBlocks( blocks, patterns ) {
for ( let i = 0; i < blocks.length; i++ ) {
const block = blocks[ i ];

// Check and parse innerBlocks if present
if ( block.innerBlocks && block.innerBlocks.length > 0 ) {
parseBlocks( block.innerBlocks, patterns ); // Recursively parse inner blocks
}

if ( block.name === 'core/pattern' ) {
const _pattern = patterns.find(
( { name } ) => name === block.attributes.slug
);
if ( _pattern ) {
const toInsert = deepParsePattern( _pattern, patterns );
blocks.splice( i, 1, ...toInsert );
i += toInsert.length - 1; // Adjust loop counter for the new blocks
}
}
}
}

function deepParsePattern( pattern, patterns ) {
const blocks = parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} );

parseBlocks( blocks, patterns ); // Start parsing with the top-level blocks

return blocks;
}

function preparePatterns( patterns, template, currentThemeStylesheet ) {
// Filter out duplicates.
const filterOutDuplicatesByName = ( currentItem, index, items ) =>
Expand All @@ -60,9 +92,7 @@ function preparePatterns( patterns, template, currentThemeStylesheet ) {
...pattern,
keywords: pattern.keywords || [],
type: PATTERN_TYPES.theme,
blocks: parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} ).map( ( block ) =>
blocks: deepParsePattern( pattern, patterns ).map( ( block ) =>
injectThemeAttributeInBlockTemplateContent(
block,
currentThemeStylesheet
Expand Down
Loading