11/**
22 * External dependencies
33 */
4- import { mergeWith } from 'lodash ' ;
4+ import deepmerge from 'deepmerge ' ;
55
66/**
77 * WordPress dependencies
@@ -14,6 +14,41 @@ import { isPhrasingContent, getPhrasingContentSchema } from '@wordpress/dom';
1414import { hasBlockSupport } from '..' ;
1515import { getRawTransforms } from './get-raw-transforms' ;
1616
17+ const customMerge = ( key ) => {
18+ return ( srcValue , objValue ) => {
19+ switch ( key ) {
20+ case 'children' : {
21+ if ( objValue === '*' || srcValue === '*' ) {
22+ return '*' ;
23+ }
24+
25+ return { ...objValue , ...srcValue } ;
26+ }
27+ case 'attributes' :
28+ case 'require' : {
29+ return [ ...( objValue || [ ] ) , ...( srcValue || [ ] ) ] ;
30+ }
31+ case 'isMatch' : {
32+ // If one of the values being merge is undefined (matches everything),
33+ // the result of the merge will be undefined.
34+ if ( ! objValue || ! srcValue ) {
35+ return undefined ;
36+ }
37+ // When merging two isMatch functions, the result is a new function
38+ // that returns if one of the source functions returns true.
39+ return ( ...args ) => {
40+ return objValue ( ...args ) || srcValue ( ...args ) ;
41+ } ;
42+ }
43+ }
44+
45+ return deepmerge ( objValue , srcValue , {
46+ customMerge,
47+ clone : false ,
48+ } ) ;
49+ } ;
50+ } ;
51+
1752export function getBlockContentSchemaFromTransforms ( transforms , context ) {
1853 const phrasingContentSchema = getPhrasingContentSchema ( context ) ;
1954 const schemaArgs = { phrasingContentSchema, isPaste : context === 'paste' } ;
@@ -51,32 +86,9 @@ export function getBlockContentSchemaFromTransforms( transforms, context ) {
5186 ) ;
5287 } ) ;
5388
54- return mergeWith ( { } , ...schemas , ( objValue , srcValue , key ) => {
55- switch ( key ) {
56- case 'children' : {
57- if ( objValue === '*' || srcValue === '*' ) {
58- return '*' ;
59- }
60-
61- return { ...objValue , ...srcValue } ;
62- }
63- case 'attributes' :
64- case 'require' : {
65- return [ ...( objValue || [ ] ) , ...( srcValue || [ ] ) ] ;
66- }
67- case 'isMatch' : {
68- // If one of the values being merge is undefined (matches everything),
69- // the result of the merge will be undefined.
70- if ( ! objValue || ! srcValue ) {
71- return undefined ;
72- }
73- // When merging two isMatch functions, the result is a new function
74- // that returns if one of the source functions returns true.
75- return ( ...args ) => {
76- return objValue ( ...args ) || srcValue ( ...args ) ;
77- } ;
78- }
79- }
89+ return deepmerge . all ( schemas , {
90+ customMerge,
91+ clone : false ,
8092 } ) ;
8193}
8294
0 commit comments