From 387cd65b7ac0e371c4ff30452131d3ddfcaf5652 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 30 Mar 2023 16:05:56 +1100 Subject: [PATCH] Add a copy function that lets blocks customize how they are duplicated/copied --- packages/blocks/src/api/factory.js | 32 +++++++++++++----------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/packages/blocks/src/api/factory.js b/packages/blocks/src/api/factory.js index 6df8eb00005a8..2842c68de1c81 100644 --- a/packages/blocks/src/api/factory.js +++ b/packages/blocks/src/api/factory.js @@ -83,37 +83,33 @@ export function createBlocksFromInnerBlocksTemplate( * Given a block object, returns a copy of the block object while sanitizing its attributes, * optionally merging new attributes and/or replacing its inner blocks. * - * @param {Object} block Block instance. - * @param {Object} mergeAttributes Block attributes. - * @param {?Array} newInnerBlocks Nested blocks. + * @param {Object} block Block instance. * * @return {Object} A cloned block. */ -export function __experimentalCloneSanitizedBlock( - block, - mergeAttributes = {}, - newInnerBlocks -) { +export function __experimentalCloneSanitizedBlock( block ) { const clientId = uuid(); const sanitizedAttributes = __experimentalSanitizeBlockAttributes( block.name, - { - ...block.attributes, - ...mergeAttributes, - } + block.attributes ); - return { + let newBlock = { ...block, clientId, attributes: sanitizedAttributes, - innerBlocks: - newInnerBlocks || - block.innerBlocks.map( ( innerBlock ) => - __experimentalCloneSanitizedBlock( innerBlock ) - ), + innerBlocks: block.innerBlocks.map( ( innerBlock ) => + __experimentalCloneSanitizedBlock( innerBlock ) + ), }; + + const blockType = getBlockType( block.name ); + if ( blockType?.copy ) { + newBlock = blockType.copy( newBlock ); + } + + return newBlock; } /**