Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash by moving editor style logic into a hook with useMemo #53596

Merged
merged 4 commits into from Aug 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 53 additions & 40 deletions packages/edit-post/src/components/layout/index.js
Expand Up @@ -32,7 +32,7 @@ import {
InterfaceSkeleton,
store as interfaceStore,
} from '@wordpress/interface';
import { useState, useEffect, useCallback } from '@wordpress/element';
import { useState, useEffect, useCallback, useMemo } from '@wordpress/element';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { store as noticesStore } from '@wordpress/notices';

Expand Down Expand Up @@ -71,6 +71,56 @@ const interfaceLabels = {
footer: __( 'Editor footer' ),
};

function useEditorStyles() {
const { hasThemeStyleSupport, editorSettings } = useSelect(
( select ) => ( {
hasThemeStyleSupport:
select( editPostStore ).isFeatureActive( 'themeStyles' ),
editorSettings: select( editorStore ).getEditorSettings(),
} )
);

// Compute the default styles.
const { defaultEditorStyles, presetStyles } = useMemo( () => {
const _presetStyles =
editorSettings.styles?.filter(
( style ) =>
style.__unstableType && style.__unstableType !== 'theme'
) ?? [];
return {
presetStyles: _presetStyles,
defaultEditorStyles: [
...editorSettings.defaultEditorStyles,
..._presetStyles,
],
};
}, [ editorSettings.defaultEditorStyles, editorSettings.styles ] );

// Has theme styles if the theme supports them and if some styles were not preset styles (in which case they're theme styles).
const hasThemeStyles =
hasThemeStyleSupport &&
presetStyles.length !== ( editorSettings.styles?.length ?? 0 );

// If theme styles are not present or displayed, ensure that
// base layout styles are still present in the editor.
if ( ! editorSettings.disableLayoutStyles && ! hasThemeStyles ) {
defaultEditorStyles.push( {
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
css: getLayoutStyles( {
style: {},
selector: 'body',
hasBlockGapSupport: false,
hasFallbackGapSupport: true,
fallbackGapValue: '0.5em',
} ),
} );
}

return useMemo(
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
() => ( hasThemeStyles ? editorSettings.styles : defaultEditorStyles ),
[ hasThemeStyles, editorSettings.styles, defaultEditorStyles ]
);
}

function Layout() {
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isHugeViewport = useViewportMatch( 'huge', '>=' );
Expand All @@ -95,45 +145,10 @@ function Layout() {
showBlockBreadcrumbs,
isTemplateMode,
documentLabel,
styles,
} = useSelect( ( select ) => {
const { getEditorSettings, getPostTypeLabel } = select( editorStore );
const { isFeatureActive } = select( editPostStore );
const editorSettings = getEditorSettings();
const postTypeLabel = getPostTypeLabel();
const hasThemeStyles = isFeatureActive( 'themeStyles' );

const themeStyles = [];
const presetStyles = [];
editorSettings.styles?.forEach( ( style ) => {
if ( ! style.__unstableType || style.__unstableType === 'theme' ) {
themeStyles.push( style );
} else {
presetStyles.push( style );
}
} );

const defaultEditorStyles = [
...editorSettings.defaultEditorStyles,
...presetStyles,
];

// If theme styles are not present or displayed, ensure that
// base layout styles are still present in the editor.
if (
! editorSettings.disableLayoutStyles &&
! ( hasThemeStyles && themeStyles.length )
) {
defaultEditorStyles.push( {
css: getLayoutStyles( {
style: {},
selector: 'body',
hasBlockGapSupport: false,
hasFallbackGapSupport: true,
fallbackGapValue: '0.5em',
} ),
} );
}

return {
isTemplateMode: select( editPostStore ).isEditingTemplate(),
Expand Down Expand Up @@ -166,13 +181,11 @@ function Layout() {
),
// translators: Default label for the Document in the Block Breadcrumb.
documentLabel: postTypeLabel || _x( 'Document', 'noun' ),
styles:
hasThemeStyles && themeStyles.length
? editorSettings.styles
: defaultEditorStyles,
};
}, [] );

const styles = useEditorStyles();

const openSidebarPanel = () =>
openGeneralSidebar(
hasBlockSelected ? 'edit-post/block' : 'edit-post/document'
Expand Down