2023-08-28 01:28:05 +00:00
|
|
|
// Reference: https://github.com/WordPress/gutenberg/blob/release/16.4/packages/block-editor/src/components/block-preview/auto.js
|
|
|
|
|
|
|
|
/* eslint-disable @woocommerce/dependency-group */
|
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-06-05 12:27:35 +00:00
|
|
|
import { useResizeObserver, pure } from '@wordpress/compose';
|
2024-06-20 11:26:06 +00:00
|
|
|
import { useContext, useEffect, useMemo, useState } from '@wordpress/element';
|
2024-06-05 12:27:35 +00:00
|
|
|
import { Disabled, Popover } from '@wordpress/components';
|
2023-08-28 01:28:05 +00:00
|
|
|
import {
|
|
|
|
__unstableEditorStyles as EditorStyles,
|
|
|
|
__unstableIframe as Iframe,
|
2023-09-13 08:01:28 +00:00
|
|
|
privateApis as blockEditorPrivateApis,
|
2023-08-28 01:28:05 +00:00
|
|
|
BlockList,
|
2024-05-28 08:16:25 +00:00
|
|
|
store as blockEditorStore,
|
2023-08-28 01:28:05 +00:00
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
} from '@wordpress/block-editor';
|
2023-09-13 08:01:28 +00:00
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { unlock } from '@wordpress/edit-site/build-module/lock-unlock';
|
2023-08-28 01:28:05 +00:00
|
|
|
|
2023-09-05 06:21:19 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { LogoBlockContext } from './logo-block-context';
|
2023-09-13 08:01:28 +00:00
|
|
|
import { SYSTEM_FONT_SLUG } from './sidebar/global-styles/font-pairing-variations/constants';
|
2023-11-15 01:20:56 +00:00
|
|
|
import { PreloadFonts } from './preload-fonts';
|
2024-02-01 17:16:12 +00:00
|
|
|
import { FontFamily } from '../types/font';
|
|
|
|
import { FontFamiliesLoaderDotCom } from './sidebar/global-styles/font-pairing-variations/font-families-loader-dot-com';
|
|
|
|
import { CustomizeStoreContext } from '.';
|
|
|
|
import { isAIFlow } from '../guards';
|
2024-05-28 08:16:25 +00:00
|
|
|
import { selectBlockOnHover } from './utils/select-block-on-hover';
|
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2024-06-05 12:27:35 +00:00
|
|
|
import { PopoverStatus, usePopoverHandler } from './hooks/use-popover-handler';
|
|
|
|
import { noop } from 'lodash';
|
|
|
|
import { useAddAutoBlockPreviewEventListenersAndObservers } from './hooks/auto-block-preview-event-listener';
|
|
|
|
import { IsResizingContext } from './resizable-frame';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2024-06-13 07:37:34 +00:00
|
|
|
import { useQuery } from '@woocommerce/navigation';
|
2024-06-14 10:43:07 +00:00
|
|
|
import clsx from 'clsx';
|
2024-06-20 11:26:06 +00:00
|
|
|
import { SelectedBlockContext } from './context/selected-block-ref-context';
|
2023-09-05 06:21:19 +00:00
|
|
|
|
2023-08-28 01:28:05 +00:00
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
const { Provider: DisabledProvider } = Disabled.Context;
|
|
|
|
|
2023-08-30 05:38:20 +00:00
|
|
|
// This is used to avoid rendering the block list if the sizes change.
|
|
|
|
let MemoizedBlockList: typeof BlockList | undefined;
|
|
|
|
|
2023-09-13 08:01:28 +00:00
|
|
|
const { useGlobalSetting } = unlock( blockEditorPrivateApis );
|
2023-11-01 11:03:04 +00:00
|
|
|
const MAX_HEIGHT = 2000;
|
2023-09-13 08:01:28 +00:00
|
|
|
|
2023-08-28 01:28:05 +00:00
|
|
|
export type ScaledBlockPreviewProps = {
|
|
|
|
viewportWidth?: number;
|
|
|
|
containerWidth: number;
|
|
|
|
minHeight?: number;
|
|
|
|
settings: {
|
|
|
|
styles: string[];
|
|
|
|
[ key: string ]: unknown;
|
|
|
|
};
|
|
|
|
additionalStyles: string;
|
2023-10-05 12:36:08 +00:00
|
|
|
isScrollable?: boolean;
|
2023-11-01 11:03:04 +00:00
|
|
|
autoScale?: boolean;
|
|
|
|
setLogoBlockContext?: boolean;
|
2023-11-15 13:06:05 +00:00
|
|
|
CustomIframeComponent?: React.ComponentType<
|
|
|
|
Parameters< typeof Iframe >[ 0 ]
|
|
|
|
>;
|
2024-06-05 12:27:35 +00:00
|
|
|
isPatternPreview: boolean;
|
2023-08-28 01:28:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function ScaledBlockPreview( {
|
|
|
|
viewportWidth,
|
|
|
|
containerWidth,
|
|
|
|
settings,
|
|
|
|
additionalStyles,
|
2023-10-05 12:36:08 +00:00
|
|
|
isScrollable = true,
|
2023-11-01 11:03:04 +00:00
|
|
|
autoScale = true,
|
2024-06-05 12:27:35 +00:00
|
|
|
isPatternPreview,
|
2023-11-15 13:06:05 +00:00
|
|
|
CustomIframeComponent = Iframe,
|
2023-08-28 01:28:05 +00:00
|
|
|
}: ScaledBlockPreviewProps ) {
|
2023-11-01 11:03:04 +00:00
|
|
|
const [ contentHeight, setContentHeight ] = useState< number | null >(
|
|
|
|
null
|
|
|
|
);
|
2024-06-05 12:27:35 +00:00
|
|
|
const { setLogoBlockIds, logoBlockIds } = useContext( LogoBlockContext );
|
2023-09-13 08:01:28 +00:00
|
|
|
const [ fontFamilies ] = useGlobalSetting(
|
|
|
|
'typography.fontFamilies.theme'
|
|
|
|
) as [ FontFamily[] ];
|
|
|
|
const externalFontFamilies = fontFamilies.filter(
|
|
|
|
( { slug } ) => slug !== SYSTEM_FONT_SLUG
|
|
|
|
);
|
2023-09-05 06:21:19 +00:00
|
|
|
|
2024-02-01 17:16:12 +00:00
|
|
|
const { context } = useContext( CustomizeStoreContext );
|
|
|
|
|
2023-08-28 01:28:05 +00:00
|
|
|
if ( ! viewportWidth ) {
|
|
|
|
viewportWidth = containerWidth;
|
|
|
|
}
|
|
|
|
|
2024-06-05 12:27:35 +00:00
|
|
|
const [ iframeRef, setIframeRef ] = useState< HTMLElement | null >( null );
|
|
|
|
|
|
|
|
const [
|
|
|
|
popoverStatus,
|
|
|
|
virtualElement,
|
|
|
|
updatePopoverPosition,
|
2024-06-12 09:18:06 +00:00
|
|
|
hidePopover,
|
2024-06-05 12:27:35 +00:00
|
|
|
] = usePopoverHandler();
|
|
|
|
|
2024-05-28 08:16:25 +00:00
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
const { selectBlock, setBlockEditingMode } =
|
|
|
|
useDispatch( blockEditorStore );
|
|
|
|
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
const { getBlockParents } = useSelect( blockEditorStore );
|
|
|
|
|
2024-06-20 11:26:06 +00:00
|
|
|
const { setSelectedBlockRef } = useContext( SelectedBlockContext );
|
|
|
|
|
|
|
|
const selectedBlockClientId = useSelect( ( select ) => {
|
|
|
|
const block = select( 'core/block-editor' ).getSelectedBlock();
|
|
|
|
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
return block?.clientId;
|
|
|
|
} );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( selectedBlockClientId && iframeRef ) {
|
|
|
|
const el = iframeRef.querySelector(
|
|
|
|
`#block-${ selectedBlockClientId }`
|
|
|
|
) as HTMLElement;
|
|
|
|
|
|
|
|
if ( ! el ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const observer = new MutationObserver( () => {
|
|
|
|
setSelectedBlockRef( el );
|
|
|
|
} );
|
|
|
|
|
|
|
|
observer.observe( el, {
|
|
|
|
attributes: true,
|
|
|
|
} );
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
observer.disconnect();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, [ iframeRef, selectedBlockClientId, setSelectedBlockRef ] );
|
|
|
|
|
2023-11-01 11:03:04 +00:00
|
|
|
// Avoid scrollbars for pattern previews.
|
|
|
|
const editorStyles = useMemo( () => {
|
|
|
|
if ( ! isScrollable && settings.styles ) {
|
|
|
|
return [
|
|
|
|
...settings.styles,
|
|
|
|
{
|
|
|
|
css: 'body{height:auto;overflow:hidden;border:none;padding:0;}',
|
|
|
|
__unstableType: 'presets',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings.styles;
|
|
|
|
}, [ settings.styles, isScrollable ] );
|
|
|
|
|
|
|
|
const scale = containerWidth / viewportWidth;
|
|
|
|
const aspectRatio = contentHeight
|
|
|
|
? containerWidth / ( contentHeight * scale )
|
|
|
|
: 0;
|
2024-06-05 12:27:35 +00:00
|
|
|
|
2023-08-28 01:28:05 +00:00
|
|
|
// Initialize on render instead of module top level, to avoid circular dependency issues.
|
2023-08-30 05:38:20 +00:00
|
|
|
MemoizedBlockList = MemoizedBlockList || pure( BlockList );
|
2023-08-28 01:28:05 +00:00
|
|
|
|
2024-06-05 12:27:35 +00:00
|
|
|
const isResizing = useContext( IsResizingContext );
|
2024-06-13 07:37:34 +00:00
|
|
|
const query = useQuery();
|
2024-06-05 12:27:35 +00:00
|
|
|
|
|
|
|
useAddAutoBlockPreviewEventListenersAndObservers(
|
|
|
|
{
|
|
|
|
documentElement: iframeRef,
|
|
|
|
autoScale,
|
|
|
|
isPatternPreview,
|
|
|
|
contentHeight,
|
|
|
|
logoBlockIds,
|
2024-06-13 07:37:34 +00:00
|
|
|
query,
|
2024-06-05 12:27:35 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-12 09:18:06 +00:00
|
|
|
hidePopover,
|
2024-06-05 12:27:35 +00:00
|
|
|
selectBlockOnHover,
|
|
|
|
selectBlock,
|
|
|
|
getBlockParents,
|
|
|
|
setBlockEditingMode,
|
|
|
|
updatePopoverPosition,
|
|
|
|
setLogoBlockIds,
|
|
|
|
setContentHeight,
|
2024-05-28 08:16:25 +00:00
|
|
|
}
|
2024-06-05 12:27:35 +00:00
|
|
|
);
|
2023-11-01 11:03:04 +00:00
|
|
|
|
2023-08-28 01:28:05 +00:00
|
|
|
return (
|
2024-06-05 12:27:35 +00:00
|
|
|
<>
|
|
|
|
{ ! isPatternPreview &&
|
|
|
|
virtualElement &&
|
|
|
|
popoverStatus === PopoverStatus.VISIBLE &&
|
|
|
|
! isResizing && (
|
|
|
|
<Popover
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
anchor={ virtualElement }
|
|
|
|
as="div"
|
|
|
|
variant="unstyled"
|
|
|
|
className="components-tooltip woocommerce-customize-store_popover-tooltip"
|
|
|
|
>
|
|
|
|
<span>
|
|
|
|
{ __(
|
|
|
|
'You can edit your content later in the Editor',
|
|
|
|
'woocommerce'
|
|
|
|
) }
|
|
|
|
</span>
|
|
|
|
</Popover>
|
|
|
|
) }
|
|
|
|
<DisabledProvider value={ true }>
|
|
|
|
<div
|
2024-06-14 10:43:07 +00:00
|
|
|
className={ clsx( 'block-editor-block-preview__content', {
|
|
|
|
'woocommerce-customize-store-assembler':
|
|
|
|
! isPatternPreview,
|
|
|
|
} ) }
|
2023-11-01 11:03:04 +00:00
|
|
|
style={
|
|
|
|
autoScale
|
|
|
|
? {
|
2024-06-05 12:27:35 +00:00
|
|
|
transform: `scale(${ scale })`,
|
|
|
|
// Using width + aspect-ratio instead of height here triggers browsers' native
|
|
|
|
// handling of scrollbar's visibility. It prevents the flickering issue seen
|
|
|
|
// in https://github.com/WordPress/gutenberg/issues/52027.
|
|
|
|
// See https://github.com/WordPress/gutenberg/pull/52921 for more info.
|
|
|
|
aspectRatio,
|
|
|
|
maxHeight:
|
|
|
|
contentHeight !== null &&
|
|
|
|
contentHeight > MAX_HEIGHT
|
|
|
|
? MAX_HEIGHT * scale
|
|
|
|
: undefined,
|
2023-11-01 11:03:04 +00:00
|
|
|
}
|
|
|
|
: {}
|
|
|
|
}
|
|
|
|
>
|
2024-06-05 12:27:35 +00:00
|
|
|
<CustomIframeComponent
|
|
|
|
aria-hidden
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore disabled prop exists
|
|
|
|
scrolling={ isScrollable ? 'yes' : 'no' }
|
|
|
|
tabIndex={ -1 }
|
|
|
|
readonly={ false }
|
|
|
|
style={
|
|
|
|
autoScale
|
|
|
|
? {
|
|
|
|
position: 'absolute',
|
|
|
|
width: viewportWidth,
|
|
|
|
pointerEvents: 'none',
|
|
|
|
height: contentHeight,
|
|
|
|
// This is a catch-all max-height for patterns.
|
|
|
|
// See: https://github.com/WordPress/gutenberg/pull/38175.
|
|
|
|
maxHeight: MAX_HEIGHT,
|
|
|
|
}
|
|
|
|
: {}
|
|
|
|
}
|
|
|
|
contentRef={ ( bodyElement: HTMLElement ) => {
|
|
|
|
if ( ! bodyElement || iframeRef !== null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const documentElement =
|
|
|
|
bodyElement.ownerDocument.documentElement;
|
|
|
|
|
|
|
|
setIframeRef( documentElement );
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<EditorStyles styles={ editorStyles } />
|
|
|
|
<style>
|
|
|
|
{ `
|
2023-08-28 01:28:05 +00:00
|
|
|
.block-editor-block-list__block::before,
|
2024-05-28 08:16:25 +00:00
|
|
|
.has-child-selected > .is-selected::after,
|
|
|
|
.is-hovered:not(.is-selected.is-hovered)::after,
|
2023-08-28 01:28:05 +00:00
|
|
|
.block-list-appender {
|
|
|
|
display: none !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
.block-editor-block-list__block.is-selected {
|
|
|
|
box-shadow: none !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
.block-editor-rich-text__editable {
|
|
|
|
pointer-events: none !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
.wp-block-site-title .block-editor-rich-text__editable {
|
|
|
|
pointer-events: all !important;
|
|
|
|
}
|
|
|
|
|
2023-09-18 09:29:29 +00:00
|
|
|
.wp-block-navigation-item .wp-block-navigation-item__content,
|
2023-08-28 01:28:05 +00:00
|
|
|
.wp-block-navigation .wp-block-pages-list__item__link {
|
|
|
|
pointer-events: all !important;
|
|
|
|
cursor: pointer !important;
|
|
|
|
}
|
|
|
|
|
2024-06-21 13:18:54 +00:00
|
|
|
.components-resizable-box__handle {
|
|
|
|
display: none !important;
|
|
|
|
}
|
|
|
|
|
2023-08-28 01:28:05 +00:00
|
|
|
${ additionalStyles }
|
|
|
|
` }
|
2024-06-05 12:27:35 +00:00
|
|
|
</style>
|
|
|
|
<MemoizedBlockList renderAppender={ false } />
|
|
|
|
<PreloadFonts />
|
|
|
|
{ isAIFlow( context.flowType ) && (
|
|
|
|
<FontFamiliesLoaderDotCom
|
|
|
|
fontFamilies={ externalFontFamilies }
|
|
|
|
onLoad={ noop }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</CustomIframeComponent>
|
|
|
|
</div>
|
|
|
|
</DisabledProvider>
|
|
|
|
</>
|
2023-08-28 01:28:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const AutoHeightBlockPreview = (
|
|
|
|
props: Omit< ScaledBlockPreviewProps, 'containerWidth' >
|
|
|
|
) => {
|
|
|
|
const [ containerResizeListener, { width: containerWidth } ] =
|
|
|
|
useResizeObserver();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div style={ { position: 'relative', width: '100%', height: 0 } }>
|
|
|
|
{ containerResizeListener }
|
|
|
|
</div>
|
|
|
|
<div className="auto-block-preview__container">
|
|
|
|
{ !! containerWidth && (
|
|
|
|
<ScaledBlockPreview
|
|
|
|
{ ...props }
|
|
|
|
containerWidth={ containerWidth }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|