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

Fix useZoomOut inserter behavior #67591

Merged
merged 16 commits into from
Dec 6, 2024
Merged
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
64 changes: 48 additions & 16 deletions packages/block-editor/src/hooks/use-zoom-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { useEffect, useRef } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -12,32 +12,64 @@ import { unlock } from '../lock-unlock';

/**
* A hook used to set the editor mode to zoomed out mode, invoking the hook sets the mode.
* Concepts:
* - If we most recently changed the zoom level for them (in or out), we always resetZoomLevel() level when unmounting.
* - If the user most recently changed the zoom level (manually toggling), we do nothing when unmounting.
*
* @param {boolean} zoomOut If we should enter into zoomOut mode or not
* @param {boolean} enabled If we should enter into zoomOut mode or not
*/
export function useZoomOut( zoomOut = true ) {
export function useZoomOut( enabled = true ) {
const { setZoomLevel, resetZoomLevel } = unlock(
useDispatch( blockEditorStore )
);
const { isZoomOut } = unlock( useSelect( blockEditorStore ) );

/**
* We need access to both the value and the function. The value is to trigger a useEffect hook
* and the function is to check zoom out within another hook without triggering a re-render.
*/
const { isZoomedOut, isZoomOut } = useSelect( ( select ) => {
const { isZoomOut: _isZoomOut } = unlock( select( blockEditorStore ) );
return {
isZoomedOut: _isZoomOut(),
isZoomOut: _isZoomOut,
};
}, [] );

const controlZoomLevelRef = useRef( false );
const isEnabledRef = useRef( enabled );

/**
* This hook tracks if the zoom state was changed manually by the user via clicking
* the zoom out button. We only want this to run when isZoomedOut changes, so we use
* a ref to track the enabled state.
*/
useEffect( () => {
const isZoomOutOnMount = isZoomOut();
// If the zoom state changed (isZoomOut) and it does not match the requested zoom
// state (zoomOut), then it means the user manually changed the zoom state while
// this hook was mounted, and we should no longer control the zoom state.
if ( isZoomedOut !== isEnabledRef.current ) {
controlZoomLevelRef.current = false;
}
}, [ isZoomedOut ] );

return () => {
if ( isZoomOutOnMount ) {
useEffect( () => {
isEnabledRef.current = enabled;

if ( enabled !== isZoomOut() ) {
controlZoomLevelRef.current = true;

if ( enabled ) {
setZoomLevel( 'auto-scaled' );
} else {
resetZoomLevel();
}
};
}, [] );

useEffect( () => {
if ( zoomOut ) {
setZoomLevel( 'auto-scaled' );
} else {
resetZoomLevel();
}
}, [ zoomOut, setZoomLevel, resetZoomLevel ] );

return () => {
// If we are controlling zoom level and are zoomed out, reset the zoom level.
if ( controlZoomLevelRef.current && isZoomOut() ) {
resetZoomLevel();
}
};
}, [ enabled, isZoomOut, resetZoomLevel, setZoomLevel ] );
}
1 change: 1 addition & 0 deletions test/e2e/specs/site-editor/preload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
test.describe( 'Preload', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'emptytheme' );
await requestUtils.resetPreferences();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should really be in a different PR, but I wanted to see if adding this would fix the e2e issue I'm running into. More context in the PR that implemented the Preload test.

tl;dr: The Preload test will have different results depending on what runs before it, and this should help normalize it. I just happened to run into the issue by having my test run on the same shard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did work -- I would have liked this fix a different PR but it auto-merged once the tests passed.

} );

test.afterAll( async ( { requestUtils } ) => {
Expand Down
Loading
Loading