Skip to content

Commit

Permalink
Starter page templates: correctly insert the pattern to the Content b…
Browse files Browse the repository at this point in the history
…lock
  • Loading branch information
fushar committed Dec 12, 2024
1 parent 6be299d commit 1acb3d2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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

0 comments on commit 1acb3d2

Please sign in to comment.