-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Address feedback on addition of global styles to editor setti…
…ngs (#61022)
- Loading branch information
1 parent
a1b5c44
commit 508126f
Showing
3 changed files
with
79 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/editor/src/components/use-global-styles-data/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import deepmerge from 'deepmerge'; | ||
import { isPlainObject } from 'is-plain-object'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
const DEFAULT_STYLES = {}; | ||
|
||
function useGlobalStylesBaseData() { | ||
const baseConfig = useSelect( ( select ) => { | ||
return select( | ||
coreStore | ||
).__experimentalGetCurrentThemeBaseGlobalStyles(); | ||
}, [] ); | ||
|
||
return [ !! baseConfig, baseConfig?.styles ?? DEFAULT_STYLES ]; | ||
} | ||
|
||
function useGlobalStylesUserData() { | ||
return useSelect( ( select ) => { | ||
const { | ||
getEditedEntityRecord, | ||
hasFinishedResolution, | ||
__experimentalGetCurrentGlobalStylesId, | ||
} = select( coreStore ); | ||
|
||
const globalStylesId = __experimentalGetCurrentGlobalStylesId(); | ||
const record = globalStylesId | ||
? getEditedEntityRecord( 'root', 'globalStyles', globalStylesId ) | ||
: undefined; | ||
|
||
let hasResolved = false; | ||
if ( | ||
hasFinishedResolution( '__experimentalGetCurrentGlobalStylesId' ) | ||
) { | ||
hasResolved = globalStylesId | ||
? hasFinishedResolution( 'getEditedEntityRecord', [ | ||
'root', | ||
'globalStyles', | ||
globalStylesId, | ||
] ) | ||
: true; | ||
} | ||
|
||
return [ hasResolved, record?.styles ?? DEFAULT_STYLES ]; | ||
}, [] ); | ||
} | ||
|
||
function mergeBaseAndUserStyles( base, user ) { | ||
return deepmerge( base, user, { | ||
// We only pass as arrays the presets, | ||
// in which case we want the new array of values | ||
// to override the old array (no merging). | ||
isMergeableObject: isPlainObject, | ||
} ); | ||
} | ||
|
||
export function useGlobalStylesData() { | ||
const [ isBaseStylesReady, baseStyles ] = useGlobalStylesBaseData(); | ||
const [ isUserStylesReady, userStyles ] = useGlobalStylesUserData(); | ||
const mergedStyles = useMemo( | ||
() => mergeBaseAndUserStyles( baseStyles, userStyles ), | ||
[ baseStyles, userStyles ] | ||
); | ||
|
||
return { | ||
isReady: isBaseStylesReady && isUserStylesReady, | ||
base: baseStyles ?? DEFAULT_STYLES, | ||
user: userStyles ?? DEFAULT_STYLES, | ||
merged: mergedStyles, | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.