Repository navigation
Block Bindings: Add editor support for innerBlocks bindings #79940
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
Closed
cbravobernal
wants to merge
11
commits into
add/61225-innerblocks-bindings-server
from
add/61225-innerblocks-bindings-editor
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c9e9bae
Block Bindings: Add experimental inner blocks server slice
cbravobernal 501627b
Block Bindings: Guard inherited context access
cbravobernal 0e019cf
Block Bindings: Empty nested structural children
cbravobernal 205a5a2
Block Bindings: Restrict structural bindings to pattern overrides
cbravobernal ab2ab5d
Block Bindings: Isolate structural binding tests
cbravobernal 6c801ce
Block Bindings: Add experimental inner blocks editor slice
cbravobernal 07145e9
Block Bindings: Keep empty hosts out of the experiment
cbravobernal c3e9eb1
Block Bindings: Tighten structural override behavior
cbravobernal 06b0560
Block Bindings: Make structural pattern overrides editable
cbravobernal 83577e2
Block Bindings: Allow insertion in structural overrides
cbravobernal 51425ab
Merge branch 'add/61225-innerblocks-bindings-server' into add/61225-i…
cbravobernal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 = {}; | ||
|
|
||
|
|
@@ -75,6 +78,7 @@ function UncontrolledInnerBlocks( props ) { | |
| blockType, | ||
| parentLock, | ||
| defaultLayout, | ||
| boundInnerBlocks, | ||
| } = props; | ||
|
|
||
| useNestedSettingsUpdate( | ||
|
|
@@ -89,7 +93,8 @@ function UncontrolledInnerBlocks( props ) { | |
| templateLock, | ||
| captureToolbars, | ||
| orientation, | ||
| layout | ||
| layout, | ||
| boundInnerBlocks | ||
| ); | ||
|
|
||
| useInnerBlockTemplateSync( | ||
|
|
@@ -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 ( | ||
|
|
@@ -195,6 +257,8 @@ export function useInnerBlocksProps( props = {}, options = {} ) { | |
| ( select ) => { | ||
| const { | ||
| getBlockName, | ||
| getBlockAttributes, | ||
| getSettings, | ||
| isZoomOut, | ||
| getTemplateLock, | ||
| getBlockRootClientId, | ||
|
|
@@ -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'; | ||
|
|
||
|
|
@@ -243,6 +313,7 @@ export function useInnerBlocksProps( props = {}, options = {} ) { | |
| parentClientId, | ||
| isDropZoneDisabled: _isDropZoneDisabled, | ||
| defaultLayout, | ||
| innerBlocksBinding, | ||
| }; | ||
| }, | ||
| [ clientId ] | ||
|
|
@@ -255,6 +326,7 @@ export function useInnerBlocksProps( props = {}, options = {} ) { | |
| parentClientId, | ||
| isDropZoneDisabled, | ||
| defaultLayout, | ||
| innerBlocksBinding, | ||
| } = selected; | ||
|
|
||
| const blockDropZoneRef = useBlockDropZone( { | ||
|
|
@@ -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.` | ||
| ); | ||
| } | ||
| 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we simplify this conditional. |
||
|
|
||
| return { | ||
| ...props, | ||
|
|
@@ -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, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?