Skip to content

Commit

Permalink
New site editor useSetting
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jun 8, 2021
1 parent d3cdc67 commit 3918f63
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions packages/edit-site/src/components/editor/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,24 @@ function getPresetMetadataFromStyleProperty( styleProperty ) {
return getPresetMetadataFromStyleProperty.MAP[ styleProperty ];
}

function getHighestPriorityOrigin( presetByOrigin, path ) {
if ( presetByOrigin && presetPaths[ path ] ) {
const origins = [ 'user', 'theme', 'core' ];
for ( const origin of origins ) {
if ( presetByOrigin[ origin ] ) {
return presetByOrigin[ origin ];
}
}
return undefined;
}
return presetByOrigin;
}
const PATHS_WITH_MERGE = {
'color.gradients': true,
'color.palette': true,
'typography.fontFamilies': true,
'typography.fontSizes': true,
};

export function useSetting( path, blockName = '' ) {
const settings = useSelect( ( select ) => {
return select( editSiteStore ).getSettings();
} );
const topLevelPath = `__experimentalFeatures.${ path }`;
const blockPath = `__experimentalFeatures.blocks.${ blockName }.${ path }`;
return (
getHighestPriorityOrigin( get( settings, blockPath ), path ) ??
getHighestPriorityOrigin( get( settings, topLevelPath ), path )
);
const result = get( settings, blockPath ) ?? get( settings, topLevelPath );
if ( PATHS_WITH_MERGE[ path ] ) {
return result.user ?? result.theme ?? result.core;
}
return result;
}

export function getPresetVariable( styles, context, propertyName, value ) {
Expand Down

0 comments on commit 3918f63

Please sign in to comment.