diff --git a/packages/block-editor/src/components/block-switcher/pattern-transformations-menu.js b/packages/block-editor/src/components/block-switcher/pattern-transformations-menu.js index 1b58ab4ee8caa0..1bea3740d79ee7 100644 --- a/packages/block-editor/src/components/block-switcher/pattern-transformations-menu.js +++ b/packages/block-editor/src/components/block-switcher/pattern-transformations-menu.js @@ -5,7 +5,7 @@ import { __ } from '@wordpress/i18n'; import { useState, useMemo } from '@wordpress/element'; import { useInstanceId } from '@wordpress/compose'; import { chevronRight } from '@wordpress/icons'; -import { cloneBlock } from '@wordpress/blocks'; +import { cloneBlock, getBlockType } from '@wordpress/blocks'; import { MenuGroup, MenuItem, @@ -105,10 +105,29 @@ function PatternTransformationsMenu( { transformedBlocksSet ); if ( ! match ) return; - // Found a match so update it with the selected block's attributes. + // Found a match, so get the `retainAttributes` from + // block type's transforms to retain these and keep + // everything else from the pattern's block. + // If `retainAttributes` are not set, update the match + // with all the selected block's attributes. + const blockType = getBlockType( block.name ); + const retainAttributes = + blockType.transforms?.retainAttributes; + let retainedBlockAttributes = block.attributes; + if ( retainAttributes?.length ) { + retainedBlockAttributes = retainAttributes.reduce( + ( _accumulator, attribute ) => { + if ( block.attributes[ attribute ] ) + _accumulator[ attribute ] = + block.attributes[ attribute ]; + return _accumulator; + }, + {} + ); + } match.attributes = { ...match.attributes, - ...block.attributes, + ...retainedBlockAttributes, }; // When we have a match with inner blocks keep only the // blocks from the selected block and skip the inner blocks diff --git a/packages/block-library/src/heading/transforms.js b/packages/block-library/src/heading/transforms.js index 8e6768c0d19d8a..ef0e1dddbce83a 100644 --- a/packages/block-library/src/heading/transforms.js +++ b/packages/block-library/src/heading/transforms.js @@ -10,6 +10,7 @@ import { getLevelFromHeadingNodeName } from './shared'; import { name } from './block.json'; const transforms = { + retainAttributes: [ 'content' ], from: [ { type: 'block', diff --git a/packages/block-library/src/paragraph/transforms.js b/packages/block-library/src/paragraph/transforms.js index 5179a7f05c22fa..3ee12658bda9c4 100644 --- a/packages/block-library/src/paragraph/transforms.js +++ b/packages/block-library/src/paragraph/transforms.js @@ -9,6 +9,7 @@ import { createBlock, getBlockAttributes } from '@wordpress/blocks'; import { name } from './block.json'; const transforms = { + retainAttributes: [ 'content' ], from: [ { type: 'raw',