Skip to content

Commit

Permalink
Consolidating all resolutions in the background panel - removing useG…
Browse files Browse the repository at this point in the history
…lobalStyleLinks in the process

Ensuring only uploaded block images get the defaults.
  • Loading branch information
ramonjd committed Aug 2, 2024
1 parent 0cda47f commit b0eaa1a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
useRef,
useState,
useEffect,
useMemo,
} from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { focus } from '@wordpress/dom';
Expand All @@ -42,11 +43,15 @@ import { isBlobURL } from '@wordpress/blob';
/**
* Internal dependencies
*/
import { useToolsPanelDropdownMenuProps } from './utils';
import { useToolsPanelDropdownMenuProps, getResolvedRefValue } from './utils';
import { setImmutably } from '../../utils/object';
import MediaReplaceFlow from '../media-replace-flow';
import { store as blockEditorStore } from '../../store';
import { getResolvedThemeFilePath } from './theme-file-uri-utils';
import {
globalStylesDataKey,
globalStylesLinksDataKey,
} from '../../store/private-keys';

const IMAGE_BACKGROUND_TYPE = 'image';
const DEFAULT_CONTROLS = {
Expand Down Expand Up @@ -269,7 +274,7 @@ function BackgroundImageControls( {
inheritedValue,
onRemoveImage = noop,
displayInPanel,
themeFileURIs,
defaultValues,
} ) {
const mediaUpload = useSelect(
( select ) => select( blockEditorStore ).getSettings().mediaUpload,
Expand Down Expand Up @@ -318,7 +323,8 @@ function BackgroundImageControls( {
return;
}

const sizeValue = style?.background?.backgroundSize;
const sizeValue =
style?.background?.backgroundSize || defaultValues?.backgroundSize;
const positionValue = style?.background?.backgroundPosition;

onChange(
Expand All @@ -334,6 +340,7 @@ function BackgroundImageControls( {
! positionValue && ( 'auto' === sizeValue || ! sizeValue )
? '50% 0'
: positionValue,
backgroundSize: sizeValue,
} )
);
};
Expand Down Expand Up @@ -393,10 +400,7 @@ function BackgroundImageControls( {
name={
<InspectorImagePreviewItem
className="block-editor-global-styles-background-panel__image-preview"
imgUrl={ getResolvedThemeFilePath(
url,
themeFileURIs
) }
imgUrl={ url }
filename={ title }
label={ imgLabel }
/>
Expand Down Expand Up @@ -437,7 +441,6 @@ function BackgroundSizeControls( {
style,
inheritedValue,
defaultValues,
themeFileURIs,
} ) {
const sizeValue =
style?.background?.backgroundSize ||
Expand Down Expand Up @@ -564,7 +567,7 @@ function BackgroundSizeControls( {
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Focal point' ) }
url={ getResolvedThemeFilePath( imageValue, themeFileURIs ) }
url={ imageValue }
value={ backgroundPositionToCoords( positionValue ) }
onChange={ updateBackgroundPosition }
/>
Expand Down Expand Up @@ -679,6 +682,54 @@ export default function BackgroundPanel( {
headerLabel = __( 'Background image' ),
themeFileURIs,
} ) {
const { globalStyles, _links } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const _settings = getSettings();
return {
globalStyles: _settings[ globalStylesDataKey ],
_links: _settings[ globalStylesLinksDataKey ],
};
}, [] );

themeFileURIs = themeFileURIs || _links?.[ 'wp:theme-file' ];

/*
* Resolve any inherited "ref" pointers.
* Should the block editor need inherited values
* across all controls, this could be abstracted.
*/
const resolvedInheritedValue = useMemo( () => {
const resolvedValues = {
background: {},
};

if ( ! inheritedValue?.background ) {
return inheritedValue;
}

Object.entries( inheritedValue?.background ).forEach(
( [ key, backgroundValue ] ) => {
resolvedValues.background[ key ] = getResolvedRefValue(
backgroundValue,
{
styles: globalStyles,
}
);
if (
'backgroundImage' === key &&
resolvedValues.background[ key ]?.url
) {
resolvedValues.background[ key ].url =
getResolvedThemeFilePath(
resolvedValues.background[ key ].url,
themeFileURIs
);
}
}
);
return resolvedValues;
}, [ globalStyles, inheritedValue ] );

const resetAllFilter = useCallback( ( previousValue ) => {
return {
...previousValue,
Expand All @@ -690,11 +741,11 @@ export default function BackgroundPanel( {
onChange( setImmutably( value, [ 'background' ], {} ) );

const { title, url } = value?.background?.backgroundImage || {
...inheritedValue?.background?.backgroundImage,
...resolvedInheritedValue?.background?.backgroundImage,
};
const hasImageValue =
hasBackgroundImageValue( value ) ||
hasBackgroundImageValue( inheritedValue );
hasBackgroundImageValue( resolvedInheritedValue );

const shouldShowBackgroundImageControls =
hasImageValue &&
Expand Down Expand Up @@ -724,17 +775,17 @@ export default function BackgroundPanel( {
<BackgroundControlsPanel
label={ title }
filename={ title }
url={ getResolvedThemeFilePath( url, themeFileURIs ) }
url={ url }
onToggle={ setIsDropDownOpen }
hasImageValue={ hasImageValue }
>
<VStack spacing={ 3 } className="single-column">
<BackgroundImageControls
onChange={ onChange }
style={ value }
inheritedValue={ inheritedValue }
themeFileURIs={ themeFileURIs }
inheritedValue={ resolvedInheritedValue }
displayInPanel
defaultValues={ defaultValues }
onRemoveImage={ () => {
setIsDropDownOpen( false );
resetBackground();
Expand All @@ -745,17 +796,15 @@ export default function BackgroundPanel( {
panelId={ panelId }
style={ value }
defaultValues={ defaultValues }
inheritedValue={ inheritedValue }
themeFileURIs={ themeFileURIs }
inheritedValue={ resolvedInheritedValue }
/>
</VStack>
</BackgroundControlsPanel>
) : (
<BackgroundImageControls
onChange={ onChange }
style={ value }
inheritedValue={ inheritedValue }
themeFileURIs={ themeFileURIs }
inheritedValue={ resolvedInheritedValue }
/>
) }
</div>
Expand Down
5 changes: 0 additions & 5 deletions packages/block-editor/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,6 @@ export function useGlobalStyle(
return [ result, setStyle ];
}

export function useGlobalStyleLinks() {
const { merged: mergedConfig } = useContext( GlobalStylesContext );
return mergedConfig?._links;
}

/**
* React hook that overrides a global settings object with block and element specific settings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export {
useGlobalSetting,
useGlobalStyle,
useSettingsForBlockElement,
useGlobalStyleLinks,
} from './hooks';
export { getBlockCSSSelector } from './get-block-css-selector';
export {
Expand Down
9 changes: 2 additions & 7 deletions packages/block-editor/src/hooks/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import {
useHasBackgroundPanel,
hasBackgroundImageValue,
} from '../components/global-styles/background-panel';
import {
globalStylesDataKey,
globalStylesLinksDataKey,
} from '../store/private-keys';
import { globalStylesDataKey } from '../store/private-keys';

export const BACKGROUND_SUPPORT_KEY = 'background';

Expand Down Expand Up @@ -138,14 +135,13 @@ export function BackgroundImagePanel( {
setAttributes,
settings,
} ) {
const { style, inheritedValue, _links } = useSelect(
const { style, inheritedValue } = useSelect(
( select ) => {
const { getBlockAttributes, getSettings } =
select( blockEditorStore );
const _settings = getSettings();
return {
style: getBlockAttributes( clientId )?.style,
_links: _settings[ globalStylesLinksDataKey ],
/*
* @TODO 1. Pass inherited value down to all block style controls,
* See: packages/block-editor/src/hooks/style.js
Expand Down Expand Up @@ -191,7 +187,6 @@ export function BackgroundImagePanel( {
settings={ updatedSettings }
onChange={ onChange }
value={ style }
themeFileURIs={ _links?.[ 'wp:theme-file' ] }
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const BACKGROUND_DEFAULT_VALUES = {
const {
useGlobalStyle,
useGlobalSetting,
useGlobalStyleLinks,
BackgroundPanel: StylesBackgroundPanel,
} = unlock( blockEditorPrivateApis );

Expand All @@ -42,7 +41,6 @@ export default function BackgroundPanel() {
const [ inheritedStyle, setStyle ] = useGlobalStyle( '', undefined, 'all', {
shouldDecodeEncode: false,
} );
const _links = useGlobalStyleLinks();
const [ settings ] = useGlobalSetting( '' );

return (
Expand All @@ -52,7 +50,6 @@ export default function BackgroundPanel() {
onChange={ setStyle }
settings={ settings }
defaultValues={ BACKGROUND_DEFAULT_VALUES }
themeFileURIs={ _links?.[ 'wp:theme-file' ] }
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ const {
FiltersPanel: StylesFiltersPanel,
ImageSettingsPanel,
AdvancedPanel: StylesAdvancedPanel,
useGlobalStyleLinks,
} = unlock( blockEditorPrivateApis );

function ScreenBlock( { name, variation } ) {
Expand All @@ -104,7 +103,6 @@ function ScreenBlock( { name, variation } ) {
const [ rawSettings, setSettings ] = useGlobalSetting( '', name );
const settings = useSettingsForBlockElement( rawSettings, name );
const blockType = getBlockType( name );
const _links = useGlobalStyleLinks();

// Only allow `blockGap` support if serialization has not been skipped, to be sure global spacing can be applied.
if (
Expand Down Expand Up @@ -271,7 +269,6 @@ function ScreenBlock( { name, variation } ) {
onChange={ setStyle }
settings={ settings }
defaultValues={ BACKGROUND_BLOCK_DEFAULT_VALUES }
themeFileURIs={ _links?.[ 'wp:theme-file' ] }
/>
) }
{ hasTypographyPanel && (
Expand Down

0 comments on commit b0eaa1a

Please sign in to comment.