2023-10-02 08:28:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { BlockInstance } from '@wordpress/blocks';
|
|
|
|
import { isEqual } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2024-05-28 07:32:26 +00:00
|
|
|
import { PatternWithBlocks } from '~/customize-store/types/pattern';
|
2023-10-02 08:28:13 +00:00
|
|
|
|
|
|
|
export const findPatternByBlock = (
|
|
|
|
patterns: PatternWithBlocks[],
|
|
|
|
block: BlockInstance
|
|
|
|
) => {
|
|
|
|
const blockAttributes = block.attributes;
|
|
|
|
if (
|
|
|
|
blockAttributes.className &&
|
|
|
|
blockAttributes.className.includes( 'preview-opacity' )
|
|
|
|
) {
|
|
|
|
// Remove the preview opacity class from the footer block because it's added by the editor, not the pattern.
|
|
|
|
blockAttributes.className = blockAttributes.className.replaceAll(
|
|
|
|
' preview-opacity',
|
|
|
|
''
|
|
|
|
);
|
|
|
|
}
|
2024-03-05 17:21:44 +00:00
|
|
|
return patterns.find( ( pattern ) => {
|
|
|
|
const patternBlocks = pattern.blocks[ 0 ];
|
|
|
|
if ( patternBlocks.innerBlocks.length !== block.innerBlocks.length ) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-10-02 08:28:13 +00:00
|
|
|
|
2024-03-05 17:21:44 +00:00
|
|
|
return isEqual( patternBlocks.attributes, blockAttributes );
|
|
|
|
} );
|
2023-10-02 08:28:13 +00:00
|
|
|
};
|