Skip to content
Next Next commit
Add global styles to editor settings
  • Loading branch information
michalczaplinski authored and aaronrobertshaw committed May 10, 2024
commit 3c2185da8f667f3973627474032d1a65db669216
12 changes: 11 additions & 1 deletion packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PostLockedModal,
store as editorStore,
privateApis as editorPrivateApis,
useStyles,
} from '@wordpress/editor';
import { useMemo } from '@wordpress/element';
import { SlotFillProvider } from '@wordpress/components';
Expand Down Expand Up @@ -73,14 +74,23 @@ function Editor( {
[ currentPost.postType, currentPost.postId ]
);

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

const editorSettings = useMemo(
() => ( {
...settings,
__globalStyles: styles,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
defaultRenderingMode: 'post-only',
} ),
[ settings, onNavigateToEntityRecord, onNavigateToPreviousEntityRecord ]
[
settings,
styles,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
]
);

const initialPost = useMemo( () => {
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +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';

// State Related Components.
export { default as EditorProvider } from './provider';
Expand Down
46 changes: 46 additions & 0 deletions packages/editor/src/components/use-styles/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

export function useStyles() {
const { styles, isReady } = useSelect( ( select ) => {
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
const _globalStylesId =
select( coreStore ).__experimentalGetCurrentGlobalStylesId();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we combine those 2 select( coreStore ) into a single one?

const record = _globalStylesId
? getEditedEntityRecord( 'root', 'globalStyles', _globalStylesId )
: undefined;

let hasResolved = false;
if (
hasFinishedResolution( '__experimentalGetCurrentGlobalStylesId' )
) {
hasResolved = _globalStylesId
? hasFinishedResolution( 'getEditedEntityRecord', [
'root',
'globalStyles',
_globalStylesId,
] )
: true;
}

return {
isReady: hasResolved,
styles: record?.styles,
};
}, [] );

// Make sure to recompute the styles when hasResolved changes.
const config = useMemo( () => {
return {
styles: styles ?? 'no styless',
isReady,
};
}, [ isReady, styles ] );

return config;
}