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 Tools: Don't render inserter in Zoom Out Dropzone when text is visible #67608

Closed
Closed
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
24 changes: 24 additions & 0 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,18 @@ _Returns_

- `?boolean`: Whether the template is valid or not.

### showZoomOutModeInserter

Returns whether the zoom out mode inserter is enabled or not.

_Parameters_

- _state_ `Object`: Global application state.

_Returns_

- `boolean`: Whether the drop pattern is enabled or not.

### wasBlockJustInserted

Tells if the block with the passed clientId was just inserted.
Expand Down Expand Up @@ -1721,6 +1733,18 @@ _Parameters_

- _isNavigationMode_ `boolean`: Enable/Disable navigation mode.

### setShowZoomOutModeInserter

Action that shows the zoom out mode inserter.

_Parameters_

- _showZoomOutModeInserter_ `boolean`: show the zoom out mode inserter.

_Returns_

- `Object`: Action object.

### setTemplateValidity

Action that resets the template validity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
__unstableAnimatePresence as AnimatePresence,
} from '@wordpress/components';
import { useReducedMotion } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand All @@ -21,6 +21,80 @@ import { __ } from '@wordpress/i18n';
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';

const useRenderDropZone = ( {
position,
insertionPoint,
blockInsertionPointVisible,
blockInsertionPoint,
clientId,
sectionClientIds,
selectedBlockIds,
} ) => {
const [ isVisible, setIsVisible ] = useState( false );
const { setShowZoomOutModeInserter } = useDispatch( blockEditorStore );

useEffect( () => {
const hasTopInsertionPoint =
insertionPoint?.index === 0 &&
clientId === sectionClientIds[ insertionPoint.index ];
const hasBottomInsertionPoint =
insertionPoint &&
insertionPoint.hasOwnProperty( 'index' ) &&
clientId === sectionClientIds[ insertionPoint.index - 1 ];

let visibility = false;
let selectedBlockId = null;

if ( position === 'top' ) {
visibility =
hasTopInsertionPoint ||
( blockInsertionPointVisible &&
blockInsertionPoint.index === 0 &&
clientId ===
sectionClientIds[ blockInsertionPoint.index ] );
selectedBlockId =
sectionClientIds[
hasTopInsertionPoint
? insertionPoint.index
: blockInsertionPoint.index
];
}

if ( position === 'bottom' ) {
visibility =
hasBottomInsertionPoint ||
( blockInsertionPointVisible &&
clientId ===
sectionClientIds[ blockInsertionPoint.index - 1 ] );
selectedBlockId =
sectionClientIds[
hasBottomInsertionPoint
? insertionPoint.index - 1
: blockInsertionPoint.index - 1
];
}

if ( visibility ) {
setShowZoomOutModeInserter(
! selectedBlockIds.includes( selectedBlockId )
);
}

setIsVisible( visibility );
}, [
position,
insertionPoint,
blockInsertionPointVisible,
blockInsertionPoint,
clientId,
sectionClientIds,
selectedBlockIds,
setShowZoomOutModeInserter,
] );

return isVisible;
};

export function ZoomOutSeparator( {
clientId,
rootClientId = '',
Expand All @@ -33,11 +107,13 @@ export function ZoomOutSeparator( {
insertionPoint,
blockInsertionPointVisible,
blockInsertionPoint,
selectedBlockIds,
} = useSelect( ( select ) => {
const {
getInsertionPoint,
getBlockOrder,
getSectionRootClientId,
getSelectedBlockClientIds,
isBlockInsertionPointVisible,
getBlockInsertionPoint,
} = unlock( select( blockEditorStore ) );
Expand All @@ -51,50 +127,33 @@ export function ZoomOutSeparator( {
insertionPoint: getInsertionPoint(),
blockInsertionPoint: getBlockInsertionPoint(),
blockInsertionPointVisible: isBlockInsertionPointVisible(),
selectedBlockIds: getSelectedBlockClientIds(),
};
}, [] );

const isReducedMotion = useReducedMotion();

if ( ! clientId ) {
return;
}

let isVisible = false;
const isVisible = useRenderDropZone( {
position,
insertionPoint,
blockInsertionPointVisible,
blockInsertionPoint,
clientId,
sectionClientIds,
selectedBlockIds,
} );

const isSectionBlock =
rootClientId === sectionRootClientId &&
sectionClientIds &&
sectionClientIds.includes( clientId );

if ( ! isSectionBlock ) {
return null;
}
const isReducedMotion = useReducedMotion();

const hasTopInsertionPoint =
insertionPoint?.index === 0 &&
clientId === sectionClientIds[ insertionPoint.index ];
const hasBottomInsertionPoint =
insertionPoint &&
insertionPoint.hasOwnProperty( 'index' ) &&
clientId === sectionClientIds[ insertionPoint.index - 1 ];
// We want to show the zoom out separator in either of these conditions:
// 1. If the inserter has an insertion index set
// 2. We are dragging a pattern over an insertion point
if ( position === 'top' ) {
isVisible =
hasTopInsertionPoint ||
( blockInsertionPointVisible &&
blockInsertionPoint.index === 0 &&
clientId === sectionClientIds[ blockInsertionPoint.index ] );
if ( ! clientId ) {
return;
}

if ( position === 'bottom' ) {
isVisible =
hasBottomInsertionPoint ||
( blockInsertionPointVisible &&
clientId ===
sectionClientIds[ blockInsertionPoint.index - 1 ] );
if ( ! isSectionBlock ) {
return null;
}

return (
Expand Down
15 changes: 12 additions & 3 deletions packages/block-editor/src/components/block-tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function selector( select ) {
isTyping,
isDragging,
isZoomOut,
showZoomOutModeInserter,
} = unlock( select( blockEditorStore ) );

const clientId =
Expand All @@ -44,6 +45,7 @@ function selector( select ) {
isTyping: isTyping(),
isZoomOutMode: isZoomOut(),
isDragging: isDragging(),
showZoomOutModeInserter: showZoomOutModeInserter(),
};
}

Expand All @@ -61,8 +63,14 @@ export default function BlockTools( {
__unstableContentRef,
...props
} ) {
const { clientId, hasFixedToolbar, isTyping, isZoomOutMode, isDragging } =
useSelect( selector, [] );
const {
clientId,
hasFixedToolbar,
isTyping,
isZoomOutMode,
isDragging,
showZoomOutModeInserter,
} = useSelect( selector, [] );

const isMatch = useShortcutEventMatch();
const {
Expand Down Expand Up @@ -193,6 +201,7 @@ export default function BlockTools( {
}
}
}

const blockToolbarRef = usePopoverScroll( __unstableContentRef );
const blockToolbarAfterRef = usePopoverScroll( __unstableContentRef );

Expand Down Expand Up @@ -234,7 +243,7 @@ export default function BlockTools( {
name="__unstable-block-tools-after"
ref={ blockToolbarAfterRef }
/>
{ isZoomOutMode && ! isDragging && (
{ isZoomOutMode && ! isDragging && showZoomOutModeInserter && (
<ZoomOutModeInserters
__unstableContentRef={ __unstableContentRef }
/>
Expand Down
14 changes: 14 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,20 @@ export function setTemplateValidity( isValid ) {
};
}

/**
* Action that shows the zoom out mode inserter.
*
* @param {boolean} showZoomOutModeInserter show the zoom out mode inserter.
*
* @return {Object} Action object.
*/
export function setShowZoomOutModeInserter( showZoomOutModeInserter ) {
return {
type: 'SET_ZOOM_OUT_MODE_INSERTER',
showZoomOutModeInserter,
};
}

/**
* Action that synchronizes the template with the list of blocks.
*
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ export const SETTINGS_DEFAULTS = {

isPreviewMode: false,

showZoomOutModeInserter: true,

// These settings will be completely revamped in the future.
// The goal is to evolve this into an API which will instruct
// the block inspector to animate transitions between what it
Expand Down
24 changes: 24 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,29 @@ export function template( state = { isValid: true }, action ) {
return state;
}

/**
* Reducer returning whether the zoom out mode inserter should be shown or not.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function zoomOutModeInserter(
state = { showZoomOutModeInserter: true },
action
) {
switch ( action.type ) {
case 'SET_ZOOM_OUT_MODE_INSERTER':
return {
...state,
showZoomOutModeInserter: action.showZoomOutModeInserter,
};
}

return state;
}

/**
* Reducer returning the editor setting.
*
Expand Down Expand Up @@ -2119,6 +2142,7 @@ const combinedReducers = combineReducers( {
insertionPoint,
insertionCue,
template,
zoomOutModeInserter,
settings,
preferences,
lastBlockAttributesChange,
Expand Down
11 changes: 11 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,17 @@ export function isBlockInsertionPointVisible( state ) {
return state.insertionCue !== null;
}

/**
* Returns whether the zoom out mode inserter is enabled or not.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether the drop pattern is enabled or not.
*/
export function showZoomOutModeInserter( state ) {
return state.zoomOutModeInserter.showZoomOutModeInserter;
}

/**
* Returns whether the blocks matches the template or not.
*
Expand Down
Loading