Skip to content

Commit

Permalink
Block Editor: Remove 'rootClientId' argument for block lock selectors (
Browse files Browse the repository at this point in the history
…#62547)


Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: richtabor <richtabor@git.wordpress.org>
  • Loading branch information
4 people committed Jun 18, 2024
1 parent ff3f5ed commit e811b72
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 89 deletions.
4 changes: 0 additions & 4 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ _Parameters_

- _state_ `Object`: Editor state.
- _clientId_ `string`: The block client Id.
- _rootClientId_ `?string`: Optional root client ID of block list.

_Returns_

Expand All @@ -95,7 +94,6 @@ _Parameters_

- _state_ `Object`: Editor state.
- _clientIds_ `string`: The block client IDs to be moved.
- _rootClientId_ `?string`: Optional root client ID of block list.

_Returns_

Expand All @@ -109,7 +107,6 @@ _Parameters_

- _state_ `Object`: Editor state.
- _clientId_ `string`: The block client Id.
- _rootClientId_ `?string`: Optional root client ID of block list.

_Returns_

Expand All @@ -123,7 +120,6 @@ _Parameters_

- _state_ `Object`: Editor state.
- _clientIds_ `string`: The block client IDs to be removed.
- _rootClientId_ `?string`: Optional root client ID of block list.

_Returns_

Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/block-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export default function BlockActions( {
: null;

return {
canMove: canMoveBlocks( clientIds, rootClientId ),
canRemove: canRemoveBlocks( clientIds, rootClientId ),
canMove: canMoveBlocks( clientIds ),
canRemove: canRemoveBlocks( clientIds ),
canInsertBlock: canInsertDefaultBlock || !! directInsertBlock,
canCopyStyles: blocks.every( ( block ) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const BlockDraggable = ( {

return {
srcRootClientId: rootClientId,
isDraggable: canMoveBlocks( clientIds, rootClientId ),
isDraggable: canMoveBlocks( clientIds ),
icon: variation?.icon || _getBlockType( blockName )?.icon,
visibleInserter: isBlockInsertionPointVisible(),
getBlockType: _getBlockType,
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,8 @@ function BlockListBlockProvider( props ) {
}

const _isSelected = isBlockSelected( clientId );
const canRemove = canRemoveBlock( clientId, rootClientId );
const canMove = canMoveBlock( clientId, rootClientId );
const canRemove = canRemoveBlock( clientId );
const canMove = canMoveBlock( clientId );
const match = getActiveBlockVariation( blockName, attributes );
const isMultiSelected = isBlockMultiSelected( clientId );
const checkDeep = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,8 @@ const applyWithSelect = withSelect( ( select, { clientId, rootClientId } ) => {
const attributes = getBlockAttributes( clientId );
const isSelected = isBlockSelected( clientId );
const templateLock = getTemplateLock( rootClientId );
const canRemove = canRemoveBlock( clientId, rootClientId );
const canMove = canMoveBlock( clientId, rootClientId );
const canRemove = canRemoveBlock( clientId );
const canMove = canMoveBlock( clientId );

// The fallback to `{}` is a temporary fix.
// This function should never be called when a block is not present in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ export default function useBlockLock( clientId ) {
canRemoveBlock,
canLockBlockType,
getBlockName,
getBlockRootClientId,
getTemplateLock,
} = select( blockEditorStore );
const rootClientId = getBlockRootClientId( clientId );

const canEdit = canEditBlock( clientId );
const canMove = canMoveBlock( clientId, rootClientId );
const canRemove = canRemoveBlock( clientId, rootClientId );
const canMove = canMoveBlock( clientId );
const canRemove = canRemoveBlock( clientId );

return {
canEdit,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/block-mover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function BlockMover( {
const blockOrder = getBlockOrder( _rootClientId );

return {
canMove: canMoveBlocks( clientIds, _rootClientId ),
canMove: canMoveBlocks( clientIds ),
rootClientId: _rootClientId,
isFirst: firstIndex === 0,
isLast: lastIndex === blockOrder.length - 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default compose(
numberOfBlocks: blockOrder.length - 1,
isFirst: firstIndex === 0,
isLast: lastIndex === blockOrder.length - 1,
canMove: canMoveBlocks( clientIds, rootClientId ),
canMove: canMoveBlocks( clientIds ),
rootClientId,
};
} ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,12 @@ const EMPTY_OBJECT = {};
export function useBlockVariationTransforms( { clientIds, blocks } ) {
const { activeBlockVariation, blockVariationTransformations } = useSelect(
( select ) => {
const {
getBlockRootClientId,
getBlockAttributes,
canRemoveBlocks,
} = select( blockEditorStore );
const { getBlockAttributes, canRemoveBlocks } =
select( blockEditorStore );
const { getActiveBlockVariation, getBlockVariations } =
select( blocksStore );
const rootClientId = getBlockRootClientId(
Array.isArray( clientIds ) ? clientIds[ 0 ] : clientIds
);
const canRemove = canRemoveBlocks( clientIds, rootClientId );

const canRemove = canRemoveBlocks( clientIds );
// Only handle single selected blocks for now.
if ( blocks.length !== 1 || ! canRemove ) {
return EMPTY_OBJECT;
Expand Down
13 changes: 3 additions & 10 deletions packages/block-editor/src/components/block-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,14 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => {
isTemplate,
} = useSelect(
( select ) => {
const {
getBlockRootClientId,
getBlocksByClientId,
getBlockAttributes,
canRemoveBlocks,
} = select( blockEditorStore );
const { getBlocksByClientId, getBlockAttributes, canRemoveBlocks } =
select( blockEditorStore );
const { getBlockStyles, getBlockType, getActiveBlockVariation } =
select( blocksStore );
const _blocks = getBlocksByClientId( clientIds );
if ( ! _blocks.length || _blocks.some( ( block ) => ! block ) ) {
return { invalidBlocks: true };
}
const rootClientId = getBlockRootClientId(
Array.isArray( clientIds ) ? clientIds[ 0 ] : clientIds
);
const [ { name: firstBlockName } ] = _blocks;
const _isSingleBlockSelected = _blocks.length === 1;
const blockType = getBlockType( firstBlockName );
Expand All @@ -227,7 +220,7 @@ export const BlockSwitcher = ( { clientIds, disabled, isUsingBindings } ) => {
}

return {
canRemove: canRemoveBlocks( clientIds, rootClientId ),
canRemove: canRemoveBlocks( clientIds ),
hasBlockStyles:
_isSingleBlockSelected &&
!! getBlockStyles( firstBlockName )?.length,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ function BlockSelectionButton( { clientId, rootClientId } ) {
isBlockTemplatePart,
isNextBlockTemplatePart,
isPrevBlockTemplatePart,
canRemove: canRemoveBlock( clientId, rootClientId ),
canMove: canMoveBlock( clientId, rootClientId ),
canRemove: canRemoveBlock( clientId ),
canMove: canMoveBlock( clientId ),
};
},
[ clientId, rootClientId ]
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function ListViewBlock( {
} = getBlocksToUpdate();

// Don't update the selection if the blocks cannot be deleted.
if ( ! canRemoveBlocks( blocksToDelete, firstBlockRootClientId ) ) {
if ( ! canRemoveBlocks( blocksToDelete ) ) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,7 @@ export default function useClipboardHandler( { selectBlock } ) {

if ( event.type === 'cut' ) {
// Don't update the selection if the blocks cannot be deleted.
if (
! canRemoveBlocks(
selectedBlockClientIds,
firstBlockRootClientId
)
) {
if ( ! canRemoveBlocks( selectedBlockClientIds ) ) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const useTransformCommands = () => {
selectedBlocks,
rootClientId
),
canRemove: canRemoveBlocks( selectedBlockClientIds, rootClientId ),
canRemove: canRemoveBlocks( selectedBlockClientIds ),
invalidSelection: false,
};
}, [] );
Expand Down Expand Up @@ -150,8 +150,7 @@ const useActionsCommands = () => {
const rootClientId = getBlockRootClientId( clientIds[ 0 ] );

const canMove =
canMoveBlocks( clientIds, rootClientId ) &&
getBlockCount( rootClientId ) !== 1;
canMoveBlocks( clientIds ) && getBlockCount( rootClientId ) !== 1;

const commands = [];

Expand Down Expand Up @@ -260,7 +259,7 @@ const useQuickActionsCommands = () => {
canInsertBlockType( block.name, rootClientId )
);
} );
const canRemove = canRemoveBlocks( clientIds, rootClientId );
const canRemove = canRemoveBlocks( clientIds );

const commands = [];

Expand Down
12 changes: 3 additions & 9 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ const createOnMove =
( clientIds, rootClientId ) =>
( { select, dispatch } ) => {
// If one of the blocks is locked or the parent is locked, we cannot move any block.
const canMoveBlocks = select.canMoveBlocks( clientIds, rootClientId );
const canMoveBlocks = select.canMoveBlocks( clientIds );
if ( ! canMoveBlocks ) {
return;
}
Expand All @@ -431,10 +431,7 @@ export const moveBlocksUp = createOnMove( 'MOVE_BLOCKS_UP' );
export const moveBlocksToPosition =
( clientIds, fromRootClientId = '', toRootClientId = '', index ) =>
( { select, dispatch } ) => {
const canMoveBlocks = select.canMoveBlocks(
clientIds,
fromRootClientId
);
const canMoveBlocks = select.canMoveBlocks( clientIds );

// If one of the blocks is locked or the parent is locked, we cannot move any block.
if ( ! canMoveBlocks ) {
Expand All @@ -443,10 +440,7 @@ export const moveBlocksToPosition =

// If moving inside the same root block the move is always possible.
if ( fromRootClientId !== toRootClientId ) {
const canRemoveBlocks = select.canRemoveBlocks(
clientIds,
fromRootClientId
);
const canRemoveBlocks = select.canRemoveBlocks( clientIds );

// If we're moving to another block, it means we're deleting blocks from
// the original block, so we need to check if removing is possible.
Expand Down
6 changes: 1 addition & 5 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ export const privateRemoveBlocks =
}

clientIds = castArray( clientIds );
const rootClientId = select.getBlockRootClientId( clientIds[ 0 ] );
const canRemoveBlocks = select.canRemoveBlocks(
clientIds,
rootClientId
);
const canRemoveBlocks = select.canRemoveBlocks( clientIds );

if ( ! canRemoveBlocks ) {
return;
Expand Down
45 changes: 19 additions & 26 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1712,20 +1712,21 @@ export function canInsertBlocks( state, clientIds, rootClientId = null ) {
/**
* Determines if the given block is allowed to be deleted.
*
* @param {Object} state Editor state.
* @param {string} clientId The block client Id.
* @param {?string} rootClientId Optional root client ID of block list.
* @param {Object} state Editor state.
* @param {string} clientId The block client Id.
*
* @return {boolean} Whether the given block is allowed to be removed.
*/
export function canRemoveBlock( state, clientId, rootClientId = null ) {
export function canRemoveBlock( state, clientId ) {
const attributes = getBlockAttributes( state, clientId );
if ( attributes === null ) {
return true;
}
if ( attributes.lock?.remove !== undefined ) {
return ! attributes.lock.remove;
}

const rootClientId = getBlockRootClientId( state, clientId );
if ( getTemplateLock( state, rootClientId ) ) {
return false;
}
Expand All @@ -1736,35 +1737,33 @@ export function canRemoveBlock( state, clientId, rootClientId = null ) {
/**
* Determines if the given blocks are allowed to be removed.
*
* @param {Object} state Editor state.
* @param {string} clientIds The block client IDs to be removed.
* @param {?string} rootClientId Optional root client ID of block list.
* @param {Object} state Editor state.
* @param {string} clientIds The block client IDs to be removed.
*
* @return {boolean} Whether the given blocks are allowed to be removed.
*/
export function canRemoveBlocks( state, clientIds, rootClientId = null ) {
return clientIds.every( ( clientId ) =>
canRemoveBlock( state, clientId, rootClientId )
);
export function canRemoveBlocks( state, clientIds ) {
return clientIds.every( ( clientId ) => canRemoveBlock( state, clientId ) );
}

/**
* Determines if the given block is allowed to be moved.
*
* @param {Object} state Editor state.
* @param {string} clientId The block client Id.
* @param {?string} rootClientId Optional root client ID of block list.
* @param {Object} state Editor state.
* @param {string} clientId The block client Id.
*
* @return {boolean | undefined} Whether the given block is allowed to be moved.
*/
export function canMoveBlock( state, clientId, rootClientId = null ) {
export function canMoveBlock( state, clientId ) {
const attributes = getBlockAttributes( state, clientId );
if ( attributes === null ) {
return true;
}
if ( attributes.lock?.move !== undefined ) {
return ! attributes.lock.move;
}

const rootClientId = getBlockRootClientId( state, clientId );
if ( getTemplateLock( state, rootClientId ) === 'all' ) {
return false;
}
Expand All @@ -1774,16 +1773,13 @@ export function canMoveBlock( state, clientId, rootClientId = null ) {
/**
* Determines if the given blocks are allowed to be moved.
*
* @param {Object} state Editor state.
* @param {string} clientIds The block client IDs to be moved.
* @param {?string} rootClientId Optional root client ID of block list.
* @param {Object} state Editor state.
* @param {string} clientIds The block client IDs to be moved.
*
* @return {boolean} Whether the given blocks are allowed to be moved.
*/
export function canMoveBlocks( state, clientIds, rootClientId = null ) {
return clientIds.every( ( clientId ) =>
canMoveBlock( state, clientId, rootClientId )
);
export function canMoveBlocks( state, clientIds ) {
return clientIds.every( ( clientId ) => canMoveBlock( state, clientId ) );
}

/**
Expand Down Expand Up @@ -3041,10 +3037,7 @@ export const isGroupable = createRegistrySelector(
rootClientId
);
const _isGroupable = groupingBlockAvailable && _clientIds.length;
return (
_isGroupable &&
canRemoveBlocks( state, _clientIds, rootClientId )
);
return _isGroupable && canRemoveBlocks( state, _clientIds );
}
);

Expand Down

0 comments on commit e811b72

Please sign in to comment.