Skip to content

Commit

Permalink
Try adding merged global styles
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed May 9, 2024
1 parent b8768b8 commit b7ecbba
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"@wordpress/wordcount": "file:../wordcount",
"classnames": "^2.3.1",
"date-fns": "^2.28.0",
"deepmerge": "^4.3.0",
"is-plain-object": "^5.0.0",
"memize": "^2.1.0",
"react-autosize-textarea": "^7.1.0",
"rememo": "^4.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ function useBlockEditorSettings( settings, postType, postId ) {
[ postType, postId, isLargeViewport ]
);

// Get style data from global styles.
const { styles } = useGlobalStylesData();
const globalStylesData = useGlobalStylesData();

const settingsBlockPatterns =
settings.__experimentalAdditionalBlockPatterns ?? // WP 6.0
Expand Down Expand Up @@ -243,7 +242,7 @@ function useBlockEditorSettings( settings, postType, postId ) {
BLOCK_EDITOR_SETTINGS.includes( key )
)
),
[ globalStylesDataKey ]: styles,
[ globalStylesDataKey ]: globalStylesData,
allowedBlockTypes,
allowRightClickOverrides,
focusMode: focusMode && ! forceDisableFocusMode,
Expand Down Expand Up @@ -307,7 +306,9 @@ function useBlockEditorSettings( settings, postType, postId ) {
pageForPosts,
postType,
setIsInserterOpened,
styles,
globalStylesData,
globalStylesDataKey,
selectBlockPatternsKey,
]
);
}
Expand Down
50 changes: 42 additions & 8 deletions packages/editor/src/components/use-global-styles-data/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
/**
* 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 = {};

export function useGlobalStylesData() {
const { styles, isReady } = useSelect( ( select ) => {
function useGlobalStylesBaseData() {
const baseConfig = useSelect( ( select ) => {
return select(
coreStore
).__experimentalGetCurrentThemeBaseGlobalStyles();
}, [] );

return [ !! baseConfig, baseConfig?.styles ?? DEFAULT_STYLES ];
}

function useGlobalStylesUserData() {
return useSelect( ( select ) => {
const {
getEditedEntityRecord,
hasFinishedResolution,
Expand All @@ -32,14 +49,31 @@ export function useGlobalStylesData() {
: true;
}

return {
isReady: hasResolved,
styles: record?.styles,
};
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 {
styles: styles ?? DEFAULT_STYLES,
isReady,
isReady: isBaseStylesReady && isUserStylesReady,
base: baseStyles ?? DEFAULT_STYLES,
user: userStyles ?? DEFAULT_STYLES,
merged: mergedStyles,
};
}

0 comments on commit b7ecbba

Please sign in to comment.