Skip to content

Commit

Permalink
Implement visibility logic for zoom out mode inserter and implement r…
Browse files Browse the repository at this point in the history
…elated selectors and actions
  • Loading branch information
yogeshbhutkar committed Dec 6, 2024
1 parent a605b82 commit 4c96916
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 44 deletions.
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
127 changes: 93 additions & 34 deletions packages/block-editor/src/components/block-list/zoom-out-separator.js
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
8 changes: 5 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,7 +45,7 @@ function selector( select ) {
isTyping: isTyping(),
isZoomOutMode: isZoomOut(),
isDragging: isDragging(),
isInserterOpened: getSettings().__experimentalIsInserterOpened,
showZoomOutModeInserter: showZoomOutModeInserter(),
};
}

Expand All @@ -68,7 +69,7 @@ export default function BlockTools( {
isTyping,
isZoomOutMode,
isDragging,
isInserterOpened,
showZoomOutModeInserter,
} = useSelect( selector, [] );

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

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

Expand Down Expand Up @@ -241,7 +243,7 @@ export default function BlockTools( {
name="__unstable-block-tools-after"
ref={ blockToolbarAfterRef }
/>
{ isZoomOutMode && ! isDragging && ! isInserterOpened && (
{ 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
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,6 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {

const forceDisableFocusMode = settings.focusMode === false;

const isInserterOpened = useSelect(
( select ) => select( editorStore ).isInserterOpened(),
[]
);

return useMemo( () => {
const blockEditorSettings = {
...Object.fromEntries(
Expand Down Expand Up @@ -331,7 +326,6 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
postType === 'wp_navigation'
? [ [ 'core/navigation', {}, [] ] ]
: settings.template,
__experimentalIsInserterOpened: isInserterOpened,
__experimentalSetIsInserterOpened: setIsInserterOpened,
[ sectionRootClientIdKey ]: sectionRootClientId,
editorTool:
Expand Down Expand Up @@ -366,7 +360,6 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
globalStylesData,
globalStylesLinksData,
renderingMode,
isInserterOpened,
] );
}

Expand Down

0 comments on commit 4c96916

Please sign in to comment.