From 4d0f303bf0bf231db12221f03548cbc067c302b1 Mon Sep 17 00:00:00 2001 From: Marin Atanasov <8436925+tyxla@users.noreply.github.com> Date: Wed, 14 Jun 2023 13:00:11 +0300 Subject: [PATCH] Lodash: Replace _.mergeWith() with deepmerge in blocks (#50637) --- package-lock.json | 1 + packages/blocks/package.json | 1 + packages/blocks/src/api/raw-handling/utils.js | 66 +++++++++++-------- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index a985c7f3c6c09c..aa3bb869bd9230 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17371,6 +17371,7 @@ "@wordpress/shortcode": "file:packages/shortcode", "change-case": "^4.1.2", "colord": "^2.7.0", + "deepmerge": "^4.3.0", "fast-deep-equal": "^3.1.3", "hpq": "^1.3.0", "is-plain-object": "^5.0.0", diff --git a/packages/blocks/package.json b/packages/blocks/package.json index 16ecade2a1ee06..8ab2305db8a1bc 100644 --- a/packages/blocks/package.json +++ b/packages/blocks/package.json @@ -45,6 +45,7 @@ "@wordpress/shortcode": "file:../shortcode", "change-case": "^4.1.2", "colord": "^2.7.0", + "deepmerge": "^4.3.0", "fast-deep-equal": "^3.1.3", "hpq": "^1.3.0", "is-plain-object": "^5.0.0", diff --git a/packages/blocks/src/api/raw-handling/utils.js b/packages/blocks/src/api/raw-handling/utils.js index 8d1b4a5ef3684e..76818f2663627d 100644 --- a/packages/blocks/src/api/raw-handling/utils.js +++ b/packages/blocks/src/api/raw-handling/utils.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { mergeWith } from 'lodash'; +import deepmerge from 'deepmerge'; /** * WordPress dependencies @@ -14,6 +14,41 @@ import { isPhrasingContent, getPhrasingContentSchema } from '@wordpress/dom'; import { hasBlockSupport } from '..'; import { getRawTransforms } from './get-raw-transforms'; +const customMerge = ( key ) => { + return ( srcValue, objValue ) => { + switch ( key ) { + case 'children': { + if ( objValue === '*' || srcValue === '*' ) { + return '*'; + } + + return { ...objValue, ...srcValue }; + } + case 'attributes': + case 'require': { + return [ ...( objValue || [] ), ...( srcValue || [] ) ]; + } + case 'isMatch': { + // If one of the values being merge is undefined (matches everything), + // the result of the merge will be undefined. + if ( ! objValue || ! srcValue ) { + return undefined; + } + // When merging two isMatch functions, the result is a new function + // that returns if one of the source functions returns true. + return ( ...args ) => { + return objValue( ...args ) || srcValue( ...args ); + }; + } + } + + return deepmerge( objValue, srcValue, { + customMerge, + clone: false, + } ); + }; +}; + export function getBlockContentSchemaFromTransforms( transforms, context ) { const phrasingContentSchema = getPhrasingContentSchema( context ); const schemaArgs = { phrasingContentSchema, isPaste: context === 'paste' }; @@ -51,32 +86,9 @@ export function getBlockContentSchemaFromTransforms( transforms, context ) { ); } ); - return mergeWith( {}, ...schemas, ( objValue, srcValue, key ) => { - switch ( key ) { - case 'children': { - if ( objValue === '*' || srcValue === '*' ) { - return '*'; - } - - return { ...objValue, ...srcValue }; - } - case 'attributes': - case 'require': { - return [ ...( objValue || [] ), ...( srcValue || [] ) ]; - } - case 'isMatch': { - // If one of the values being merge is undefined (matches everything), - // the result of the merge will be undefined. - if ( ! objValue || ! srcValue ) { - return undefined; - } - // When merging two isMatch functions, the result is a new function - // that returns if one of the source functions returns true. - return ( ...args ) => { - return objValue( ...args ) || srcValue( ...args ); - }; - } - } + return deepmerge.all( schemas, { + customMerge, + clone: false, } ); }