Skip to content

Commit

Permalink
Prevent reordering of header and footer template parts when zoomed out (
Browse files Browse the repository at this point in the history
WordPress#60054)

* don't show movers in zoom out for template parts, try to remove movers for immediate siblings blocks

* fix bug because of typo in function name

* don't hide the movers, disable them

* rename props to be more generic
  • Loading branch information
draganescu authored and carstingaxion committed Mar 27, 2024
1 parent 29d7285 commit 67608db
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/block-editor/src/components/block-mover/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const BlockMoverButton = forwardRef(
? clientIds
: [ clientIds ];
const blocksCount = normalizedClientIds.length;
const { disabled } = props;

const {
blockType,
Expand Down Expand Up @@ -98,7 +99,9 @@ const BlockMoverButton = forwardRef(

return {
blockType: block ? getBlockType( block.name ) : null,
isDisabled: direction === 'up' ? isFirstBlock : isLastBlock,
isDisabled:
disabled ||
( direction === 'up' ? isFirstBlock : isLastBlock ),
rootClientId: blockRootClientId,
firstIndex: firstBlockIndex,
isFirst: isFirstBlock,
Expand Down
9 changes: 8 additions & 1 deletion packages/block-editor/src/components/block-mover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import BlockDraggable from '../block-draggable';
import { BlockMoverUpButton, BlockMoverDownButton } from './button';
import { store as blockEditorStore } from '../../store';

function BlockMover( { clientIds, hideDragHandle } ) {
function BlockMover( {
clientIds,
hideDragHandle,
isBlockMoverUpButtonDisabled,
isBlockMoverDownButtonDisabled,
} ) {
const { canMove, rootClientId, isFirst, isLast, orientation } = useSelect(
( select ) => {
const {
Expand Down Expand Up @@ -83,6 +88,7 @@ function BlockMover( { clientIds, hideDragHandle } ) {
<ToolbarItem>
{ ( itemProps ) => (
<BlockMoverUpButton
disabled={ isBlockMoverUpButtonDisabled }
clientIds={ clientIds }
{ ...itemProps }
/>
Expand All @@ -91,6 +97,7 @@ function BlockMover( { clientIds, hideDragHandle } ) {
<ToolbarItem>
{ ( itemProps ) => (
<BlockMoverDownButton
disabled={ isBlockMoverDownButtonDisabled }
clientIds={ clientIds }
{ ...itemProps }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function BlockSelectionButton( { clientId, rootClientId } ) {
hasBlockMovingClientId,
getBlockListSettings,
__unstableGetEditorMode,
getNextBlockClientId,
getPreviousBlockClientId,
} = select( blockEditorStore );
const { getActiveBlockVariation, getBlockType } =
select( blocksStore );
Expand All @@ -69,6 +71,26 @@ function BlockSelectionButton( { clientId, rootClientId } ) {
const orientation =
getBlockListSettings( rootClientId )?.orientation;
const match = getActiveBlockVariation( name, attributes );
const isBlockTemplatePart =
blockType?.name === 'core/template-part';

let isNextBlockTemplatePart = false;
const nextClientId = getNextBlockClientId();
if ( nextClientId ) {
const { name: nextName } = getBlock( nextClientId );
const nextBlockType = getBlockType( nextName );
isNextBlockTemplatePart =
nextBlockType?.name === 'core/template-part';
}

let isPrevBlockTemplatePart = false;
const prevClientId = getPreviousBlockClientId();
if ( prevClientId ) {
const { name: prevName } = getBlock( prevClientId );
const prevBlockType = getBlockType( prevName );
isPrevBlockTemplatePart =
prevBlockType?.name === 'core/template-part';
}

return {
blockMovingMode: hasBlockMovingClientId(),
Expand All @@ -80,11 +102,22 @@ function BlockSelectionButton( { clientId, rootClientId } ) {
index + 1,
orientation
),
isBlockTemplatePart,
isNextBlockTemplatePart,
isPrevBlockTemplatePart,
};
},
[ clientId, rootClientId ]
);
const { label, icon, blockMovingMode, editorMode } = selected;
const {
label,
icon,
blockMovingMode,
editorMode,
isBlockTemplatePart,
isNextBlockTemplatePart,
isPrevBlockTemplatePart,
} = selected;
const { setNavigationMode, removeBlock } = useDispatch( blockEditorStore );
const ref = useRef();

Expand Down Expand Up @@ -252,8 +285,17 @@ function BlockSelectionButton( { clientId, rootClientId } ) {
<BlockIcon icon={ icon } showColors />
</FlexItem>
<FlexItem>
{ editorMode === 'zoom-out' && (
<BlockMover clientIds={ [ clientId ] } hideDragHandle />
{ editorMode === 'zoom-out' && ! isBlockTemplatePart && (
<BlockMover
clientIds={ [ clientId ] }
hideDragHandle
isBlockMoverUpButtonDisabled={
isPrevBlockTemplatePart
}
isBlockMoverDownButtonDisabled={
isNextBlockTemplatePart
}
/>
) }
{ editorMode === 'navigation' && (
<BlockDraggable clientIds={ [ clientId ] }>
Expand Down

0 comments on commit 67608db

Please sign in to comment.