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

Add stylebook screen for classic themes #66851

Merged
merged 37 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4bca433
Boilerplate for classic stylebook
tellthemachines Nov 8, 2024
75b8d04
Add provider and settings
tellthemachines Nov 8, 2024
450891a
Fix link to dashboard
tellthemachines Nov 8, 2024
34a8ce9
Add customizer styles to the editor.
tellthemachines Nov 11, 2024
92dd468
Integrate overview section.
tellthemachines Nov 11, 2024
7085e03
Make items non-clickable
tellthemachines Nov 12, 2024
ead2507
Display classic theme color and gradient palettes
tellthemachines Nov 12, 2024
7a2ca05
Remove aria label if area isn't clickable
tellthemachines Nov 12, 2024
13f7efa
Update to use site editor layout with new route
tellthemachines Nov 19, 2024
4578bf9
Make style book default mobile view
tellthemachines Nov 20, 2024
f333acc
Rename things to be more future proof
tellthemachines Nov 20, 2024
9253755
update name
tellthemachines Nov 20, 2024
1d1fdea
Canvas shouldn't go into edit mode when resizing
tellthemachines Nov 20, 2024
21af508
Add core PR
tellthemachines Nov 22, 2024
9439ddc
lint PHP
tellthemachines Nov 22, 2024
63c2a95
Use site editor URL
tellthemachines Nov 25, 2024
2c93c4d
remove whitespace
tellthemachines Nov 25, 2024
f811ec3
Check existence of $submenu global
tellthemachines Nov 26, 2024
7727bbd
Update to single entry point for Stylebook and Patterns
tellthemachines Nov 28, 2024
a59c4b3
Remove wp_die handler as it's been added elsewhere
tellthemachines Dec 2, 2024
46341c5
Switch back mobile view
tellthemachines Dec 2, 2024
6f5bcac
Try showing the site preview instead of stylebook as Design entry point
tellthemachines Dec 2, 2024
cd0d065
Update Styles section sidebar to go back to Design
tellthemachines Dec 5, 2024
157108c
Remove top margin in preview
tellthemachines Dec 5, 2024
f230ba1
Update strings
tellthemachines Dec 6, 2024
9bb739b
only allow users with appropriate permissions
tellthemachines Dec 6, 2024
1e591b4
Move styles submenu item into compat folder
tellthemachines Dec 6, 2024
35a38c8
remove require
tellthemachines Dec 6, 2024
70bd056
Make preview very non-interactive
tellthemachines Dec 6, 2024
a122f2d
Make duotones work if declared.
tellthemachines Dec 6, 2024
fa9f97e
Only show Design submenu item if theme supports editor styles
tellthemachines Dec 6, 2024
0a50d01
Support theme.json in the absence of editor styles.
tellthemachines Dec 9, 2024
78d25eb
useMultiOriginPalettes instead of complicated logic
tellthemachines Dec 9, 2024
bc91ba9
Try fixing routes
tellthemachines Dec 9, 2024
344964d
Make sure editor canvas button exists
tellthemachines Dec 10, 2024
2dbc13f
Default value for prop
tellthemachines Dec 10, 2024
859de22
Add a test for classic style book
tellthemachines Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backport-changelog/6.8/7865.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/7865

* https://github.com/WordPress/gutenberg/pull/66851
13 changes: 13 additions & 0 deletions lib/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ function gutenberg_get_block_editor_settings( $settings ) {
$global_styles[] = $block_classes;
}

// Get any additional css from the customizer and add it before global styles custom CSS.
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
$global_styles[] = array(
'css' => wp_get_custom_css(),
'__unstableType' => 'user',
'isGlobalStyles' => false,
);

/*
* Add the custom CSS as a separate stylesheet so any invalid CSS
* entered by users does not break other global styles.
Expand All @@ -74,6 +81,12 @@ function gutenberg_get_block_editor_settings( $settings ) {
$block_classes['css'] = $actual_css;
$global_styles[] = $block_classes;
}
// Get any additional css from the customizer.
$global_styles[] = array(
'css' => wp_get_custom_css(),
'__unstableType' => 'user',
'isGlobalStyles' => false,
);
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
}

$settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() );
Expand Down
26 changes: 25 additions & 1 deletion lib/compat/wordpress-6.8/site-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,33 @@ function gutenberg_redirect_site_editor_deprecated_urls() {
* @return callable The default handler or a custom handler.
*/
function gutenberg_styles_wp_die_handler( $default_handler ) {
if ( ! wp_is_block_theme() && str_contains( $_SERVER['REQUEST_URI'], 'site-editor.php' ) && isset( $_GET['p'] ) ) {
if ( ! wp_is_block_theme() && str_contains( $_SERVER['REQUEST_URI'], 'site-editor.php' ) && current_user_can( 'edit_theme_options' ) ) {
return '__return_false';
}
return $default_handler;
}
add_filter( 'wp_die_handler', 'gutenberg_styles_wp_die_handler' );

/**
* Add a Styles submenu under the Appearance menu
* for Classic themes.
*
* @global array $submenu
*/
function gutenberg_add_styles_submenu_item() {
if ( ! wp_is_block_theme() && ( current_theme_supports( 'editor-styles' ) || wp_theme_has_theme_json() ) ) {
global $submenu;

$styles_menu_item = array(
__( 'Design', 'gutenberg' ),
'edit_theme_options',
'site-editor.php',
);
// If $submenu exists, insert the Styles submenu item at position 2.
if ( $submenu && isset( $submenu['themes.php'] ) ) {
// This might not work as expected if the submenu has already been modified.
array_splice( $submenu['themes.php'], 1, 1, array( $styles_menu_item ) );
}
}
}
add_action( 'admin_init', 'gutenberg_add_styles_submenu_item' );
58 changes: 58 additions & 0 deletions packages/edit-site/src/components/maybe-editor/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* WordPress dependencies
*/

import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/

import Editor from '../editor';

export function MaybeEditor( { showEditor = true } ) {
const { isBlockBasedTheme, siteUrl } = useSelect( ( select ) => {
const { getEntityRecord, getCurrentTheme } = select( coreStore );
const siteData = getEntityRecord( 'root', '__unstableBase' );

return {
isBlockBasedTheme: getCurrentTheme()?.is_block_theme,
siteUrl: siteData?.home,
};
}, [] );

// If theme is block based, return the Editor, otherwise return the site preview.
return isBlockBasedTheme || showEditor ? (
<Editor />
) : (
<iframe
src={ siteUrl }
title={ __( 'Site Preview' ) }
style={ {
display: 'block',
width: '100%',
height: '100%',
backgroundColor: '#fff',
} }
onLoad={ ( event ) => {
// Hide the admin bar in the front-end preview.
const document = event.target.contentDocument;
document.getElementById( 'wpadminbar' ).remove();
document
.getElementsByTagName( 'html' )[ 0 ]
.setAttribute( 'style', 'margin-top: 0 !important;' );
// Make interactive elements unclickable.
const interactiveElements = document.querySelectorAll(
'a, button, input, details, audio'
);
interactiveElements.forEach( ( element ) => {
element.style.pointerEvents = 'none';
element.tabIndex = -1;
element.setAttribute( 'aria-hidden', 'true' );
} );
} }
/>
);
}
11 changes: 10 additions & 1 deletion packages/edit-site/src/components/resizable-frame/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
import { useInstanceId, useReducedMotion } from '@wordpress/compose';
import { __, isRTL } from '@wordpress/i18n';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
Expand Down Expand Up @@ -106,6 +108,10 @@ function ResizableFrame( {
'edit-site-resizable-frame-handle-help'
);
const defaultAspectRatio = defaultSize.width / defaultSize.height;
const isBlockTheme = useSelect( ( select ) => {
const { getCurrentTheme } = select( coreStore );
return getCurrentTheme()?.is_block_theme;
}, [] );

const handleResizeStart = ( _event, _direction, ref ) => {
// Remember the starting width so we don't have to get `ref.offsetWidth` on
Expand Down Expand Up @@ -153,7 +159,10 @@ function ResizableFrame( {
const remainingWidth =
ref.ownerDocument.documentElement.offsetWidth - ref.offsetWidth;

if ( remainingWidth > SNAP_TO_EDIT_CANVAS_MODE_THRESHOLD ) {
if (
remainingWidth > SNAP_TO_EDIT_CANVAS_MODE_THRESHOLD ||
! isBlockTheme
) {
// Reset the initial aspect ratio if the frame is resized slightly
// above the sidebar but not far enough to trigger full screen.
setFrameSize( INITIAL_FRAME_SIZE );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import { __experimentalItemGroup as ItemGroup } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { layout, symbol, navigation, styles, page } from '@wordpress/icons';
import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
Expand All @@ -16,40 +17,54 @@ import { SidebarNavigationItemGlobalStyles } from '../sidebar-navigation-screen-
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';

export function MainSidebarNavigationContent() {
export function MainSidebarNavigationContent( { isBlockBasedTheme = true } ) {
return (
<ItemGroup className="edit-site-sidebar-navigation-screen-main">
<SidebarNavigationItem
uid="navigation-navigation-item"
to="/navigation"
withChevron
icon={ navigation }
>
{ __( 'Navigation' ) }
</SidebarNavigationItem>
<SidebarNavigationItemGlobalStyles
to="/styles"
uid="global-styles-navigation-item"
icon={ styles }
>
{ __( 'Styles' ) }
</SidebarNavigationItemGlobalStyles>
<SidebarNavigationItem
uid="page-navigation-item"
to="/page"
withChevron
icon={ page }
>
{ __( 'Pages' ) }
</SidebarNavigationItem>
<SidebarNavigationItem
uid="template-navigation-item"
to="/template"
withChevron
icon={ layout }
>
{ __( 'Templates' ) }
</SidebarNavigationItem>
<ItemGroup>
{ isBlockBasedTheme && (
<>
<SidebarNavigationItem
uid="navigation-navigation-item"
to="/navigation"
withChevron
icon={ navigation }
>
{ __( 'Navigation' ) }
</SidebarNavigationItem>
<SidebarNavigationItemGlobalStyles
to="/styles"
uid="global-styles-navigation-item"
icon={ styles }
>
{ __( 'Styles' ) }
</SidebarNavigationItemGlobalStyles>
<SidebarNavigationItem
uid="page-navigation-item"
to="/page"
withChevron
icon={ page }
>
{ __( 'Pages' ) }
</SidebarNavigationItem>
<SidebarNavigationItem
uid="template-navigation-item"
to="/template"
withChevron
icon={ layout }
>
{ __( 'Templates' ) }
</SidebarNavigationItem>
</>
) }
{ ! isBlockBasedTheme && (
<SidebarNavigationItem
uid="stylebook-navigation-item"
to="/stylebook"
withChevron
icon={ styles }
>
{ __( 'Styles' ) }
</SidebarNavigationItem>
) }
<SidebarNavigationItem
uid="patterns-navigation-item"
to="/pattern"
Expand All @@ -63,6 +78,10 @@ export function MainSidebarNavigationContent() {
}

export default function SidebarNavigationScreenMain() {
const isBlockBasedTheme = useSelect(
( select ) => select( coreStore ).getCurrentTheme()?.is_block_theme,
[]
);
const { setEditorCanvasContainerView } = unlock(
useDispatch( editSiteStore )
);
Expand All @@ -76,10 +95,20 @@ export default function SidebarNavigationScreenMain() {
<SidebarNavigationScreen
isRoot
title={ __( 'Design' ) }
description={ __(
'Customize the appearance of your website using the block editor.'
) }
content={ <MainSidebarNavigationContent /> }
description={
isBlockBasedTheme
? __(
'Customize the appearance of your website using the block editor.'
)
: __(
'Explore block styles and patterns to refine your site'
)
}
content={
<MainSidebarNavigationContent
isBlockBasedTheme={ isBlockBasedTheme }
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
/>
}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
} from '@wordpress/components';
import { getTemplatePartIcon } from '@wordpress/editor';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { file } from '@wordpress/icons';
import { privateApis as routerPrivateApis } from '@wordpress/router';

Expand Down Expand Up @@ -115,14 +113,9 @@ export default function SidebarNavigationScreenPatterns( { backPath } ) {
const { templatePartAreas, hasTemplateParts, isLoading } =
useTemplatePartAreas();
const { patternCategories, hasPatterns } = usePatternCategories();
const isBlockBasedTheme = useSelect(
( select ) => select( coreStore ).getCurrentTheme()?.is_block_theme,
[]
);

return (
<SidebarNavigationScreen
isRoot={ ! isBlockBasedTheme }
title={ __( 'Patterns' ) }
description={ __(
'Manage what patterns are available when editing the site.'
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/site-editor-routes/home.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* Internal dependencies
*/
import Editor from '../editor';
import SidebarNavigationScreenMain from '../sidebar-navigation-screen-main';
import { MaybeEditor } from '../maybe-editor';

export const homeRoute = {
name: 'home',
path: '/',
areas: {
sidebar: <SidebarNavigationScreenMain />,
preview: <Editor />,
preview: <MaybeEditor showEditor={ false } />,
mobile: <SidebarNavigationScreenMain />,
},
};
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/site-editor-routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { templatesRoute } from './templates';
import { templateItemRoute } from './template-item';
import { pagesRoute } from './pages';
import { pageItemRoute } from './page-item';
import { stylebookRoute } from './stylebook';

const routes = [
pageItemRoute,
Expand All @@ -33,6 +34,7 @@ const routes = [
navigationRoute,
stylesRoute,
homeRoute,
stylebookRoute,
];

export function useRegisterSiteEditorRoutes() {
Expand Down
28 changes: 28 additions & 0 deletions packages/edit-site/src/components/site-editor-routes/stylebook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import { StyleBookPreview } from '../style-book';

export const stylebookRoute = {
name: 'stylebook',
path: '/stylebook',
areas: {
sidebar: (
<SidebarNavigationScreen
title={ __( 'Styles' ) }
backPath="/"
description={ __(
`Preview your website's visual identity: colors, typography, and blocks.`
) }
/>
),
preview: <StyleBookPreview />,
mobile: <StyleBookPreview />,
},
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* Internal dependencies
*/
import Editor from '../editor';
import { MaybeEditor } from '../maybe-editor';
import SidebarNavigationScreenTemplatesBrowse from '../sidebar-navigation-screen-templates-browse';

export const templateItemRoute = {
name: 'template-item',
path: '/wp_template/*postId',
areas: {
sidebar: <SidebarNavigationScreenTemplatesBrowse backPath="/" />,
mobile: <Editor />,
preview: <Editor />,
mobile: <MaybeEditor />,
preview: <MaybeEditor />,
},
};
Loading
Loading