Skip to content

Commit

Permalink
InnerBlocks: combine store subscriptions (#57032)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored and artemiomorales committed Jan 4, 2024
1 parent 8864398 commit 473c4fa
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 93 deletions.
90 changes: 65 additions & 25 deletions packages/block-editor/src/components/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ import { useSettings } from '../use-settings';

const EMPTY_OBJECT = {};

function BlockContext( { children, clientId } ) {
const context = useBlockContext( clientId );
return (
<BlockContextProvider value={ context }>
{ children }
</BlockContextProvider>
);
}

/**
* InnerBlocks is a component which allows a single block to have multiple blocks
* as children. The UncontrolledInnerBlocks component is used whenever the inner
Expand Down Expand Up @@ -60,10 +69,15 @@ function UncontrolledInnerBlocks( props ) {
orientation,
placeholder,
layout,
name,
blockType,
innerBlocks,
parentLock,
} = props;

useNestedSettingsUpdate(
clientId,
parentLock,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
Expand All @@ -78,19 +92,12 @@ function UncontrolledInnerBlocks( props ) {

useInnerBlockTemplateSync(
clientId,
innerBlocks,
template,
templateLock,
templateInsertUpdatesSelection
);

const context = useBlockContext( clientId );
const name = useSelect(
( select ) => {
return select( blockEditorStore ).getBlock( clientId )?.name;
},
[ clientId ]
);

const defaultLayoutBlockSupport =
getBlockSupport( name, 'layout' ) ||
getBlockSupport( name, '__experimentalLayout' ) ||
Expand All @@ -114,20 +121,22 @@ function UncontrolledInnerBlocks( props ) {
[ defaultLayout, usedLayout, allowSizingOnChildren ]
);

// This component needs to always be synchronous as it's the one changing
// the async mode depending on the block selection.
return (
<BlockContextProvider value={ context }>
<BlockListItems
rootClientId={ clientId }
renderAppender={ renderAppender }
__experimentalAppenderTagName={ __experimentalAppenderTagName }
layout={ memoedLayout }
wrapperRef={ wrapperRef }
placeholder={ placeholder }
/>
</BlockContextProvider>
const items = (
<BlockListItems
rootClientId={ clientId }
renderAppender={ renderAppender }
__experimentalAppenderTagName={ __experimentalAppenderTagName }
layout={ memoedLayout }
wrapperRef={ wrapperRef }
placeholder={ placeholder }
/>
);

if ( Object.keys( blockType.providesContext ).length === 0 ) {
return items;
}

return <BlockContext clientId={ clientId }>{ items }</BlockContext>;
}

/**
Expand Down Expand Up @@ -180,7 +189,16 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
__unstableLayoutClassNames: layoutClassNames = '',
} = useBlockEditContext();
const isSmallScreen = useViewportMatch( 'medium', '<' );
const { __experimentalCaptureToolbars, hasOverlay } = useSelect(
const {
__experimentalCaptureToolbars,
hasOverlay,
name,
blockType,
innerBlocks,
parentLock,
parentClientId,
isDropZoneDisabled,
} = useSelect(
( select ) => {
if ( ! clientId ) {
return {};
Expand All @@ -191,14 +209,21 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
isBlockSelected,
hasSelectedInnerBlock,
__unstableGetEditorMode,
getBlocks,
getTemplateLock,
getBlockRootClientId,
__unstableIsWithinBlockOverlay,
__unstableHasActiveBlockOverlayActive,
getBlockEditingMode,
} = select( blockEditorStore );
const { hasBlockSupport, getBlockType } = select( blocksStore );
const blockName = getBlockName( clientId );
const enableClickThrough =
__unstableGetEditorMode() === 'navigation' || isSmallScreen;
const blockEditingMode = getBlockEditingMode( clientId );
const _parentClientId = getBlockRootClientId( clientId );
return {
__experimentalCaptureToolbars: select(
blocksStore
).hasBlockSupport(
__experimentalCaptureToolbars: hasBlockSupport(
blockName,
'__experimentalExposeControlsToChildren',
false
Expand All @@ -208,6 +233,15 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
! isBlockSelected( clientId ) &&
! hasSelectedInnerBlock( clientId, true ) &&
enableClickThrough,
name: blockName,
blockType: getBlockType( blockName ),
innerBlocks: getBlocks( clientId ),
parentLock: getTemplateLock( _parentClientId ),
parentClientId: _parentClientId,
isDropZoneDisabled:
blockEditingMode !== 'default' ||
__unstableHasActiveBlockOverlayActive( clientId ) ||
__unstableIsWithinBlockOverlay( clientId ),
};
},
[ clientId, isSmallScreen ]
Expand All @@ -216,6 +250,8 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
const blockDropZoneRef = useBlockDropZone( {
dropZoneElement,
rootClientId: clientId,
parentClientId,
isDisabled: isDropZoneDisabled,
} );

const ref = useMergeRefs( [
Expand All @@ -226,6 +262,10 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
const innerBlocksProps = {
__experimentalCaptureToolbars,
layout,
name,
blockType,
innerBlocks,
parentLock,
...options,
};
const InnerBlocks =
Expand Down
26 changes: 19 additions & 7 deletions packages/block-editor/src/components/inner-blocks/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,26 @@ function UncontrolledInnerBlocks( props ) {

const context = useBlockContext( clientId );

const { nestingLevel, innerBlocks, parentLock } = useSelect(
( select ) => {
const {
getBlockParents,
getBlocks,
getTemplateLock,
getBlockRootClientId,
} = select( blockEditorStore );
return {
nestingLevel: getBlockParents( clientId )?.length,
innerBlocks: getBlocks( clientId ),
parentLock: getTemplateLock( getBlockRootClientId( clientId ) ),
};
},
[ clientId ]
);

useNestedSettingsUpdate(
clientId,
parentLock,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
Expand All @@ -121,18 +139,12 @@ function UncontrolledInnerBlocks( props ) {

useInnerBlockTemplateSync(
clientId,
innerBlocks,
template,
templateLock,
templateInsertUpdatesSelection
);

const nestingLevel = useSelect(
( select ) => {
return select( blockEditorStore ).getBlockParents( clientId )
?.length;
},
[ clientId ]
);
if ( nestingLevel >= MAX_NESTING_DEPTH ) {
return <WarningMaxDepthExceeded clientId={ clientId } />;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { store as blockEditorStore } from '../../store';
* then we replace the inner blocks with the correct value after synchronizing it with the template.
*
* @param {string} clientId The block client ID.
* @param {Array} innerBlocks
* @param {Object} template The template to match.
* @param {string} templateLock The template lock state for the inner blocks. For
* example, if the template lock is set to "all",
Expand All @@ -36,10 +37,14 @@ import { store as blockEditorStore } from '../../store';
*/
export default function useInnerBlockTemplateSync(
clientId,
innerBlocks,
template,
templateLock,
templateInsertUpdatesSelection
) {
// Instead of adding a useSelect mapping here, please add to the useSelect
// mapping in InnerBlocks! Every subscription impacts performance.

const {
getBlocks,
getSelectedBlocksInitialCaretPosition,
Expand All @@ -48,13 +53,6 @@ export default function useInnerBlockTemplateSync(
const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const { innerBlocks } = useSelect(
( select ) => ( {
innerBlocks: select( blockEditorStore ).getBlocks( clientId ),
} ),
[ clientId ]
);

// Maintain a reference to the previous value so we can do a deep equality check.
const existingTemplate = useRef( null );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { useLayoutEffect, useMemo, useState } from '@wordpress/element';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { useDispatch, useRegistry } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import isShallowEqual from '@wordpress/is-shallow-equal';

Expand Down Expand Up @@ -32,6 +32,7 @@ function useShallowMemo( value ) {
* came from props.
*
* @param {string} clientId The client ID of the block to update.
* @param {string} parentLock
* @param {string[]} allowedBlocks An array of block names which are permitted
* in inner blocks.
* @param {string[]} prioritizedInserterBlocks Block names and/or block variations to be prioritized in the inserter, in the format {blockName}/{variationName}.
Expand All @@ -53,6 +54,7 @@ function useShallowMemo( value ) {
*/
export default function useNestedSettingsUpdate(
clientId,
parentLock,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
Expand All @@ -64,21 +66,12 @@ export default function useNestedSettingsUpdate(
orientation,
layout
) {
// Instead of adding a useSelect mapping here, please add to the useSelect
// mapping in InnerBlocks! Every subscription impacts performance.

const { updateBlockListSettings } = useDispatch( blockEditorStore );
const registry = useRegistry();

const { parentLock } = useSelect(
( select ) => {
const rootClientId =
select( blockEditorStore ).getBlockRootClientId( clientId );
return {
parentLock:
select( blockEditorStore ).getTemplateLock( rootClientId ),
};
},
[ clientId ]
);

// Implementors often pass a new array on every render,
// and the contents of the arrays are just strings, so the entire array
// can be passed as dependencies but We need to include the length of the array,
Expand Down
30 changes: 3 additions & 27 deletions packages/block-editor/src/components/use-block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,38 +209,15 @@ export default function useBlockDropZone( {
// values returned by the `getRootBlockClientId` selector, which also uses
// an empty string to represent top-level blocks.
rootClientId: targetRootClientId = '',
parentClientId: parentBlockClientId = '',
isDisabled = false,
} = {} ) {
const registry = useRegistry();
const [ dropTarget, setDropTarget ] = useState( {
index: null,
operation: 'insert',
} );

const { isDisabled, parentBlockClientId, rootBlockIndex } = useSelect(
( select ) => {
const {
__unstableIsWithinBlockOverlay,
__unstableHasActiveBlockOverlayActive,
getBlockIndex,
getBlockParents,
getBlockEditingMode,
} = select( blockEditorStore );
const blockEditingMode = getBlockEditingMode( targetRootClientId );
return {
parentBlockClientId:
getBlockParents( targetRootClientId, true )[ 0 ] || '',
rootBlockIndex: getBlockIndex( targetRootClientId ),
isDisabled:
blockEditingMode !== 'default' ||
__unstableHasActiveBlockOverlayActive(
targetRootClientId
) ||
__unstableIsWithinBlockOverlay( targetRootClientId ),
};
},
[ targetRootClientId ]
);

const { getBlockListSettings, getBlocks, getBlockIndex } =
useSelect( blockEditorStore );
const { showInsertionPoint, hideInsertionPoint } =
Expand Down Expand Up @@ -299,7 +276,7 @@ export default function useBlockDropZone( {
? getBlockListSettings( parentBlockClientId )
?.orientation
: undefined,
rootBlockIndex,
rootBlockIndex: getBlockIndex( targetRootClientId ),
}
);

Expand Down Expand Up @@ -330,7 +307,6 @@ export default function useBlockDropZone( {
showInsertionPoint,
getBlockIndex,
parentBlockClientId,
rootBlockIndex,
]
),
200
Expand Down
Loading

0 comments on commit 473c4fa

Please sign in to comment.