Skip to content
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/**
* WordPress dependencies
*/
import { useDispatch, useSelect, useRegistry } from '@wordpress/data';
import { useDispatch, useSelect, useRegistry, select } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as editorStore } from '@wordpress/editor';
import { useLayoutEffect } from '@wordpress/element';
import { store as preferencesStore } from '@wordpress/preferences';

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

export function useAdaptEditorToCanvas( canvas ) {
const { clearSelectedBlock } = useDispatch( blockEditorStore );
const {
Expand All @@ -17,6 +22,8 @@ export function useAdaptEditorToCanvas( canvas ) {
} = useDispatch( editorStore );
const { get: getPreference } = useSelect( preferencesStore );
const registry = useRegistry();
const { resetZoomLevel } = unlock( useDispatch( blockEditorStore ) );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The useDispatch( blockEditorStore ) is also defined above. Let's move and combine them in single call.

const { isZoomOut } = unlock( select( blockEditorStore ) );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The components shouldn't use global select it doesn't react to store changes.

Suggested change
const { isZoomOut } = unlock( select( blockEditorStore ) );
const { isZoomOut } = unlock( useSelect( blockEditorStore ) );

useLayoutEffect( () => {
const isMediumOrBigger =
window.matchMedia( '(min-width: 782px)' ).matches;
Expand All @@ -39,6 +46,10 @@ export function useAdaptEditorToCanvas( canvas ) {
} else {
setIsListViewOpened( false );
}

if ( isMediumOrBigger && isZoomOut() ) {
resetZoomLevel();
}
} );
}, [
canvas,
Expand All @@ -49,5 +60,7 @@ export function useAdaptEditorToCanvas( canvas ) {
setIsInserterOpened,
setIsListViewOpened,
getPreference,
resetZoomLevel,
isZoomOut,
] );
}