Skip to content

Commit

Permalink
Locked Templates: blocks with contentOnly locking should not be trans…
Browse files Browse the repository at this point in the history
…formable (#64917)

In the block-switcher, hide the drop down with block transforms and variation if a selected block, or one of multiple, is content locked. 

Co-authored-by: ramonjd <[email protected]>
Co-authored-by: talldan <[email protected]>
Co-authored-by: getdave <[email protected]>
Co-authored-by: jasmussen <[email protected]>
Co-authored-by: richtabor <[email protected]>
  • Loading branch information
6 people authored Sep 7, 2024
1 parent 219be48 commit 886c2b4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function BlockInspectorLockedBlocks( { topLevelLockedBlock } ) {
{ ...blockInformation }
className={ blockInformation.isSynced && 'is-synced' }
/>
<BlockVariationTransforms blockClientId={ topLevelLockedBlock } />
<BlockInfo.Slot />
{ hasBlockStyles && (
<BlockStylesPanel clientId={ topLevelLockedBlock } />
Expand Down
22 changes: 19 additions & 3 deletions packages/block-editor/src/components/block-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ const BlockIndicator = ( { icon, showTitle, blockTitle } ) => (

export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => {
const {
hasContentOnlyLocking,
canRemove,
hasBlockStyles,
icon,
Expand All @@ -206,8 +207,12 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => {
isTemplate,
} = useSelect(
( select ) => {
const { getBlocksByClientId, getBlockAttributes, canRemoveBlocks } =
select( blockEditorStore );
const {
getTemplateLock,
getBlocksByClientId,
getBlockAttributes,
canRemoveBlocks,
} = select( blockEditorStore );
const { getBlockStyles, getBlockType, getActiveBlockVariation } =
select( blocksStore );
const _blocks = getBlocksByClientId( clientIds );
Expand All @@ -219,16 +224,22 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => {
const blockType = getBlockType( firstBlockName );

let _icon;
let _hasTemplateLock;
if ( _isSingleBlockSelected ) {
const match = getActiveBlockVariation(
firstBlockName,
getBlockAttributes( clientIds[ 0 ] )
);
// Take into account active block variations.
_icon = match?.icon || blockType.icon;
_hasTemplateLock =
getTemplateLock( clientIds[ 0 ] ) === 'contentOnly';
} else {
const isSelectionOfSameType =
new Set( _blocks.map( ( { name } ) => name ) ).size === 1;
_hasTemplateLock = clientIds.some(
( id ) => getTemplateLock( id ) === 'contentOnly'
);
// When selection consists of blocks of multiple types, display an
// appropriate icon to communicate the non-uniformity.
_icon = isSelectionOfSameType ? blockType.icon : copy;
Expand All @@ -244,6 +255,7 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => {
_isSingleBlockSelected && isReusableBlock( _blocks[ 0 ] ),
isTemplate:
_isSingleBlockSelected && isTemplatePart( _blocks[ 0 ] ),
hasContentOnlyLocking: _hasTemplateLock,
};
},
[ clientIds ]
Expand All @@ -252,6 +264,7 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => {
clientId: clientIds?.[ 0 ],
maximumLength: 35,
} );

if ( invalidBlocks ) {
return null;
}
Expand All @@ -261,7 +274,10 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => {
? blockTitle
: __( 'Multiple blocks selected' );

const hideDropdown = disabled || ( ! hasBlockStyles && ! canRemove );
const hideDropdown =
disabled ||
( ! hasBlockStyles && ! canRemove ) ||
hasContentOnlyLocking;

if ( hideDropdown ) {
return (
Expand Down
13 changes: 10 additions & 3 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function PrivateBlockToolbar( {
showParentSelector,
isUsingBindings,
hasParentPattern,
hasContentOnlyLocking,
} = useSelect( ( select ) => {
const {
getBlockName,
Expand All @@ -76,6 +77,7 @@ export function PrivateBlockToolbar( {
getBlockEditingMode,
getBlockAttributes,
getBlockParentsByBlockName,
getTemplateLock,
} = select( blockEditorStore );
const selectedBlockClientIds = getSelectedBlockClientIds();
const selectedBlockClientId = selectedBlockClientIds[ 0 ];
Expand Down Expand Up @@ -103,6 +105,10 @@ export function PrivateBlockToolbar( {
.length > 0
);

// If one or more selected blocks are locked, do not show the BlockGroupToolbar.
const _hasTemplateLock = selectedBlockClientIds.some(
( id ) => getTemplateLock( id ) === 'contentOnly'
);
return {
blockClientId: selectedBlockClientId,
blockClientIds: selectedBlockClientIds,
Expand All @@ -123,6 +129,7 @@ export function PrivateBlockToolbar( {
_isDefaultEditingMode,
isUsingBindings: _isUsingBindings,
hasParentPattern: _hasParentPattern,
hasContentOnlyLocking: _hasTemplateLock,
};
}, [] );

Expand Down Expand Up @@ -205,9 +212,9 @@ export function PrivateBlockToolbar( {
</ToolbarGroup>
</div>
) }
{ shouldShowVisualToolbar && isMultiToolbar && (
<BlockGroupToolbar />
) }
{ ! hasContentOnlyLocking &&
shouldShowVisualToolbar &&
isMultiToolbar && <BlockGroupToolbar /> }
{ shouldShowVisualToolbar && (
<>
<BlockControls.Slot
Expand Down

1 comment on commit 886c2b4

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 886c2b4.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/10747645543
📝 Reported issues:

Please sign in to comment.