Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Introduce the 'useBlockLock' hook #40210

Merged
merged 3 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/block-editor/src/components/block-lock/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as BlockLockMenuItem } from './menu-item';
export { default as BlockLockModal } from './modal';
export { default as BlockLockToolbar } from './toolbar';
export { default as useBlockLock } from './use-block-lock';
27 changes: 4 additions & 23 deletions packages/block-editor/src/components/block-lock/menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,24 @@
import { __ } from '@wordpress/i18n';
import { useReducer } from '@wordpress/element';
import { MenuItem } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { lock, unlock } from '@wordpress/icons';

/**
* Internal dependencies
*/
import useBlockLock from './use-block-lock';
import BlockLockModal from './modal';
import { store as blockEditorStore } from '../../store';

export default function BlockLockMenuItem( { clientId } ) {
const { canLockBlock, isLocked } = useSelect(
( select ) => {
const {
canMoveBlock,
canRemoveBlock,
canLockBlockType,
getBlockName,
getBlockRootClientId,
} = select( blockEditorStore );
const rootClientId = getBlockRootClientId( clientId );

return {
canLockBlock: canLockBlockType( getBlockName( clientId ) ),
isLocked:
! canMoveBlock( clientId, rootClientId ) ||
! canRemoveBlock( clientId, rootClientId ),
};
},
[ clientId ]
);
const { canMove, canRemove, canLock } = useBlockLock( clientId, true );
const isLocked = ! canMove || ! canRemove;

const [ isModalOpen, toggleModal ] = useReducer(
( isActive ) => ! isActive,
false
);

if ( ! canLockBlock ) {
if ( ! canLock ) {
return null;
}

Expand Down
20 changes: 3 additions & 17 deletions packages/block-editor/src/components/block-lock/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,18 @@ import {
} from '@wordpress/components';
import { lock as lockIcon, unlock as unlockIcon } from '@wordpress/icons';
import { useInstanceId } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { useDispatch } from '@wordpress/data';

/**
* Internal dependencies
*/
import useBlockLock from './use-block-lock';
import useBlockDisplayInformation from '../use-block-display-information';
import { store as blockEditorStore } from '../../store';

export default function BlockLockModal( { clientId, onClose } ) {
const [ lock, setLock ] = useState( { move: false, remove: false } );
const { canMove, canRemove } = useSelect(
( select ) => {
const {
canMoveBlock,
canRemoveBlock,
getBlockRootClientId,
} = select( blockEditorStore );
const rootClientId = getBlockRootClientId( clientId );

return {
canMove: canMoveBlock( clientId, rootClientId ),
canRemove: canRemoveBlock( clientId, rootClientId ),
};
},
[ clientId ]
);
const { canMove, canRemove } = useBlockLock( clientId, true );
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const blockInformation = useBlockDisplayInformation( clientId );
const instanceId = useInstanceId(
Expand Down
23 changes: 3 additions & 20 deletions packages/block-editor/src/components/block-lock/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,24 @@ import { __, sprintf } from '@wordpress/i18n';
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
import { useReducer } from '@wordpress/element';
import { lock } from '@wordpress/icons';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import BlockLockModal from './modal';
import useBlockLock from './use-block-lock';
import useBlockDisplayInformation from '../use-block-display-information';
import { store as blockEditorStore } from '../../store';

export default function BlockLockToolbar( { clientId } ) {
const blockInformation = useBlockDisplayInformation( clientId );
const { canMove, canRemove, canLockBlock } = useSelect(
( select ) => {
const {
canMoveBlock,
canRemoveBlock,
canLockBlockType,
getBlockName,
} = select( blockEditorStore );

return {
canMove: canMoveBlock( clientId ),
canRemove: canRemoveBlock( clientId ),
canLockBlock: canLockBlockType( getBlockName( clientId ) ),
};
},
[ clientId ]
);
const { canMove, canRemove, canLock } = useBlockLock( clientId );

const [ isModalOpen, toggleModal ] = useReducer(
( isActive ) => ! isActive,
false
);

if ( ! canLockBlock ) {
if ( ! canLock ) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';

/**
* Return details about the block lock status.
*
* @param {string} clientId The block client Id.
* @param {boolean} checkRoot Optional. Use root client ID when checking lock status.
*
* @return {Object} Block lock status
*/
export default function useBlockLock( clientId, checkRoot = false ) {
return useSelect(
( select ) => {
const {
canMoveBlock,
canRemoveBlock,
canLockBlockType,
getBlockName,
getBlockRootClientId,
} = select( blockEditorStore );
const rootClientId = checkRoot
? getBlockRootClientId( clientId )
: null;

return {
canMove: canMoveBlock( clientId, rootClientId ),
canRemove: canRemoveBlock( clientId, rootClientId ),
canLock: canLockBlockType( getBlockName( clientId ) ),
};
},
[ clientId, checkRoot ]
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import classnames from 'classnames';
*/
import { Button } from '@wordpress/components';
import { forwardRef } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { Icon, lock } from '@wordpress/icons';
import { SPACE, ENTER } from '@wordpress/keycodes';

Expand All @@ -19,7 +18,7 @@ import BlockIcon from '../block-icon';
import useBlockDisplayInformation from '../use-block-display-information';
import BlockTitle from '../block-title';
import ListViewExpander from './expander';
import { store as blockEditorStore } from '../../store';
import { useBlockLock } from '../block-lock';

function ListViewBlockSelectButton(
{
Expand All @@ -36,14 +35,8 @@ function ListViewBlockSelectButton(
ref
) {
const blockInformation = useBlockDisplayInformation( clientId );
const isLocked = useSelect(
( select ) => {
const { canMoveBlock, canRemoveBlock } = select( blockEditorStore );

return ! canMoveBlock( clientId ) || ! canRemoveBlock( clientId );
},
[ clientId ]
);
const { canMove, canRemove } = useBlockLock( clientId );
const isLocked = ! canMove || ! canRemove;

// The `href` attribute triggers the browser's native HTML drag operations.
// When the link is dragged, the element's outerHTML is set in DataTransfer object as text/html.
Expand Down