Skip to content

Commit

Permalink
Fix share controls with children
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 7, 2023
1 parent 738c07a commit aa5e8f5
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 78 deletions.
30 changes: 2 additions & 28 deletions packages/block-editor/src/components/block-controls/hook.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import groups from './groups';
import { store as blockEditorStore } from '../../store';
import { useBlockEditContext } from '../block-edit/context';
import useDisplayBlockControls from '../use-display-block-controls';

export default function useBlockControlsFill( group, shareWithChildBlocks ) {
const isDisplayed = useDisplayBlockControls();
const { clientId } = useBlockEditContext();
const isParentDisplayed = useSelect(
( select ) => {
if ( ! shareWithChildBlocks ) {
return false;
}

const { getBlockName, hasSelectedInnerBlock } =
select( blockEditorStore );
const { hasBlockSupport } = select( blocksStore );

return (
hasBlockSupport(
getBlockName( clientId ),
'__experimentalExposeControlsToChildren',
false
) && hasSelectedInnerBlock( clientId )
);
},
[ shareWithChildBlocks, clientId ]
);

const { isDisplayed, isParentDisplayed } = useDisplayBlockControls();
if ( isDisplayed ) {
return groups[ group ]?.Fill;
}
if ( isParentDisplayed ) {
if ( isParentDisplayed && shareWithChildBlocks ) {
return groups.parent.Fill;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { createPrivateSlotFill } = unlock( componentsPrivateApis );
const { Fill, Slot } = createPrivateSlotFill( 'BlockInformation' );

const BlockInfo = ( props ) => {
const isDisplayed = useDisplayBlockControls();
const { isDisplayed } = useDisplayBlockControls();
if ( ! isDisplayed ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function InspectorControlsFill( {
group = __experimentalGroup;
}

const isDisplayed = useDisplayBlockControls();
const { isDisplayed } = useDisplayBlockControls();
const Fill = groups[ group ]?.Fill;
if ( ! Fill ) {
warning( `Unknown InspectorControls group "${ group }" provided.` );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function InspectorControlsFill( {
);
group = __experimentalGroup;
}
const isDisplayed = useDisplayBlockControls();
const { isDisplayed } = useDisplayBlockControls();

const Fill = groups[ group ]?.Fill;
if ( ! Fill ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as blocksStore } from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -13,23 +14,28 @@ export default function useDisplayBlockControls() {
const { isSelected, clientId, name } = useBlockEditContext();
return useSelect(
( select ) => {
if ( isSelected ) {
return true;
}

const {
getBlockName,
isFirstMultiSelectedBlock,
getMultiSelectedBlockClientIds,
hasSelectedInnerBlock,
} = select( blockEditorStore );
const { hasBlockSupport } = select( blocksStore );

if ( isFirstMultiSelectedBlock( clientId ) ) {
return getMultiSelectedBlockClientIds().every(
( id ) => getBlockName( id ) === name
);
}

return false;
return {
isDisplayed:
isSelected ||
( isFirstMultiSelectedBlock( clientId ) &&
getMultiSelectedBlockClientIds().every(
( id ) => getBlockName( id ) === name
) ),
isParentDisplayed:
hasBlockSupport(
getBlockName( clientId ),
'__experimentalExposeControlsToChildren',
false
) && hasSelectedInnerBlock( clientId ),
};
},
[ clientId, isSelected, name ]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ export default function useDisplayBlockControls() {
false
);

if ( ! hideControls && isSelected ) {
return true;
}

return false;
return { isDisplayed: ! hideControls && isSelected };
},
[ clientId, isSelected, name ]
);
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ function BlockEditAlignmentToolbarControlsPure( {
}

export default {
shareWithChildBlocks: true,
edit: BlockEditAlignmentToolbarControlsPure,
attributeKeys: [ 'align' ],
hasSupport( name ) {
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/duotone.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function DuotonePanelPure( { style, setAttributes, name } ) {
}

export default {
shareWithChildBlocks: true,
edit: DuotonePanelPure,
attributeKeys: [ 'style' ],
hasSupport( name ) {
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ function LayoutPanelPure( { layout, setAttributes, name: blockName } ) {
}

export default {
shareWithChildBlocks: true,
edit: LayoutPanelPure,
attributeKeys: [ 'layout' ],
hasSupport( name ) {
Expand Down
71 changes: 40 additions & 31 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,45 +375,54 @@ export function createBlockEditFilter( features ) {
} );
const withBlockEditHooks = createHigherOrderComponent(
( OriginalBlockEdit ) => ( props ) => {
const shouldDisplayControls = useDisplayBlockControls();
const { isDisplayed, isParentDisplayed } =
useDisplayBlockControls();
// CAUTION: code added before this line will be executed for all
// blocks, not just those that support the feature! Code added
// above this line should be carefully evaluated for its impact on
// performance.
return [
...features.map(
( { Edit, hasSupport, attributeKeys = [] }, i ) => {
if (
! shouldDisplayControls ||
! hasSupport( props.name )
) {
return null;
}
...features.map( ( feature, i ) => {
const {
Edit,
hasSupport,
attributeKeys = [],
shareWithChildBlocks,
} = feature;
const shouldDisplayControls =
isDisplayed ||
( isParentDisplayed && shareWithChildBlocks );

const neededProps = {};
for ( const key of attributeKeys ) {
if ( props.attributes[ key ] ) {
neededProps[ key ] = props.attributes[ key ];
}
if (
! shouldDisplayControls ||
! hasSupport( props.name )
) {
return null;
}

const neededProps = {};
for ( const key of attributeKeys ) {
if ( props.attributes[ key ] ) {
neededProps[ key ] = props.attributes[ key ];
}
return (
<Edit
// We can use the index because the array length
// is fixed per page load right now.
key={ i }
name={ props.name }
clientId={ props.clientId }
setAttributes={ props.setAttributes }
__unstableParentLayout={
props.__unstableParentLayout
}
// This component is pure, so only pass needed
// props!!!
{ ...neededProps }
/>
);
}
),
return (
<Edit
// We can use the index because the array length
// is fixed per page load right now.
key={ i }
name={ props.name }
clientId={ props.clientId }
setAttributes={ props.setAttributes }
__unstableParentLayout={
props.__unstableParentLayout
}
// This component is pure, so only pass needed
// props!!!
{ ...neededProps }
/>
);
} ),
<OriginalBlockEdit key="edit" { ...props } />,
];
},
Expand Down

0 comments on commit aa5e8f5

Please sign in to comment.