From 8fe229588ef0226ad2546ee87eef379f095a4bad Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 7 Jun 2021 12:07:53 +0100 Subject: [PATCH] Avoid keeping the same client ID when transforming blocks (#32453) * Avoid keeping the same client ID when transforming blocks * Fix unit tests --- packages/blocks/src/api/factory.js | 22 +++++++--------------- packages/blocks/src/api/test/factory.js | 4 ---- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/packages/blocks/src/api/factory.js b/packages/blocks/src/api/factory.js index f01edef37f4f48..a0fab5c32cd39c 100644 --- a/packages/blocks/src/api/factory.js +++ b/packages/blocks/src/api/factory.js @@ -5,7 +5,7 @@ import { v4 as uuid } from 'uuid'; import { every, castArray, - findIndex, + some, isObjectLike, filter, first, @@ -538,28 +538,18 @@ export function switchToBlockType( blocks, name ) { return null; } - const firstSwitchedBlock = findIndex( + const hasSwitchedBlock = some( transformationResults, ( result ) => result.name === name ); // Ensure that at least one block object returned by the transformation has // the expected "destination" block type. - if ( firstSwitchedBlock < 0 ) { + if ( ! hasSwitchedBlock ) { return null; } - return transformationResults.map( ( result, index ) => { - const transformedBlock = { - ...result, - // The first transformed block whose type matches the "destination" - // type gets to keep the existing client ID of the first block. - clientId: - index === firstSwitchedBlock - ? firstBlock.clientId - : result.clientId, - }; - + const ret = transformationResults.map( ( result ) => { /** * Filters an individual transform result from block transformation. * All of the original blocks are passed, since transformations are @@ -570,10 +560,12 @@ export function switchToBlockType( blocks, name ) { */ return applyFilters( 'blocks.switchToBlockType.transformedBlock', - transformedBlock, + result, blocks ); } ); + + return ret; } /** diff --git a/packages/blocks/src/api/test/factory.js b/packages/blocks/src/api/test/factory.js index a840f65135cc61..fc38d753e997b6 100644 --- a/packages/blocks/src/api/test/factory.js +++ b/packages/blocks/src/api/test/factory.js @@ -1537,15 +1537,11 @@ describe( 'block factory', () => { // to keep the existing block's client ID. expect( transformedBlocks ).toHaveLength( 2 ); expect( transformedBlocks[ 0 ] ).toHaveProperty( 'clientId' ); - expect( transformedBlocks[ 0 ].clientId ).not.toBe( - block.clientId - ); expect( transformedBlocks[ 0 ].name ).toBe( 'core/text-block' ); expect( transformedBlocks[ 0 ].isValid ).toBe( true ); expect( transformedBlocks[ 0 ].attributes ).toEqual( { value: 'chicken ribs', } ); - expect( transformedBlocks[ 1 ].clientId ).toBe( block.clientId ); expect( transformedBlocks[ 1 ] ).toHaveProperty( 'clientId' ); expect( transformedBlocks[ 1 ].name ).toBe( 'core/updated-text-block'