Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Remove some utility functions #61784

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { __ } from '@wordpress/i18n';
import BorderRadiusControl from '../border-radius-control';
import { useColorsPerOrigin } from './hooks';
import { getValueFromVariable, TOOLSPANEL_DROPDOWNMENU_PROPS } from './utils';
import { overrideOrigins } from '../../store/get-block-settings';
import { setImmutably } from '../../utils/object';
import { useBorderPanelLabel } from '../../hooks/border';
import { ShadowPopover, useShadowPresets } from './shadow-panel-components';
Expand Down Expand Up @@ -161,9 +160,13 @@ export default function BorderPanel( {
// Shadow
const shadow = decodeValue( inheritedValue?.shadow );
const shadowPresets = settings?.shadow?.presets ?? {};
const overriddenShadowPresets = overrideOrigins( shadowPresets ) ?? [];
const mergedShadowPresets =
shadowPresets.custom ??
shadowPresets.theme ??
shadowPresets.default ??
[];
const setShadow = ( newValue ) => {
const slug = overriddenShadowPresets?.find(
const slug = mergedShadowPresets?.find(
( { shadow: shadowName } ) => shadowName === newValue
)?.slug;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import {
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useCallback } from '@wordpress/element';
import { useCallback, useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import { mergeOrigins, hasOriginValue } from '../../store/get-block-settings';
import FontFamilyControl from '../font-family';
import FontAppearanceControl from '../font-appearance-control';
import LineHeightControl from '../line-height-control';
Expand Down Expand Up @@ -62,7 +61,9 @@ function useHasFontSizeControl( settings ) {
}

function useHasFontFamilyControl( settings ) {
return hasOriginValue( settings?.typography?.fontFamilies );
return [ 'default', 'theme', 'custom' ].some(
( key ) => settings?.typography?.fontFamilies?.[ key ]?.length
);
}

function useHasLineHeightControl( settings ) {
Expand Down Expand Up @@ -170,8 +171,12 @@ export default function TypographyPanel( {

// Font Family
const hasFontFamilyEnabled = useHasFontFamilyControl( settings );
const fontFamilies = settings?.typography?.fontFamilies ?? {};
const mergedFontFamilies = fontFamilies ? mergeOrigins( fontFamilies ) : [];
const fontFamilies = settings?.typography?.fontFamilies;
const mergedFontFamilies = useMemo( () => {
return [ 'default', 'theme', 'custom' ].flatMap(
( key ) => fontFamilies?.[ key ] ?? []
);
}, [ fontFamilies ] );
const fontFamily = decodeValue( inheritedValue?.typography?.fontFamily );
const setFontFamily = ( newValue ) => {
const slug = mergedFontFamilies?.find(
Expand Down
47 changes: 1 addition & 46 deletions packages/block-editor/src/store/get-block-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,51 +91,6 @@ const removeCustomPrefixes = ( path ) => {
return prefixedFlags[ path ] || path;
};

/**
* For settings like `color.palette`, which have a value that is an object
* with `default`, `theme`, `custom`, with field values that are arrays of
* items, merge these three arrays into one and return it. The calculation
* is memoized so that identical input values produce identical output.
* @param {Object} value Object to merge
* @return {Array} Array of merged items
*/
export function mergeOrigins( value ) {
let result = mergeCache.get( value );
if ( ! result ) {
result = [ 'default', 'theme', 'custom' ].flatMap(
( key ) => value[ key ] ?? []
);
mergeCache.set( value, result );
}
return result;
}
const mergeCache = new WeakMap();

/**
* For settings like `color.palette`, which have a value that is an object
* with `default`, `theme`, `custom`, with field values that are arrays of
* items, returns the one with the highest priority among these three arrays.
* @param {Object} value Object to extract from
* @return {Array} Array of items extracted from the three origins
*/
export function overrideOrigins( value ) {
return value.custom ?? value.theme ?? value.default;
}

/**
* For settings like `color.palette`, which have a value that is an object
* with `default`, `theme`, `custom`, with field values that are arrays of
* items, see if any of the three origins have values.
*
* @param {Object} value Object to check
* @return {boolean} Whether the object has values in any of the three origins
*/
export function hasOriginValue( value ) {
return [ 'default', 'theme', 'custom' ].some(
( key ) => value?.[ key ]?.length
);
}

export function getBlockSettings( state, clientId, ...paths ) {
const blockName = getBlockName( state, clientId );
const candidates = [];
Expand Down Expand Up @@ -215,7 +170,7 @@ export function getBlockSettings( state, clientId, ...paths ) {
// Return if the setting was found in either the block instance or the store.
if ( result !== undefined ) {
if ( PATHS_WITH_OVERRIDE[ normalizedPath ] ) {
return overrideOrigins( result );
return result.custom ?? result.theme ?? result.default;
}
return result;
}
Expand Down
Loading