Skip to content

Commit

Permalink
Update approach to adding global styles data to editor settings
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed May 9, 2024
1 parent a3ada64 commit a81e965
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 53 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/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useFlashEditableBlocks } from './components/use-flash-editable-blocks';
import {
selectBlockPatternsKey,
reusableBlocksSelectKey,
globalStylesDataKey,
} from './store/private-keys';
import { requiresWrapperOnCopy } from './components/writing-flow/utils';
import { PrivateRichText } from './components/rich-text/';
Expand Down Expand Up @@ -72,6 +73,7 @@ lock( privateApis, {
useReusableBlocksRenameHint,
usesContextKey,
useFlashEditableBlocks,
globalStylesDataKey,
selectBlockPatternsKey,
requiresWrapperOnCopy,
PrivateRichText,
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/store/private-keys.js
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' );
4 changes: 4 additions & 0 deletions packages/editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,10 @@ _Returns_

Undocumented declaration.

### useGlobalStylesData

Undocumented declaration.

### usePostScheduleLabel

Undocumented declaration.
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
"@wordpress/wordcount": "file:../wordcount",
"clsx": "^2.1.1",
"date-fns": "^3.6.0",
"deepmerge": "^4.3.0",
"is-plain-object": "^5.0.0",
"memize": "^2.1.0",
"react-autosize-textarea": "^7.1.0",
"remove-accents": "^0.5.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export { default as UnsavedChangesWarning } from './unsaved-changes-warning';
export { default as WordCount } from './word-count';
export { default as TimeToRead } from './time-to-read';
export { default as CharacterCount } from './character-count';
export { useStyles } from './use-styles';
export { useGlobalStylesData } from './use-global-styles-data';

// State Related Components.
export { default as EditorProvider } from './provider';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import inserterMediaCategories from '../media-categories';
import { mediaUpload } from '../../utils';
import { store as editorStore } from '../../store';
import { lock, unlock } from '../../lock-unlock';
import { useStyles } from '../use-styles';
import { useGlobalStylesData } from '../use-global-styles-data';

const EMPTY_BLOCKS_LIST = [];

Expand Down Expand Up @@ -174,8 +174,7 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
[ postType, postId, isLargeViewport, renderingMode ]
);

// get the styles from the global styles
const { styles } = useStyles();
const globalStylesData = useGlobalStylesData();

const settingsBlockPatterns =
settings.__experimentalAdditionalBlockPatterns ?? // WP 6.0
Expand Down Expand Up @@ -255,6 +254,8 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
}, [ settings.allowedBlockTypes, hiddenBlockTypes, blockTypes ] );

const forceDisableFocusMode = settings.focusMode === false;
const { globalStylesDataKey, selectBlockPatternsKey } =
unlock( privateApis );

return useMemo( () => {
const blockEditorSettings = {
Expand All @@ -263,7 +264,7 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
BLOCK_EDITOR_SETTINGS.includes( key )
)
),
__experimentalStyles: styles,
[ globalStylesDataKey ]: globalStylesData,
allowedBlockTypes,
allowRightClickOverrides,
focusMode: focusMode && ! forceDisableFocusMode,
Expand All @@ -272,7 +273,7 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
keepCaretInsideBlock,
mediaUpload: hasUploadPermissions ? mediaUpload : undefined,
__experimentalBlockPatterns: blockPatterns,
[ unlock( privateApis ).selectBlockPatternsKey ]: ( select ) =>
[ selectBlockPatternsKey ]: ( select ) =>
unlock( select( coreStore ) ).getBlockPatternsForPostType(
postType
),
Expand Down Expand Up @@ -332,7 +333,9 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
postType,
setIsInserterOpened,
sectionRootClientId,
styles,
globalStylesData,
globalStylesDataKey,
selectBlockPatternsKey,
] );
}

Expand Down
79 changes: 79 additions & 0 deletions packages/editor/src/components/use-global-styles-data/index.js
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,
};
}
46 changes: 0 additions & 46 deletions packages/editor/src/components/use-styles/index.js

This file was deleted.

0 comments on commit a81e965

Please sign in to comment.