-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Restores setting zoom out mode to useZoomOut hook #65999
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,39 +9,55 @@ import { useEffect, useRef } from '@wordpress/element'; | |
*/ | ||
import { store as blockEditorStore } from '../store'; | ||
import { unlock } from '../lock-unlock'; | ||
|
||
/** | ||
* A hook used to set the zoomed out view, invoking the hook sets the mode. | ||
* A hook used to set the editor mode to zoomed out mode, invoking the hook sets the mode. | ||
* | ||
* @param {boolean} zoomOut If we should zoom out or not. | ||
* @param {boolean} zoomOut If we should enter into zoomOut mode or not | ||
*/ | ||
export function useZoomOut( zoomOut = true ) { | ||
const { setZoomLevel } = unlock( useDispatch( blockEditorStore ) ); | ||
const { isZoomOut } = unlock( useSelect( blockEditorStore ) ); | ||
const { __unstableSetEditorMode, setZoomLevel } = unlock( | ||
useDispatch( blockEditorStore ) | ||
); | ||
const { __unstableGetEditorMode } = unlock( useSelect( blockEditorStore ) ); | ||
|
||
const originalIsZoomOutRef = useRef( null ); | ||
const originalEditingModeRef = useRef( null ); | ||
const mode = __unstableGetEditorMode(); | ||
|
||
useEffect( () => { | ||
// Only set this on mount so we know what to return to when we unmount. | ||
if ( ! originalIsZoomOutRef.current ) { | ||
originalIsZoomOutRef.current = isZoomOut(); | ||
if ( ! originalEditingModeRef.current ) { | ||
originalEditingModeRef.current = mode; | ||
} | ||
|
||
// The effect opens the zoom-out view if we want it open and the canvas is not currently zoomed-out. | ||
if ( zoomOut && isZoomOut() === false ) { | ||
return () => { | ||
// We need to use __unstableGetEditorMode() here and not `mode`, as mode may not update on unmount | ||
if ( | ||
__unstableGetEditorMode() === 'zoom-out' && | ||
__unstableGetEditorMode() !== originalEditingModeRef.current | ||
) { | ||
__unstableSetEditorMode( originalEditingModeRef.current ); | ||
setZoomLevel( 100 ); | ||
} | ||
}; | ||
}, [] ); | ||
|
||
// The effect opens the zoom-out view if we want it open and it's not currently in zoom-out mode. | ||
useEffect( () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I came across this PR while searching for the cause of #66205. I feel like we need to check that it's not a mobile layout here, otherwise the width won't be set correctly when we open the Patterns tab in mobile view. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'll need to wait and see if #66141 gets backported. However, @PARTHVATALIYA's idea to add it to the inserter useZoomOut conditional should work well for either case. If that route will fix the issue, I think that would work well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirming that we are not intending to backport #66141 |
||
if ( zoomOut && mode !== 'zoom-out' ) { | ||
__unstableSetEditorMode( 'zoom-out' ); | ||
setZoomLevel( 50 ); | ||
} else if ( | ||
! zoomOut && | ||
isZoomOut() && | ||
originalIsZoomOutRef.current !== isZoomOut() | ||
__unstableGetEditorMode() === 'zoom-out' && | ||
originalEditingModeRef.current !== mode | ||
) { | ||
setZoomLevel( originalIsZoomOutRef.current ? 50 : 100 ); | ||
__unstableSetEditorMode( originalEditingModeRef.current ); | ||
setZoomLevel( 100 ); | ||
} | ||
|
||
return () => { | ||
if ( isZoomOut() && isZoomOut() !== originalIsZoomOutRef.current ) { | ||
setZoomLevel( originalIsZoomOutRef.current ? 50 : 100 ); | ||
} | ||
}; | ||
}, [ isZoomOut, setZoomLevel, zoomOut ] ); | ||
}, [ | ||
__unstableGetEditorMode, | ||
__unstableSetEditorMode, | ||
zoomOut, | ||
setZoomLevel, | ||
] ); // Mode is deliberately excluded from the dependencies so that the effect does not run when mode changes. | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To note that the "zoom out mode" is already replaced by edit mode.