Skip to content

Commit

Permalink
Zoom Out: Use the zoom-level value to scale the iframe (WordPress#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 and karthick-murugan committed Nov 13, 2024
1 parent f2207fb commit 2f1cd93
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

0 comments on commit 2f1cd93

Please sign in to comment.