From 9bba275a2d02fe799b9ed5437de0088189dc7707 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Thu, 12 Dec 2024 11:22:42 +0700 Subject: [PATCH] Starter page templates: correctly insert the pattern to the Content block --- .../changelog/fix-starter-page-templates | 4 ++++ .../page-patterns-plugin.tsx | 23 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 projects/packages/jetpack-mu-wpcom/changelog/fix-starter-page-templates diff --git a/projects/packages/jetpack-mu-wpcom/changelog/fix-starter-page-templates b/projects/packages/jetpack-mu-wpcom/changelog/fix-starter-page-templates new file mode 100644 index 0000000000000..e7caaa085dc62 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/fix-starter-page-templates @@ -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 diff --git a/projects/packages/jetpack-mu-wpcom/src/features/starter-page-templates/page-patterns-plugin.tsx b/projects/packages/jetpack-mu-wpcom/src/features/starter-page-templates/page-patterns-plugin.tsx index 2263c65e57710..5416ef359984b 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/starter-page-templates/page-patterns-plugin.tsx +++ b/projects/packages/jetpack-mu-wpcom/src/features/starter-page-templates/page-patterns-plugin.tsx @@ -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'; @@ -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 = { @@ -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 * @@ -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 ), }; }, [] );