From 886c2b41191ef951e9a2843ed1f43c1a506e49c1 Mon Sep 17 00:00:00 2001 From: Ramon Date: Sat, 7 Sep 2024 11:52:17 +1000 Subject: [PATCH] Locked Templates: blocks with contentOnly locking should not be transformable (#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 Co-authored-by: talldan Co-authored-by: getdave Co-authored-by: jasmussen Co-authored-by: richtabor --- .../src/components/block-inspector/index.js | 1 - .../src/components/block-switcher/index.js | 22 ++++++++++++++++--- .../src/components/block-toolbar/index.js | 13 ++++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/block-editor/src/components/block-inspector/index.js b/packages/block-editor/src/components/block-inspector/index.js index 1986ad8730c12..a18556f2fa5bd 100644 --- a/packages/block-editor/src/components/block-inspector/index.js +++ b/packages/block-editor/src/components/block-inspector/index.js @@ -72,7 +72,6 @@ function BlockInspectorLockedBlocks( { topLevelLockedBlock } ) { { ...blockInformation } className={ blockInformation.isSynced && 'is-synced' } /> - { hasBlockStyles && ( diff --git a/packages/block-editor/src/components/block-switcher/index.js b/packages/block-editor/src/components/block-switcher/index.js index c8ee422091ad1..98e7f7b2d2142 100644 --- a/packages/block-editor/src/components/block-switcher/index.js +++ b/packages/block-editor/src/components/block-switcher/index.js @@ -198,6 +198,7 @@ const BlockIndicator = ( { icon, showTitle, blockTitle } ) => ( export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => { const { + hasContentOnlyLocking, canRemove, hasBlockStyles, icon, @@ -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 ); @@ -219,6 +224,7 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => { const blockType = getBlockType( firstBlockName ); let _icon; + let _hasTemplateLock; if ( _isSingleBlockSelected ) { const match = getActiveBlockVariation( firstBlockName, @@ -226,9 +232,14 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => { ); // 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; @@ -244,6 +255,7 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => { _isSingleBlockSelected && isReusableBlock( _blocks[ 0 ] ), isTemplate: _isSingleBlockSelected && isTemplatePart( _blocks[ 0 ] ), + hasContentOnlyLocking: _hasTemplateLock, }; }, [ clientIds ] @@ -252,6 +264,7 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => { clientId: clientIds?.[ 0 ], maximumLength: 35, } ); + if ( invalidBlocks ) { return null; } @@ -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 ( diff --git a/packages/block-editor/src/components/block-toolbar/index.js b/packages/block-editor/src/components/block-toolbar/index.js index b14ceb5059049..6c4789cb2924f 100644 --- a/packages/block-editor/src/components/block-toolbar/index.js +++ b/packages/block-editor/src/components/block-toolbar/index.js @@ -66,6 +66,7 @@ export function PrivateBlockToolbar( { showParentSelector, isUsingBindings, hasParentPattern, + hasContentOnlyLocking, } = useSelect( ( select ) => { const { getBlockName, @@ -76,6 +77,7 @@ export function PrivateBlockToolbar( { getBlockEditingMode, getBlockAttributes, getBlockParentsByBlockName, + getTemplateLock, } = select( blockEditorStore ); const selectedBlockClientIds = getSelectedBlockClientIds(); const selectedBlockClientId = selectedBlockClientIds[ 0 ]; @@ -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, @@ -123,6 +129,7 @@ export function PrivateBlockToolbar( { _isDefaultEditingMode, isUsingBindings: _isUsingBindings, hasParentPattern: _hasParentPattern, + hasContentOnlyLocking: _hasTemplateLock, }; }, [] ); @@ -205,9 +212,9 @@ export function PrivateBlockToolbar( { ) } - { shouldShowVisualToolbar && isMultiToolbar && ( - - ) } + { ! hasContentOnlyLocking && + shouldShowVisualToolbar && + isMultiToolbar && } { shouldShowVisualToolbar && ( <>