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: keep original viewport width (single scale) #61424

Merged
merged 25 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 20 additions & 1 deletion packages/block-editor/src/components/iframe/content.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
.block-editor-iframe__container {
width: 100%;
height: 100%;
overflow-x: hidden;
}

.block-editor-iframe__scale-container {
width: 100%;
height: 100%;
}

.block-editor-iframe__scale-container.is-zoomed-out {
$container-width: var(--wp-block-editor-iframe-zoom-out-container-width, 100vw);
$prev-container-width: var(--wp-block-editor-iframe-zoom-out-prev-container-width, 100vw);
width: $prev-container-width;
margin-left: calc(-1 * (#{$prev-container-width} - #{$container-width}) / 2);
}

.block-editor-iframe__html {
transform-origin: top center;
transition: transform 0.3s;
Expand All @@ -9,6 +27,7 @@
$frame-size: var(--wp-block-editor-iframe-zoom-out-frame-size);
$inner-height: var(--wp-block-editor-iframe-zoom-out-inner-height);
$content-height: var(--wp-block-editor-iframe-zoom-out-content-height);
$prev-container-width: var(--wp-block-editor-iframe-zoom-out-prev-container-width);

transform: scale(#{$scale});

Expand All @@ -22,7 +41,7 @@
// so we need to adjust the height of the content to match the scale by using negative margins.
$extra-content-height: calc(#{$content-height} * (1 - #{$scale}));
$total-frame-height: calc(2 * #{$frame-size});
$total-height: calc(#{$extra-content-height} + #{$total-frame-height});
$total-height: calc(#{$extra-content-height} + #{$total-frame-height} + 2px);
margin-bottom: calc(-1 * #{$total-height});

body {
Expand Down
93 changes: 76 additions & 17 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
forwardRef,
useMemo,
useEffect,
useRef,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
Expand Down Expand Up @@ -121,13 +122,14 @@ function Iframe( {
}, [] );
const { styles = '', scripts = '' } = resolvedAssets;
const [ iframeDocument, setIframeDocument ] = useState();
const prevContainerWidth = useRef();
const [ bodyClasses, setBodyClasses ] = useState( [] );
const clearerRef = useBlockSelectionClearer();
const [ before, writingFlowRef, after ] = useWritingFlow();
const [
contentResizeListener,
{ height: contentHeight, width: contentWidth },
] = useResizeObserver();
const [ contentResizeListener, { height: contentHeight } ] =
useResizeObserver();
const [ containerResizeListener, { width: containerWidth } ] =
useResizeObserver();

const setRef = useRefEffect( ( node ) => {
node._load = () => {
Expand Down Expand Up @@ -207,9 +209,12 @@ function Iframe( {
};
}, [] );

const windowResizeRef = useRefEffect( ( node ) => {
const [ iframeWindowInnerHeight, setIframeWindowInnerHeight ] = useState();

const iframeResizeRef = useRefEffect( ( node ) => {
const nodeWindow = node.ownerDocument.defaultView;

setIframeWindowInnerHeight( nodeWindow.innerHeight );
const onResize = () => {
setIframeWindowInnerHeight( nodeWindow.innerHeight );
};
Expand All @@ -219,7 +224,28 @@ function Iframe( {
};
}, [] );

const [ iframeWindowInnerHeight, setIframeWindowInnerHeight ] = useState();
const [ windowInnerWidth, setWindowInnerWidth ] = useState();

const windowResizeRef = useRefEffect( ( node ) => {
const nodeWindow = node.ownerDocument.defaultView;

setWindowInnerWidth( nodeWindow.innerWidth );
const onResize = () => {
setWindowInnerWidth( nodeWindow.innerWidth );
};
nodeWindow.addEventListener( 'resize', onResize );
return () => {
nodeWindow.removeEventListener( 'resize', onResize );
};
}, [] );

const isZoomedOut = scale !== 1;

useEffect( () => {
if ( ! isZoomedOut ) {
prevContainerWidth.current = containerWidth;
}
}, [ containerWidth, isZoomedOut ] );

const disabledRef = useDisabled( { isDisabled: ! readonly } );
const bodyRef = useMergeRefs( [
Expand All @@ -231,7 +257,7 @@ function Iframe( {
// Avoid resize listeners when not needed, these will trigger
// unnecessary re-renders when animating the iframe width, or when
// expanding preview iframes.
scale === 1 ? null : windowResizeRef,
isZoomedOut ? iframeResizeRef : null,
] );

// Correct doctype is required to enable rendering in standards
Expand Down Expand Up @@ -272,27 +298,24 @@ function Iframe( {

useEffect( () => cleanup, [ cleanup ] );

scale =
typeof scale === 'function'
? scale( contentWidth, contentHeight )
: scale;

const isZoomedOut = scale !== 1;

useEffect( () => {
if ( ! iframeDocument || ! isZoomedOut ) {
return;
}

iframeDocument.documentElement.classList.add( 'is-zoomed-out' );

const maxWidth = 800;
iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-scale',
`${ scale }`
scale === 'default'
? Math.min( containerWidth, maxWidth ) /
prevContainerWidth.current
: scale
);
iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-frame-size',
`${ frameSize }px`
typeof frameSize === 'number' ? `${ frameSize }px` : frameSize
);
iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-content-height',
Expand All @@ -302,6 +325,14 @@ function Iframe( {
'--wp-block-editor-iframe-zoom-out-inner-height',
`${ iframeWindowInnerHeight }px`
);
iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-container-width',
`${ containerWidth }px`
);
iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-prev-container-width',
`${ prevContainerWidth.current }px`
);

return () => {
iframeDocument.documentElement.classList.remove( 'is-zoomed-out' );
Expand All @@ -318,21 +349,29 @@ function Iframe( {
iframeDocument.documentElement.style.removeProperty(
'--wp-block-editor-iframe-zoom-out-inner-height'
);
iframeDocument.documentElement.style.removeProperty(
'--wp-block-editor-iframe-zoom-out-container-width'
);
iframeDocument.documentElement.style.removeProperty(
'--wp-block-editor-iframe-zoom-out-prev-container-width'
);
};
}, [
scale,
frameSize,
iframeDocument,
iframeWindowInnerHeight,
contentHeight,
containerWidth,
windowInnerWidth,
isZoomedOut,
] );

// Make sure to not render the before and after focusable div elements in view
// mode. They're only needed to capture focus in edit mode.
const shouldRenderFocusCaptureElements = tabIndex >= 0 && ! isPreviewMode;

return (
const iframe = (
<>
{ shouldRenderFocusCaptureElements && before }
{ /* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */ }
Expand Down Expand Up @@ -406,6 +445,26 @@ function Iframe( {
{ shouldRenderFocusCaptureElements && after }
</>
);

return (
<div className="block-editor-iframe__container" ref={ windowResizeRef }>
{ containerResizeListener }
<div
className={ clsx(
'block-editor-iframe__scale-container',
isZoomedOut && 'is-zoomed-out'
) }
style={ {
'--wp-block-editor-iframe-zoom-out-container-width':
isZoomedOut && `${ containerWidth }px`,
'--wp-block-editor-iframe-zoom-out-prev-container-width':
isZoomedOut && `${ prevContainerWidth.current }px`,
} }
>
{ iframe }
</div>
</div>
);
}

function IframeIfReady( props, ref ) {
Expand Down
19 changes: 7 additions & 12 deletions packages/editor/src/components/editor-canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import EditTemplateBlocksNotification from './edit-template-blocks-notification';
import useSelectNearestEditableBlock from '../../hooks/use-select-nearest-editable-block';
import { computeIFrameScale } from './utils';

const {
LayoutStyle,
Expand Down Expand Up @@ -324,15 +323,12 @@ function EditorCanvas( {
} ),
] );

const frameSize = isZoomOutMode ? 20 : undefined;
const scale = isZoomOutMode
? ( contentWidth ) =>
computeIFrameScale(
{ width: 1000, scale: 0.55 },
{ width: 400, scale: 0.9 },
contentWidth
)
: undefined;
const zoomOutProps = isZoomOutMode
? {
scale: 'default',
frameSize: '20px',
}
: {};

return (
<BlockCanvas
Expand All @@ -347,12 +343,11 @@ function EditorCanvas( {
'has-editor-padding': showEditorPadding,
} ),
...iframeProps,
...zoomOutProps,
style: {
...iframeProps?.style,
...deviceStyles,
},
scale,
frameSize,
} }
>
{ themeSupportsLayout &&
Expand Down
93 changes: 0 additions & 93 deletions packages/editor/src/components/editor-canvas/utils.js

This file was deleted.

Loading