Skip to content

Commit

Permalink
Zoom Out: Use the zoom-level value to scale the iframe (#66280)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: draganescu <[email protected]>
Co-authored-by: getdave <[email protected]>
  • Loading branch information
5 people authored Oct 23, 2024
1 parent 8a7a381 commit a02ce46
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
8 changes: 4 additions & 4 deletions packages/block-editor/src/components/block-canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export function ExperimentalBlockCanvas( {
const clearerRef = useBlockSelectionClearer();
const localRef = useRef();
const contentRef = useMergeRefs( [ contentRefProp, clearerRef, localRef ] );
const isZoomedOut = useSelect(
( select ) => unlock( select( blockEditorStore ) ).isZoomOut(),
const zoomLevel = useSelect(
( select ) => unlock( select( blockEditorStore ) ).getZoomLevel(),
[]
);
const zoomOutIframeProps =
isZoomedOut && ! isTabletViewport
zoomLevel !== 100 && ! isTabletViewport
? {
scale: 'default',
scale: zoomLevel,
frameSize: '40px',
}
: {};
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ function Iframe( {
// but calc( 100px / 2px ) is not.
iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-scale',
scale === 'default'
scale === 'auto-scaled'
? ( Math.min( containerWidth, maxWidth ) -
parseInt( frameSize ) * 2 ) /
scaleContainerWidth
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/hooks/use-zoom-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function useZoomOut( zoomOut = true ) {

return () => {
if ( isZoomOutOnMount && isWideViewport ) {
setZoomLevel( 50 );
setZoomLevel( 'auto-scaled' );
} else {
resetZoomLevel();
}
Expand All @@ -37,7 +37,7 @@ export function useZoomOut( zoomOut = true ) {

useEffect( () => {
if ( zoomOut && isWideViewport ) {
setZoomLevel( 50 );
setZoomLevel( 'auto-scaled' );
} else {
resetZoomLevel();
}
Expand Down
12 changes: 11 additions & 1 deletion packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,17 @@ export function getSectionRootClientId( state ) {
* @return {boolean} Whether the editor is zoomed.
*/
export function isZoomOut( state ) {
return state.zoomLevel < 100;
return state.zoomLevel === 'auto-scaled' || state.zoomLevel < 100;
}

/**
* Returns whether the zoom level.
*
* @param {Object} state Global application state.
* @return {number|"auto-scaled"} Zoom level.
*/
export function getZoomLevel( state ) {
return state.zoomLevel;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/zoom-out-toggle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ZoomOutToggle = ( { disabled } ) => {
if ( isZoomOut ) {
resetZoomLevel();
} else {
setZoomLevel( 50 );
setZoomLevel( 'auto-scaled' );
}
};

Expand Down
7 changes: 5 additions & 2 deletions storybook/stories/playground/index.story.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ UndoRedo.parameters = {
sourceLink: 'storybook/stories/playground/with-undo-redo/index.js',
};

export const ZoomOut = () => {
return <EditorZoomOut />;
export const ZoomOut = ( props ) => {
return <EditorZoomOut { ...props } />;
};

ZoomOut.parameters = {
sourceLink: 'storybook/stories/playground/zoom-out/index.js',
};
ZoomOut.argTypes = {
zoomLevel: { control: { type: 'range', min: 10, max: 100, step: 5 } },
};
10 changes: 5 additions & 5 deletions storybook/stories/playground/zoom-out/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ const { unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules(
'@wordpress/edit-site'
);

function EnableZoomOut() {
function EnableZoomOut( { zoomLevel } ) {
const { setZoomLevel } = unlock( useDispatch( blockEditorStore ) );

useEffect( () => {
setZoomLevel( 50 );
}, [ setZoomLevel ] );
setZoomLevel( zoomLevel ? zoomLevel / 100 : 'auto-scaled' );
}, [ setZoomLevel, zoomLevel ] );

return null;
}

export default function EditorZoomOut() {
export default function EditorZoomOut( { zoomLevel } ) {
const [ blocks, updateBlocks ] = useState( [] );

useEffect( () => {
Expand All @@ -57,7 +57,7 @@ export default function EditorZoomOut() {
onInput={ updateBlocks }
onChange={ updateBlocks }
>
<EnableZoomOut />
<EnableZoomOut zoomLevel={ zoomLevel } />
<BlockCanvas height="500px" styles={ editorStyles }>
<style>{ contentCss }</style>
<BlockList />
Expand Down

1 comment on commit a02ce46

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in a02ce46.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11476172745
📝 Reported issues:

Please sign in to comment.