-
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.
Update approach to adding global styles data to editor settings
- Loading branch information
1 parent
a3ada64
commit a81e965
Showing
9 changed files
with
102 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const globalStylesDataKey = Symbol( 'globalStylesDataKey' ); | ||
export const selectBlockPatternsKey = Symbol( 'selectBlockPatternsKey' ); | ||
export const reusableBlocksSelectKey = Symbol( 'reusableBlocksSelect' ); |
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
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
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
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.