Skip to content

Commit

Permalink
Close inserter on exiting Zoom Out to edit (WordPress#65194)
Browse files Browse the repository at this point in the history
* Use experimental setting in single call location

* Close inserter on double click to exit Zoom Out

* Avoid additional store subscription
  • Loading branch information
getdave authored Sep 12, 2024
1 parent 394288f commit a76c475
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useRefEffect } from '@wordpress/compose';

/**
Expand All @@ -16,6 +16,10 @@ import { unlock } from '../../../lock-unlock';
* @param {string} clientId Block client ID.
*/
export function useZoomOutModeExit( { editorMode } ) {
const getSettings = useSelect(
( select ) => select( blockEditorStore ).getSettings
);

const { __unstableSetEditorMode } = unlock(
useDispatch( blockEditorStore )
);
Expand All @@ -29,6 +33,14 @@ export function useZoomOutModeExit( { editorMode } ) {
function onDoubleClick( event ) {
if ( ! event.defaultPrevented ) {
event.preventDefault();

const { __experimentalSetIsInserterOpened } = getSettings();

if (
typeof __experimentalSetIsInserterOpened === 'function'
) {
__experimentalSetIsInserterOpened( false );
}
__unstableSetEditorMode( 'edit' );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export default function ZoomOutToolbar( { clientId, __unstableContentRef } ) {
getPreviousBlockClientId,
canRemoveBlock,
canMoveBlock,
getSettings,
} = select( blockEditorStore );

const { __experimentalSetIsInserterOpened: setIsInserterOpened } =
getSettings();

const { getBlockType } = select( blocksStore );
const { name } = getBlock( clientId );
const blockType = getBlockType( name );
Expand Down Expand Up @@ -63,6 +68,7 @@ export default function ZoomOutToolbar( { clientId, __unstableContentRef } ) {
isPrevBlockTemplatePart,
canRemove: canRemoveBlock( clientId ),
canMove: canMoveBlock( clientId ),
setIsInserterOpened,
};
},
[ clientId ]
Expand All @@ -75,6 +81,7 @@ export default function ZoomOutToolbar( { clientId, __unstableContentRef } ) {
isPrevBlockTemplatePart,
canRemove,
canMove,
setIsInserterOpened,
} = selected;

const { removeBlock, __unstableSetEditorMode } =
Expand Down Expand Up @@ -132,6 +139,10 @@ export default function ZoomOutToolbar( { clientId, __unstableContentRef } ) {
icon={ edit }
label={ __( 'Edit' ) }
onClick={ () => {
// Setting may be undefined.
if ( typeof setIsInserterOpened === 'function' ) {
setIsInserterOpened( false );
}
__unstableSetEditorMode( 'edit' );
__unstableContentRef.current?.focus();
} }
Expand Down

0 comments on commit a76c475

Please sign in to comment.