Skip to content

Commit

Permalink
Zoom Out: Add a control to enter and leave zoom out mode (#63870)
Browse files Browse the repository at this point in the history
* Zoom Out: Add a control to enter and leave zoom out mode

revert changes

Use 100% for desktop and 50% for zoom out

update menu items

Allow users to set zoom out back to 100% when the patterns tab is open

Reset zoom when switching device mode

Add text for zoom

refactor click handler to a shared function

Move in and out of zoom out when resizing below tablet viewport size

* save the editing mode in a ref so we can restore it

* update logic

* Update packages/editor/src/components/preview-dropdown/index.js

Co-authored-by: Alex Lende <[email protected]>

* fix preview selection

* remove the experiment

* remove disconnected change

* make the code more readable

* simplify code

* remove changes from the rebase

* Render a down chevron in the dropdown

* don't run the use effect when the mode changes

* Remove incorrect comment

* Customized spacing for chevron in preview dropdown

* minor tweaks

* Simplify logic for preview selection and keep in line with editorMode

* Allow zoom out inserters if manually select zoom out mode from dropdown

* Use desktop icon with 50% for zoom out

---------

Co-authored-by: Alex Lende <[email protected]>
Co-authored-by: Jerry Jones <[email protected]>
Co-authored-by: Rich Tabor <[email protected]>
Co-authored-by: scruffian <[email protected]>
Co-authored-by: jeryj <[email protected]>
Co-authored-by: richtabor <[email protected]>
Co-authored-by: ajlende <[email protected]>
Co-authored-by: annezazu <[email protected]>
Co-authored-by: jasmussen <[email protected]>
Co-authored-by: MaggieCabrera <[email protected]>
Co-authored-by: stokesman <[email protected]>
Co-authored-by: paaljoachim <[email protected]>
  • Loading branch information
13 people authored Aug 5, 2024
1 parent 86c3225 commit ac9a418
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 48 deletions.
11 changes: 5 additions & 6 deletions packages/block-editor/src/components/block-tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,11 @@ export default function BlockTools( {
name="__unstable-block-tools-after"
ref={ blockToolbarAfterRef }
/>
{ window.__experimentalEnableZoomedOutPatternsTab &&
isZoomOutMode && (
<ZoomOutModeInserters
__unstableContentRef={ __unstableContentRef }
/>
) }
{ isZoomOutMode && (
<ZoomOutModeInserters
__unstableContentRef={ __unstableContentRef }
/>
) }
</InsertionPointOpenRef.Provider>
</div>
);
Expand Down
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;
}
}, [ containerWidth, isZoomedOut ] );
prevContainerWidth.current = containerWidth;
}, [ containerWidth ] );

const disabledRef = useDisabled( { isDisabled: ! readonly } );
const bodyRef = useMergeRefs( [
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/use-zoom-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ export function useZoomOut( zoomOut = true ) {
) {
__unstableSetEditorMode( originalEditingMode.current );
}
}, [ __unstableSetEditorMode, zoomOut, mode ] );
}, [ __unstableGetEditorMode, __unstableSetEditorMode, zoomOut ] ); // Mode is deliberately excluded from the dependencies so that the effect does not run when mode changes.
}
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
108 changes: 79 additions & 29 deletions packages/editor/src/components/preview-dropdown/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
Expand All @@ -14,7 +19,9 @@ import { __ } from '@wordpress/i18n';
import { desktop, mobile, tablet, external } from '@wordpress/icons';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useEffect, useRef } from '@wordpress/element';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as blockEditorStore } from '@wordpress/block-editor';

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

export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
const { deviceType, homeUrl, isTemplate, isViewable, showIconLabels } =
useSelect( ( select ) => {
const { getDeviceType, getCurrentPostType } = select( editorStore );
const { getUnstableBase, getPostType } = select( coreStore );
const { get } = select( preferencesStore );
const _currentPostType = getCurrentPostType();
return {
deviceType: getDeviceType(),
homeUrl: getUnstableBase()?.home,
isTemplate: _currentPostType === 'wp_template',
isViewable: getPostType( _currentPostType )?.viewable ?? false,
showIconLabels: get( 'core', 'showIconLabels' ),
};
}, [] );
const {
deviceType,
editorMode,
homeUrl,
isTemplate,
isViewable,
showIconLabels,
} = useSelect( ( select ) => {
const { getDeviceType, getCurrentPostType } = select( editorStore );
const { getUnstableBase, getPostType } = select( coreStore );
const { get } = select( preferencesStore );
const { __unstableGetEditorMode } = select( blockEditorStore );
const _currentPostType = getCurrentPostType();
return {
deviceType: getDeviceType(),
editorMode: __unstableGetEditorMode(),
homeUrl: getUnstableBase()?.home,
isTemplate: _currentPostType === 'wp_template',
isViewable: getPostType( _currentPostType )?.viewable ?? false,
showIconLabels: get( 'core', 'showIconLabels' ),
};
}, [] );
const { setDeviceType } = useDispatch( editorStore );
const { __unstableSetEditorMode } = useDispatch( blockEditorStore );

/**
* Save the original editing mode in a ref to restore it when we exit zoom out.
*/
const originalEditingMode = useRef( editorMode );
useEffect( () => {
if ( editorMode !== 'zoom-out' ) {
originalEditingMode.current = editorMode;
}

return () => {
if (
editorMode === 'zoom-out' &&
editorMode !== originalEditingMode.current
) {
__unstableSetEditorMode( originalEditingMode.current );
}
};
}, [ editorMode, __unstableSetEditorMode ] );

const isMobile = useViewportMatch( 'medium', '<' );
if ( isMobile ) {
return null;
Expand All @@ -48,6 +84,7 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
};
const toggleProps = {
className: 'editor-preview-dropdown__toggle',
iconPosition: 'right',
size: 'compact',
showTooltip: ! showIconLabels,
disabled,
Expand All @@ -58,9 +95,9 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
};

const deviceIcons = {
desktop,
mobile,
tablet,
desktop,
};

/**
Expand All @@ -74,6 +111,11 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
label: __( 'Desktop' ),
icon: desktop,
},
{
value: 'ZoomOut',
label: __( 'Desktop (50%)' ),
icon: desktop,
},
{
value: 'Tablet',
label: __( 'Tablet' ),
Expand All @@ -86,29 +128,37 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
},
];

const previewValue = editorMode === 'zoom-out' ? 'ZoomOut' : deviceType;

/**
* The selected choice.
* Handles the selection of a device type.
*
* @type {Object}
* @param {string} value The device type.
*/
let selectedChoice = choices.find(
( choice ) => choice.value === deviceType
);
const onSelect = ( value ) => {
let newEditorMode = originalEditingMode.current;

/**
* If no selected choice is found, default to the first
*/
if ( ! selectedChoice ) {
selectedChoice = choices[ 0 ];
}
if ( value === 'ZoomOut' ) {
newEditorMode = 'zoom-out';
setDeviceType( 'Desktop' );
} else {
setDeviceType( value );
}

__unstableSetEditorMode( newEditorMode );
};

return (
<DropdownMenu
className="editor-preview-dropdown"
className={ clsx(
'editor-preview-dropdown',
`editor-preview-dropdown--${ deviceType.toLowerCase() }`
) }
popoverProps={ popoverProps }
toggleProps={ toggleProps }
menuProps={ menuProps }
icon={ deviceIcons[ deviceType.toLowerCase() ] }
text={ editorMode === 'zoom-out' ? __( '50%' ) : undefined }
label={ __( 'View' ) }
disableOpenOnArrowDown={ disabled }
>
Expand All @@ -117,8 +167,8 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
<MenuGroup>
<MenuItemsChoice
choices={ choices }
value={ selectedChoice.value }
onSelect={ setDeviceType }
value={ previewValue }
onSelect={ onSelect }
/>
</MenuGroup>
{ isTemplate && (
Expand Down
5 changes: 5 additions & 0 deletions packages/editor/src/components/preview-dropdown/style.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.editor-preview-dropdown .editor-preview-dropdown__toggle.has-icon.has-text {
padding-right: 4px;
padding-left: 6px;
}

.editor-preview-dropdown__button-external {
width: 100%;
display: flex;
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

0 comments on commit ac9a418

Please sign in to comment.