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

Starter page templates: correctly insert the pattern to the Content block #40583

Merged
merged 1 commit into from
Dec 12, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Starter page templates: correctly insert the pattern to the Content block when rendering mode is template-locked
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PagePatternModal, PatternDefinition } from '@automattic/page-pattern-modal';
import { BlockInstance } from '@wordpress/blocks';
import { useSelect, useDispatch } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { addFilter, removeFilter } from '@wordpress/hooks';
Expand All @@ -13,7 +14,7 @@ interface PagePatternsPluginProps {
patterns: PatternDefinition[];
}
type CoreEditorPlaceholder = {
getBlocks: ( ...args: unknown[] ) => Array< { name: string; clientId: string } >;
getBlocks: ( ...args: unknown[] ) => BlockInstance[];
getEditedPostAttribute: ( ...args: unknown[] ) => unknown;
};
type CoreEditPostPlaceholder = {
Expand All @@ -23,6 +24,24 @@ type CoreNuxPlaceholder = {
areTipsEnabled: ( ...args: unknown[] ) => boolean;
};

/**
* Recursively finds the Content block if any.
*
* @param blocks - The current blocks
*/
function findPostContentBlock( blocks: BlockInstance[] ): BlockInstance | null {
for ( const block of blocks ) {
if ( block.name === 'core/post-content' || block.name === 'a8c/post-content' ) {
return block;
}
const result = findPostContentBlock( block.innerBlocks );
if ( result ) {
return result;
}
}
return null;
}

/**
* Starter page templates feature plugin
*
Expand Down Expand Up @@ -60,7 +79,7 @@ export function PagePatternsPlugin( props: PagePatternsPluginProps ) {
const currentBlocks = ( select( 'core/editor' ) as CoreEditorPlaceholder ).getBlocks();
return {
getMeta: getMetaNew,
postContentBlock: currentBlocks.find( block => block.name === 'a8c/post-content' ),
postContentBlock: findPostContentBlock( currentBlocks ),
};
}, [] );

Expand Down
Loading