Skip to content

Commit

Permalink
Block Locking: Add the 'Apply to inner blocks' option
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jun 23, 2022
1 parent 13312b3 commit 9aca01f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
39 changes: 36 additions & 3 deletions packages/block-editor/src/components/block-lock/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FlexItem,
Icon,
Modal,
ToggleControl,
} from '@wordpress/components';
import { lock as lockIcon, unlock as unlockIcon } from '@wordpress/icons';
import { useInstanceId } from '@wordpress/compose';
Expand All @@ -23,16 +24,33 @@ import useBlockLock from './use-block-lock';
import useBlockDisplayInformation from '../use-block-display-information';
import { store as blockEditorStore } from '../../store';

function getTemplateLockValue( lock ) {
// Prevents all operations.
if ( lock.remove && lock.move ) {
return 'all';
}

// Prevents inserting or removing blocks, but allows moving existing blocks.
if ( lock.remove && ! lock.move ) {
return 'insert';
}

return false;
}

export default function BlockLockModal( { clientId, onClose } ) {
const [ applyTemplateLock, setApplyTemplateLock ] = useState( false );
const [ lock, setLock ] = useState( { move: false, remove: false } );
const { canEdit, canMove, canRemove } = useBlockLock( clientId );
const { isReusable } = useSelect(
const { isReusable, hasTemplateLock } = useSelect(
( select ) => {
const { getBlockName } = select( blockEditorStore );
const blockName = getBlockName( clientId );
const blockType = getBlockType( blockName );

return {
isReusable: isReusableBlock( getBlockType( blockName ) ),
isReusable: isReusableBlock( blockType ),
hasTemplateLock: !! blockType?.attributes?.templateLock,
};
},
[ clientId ]
Expand Down Expand Up @@ -69,7 +87,12 @@ export default function BlockLockModal( { clientId, onClose } ) {
<form
onSubmit={ ( event ) => {
event.preventDefault();
updateBlockAttributes( [ clientId ], { lock } );
updateBlockAttributes( [ clientId ], {
lock,
templateLock: applyTemplateLock
? getTemplateLockValue( lock )
: undefined,
} );
onClose();
} }
>
Expand Down Expand Up @@ -171,6 +194,16 @@ export default function BlockLockModal( { clientId, onClose } ) {
/>
</li>
</ul>
{ hasTemplateLock && (
<ToggleControl
className="block-editor-block-lock-modal__template-lock"
label={ __( 'Apply to inner blocks' ) }
checked={ applyTemplateLock }
onChange={ () =>
setApplyTemplateLock( ! applyTemplateLock )
}
/>
) }
</div>
<Flex
className="block-editor-block-lock-modal__actions"
Expand Down
9 changes: 9 additions & 0 deletions packages/block-editor/src/components/block-lock/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
}
}

.block-editor-block-lock-modal__template-lock {
border-top: $border-width solid $gray-300;
padding: $grid-unit-15 0;

.components-base-control__field {
margin: 0;
}
}

.block-editor-block-lock-modal__actions {
margin-top: $grid-unit-30;
}
Expand Down

0 comments on commit 9aca01f

Please sign in to comment.