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

Global styles: send theme object to setUserConfig #61805

Merged
merged 3 commits into from
Jun 13, 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 @@ -89,10 +89,7 @@ export const useGlobalStylesReset = () => {
const canReset = !! config && ! fastDeepEqual( config, EMPTY_CONFIG );
return [
canReset,
useCallback(
() => setUserConfig( () => EMPTY_CONFIG ),
[ setUserConfig ]
),
useCallback( () => setUserConfig( EMPTY_CONFIG ), [ setUserConfig ] ),
];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,11 @@ function ScreenRevisions() {
};

const restoreRevision = ( revision ) => {
setUserConfig( () => ( {
styles: revision?.styles,
settings: revision?.settings,
_links: revision?._links,
} ) );
setUserConfig( () => revision );
setIsLoadingRevisionWithUnsavedChanges( false );
onCloseRevisions();
};

const selectRevision = ( revision ) => {
setCurrentlySelectedRevision( {
/*
* The default must be an empty object so that
* `mergeBaseAndUserConfigs()` can merge them correctly.
*/
styles: revision?.styles || {},
settings: revision?.settings || {},
_links: revision?._links || {},
id: revision?.id,
} );
};

useEffect( () => {
if (
! editorCanvasContainerView ||
Expand Down Expand Up @@ -134,11 +117,7 @@ function ScreenRevisions() {
* See: https://github.com/WordPress/gutenberg/issues/55866
*/
if ( shouldSelectFirstItem ) {
setCurrentlySelectedRevision( {
styles: firstRevision?.styles || {},
settings: firstRevision?.settings || {},
id: firstRevision?.id,
} );
setCurrentlySelectedRevision( firstRevision );
}
}, [ shouldSelectFirstItem, firstRevision ] );

Expand Down Expand Up @@ -186,7 +165,7 @@ function ScreenRevisions() {
/>
) ) }
<RevisionsButtons
onChange={ selectRevision }
onChange={ setCurrentlySelectedRevision }
selectedRevisionId={ currentlySelectedRevisionId }
userRevisions={ currentRevisions }
canApplyRevision={ isLoadButtonEnabled }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,15 @@ export default function Variation( { variation, children, isPill } ) {
const { base, user, setUserConfig } = useContext( GlobalStylesContext );
const context = useMemo(
() => ( {
user: {
settings: variation.settings ?? {},
styles: variation.styles ?? {},
_links: variation._links ?? {},
},
user: variation,
base,
merged: mergeBaseAndUserConfigs( base, variation ),
setUserConfig: () => {},
} ),
[ variation, base ]
);

const selectVariation = () => {
setUserConfig( () => ( {
settings: variation.settings,
styles: variation.styles,
_links: variation._links,
} ) );
};
const selectVariation = () => setUserConfig( variation );

const selectOnEnter = ( event ) => {
if ( event.keyCode === ENTER ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function PushChangesToGlobalStylesControl( {
// notification.
__unstableMarkNextChangeAsNotPersistent();
setAttributes( newBlockAttributes );
setUserConfig( () => newUserConfig, { undoIgnore: true } );
setUserConfig( newUserConfig, { undoIgnore: true } );
createSuccessNotice(
sprintf(
// translators: %s: Title of the block e.g. 'Heading'.
Expand All @@ -302,7 +302,7 @@ function PushChangesToGlobalStylesControl( {
onClick() {
__unstableMarkNextChangeAsNotPersistent();
setAttributes( attributes );
setUserConfig( () => userConfig, {
setUserConfig( userConfig, {
undoIgnore: true,
} );
},
Expand Down
14 changes: 12 additions & 2 deletions packages/editor/src/components/global-styles-provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ function useGlobalStylesUserConfig() {
}, [ settings, styles, _links ] );

const setConfig = useCallback(
( callback, options = {} ) => {
/**
* Set the global styles config.
* @param {Function|Object} callbackOrObject If the callbackOrObject is a function, pass the current config to the callback so the consumer can merge values.
* Otherwise, overwrite the current config with the incoming object.
* @param {Object} options Options for editEntityRecord Core selector.
*/
( callbackOrObject, options = {} ) => {
const record = getEditedEntityRecord(
'root',
'globalStyles',
Expand All @@ -175,7 +181,11 @@ function useGlobalStylesUserConfig() {
settings: record?.settings ?? {},
_links: record?._links ?? {},
};
const updatedConfig = callback( currentConfig );

const updatedConfig =
typeof callbackOrObject === 'function'
? callbackOrObject( currentConfig )
: callbackOrObject;

editEntityRecord(
'root',
Expand Down
Loading