diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 5072d577172b0a..c69ac359659cf5 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -42,17 +42,17 @@ const PatternEdit = ( { attributes, clientId } ) => { // because nested pattern blocks cannot be inserted if the parent block supports // inner blocks but doesn't have blockSettings in the state. window.queueMicrotask( () => { + // Clone blocks from the pattern before insertion to ensure they receive + // distinct client ids. See https://github.com/WordPress/gutenberg/issues/50628. + const clonedBlocks = selectedPattern.blocks.map( ( block ) => + cloneBlock( block ) + ); __unstableMarkNextChangeAsNotPersistent(); if ( syncStatus === 'partial' ) { - replaceInnerBlocks( - clientId, - selectedPattern.blocks.map( ( block ) => - cloneBlock( block ) - ) - ); + replaceInnerBlocks( clientId, clonedBlocks ); return; } - replaceBlocks( clientId, selectedPattern.blocks ); + replaceBlocks( clientId, clonedBlocks ); } ); } }, [ diff --git a/packages/block-library/src/pattern/v1/edit.js b/packages/block-library/src/pattern/v1/edit.js index aa475809ccb44f..b4900536ec274f 100644 --- a/packages/block-library/src/pattern/v1/edit.js +++ b/packages/block-library/src/pattern/v1/edit.js @@ -1,6 +1,7 @@ /** * WordPress dependencies */ +import { cloneBlock } from '@wordpress/blocks'; import { useSelect, useDispatch } from '@wordpress/data'; import { useEffect } from '@wordpress/element'; import { @@ -32,8 +33,13 @@ const PatternEdit = ( { attributes, clientId } ) => { // because nested pattern blocks cannot be inserted if the parent block supports // inner blocks but doesn't have blockSettings in the state. window.queueMicrotask( () => { + // Clone blocks from the pattern before insertion to ensure they receive + // distinct client ids. See https://github.com/WordPress/gutenberg/issues/50628. + const clonedBlocks = selectedPattern.blocks.map( ( block ) => + cloneBlock( block ) + ); __unstableMarkNextChangeAsNotPersistent(); - replaceBlocks( clientId, selectedPattern.blocks ); + replaceBlocks( clientId, clonedBlocks ); } ); } }, [