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

Zoom Out: Add a control to enter and leave zoom out mode #63870

Merged
merged 18 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 2 additions & 4 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,8 @@ function Iframe( {
const isZoomedOut = scale !== 1;

useEffect( () => {
if ( ! isZoomedOut ) {
prevContainerWidth.current = containerWidth;
Comment on lines -245 to -246
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder what led to this change? As I understand it the value of prevContainerWidth should be that of containerWidth before Zoom Out is engaged because the scale calculation needs to arrive at a scale which preserves the document width. That calculation wasn’t perfect before but this seems to have thrown it off a good deal when Zoom Out is engaged by way of the Inserter (not through the control added by this PR).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I made a PR for that here: #64478

}
}, [ containerWidth, isZoomedOut ] );
prevContainerWidth.current = containerWidth;
}, [ containerWidth ] );

const disabledRef = useDisabled( { isDisabled: ! readonly } );
const bodyRef = useMergeRefs( [
Expand Down
17 changes: 14 additions & 3 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import {
forwardRef,
useState,
useCallback,
useEffect,
useMemo,
useRef,
useLayoutEffect,
} from '@wordpress/element';
import { VisuallyHidden, SearchControl, Popover } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useDebouncedInput } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -32,7 +33,6 @@ import InserterSearchResults from './search-results';
import useInsertionPoint from './hooks/use-insertion-point';
import { store as blockEditorStore } from '../../store';
import TabbedSidebar from '../tabbed-sidebar';
import { useZoomOut } from '../../hooks/use-zoom-out';

const NOOP = () => {};
function InserterMenu(
Expand Down Expand Up @@ -149,7 +149,18 @@ function InserterMenu(
const showZoomOut =
showPatternPanel && !! window.__experimentalEnableZoomedOutPatternsTab;

useZoomOut( showZoomOut );
const { __unstableSetEditorMode } = useDispatch( blockEditorStore );
useEffect( () => {
if ( showZoomOut ) {
__unstableSetEditorMode( 'zoom-out' );
}

return () => {
if ( showZoomOut ) {
__unstableSetEditorMode( 'edit' ); // TODO: set back to whatever it was previously
}
};
}, [ showZoomOut ] );
scruffian marked this conversation as resolved.
Show resolved Hide resolved

const inserterSearch = useMemo( () => {
if ( selectedTab === 'media' ) {
Expand Down
3 changes: 1 addition & 2 deletions packages/editor/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ function Header( {
showIconLabels,
hasFixedToolbar,
isNestedEntity,
isZoomedOutView,
} = useSelect( ( select ) => {
const { get: getPreference } = select( preferencesStore );
const {
Expand Down Expand Up @@ -136,7 +135,7 @@ function Header( {
) }
<PreviewDropdown
forceIsAutosaveable={ forceIsDirty }
disabled={ isNestedEntity || isZoomedOutView }
disabled={ isNestedEntity }
/>
<PostPreviewButton
className="editor-header__post-preview-button"
Expand Down
70 changes: 64 additions & 6 deletions packages/editor/src/components/preview-dropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { desktop, mobile, tablet, external } from '@wordpress/icons';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -23,6 +24,9 @@ import { store as editorStore } from '../../store';
import PostPreviewButton from '../post-preview-button';

export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
const isZoomOutExperiment =
!! window.__experimentalEnableZoomedOutPatternsTab;

const { deviceType, homeUrl, isTemplate, isViewable, showIconLabels } =
useSelect( ( select ) => {
const { getDeviceType, getCurrentPostType } = select( editorStore );
Expand All @@ -38,6 +42,7 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
};
}, [] );
const { setDeviceType } = useDispatch( editorStore );
const { __unstableSetEditorMode } = useDispatch( blockEditorStore );
const isMobile = useViewportMatch( 'medium', '<' );
if ( isMobile ) {
return null;
Expand Down Expand Up @@ -69,11 +74,6 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
* @type {Array}
*/
const choices = [
{
value: 'Desktop',
label: __( 'Desktop' ),
icon: desktop,
},
{
value: 'Tablet',
label: __( 'Tablet' ),
Expand All @@ -86,6 +86,25 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
},
];

if ( isZoomOutExperiment ) {
choices.unshift( {
value: 'ZoomOut',
label: __( 'Zoom to 50%' ),
icon: <p>{ __( '50%' ) }</p>,
} );
choices.unshift( {
value: 'ZoomIn',
label: __( 'Zoom to 100%' ),
icon: <p>{ __( '100%' ) }</p>,
} );
} else {
choices.unshift( {
value: 'Desktop',
label: __( 'Desktop' ),
icon: desktop,
} );
}

/**
* The selected choice.
*
Expand All @@ -102,13 +121,52 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
selectedChoice = choices[ 0 ];
}

/**
* Handles the selection of a device type.
*
* @param {string} value The device type.
*/
const onSelect = ( value ) => {
setDeviceType( value );
let editorMode = 'edit'; // Rather than setting to edit, we may need to set back to whatever it was previously.
if ( value === 'Desktop' ) {
speak( __( 'Desktop selected' ), 'assertive' );
} else if ( value === 'Tablet' ) {
speak( __( 'Tablet selected' ), 'assertive' );
} else if ( value === 'Mobile' ) {
speak( __( 'Mobile selected' ), 'assertive' );
} else if ( value === 'ZoomIn' ) {
speak( __( 'Zoom to 100% selected' ), 'assertive' );
} else {
speak( __( 'Zoom to 50% selected' ), 'assertive' );
editorMode = 'zoom-out';
}

if ( isZoomOutExperiment ) {
__unstableSetEditorMode( editorMode );
}
};

const getIcon = () => {
if ( isZoomOutExperiment ) {
if ( deviceType === 'Desktop' || deviceType === 'ZoomIn' ) {
return <>{ __( '100%' ) }</>;
}
if ( deviceType === 'ZoomOut' ) {
return <>{ __( '50%' ) }</>;
}
}

return deviceIcons[ deviceType.toLowerCase() ];
};

return (
<DropdownMenu
className="editor-preview-dropdown"
popoverProps={ popoverProps }
toggleProps={ toggleProps }
menuProps={ menuProps }
icon={ deviceIcons[ deviceType.toLowerCase() ] }
icon={ getIcon() }
label={ __( 'View' ) }
disableOpenOnArrowDown={ disabled }
>
Expand Down
14 changes: 8 additions & 6 deletions packages/editor/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function VisualEditor( {
} ) {
const [ resizeObserver, sizes ] = useResizeObserver();
const isMobileViewport = useViewportMatch( 'small', '<' );
const isTabletViewport = useViewportMatch( 'medium', '<' );
const {
renderingMode,
postContentAttributes,
Expand Down Expand Up @@ -341,12 +342,13 @@ function VisualEditor( {
} ),
] );

const zoomOutProps = isZoomOutMode
? {
scale: 'default',
frameSize: '48px',
}
: {};
const zoomOutProps =
isZoomOutMode && ! isTabletViewport
? {
scale: 'default',
frameSize: '48px',
}
: {};

const forceFullHeight = postType === NAVIGATION_POST_TYPE;
const enableResizing =
Expand Down