Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
124 changes: 113 additions & 11 deletions packages/block-editor/src/components/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import clsx from 'clsx';
* WordPress dependencies
*/
import { useMergeRefs } from '@wordpress/compose';
import { forwardRef, useMemo, memo } from '@wordpress/element';
import { forwardRef, useMemo, useState, memo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import {
getBlockSupport,
store as blocksStore,
__unstableGetInnerBlocksProps as getInnerBlocksProps,
} from '@wordpress/blocks';
import warning from '@wordpress/warning';

/**
* Internal dependencies
Expand All @@ -23,13 +24,15 @@ import DefaultBlockAppender from './default-block-appender';
import useNestedSettingsUpdate from './use-nested-settings-update';
import useInnerBlockTemplateSync from './use-inner-block-template-sync';
import useBlockContext from './use-block-context';
import { useBoundInnerBlocksProps } from './use-bound-inner-blocks';
import { BlockListItems } from '../block-list';
import { BlockContextProvider } from '../block-context';
import { useBlockEditContext } from '../block-edit/context';
import useBlockSync from '../provider/use-block-sync';
import { store as blockEditorStore } from '../../store';
import useBlockDropZone from '../use-block-drop-zone';
import { unlock } from '../../lock-unlock';
import { getInnerBlocksBinding } from '../../utils/block-bindings';

const EMPTY_OBJECT = {};

Expand Down Expand Up @@ -75,6 +78,7 @@ function UncontrolledInnerBlocks( props ) {
blockType,
parentLock,
defaultLayout,
boundInnerBlocks,
} = props;

useNestedSettingsUpdate(
Expand All @@ -89,7 +93,8 @@ function UncontrolledInnerBlocks( props ) {
templateLock,
captureToolbars,
orientation,
layout
layout,
boundInnerBlocks
);

useInnerBlockTemplateSync(
Expand Down Expand Up @@ -156,6 +161,63 @@ function ControlledInnerBlocks( props ) {
return <UncontrolledInnerBlocks { ...props } />;
}

function useEditablePatternOverrideSource( clientId, binding ) {
const sourceName = binding?.source;
return useSelect(
( select ) => {
if ( ! clientId || sourceName !== 'core/pattern-overrides' ) {
return undefined;
}

const blockEditor = select( blockEditorStore );
const attributes = blockEditor.getBlockAttributes( clientId );
const blockName = attributes?.metadata?.name;
const patternParents = blockEditor.getBlockParentsByBlockName(
clientId,
'core/block',
true
);

return typeof blockName === 'string' &&
blockName !== '' &&
patternParents.length === 1
? sourceName
: undefined;
},
[ clientId, sourceName ]
);
}

function BoundInnerBlocks( { binding, ...props } ) {
const editablePatternOverrideSource = useEditablePatternOverrideSource(
props.clientId,
binding
);
const boundInnerBlocksProps = useBoundInnerBlocksProps(
props.clientId,
binding,
props.blockType,
!! editablePatternOverrideSource
);
const innerBlocksProps = {
...props,
...boundInnerBlocksProps,
boundInnerBlocks: editablePatternOverrideSource ?? false,
};
const InnerBlocks = hasControlledInnerBlocks( innerBlocksProps )
? ControlledInnerBlocks
: UncontrolledInnerBlocks;

return <InnerBlocks { ...innerBlocksProps } />;
}

function hasControlledInnerBlocks( innerBlocksProps ) {
return (
innerBlocksProps.value !== undefined &&
innerBlocksProps.onChange !== undefined
);
}

const ForwardedInnerBlocks = forwardRef( ( props, ref ) => {
const innerBlocksProps = useInnerBlocksProps( { ref }, props );
return (
Expand Down Expand Up @@ -195,6 +257,8 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
( select ) => {
const {
getBlockName,
getBlockAttributes,
getSettings,
isZoomOut,
getTemplateLock,
getBlockRootClientId,
Expand All @@ -220,6 +284,12 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
const blockEditingMode = getBlockEditingMode( clientId );
const parentClientId = getBlockRootClientId( clientId );
const [ defaultLayout ] = getBlockSettings( clientId, 'layout' );
const innerBlocksBinding = getSettings().blockBindingsInnerBlocks
? getInnerBlocksBinding(
getBlockAttributes( clientId ),
blockName
)
: undefined;

let _isDropZoneDisabled = blockEditingMode === 'disabled';

Expand All @@ -243,6 +313,7 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
parentClientId,
isDropZoneDisabled: _isDropZoneDisabled,
defaultLayout,
innerBlocksBinding,
};
},
[ clientId ]
Expand All @@ -255,6 +326,7 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
parentClientId,
isDropZoneDisabled,
defaultLayout,
innerBlocksBinding,
} = selected;

const blockDropZoneRef = useBlockDropZone( {
Expand All @@ -281,11 +353,45 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
parentLock,
defaultLayout,
...options,
// This marker is owned by BoundInnerBlocks and is never caller-controlled.
boundInnerBlocks: undefined,
};
const InnerBlocks =
innerBlocksProps.value && innerBlocksProps.onChange
? ControlledInnerBlocks
: UncontrolledInnerBlocks;
const InnerBlocks = hasControlledInnerBlocks( innerBlocksProps )
? ControlledInnerBlocks
: UncontrolledInnerBlocks;
const hasCallerControlledProps =
options.value !== undefined ||
options.onChange !== undefined ||
options.onInput !== undefined;
if ( innerBlocksBinding && hasCallerControlledProps ) {
warning(
`The "innerBlocks" block binding to source "${ innerBlocksBinding.source }" is ignored because the block's inner blocks are already controlled through caller-supplied value/onChange props.`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is this a developer warning or an user warning. Should it be translatable?

);
}
const bindingApplies = !! innerBlocksBinding && ! hasCallerControlledProps;
const [ wasBound, setWasBound ] = useState( bindingApplies );
if ( bindingApplies && ! wasBound ) {
setWasBound( true );
}
const shouldUseBoundInnerBlocks =
! hasCallerControlledProps && ( innerBlocksBinding || wasBound );

let children;
if ( ! clientId ) {
children = <BlockListItems { ...options } />;
} else if ( shouldUseBoundInnerBlocks ) {
children = (
<BoundInnerBlocks
{ ...innerBlocksProps }
clientId={ clientId }
binding={ innerBlocksBinding }
/>
);
} else {
children = (
<InnerBlocks { ...innerBlocksProps } clientId={ clientId } />
);
}
Comment on lines +380 to +394

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can we simplify this conditional.


return {
...props,
Expand All @@ -295,11 +401,7 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
'block-editor-block-list__layout',
__unstableDisableLayoutClassNames ? '' : layoutClassNames
),
children: clientId ? (
<InnerBlocks { ...innerBlocksProps } clientId={ clientId } />
) : (
<BlockListItems { ...options } />
),
children,
};
}

Expand Down
Loading
Loading