Skip to content

Commit

Permalink
Adds content locking to navigation block.
Browse files Browse the repository at this point in the history
Adds a new block api method `isNavigationBlock` to complement `isReusableBlock`.
  • Loading branch information
draganescu committed Oct 6, 2022
1 parent b662575 commit f4f02ae
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
42 changes: 24 additions & 18 deletions packages/block-editor/src/components/block-lock/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
import { lock as lockIcon, unlock as unlockIcon } from '@wordpress/icons';
import { useInstanceId } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { isReusableBlock, getBlockType } from '@wordpress/blocks';
import {
isReusableBlock,
isNavigationBlock,
getBlockType,
} from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -41,21 +45,23 @@ function getTemplateLockValue( lock ) {
export default function BlockLockModal( { clientId, onClose } ) {
const [ lock, setLock ] = useState( { move: false, remove: false } );
const { canEdit, canMove, canRemove } = useBlockLock( clientId );
const { isReusable, templateLock, hasTemplateLock } = useSelect(
( select ) => {
const { getBlockName, getBlockAttributes } =
select( blockEditorStore );
const blockName = getBlockName( clientId );
const blockType = getBlockType( blockName );
const { isReusable, isNavigation, templateLock, hasTemplateLock } =
useSelect(
( select ) => {
const { getBlockName, getBlockAttributes } =
select( blockEditorStore );
const blockName = getBlockName( clientId );
const blockType = getBlockType( blockName );

return {
isReusable: isReusableBlock( blockType ),
templateLock: getBlockAttributes( clientId )?.templateLock,
hasTemplateLock: !! blockType?.attributes?.templateLock,
};
},
[ clientId ]
);
return {
isReusable: isReusableBlock( blockType ),
isNavigation: isNavigationBlock( blockType ),
templateLock: getBlockAttributes( clientId )?.templateLock,
hasTemplateLock: !! blockType?.attributes?.templateLock,
};
},
[ clientId ]
);
const [ applyTemplateLock, setApplyTemplateLock ] = useState(
!! templateLock
);
Expand All @@ -70,9 +76,9 @@ export default function BlockLockModal( { clientId, onClose } ) {
setLock( {
move: ! canMove,
remove: ! canRemove,
...( isReusable ? { edit: ! canEdit } : {} ),
...( isReusable || isNavigation ? { edit: ! canEdit } : {} ),
} );
}, [ canEdit, canMove, canRemove, isReusable ] );
}, [ canEdit, canMove, canRemove, isReusable, isNavigation ] );

const isAllChecked = Object.values( lock ).every( Boolean );
const isMixed = Object.values( lock ).some( Boolean ) && ! isAllChecked;
Expand Down Expand Up @@ -126,7 +132,7 @@ export default function BlockLockModal( { clientId, onClose } ) {
}
/>
<ul className="block-editor-block-lock-modal__checklist">
{ isReusable && (
{ ( isReusable || isNavigation ) && (
<li className="block-editor-block-lock-modal__checklist-item">
<CheckboxControl
label={
Expand Down
13 changes: 13 additions & 0 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,19 @@ _Returns_

- `boolean`: True if a block contains at least one child blocks with inserter support and false otherwise.

### isNavigationBlock

Determines whether or not the given block is a navigation block. This is a
special block type that can used to point to a wp_navigation post type.

_Parameters_

- _blockOrType_ `Object`: Block or Block Type to test.

_Returns_

- `boolean`: Whether the given block is a navigation block.

### isReusableBlock

Determines whether or not the given block is a reusable block. This is a
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export {
hasBlockSupport,
getBlockVariations,
isReusableBlock,
isNavigationBlock,
isTemplatePart,
getChildBlockNames,
hasChildBlocks,
Expand Down
12 changes: 12 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,18 @@ export function isReusableBlock( blockOrType ) {
return blockOrType?.name === 'core/block';
}

/**
* Determines whether or not the given block is a navigation block. This is a
* special block type that can used to point to a wp_navigation post type.
*
* @param {Object} blockOrType Block or Block Type to test.
*
* @return {boolean} Whether the given block is a navigation block.
*/
export function isNavigationBlock( blockOrType ) {
return blockOrType?.name === 'core/navigation';
}

/**
* Determines whether or not the given block is a template part. This is a
* special block type that allows composing a page template out of reusable
Expand Down

0 comments on commit f4f02ae

Please sign in to comment.