Skip to content

Commit 840fae3

Browse files
authored
fix: Block customization without editor settings (#180)
* feat: Set default colors and gradients for sites without settings If a site does not have the Gutenberg plugin enabled, the editor settings endpoint is unavailable. We set a default color palette and gradients to enable customizing block styles. * fix: Include preset editor styles Ensures preset styles are loaded--e.g., utility classes for colors. * chore: Update `useEditorStyles` copy with latest from Gutenberg core Bring our copy of this private utility up to date. * refactor: Utilize `useEditorSetting` added styles parameter Embrace new abilities of this private function copy. * fix: Default colors render for separators Include default color utility classes. This is typically added by WordPress' admin via PHP. The manual addition is necessary for sites without the GutenbergKit plugin, which do not provide editor settings and theme styles. * fix: Apply default styles when editor settings provide none Even if the theme style preferences is enabled, we must apply the default styles if: 1. The theme provides no style via a block.json; 2. Or the site lacks the experimental Gutenberg REST API endpoint providing editor settings and styles. * docs: Further document nuanced editor settings fall back logic
1 parent 601a9a8 commit 840fae3

File tree

5 files changed

+443
-34
lines changed

5 files changed

+443
-34
lines changed

src/components/visual-editor/index.jsx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ import { useEditorStyles } from './use-editor-styles';
2626
import { unlock } from '../../lock-unlock';
2727
import DefaultBlockAppender from '../default-block-appender';
2828
import { useEditorVisible } from './use-editor-visible';
29+
// The Vite query parameter breaks the linter's import resolution
30+
// eslint-disable-next-line import/no-unresolved
31+
import defaultThemeStyles from './default-theme-styles.scss?inline';
32+
// eslint-disable-next-line import/no-unresolved
33+
import commonStyles from './wp-common-styles.scss?inline';
2934

3035
const {
3136
ExperimentalBlockCanvas: BlockCanvas,
@@ -54,7 +59,7 @@ function VisualEditor( { hideTitle } ) {
5459

5560
const {
5661
renderingMode,
57-
hasThemeStyleSupport,
62+
hasThemeStyles,
5863
themeHasDisabledLayoutStyles,
5964
themeSupportsLayout,
6065
hasRootPaddingAwareAlignments,
@@ -63,31 +68,40 @@ function VisualEditor( { hideTitle } ) {
6368
const _renderingMode = getRenderingMode();
6469
const { getSettings } = unlock( select( blockEditorStore ) );
6570
const _settings = getSettings();
71+
// Implies editor settings provided theme styles via the REST API.
72+
const settingsHasStyles = _settings.styles?.length > 0;
6673

6774
return {
6875
renderingMode: _renderingMode,
69-
hasThemeStyleSupport:
70-
select( editPostStore ).isFeatureActive( 'themeStyles' ),
76+
hasThemeStyles:
77+
select( editPostStore ).isFeatureActive( 'themeStyles' ) &&
78+
settingsHasStyles,
7179
themeSupportsLayout: _settings.supportsLayout,
7280
themeHasDisabledLayoutStyles: _settings.disableLayoutStyles,
7381
hasRootPaddingAwareAlignments:
7482
_settings.__experimentalFeatures?.useRootPaddingAwareAlignments,
7583
};
7684
}, [] );
7785

78-
const styles = useEditorStyles();
86+
const styles = useEditorStyles(
87+
// `commonStyles` represent manually added notable styles that are missing.
88+
// The styles likely absent due to them being injected by the WP Admin
89+
// context.
90+
commonStyles,
91+
// Add sensible default styles if theme styles are not present.
92+
hasThemeStyles ? '' : defaultThemeStyles
93+
);
7994

8095
const editorClasses = clsx( 'gutenberg-kit-visual-editor', {
81-
'has-root-padding':
82-
! hasThemeStyleSupport || ! hasRootPaddingAwareAlignments,
96+
'has-root-padding': ! hasThemeStyles || ! hasRootPaddingAwareAlignments,
8397
} );
8498

8599
const titleClasses = clsx(
86100
'gutenberg-kit-visual-editor__post-title-wrapper',
87101
'editor-visual-editor__post-title-wrapper',
88102
{
89103
'has-global-padding':
90-
hasThemeStyleSupport && hasRootPaddingAwareAlignments,
104+
hasThemeStyles && hasRootPaddingAwareAlignments,
91105
}
92106
);
93107

@@ -114,7 +128,7 @@ function VisualEditor( { hideTitle } ) {
114128
{
115129
'is-layout-flow': ! themeSupportsLayout,
116130
'has-global-padding':
117-
hasThemeStyleSupport && hasRootPaddingAwareAlignments,
131+
hasThemeStyles && hasRootPaddingAwareAlignments,
118132
}
119133
);
120134

src/components/visual-editor/use-editor-styles.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ import { useMemo } from '@wordpress/element';
1111
* Internal dependencies
1212
*/
1313
import { unlock } from '../../lock-unlock';
14-
// The Vite query parameter breaks the linter's import resolution
15-
// eslint-disable-next-line import/no-unresolved
16-
import defaultThemeStyles from './default-theme-styles.scss?inline';
17-
// eslint-disable-next-line import/no-unresolved
18-
import commonStyles from './wp-common-styles.scss?inline';
1914

2015
const { getLayoutStyles } = unlock( blockEditorPrivateApis );
2116

2217
/**
2318
* Custom hook to retrieve and memoize editor styles.
2419
*
20+
* @param {...any} additionalStyles Additional styles to add to the default styles.
21+
*
2522
* @todo This should be exported from Core so no reimplementation is needed.
23+
* @see https://github.com/WordPress/gutenberg/blob/a4d79e85a06e06b9123778e6991ac27b0bbe351d/packages/edit-post/src/components/layout/index.js#L86
2624
*
2725
* @return {any[]} An array of editor styles.
2826
*/
29-
export function useEditorStyles() {
27+
export function useEditorStyles( ...additionalStyles ) {
3028
const { hasThemeStyleSupport, editorSettings } = useSelect( ( select ) => {
3129
return {
3230
hasThemeStyleSupport:
@@ -35,12 +33,29 @@ export function useEditorStyles() {
3533
};
3634
}, [] );
3735

36+
const addedStyles = additionalStyles.join( '\n' );
37+
38+
// Compute the default styles.
3839
return useMemo( () => {
40+
const presetStyles =
41+
editorSettings.styles?.filter(
42+
( style ) =>
43+
style.__unstableType && style.__unstableType !== 'theme'
44+
) ?? [];
45+
3946
const defaultEditorStyles = [
4047
...( editorSettings?.defaultEditorStyles ?? [] ),
48+
...presetStyles,
4149
];
4250

43-
if ( ! editorSettings.disableLayoutStyles && ! hasThemeStyleSupport ) {
51+
// Has theme styles if the theme supports them and if some styles were not preset styles (in which case they're theme styles).
52+
const hasThemeStyles =
53+
hasThemeStyleSupport &&
54+
presetStyles.length !== ( editorSettings.styles?.length ?? 0 );
55+
56+
// If theme styles are not present or displayed, ensure that
57+
// base layout styles are still present in the editor.
58+
if ( ! editorSettings.disableLayoutStyles && ! hasThemeStyles ) {
4459
defaultEditorStyles.push( {
4560
css: getLayoutStyles( {
4661
style: {},
@@ -52,24 +67,20 @@ export function useEditorStyles() {
5267
} );
5368
}
5469

55-
if ( ! hasThemeStyleSupport ) {
56-
defaultEditorStyles.push( {
57-
css: defaultThemeStyles,
58-
} );
59-
}
60-
61-
const baseStyles = hasThemeStyleSupport
70+
const baseStyles = hasThemeStyles
6271
? editorSettings.styles ?? []
6372
: defaultEditorStyles;
6473

65-
// `commonStyles` represent manually added notable styles that are missing.
66-
// The styles likely absent due to them being injected by the WP Admin
67-
// context.
68-
return [ { css: commonStyles }, ...baseStyles ];
74+
if ( addedStyles ) {
75+
return [ ...baseStyles, { css: addedStyles } ];
76+
}
77+
78+
return baseStyles;
6979
}, [
7080
editorSettings.defaultEditorStyles,
7181
editorSettings.disableLayoutStyles,
7282
editorSettings.styles,
7383
hasThemeStyleSupport,
84+
addedStyles,
7485
] );
7586
}

0 commit comments

Comments
 (0)