Skip to content

Commit

Permalink
Double click block to exit zoom out mode (WordPress#64573)
Browse files Browse the repository at this point in the history
Co-authored-by: getdave <[email protected]>
Co-authored-by: jeryj <[email protected]>
Co-authored-by: MaggieCabrera <[email protected]>
  • Loading branch information
4 people authored Sep 4, 2024
1 parent abf72c9 commit fae4d1b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,8 @@ function BlockListBlockProvider( props ) {
blocksWithSameName.length &&
blocksWithSameName[ 0 ] !== clientId;

const editorMode = __unstableGetEditorMode();

return {
...previewContext,
mode: getBlockMode( clientId ),
Expand All @@ -672,6 +674,7 @@ function BlockListBlockProvider( props ) {
) && hasSelectedInnerBlock( clientId ),
blockApiVersion: blockType?.apiVersion || 1,
blockTitle: match?.title || blockType?.title,
editorMode,
isSubtreeDisabled:
blockEditingMode === 'disabled' &&
isBlockSubtreeDisabled( clientId ),
Expand All @@ -680,8 +683,7 @@ function BlockListBlockProvider( props ) {
! isDragging(),
initialPosition:
_isSelected &&
( __unstableGetEditorMode() === 'edit' ||
__unstableGetEditorMode() === 'zoom-out' ) // Don't recalculate the initialPosition when toggling in/out of zoom-out mode
( editorMode === 'edit' || editorMode === 'zoom-out' ) // Don't recalculate the initialPosition when toggling in/out of zoom-out mode
? getSelectedBlocksInitialCaretPosition()
: undefined,
isHighlighted: isBlockHighlighted( clientId ),
Expand Down Expand Up @@ -728,6 +730,7 @@ function BlockListBlockProvider( props ) {
themeSupportsLayout,
isTemporarilyEditingAsBlocks,
blockEditingMode,
editorMode,
mayDisplayControls,
mayDisplayParentControls,
index,
Expand Down Expand Up @@ -782,6 +785,7 @@ function BlockListBlockProvider( props ) {
hasOverlay,
initialPosition,
blockEditingMode,
editorMode,
isHighlighted,
isMultiSelected,
isPartiallySelected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { useFocusHandler } from './use-focus-handler';
import { useEventHandlers } from './use-selected-block-event-handlers';
import { useNavModeExit } from './use-nav-mode-exit';
import { useZoomOutModeExit } from './use-zoom-out-mode-exit';
import { useBlockRefProvider } from './use-block-refs';
import { useIntersectionObserver } from './use-intersection-observer';
import { useScrollIntoView } from './use-scroll-into-view';
Expand Down Expand Up @@ -85,6 +86,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
name,
blockApiVersion,
blockTitle,
editorMode,
isSelected,
isSubtreeDisabled,
hasOverlay,
Expand Down Expand Up @@ -115,6 +117,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
useFocusHandler( clientId ),
useEventHandlers( { clientId, isSelected } ),
useNavModeExit( clientId ),
useZoomOutModeExit( { editorMode } ),
useIsHovered( { clientId } ),
useIntersectionObserver(),
useMovingAnimation( { triggerAnimationOnChange: index, clientId } ),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { useRefEffect } from '@wordpress/compose';

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

/**
* Allows Zoom Out mode to be exited by double clicking in the selected block.
*
* @param {string} clientId Block client ID.
*/
export function useZoomOutModeExit( { editorMode } ) {
const { __unstableSetEditorMode } = unlock(
useDispatch( blockEditorStore )
);

return useRefEffect(
( node ) => {
if ( editorMode !== 'zoom-out' ) {
return;
}

function onDoubleClick( event ) {
if ( ! event.defaultPrevented ) {
event.preventDefault();
__unstableSetEditorMode( 'edit' );
}
}

node.addEventListener( 'dblclick', onDoubleClick );

return () => {
node.removeEventListener( 'dblclick', onDoubleClick );
};
},
[ editorMode, __unstableSetEditorMode ]
);
}

0 comments on commit fae4d1b

Please sign in to comment.