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 layout default value support for blocks #34194

Merged
merged 8 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
11 changes: 6 additions & 5 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ function gutenberg_get_layout_style( $selector, $layout ) {
* @return string Filtered block content.
*/
function gutenberg_render_layout_support_flag( $block_content, $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$support_layout = gutenberg_block_has_support( $block_type, array( '__experimentalLayout' ), false );
$has_innner_blocks = count( $block['innerBlocks'] ) > 0;
if ( ! $support_layout && ! $has_innner_blocks ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$support_layout = gutenberg_block_has_support( $block_type, array( '__experimentalLayout' ), false );

if ( ! $support_layout ) {
return $block_content;
}

$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : array();
$default_block_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() );
$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $default_block_layout;
if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) {
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( array(), 'theme' );
$default_layout = _wp_array_get( $tree->get_settings(), array( 'layout' ) );
Expand Down
57 changes: 29 additions & 28 deletions packages/block-editor/src/hooks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,9 @@ import { getLayoutType, getLayoutTypes } from '../layouts';

const layoutBlockSupportKey = '__experimentalLayout';

const canBlockSwitchLayout = ( blockTypeOrName ) => {
const layoutBlockSupportConfig = getBlockSupport(
blockTypeOrName,
layoutBlockSupportKey
);

return layoutBlockSupportConfig?.allowSwitching;
};

function LayoutPanel( { setAttributes, attributes, name: blockName } ) {
const { layout = {} } = attributes;
const defaultLayout = useSetting( 'layout' );
const { layout } = attributes;
const defaultThemeLayout = useSetting( 'layout' );
const themeSupportsLayout = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return getSettings().supportsLayout;
Expand All @@ -53,8 +44,19 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) {
return null;
}

const allowLayoutSwitching = canBlockSwitchLayout( blockName );
const { inherit = false, type = 'default' } = layout;
const {
allowSwitching: canBlockSwitchLayout,
allowEditing = true,
allowInheriting = true,
default: defaultBlockLayout,
} = getBlockSupport( blockName, layoutBlockSupportKey ) || {};
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should have a flag to disable all layout UI: Basically you should be able to choose a default layout but do not allow changes by the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I add an allowEditing = true flag and one more allowInheriting for displaying the respective control. Seems like many flags are needed.. Do you have any thoughts if we can simplify these?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure yet but yes, it's something we need to think about while it's still experimental.


if ( ! allowEditing ) {
return null;
}

const usedLayout = layout ? layout : defaultBlockLayout || {};
const { inherit = false, type = 'default' } = usedLayout;
const layoutType = getLayoutType( type );

const onChangeType = ( newType ) =>
Expand All @@ -65,7 +67,7 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) {
return (
<InspectorControls>
<PanelBody title={ __( 'Layout' ) }>
{ !! defaultLayout && (
{ allowInheriting && !! defaultThemeLayout && (
<ToggleControl
label={ __( 'Inherit default layout' ) }
checked={ !! inherit }
Expand All @@ -75,7 +77,7 @@ function LayoutPanel( { setAttributes, attributes, name: blockName } ) {
/>
) }

{ ! inherit && allowLayoutSwitching && (
{ ! inherit && canBlockSwitchLayout && (
<LayoutTypeSwitcher
type={ type }
onChange={ onChangeType }
Expand Down Expand Up @@ -166,21 +168,20 @@ export const withInspectorControls = createHigherOrderComponent(
*/
export const withLayoutStyles = createHigherOrderComponent(
( BlockListBlock ) => ( props ) => {
const { name, attributes, clientId } = props;
const supportLayout = hasBlockSupport( name, layoutBlockSupportKey );
const id = useInstanceId( BlockListBlock );
const defaultLayout = useSetting( 'layout' ) || {};
const hasInnerBlocks = useSelect(
( select ) => {
const { getBlockCount } = select( blockEditorStore );
return getBlockCount( clientId ) > 0;
},
[ clientId ]
const { name, attributes } = props;
const shouldRenderLayoutStyles = hasBlockSupport(
name,
layoutBlockSupportKey
);
const id = useInstanceId( BlockListBlock );
const defaultThemeLayout = useSetting( 'layout' ) || {};
const element = useContext( BlockList.__unstableElementContext );
const shouldRenderLayoutStyles = supportLayout || hasInnerBlocks;
const { layout = {} } = attributes;
const usedLayout = !! layout && layout.inherit ? defaultLayout : layout;
const { layout } = attributes;
const { default: defaultBlockLayout } =
getBlockSupport( name, layoutBlockSupportKey ) || {};
const usedLayout = layout?.inherit
? defaultThemeLayout
: layout || defaultBlockLayout || {};
const className = classnames( props?.className, {
[ `wp-container-${ id }` ]: shouldRenderLayoutStyles,
} );
Expand Down