Skip to content

Commit

Permalink
Global Styles: Remove obsolete hook resolving shared block style vari…
Browse files Browse the repository at this point in the history
…ations (#62808)

Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: scruffian <scruffian@git.wordpress.org>
  • Loading branch information
4 people authored and ellatrix committed Jun 25, 2024
1 parent dd29a23 commit ad97722
Showing 1 changed file with 7 additions and 90 deletions.
97 changes: 7 additions & 90 deletions packages/editor/src/components/global-styles-provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import { isPlainObject } from 'is-plain-object';
/**
* WordPress dependencies
*/
import { registerBlockStyle, store as blocksStore } from '@wordpress/blocks';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect, useMemo, useCallback } from '@wordpress/element';
import { useMemo, useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import setNestedValue from '../../utils/set-nested-value';

const { GlobalStylesContext, cleanEmptyObject } = unlock(
blockEditorPrivateApis
Expand All @@ -32,85 +30,6 @@ export function mergeBaseAndUserConfigs( base, user ) {
} );
}

/**
* Resolves shared block style variation definitions from the user origin
* under their respective block types and registers the block style if required.
*
* @param {Object} userConfig Current user origin global styles data.
* @return {Object} Updated global styles data.
*/
function useResolvedBlockStyleVariationsConfig( userConfig ) {
const { getBlockStyles } = useSelect( blocksStore );
const sharedVariations = userConfig?.styles?.blocks?.variations;

// Collect block style variation definitions to merge and unregistered
// block styles for automatic registration.
const [ userConfigToMerge, unregisteredStyles ] = useMemo( () => {
if ( ! sharedVariations ) {
return [];
}

const variationsConfigToMerge = {};
const unregisteredBlockStyles = [];

Object.entries( sharedVariations ).forEach(
( [ variationName, variation ] ) => {
if ( ! variation?.blockTypes?.length ) {
return;
}

variation.blockTypes.forEach( ( blockName ) => {
const blockStyles = getBlockStyles( blockName );
const registeredBlockStyle = blockStyles.find(
( { name } ) => name === variationName
);

if ( ! registeredBlockStyle ) {
unregisteredBlockStyles.push( [
blockName,
{
name: variationName,
label: variationName,
},
] );
}

const path = [
'styles',
'blocks',
blockName,
'variations',
variationName,
];
setNestedValue( variationsConfigToMerge, path, variation );
} );
}
);

return [ variationsConfigToMerge, unregisteredBlockStyles ];
}, [ sharedVariations, getBlockStyles ] );

// Automatically register missing block styles from variations.
useEffect(
() =>
unregisteredStyles?.forEach( ( unregisteredStyle ) =>
registerBlockStyle( ...unregisteredStyle )
),
[ unregisteredStyles ]
);

// Merge shared block style variation definitions into overall user config.
const updatedConfig = useMemo( () => {
if ( ! userConfigToMerge ) {
return userConfig;
}

return deepmerge( userConfigToMerge, userConfig );
}, [ userConfigToMerge, userConfig ] );

return updatedConfig;
}

function useGlobalStylesUserConfig() {
const { globalStylesId, isReady, settings, styles, _links } = useSelect(
( select ) => {
Expand Down Expand Up @@ -199,7 +118,7 @@ function useGlobalStylesUserConfig() {
options
);
},
[ globalStylesId ]
[ globalStylesId, editEntityRecord, getEditedEntityRecord ]
);

return [ isReady, config, setConfig ];
Expand All @@ -219,28 +138,26 @@ export function useGlobalStylesContext() {
const [ isUserConfigReady, userConfig, setUserConfig ] =
useGlobalStylesUserConfig();
const [ isBaseConfigReady, baseConfig ] = useGlobalStylesBaseConfig();
const userConfigWithVariations =
useResolvedBlockStyleVariationsConfig( userConfig );

const mergedConfig = useMemo( () => {
if ( ! baseConfig || ! userConfigWithVariations ) {
if ( ! baseConfig || ! userConfig ) {
return {};
}

return mergeBaseAndUserConfigs( baseConfig, userConfigWithVariations );
}, [ userConfigWithVariations, baseConfig ] );
return mergeBaseAndUserConfigs( baseConfig, userConfig );
}, [ userConfig, baseConfig ] );

const context = useMemo( () => {
return {
isReady: isUserConfigReady && isBaseConfigReady,
user: userConfigWithVariations,
user: userConfig,
base: baseConfig,
merged: mergedConfig,
setUserConfig,
};
}, [
mergedConfig,
userConfigWithVariations,
userConfig,
baseConfig,
setUserConfig,
isUserConfigReady,
Expand Down

0 comments on commit ad97722

Please sign in to comment.