// 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 */ import { useResizeObserver, pure, useRefEffect } from '@wordpress/compose'; import { useMemo, useContext } from '@wordpress/element'; import { Disabled } from '@wordpress/components'; import { __unstableEditorStyles as EditorStyles, __unstableIframe as Iframe, privateApis as blockEditorPrivateApis, BlockList, // @ts-ignore No types for this exist yet. } from '@wordpress/block-editor'; // @ts-ignore No types for this exist yet. import { unlock } from '@wordpress/edit-site/build-module/lock-unlock'; import { noop } from 'lodash'; /** * Internal dependencies */ import { LogoBlockContext } from './logo-block-context'; import { FontFamiliesLoader, FontFamily, } from './sidebar/global-styles/font-pairing-variations/font-families-loader'; import { SYSTEM_FONT_SLUG } from './sidebar/global-styles/font-pairing-variations/constants'; const MAX_HEIGHT = 2000; // @ts-ignore No types for this exist yet. const { Provider: DisabledProvider } = Disabled.Context; // This is used to avoid rendering the block list if the sizes change. let MemoizedBlockList: typeof BlockList | undefined; const { useGlobalSetting } = unlock( blockEditorPrivateApis ); export type ScaledBlockPreviewProps = { viewportWidth?: number; containerWidth: number; minHeight?: number; settings: { styles: string[]; [ key: string ]: unknown; }; additionalStyles: string; onClickNavigationItem: ( event: MouseEvent ) => void; }; function ScaledBlockPreview( { viewportWidth, containerWidth, minHeight, settings, additionalStyles, onClickNavigationItem, }: ScaledBlockPreviewProps ) { const { setLogoBlock } = useContext( LogoBlockContext ); const [ fontFamilies ] = useGlobalSetting( 'typography.fontFamilies.theme' ) as [ FontFamily[] ]; const externalFontFamilies = fontFamilies.filter( ( { slug } ) => slug !== SYSTEM_FONT_SLUG ); if ( ! viewportWidth ) { viewportWidth = containerWidth; } // @ts-ignore No types for this exist yet. const [ contentResizeListener, { height: contentHeight } ] = useResizeObserver(); // Avoid scrollbars for pattern previews. const editorStyles = useMemo( () => { return [ { css: 'body{height:auto;overflow:hidden;border:none;padding:0;}', __unstableType: 'presets', }, ...settings.styles, ]; }, [ settings.styles ] ); // Initialize on render instead of module top level, to avoid circular dependency issues. MemoizedBlockList = MemoizedBlockList || pure( BlockList ); const scale = containerWidth / viewportWidth; return ( ); } export const AutoHeightBlockPreview = ( props: Omit< ScaledBlockPreviewProps, 'containerWidth' > ) => { const [ containerResizeListener, { width: containerWidth } ] = useResizeObserver(); return ( <>
{ containerResizeListener }
{ !! containerWidth && ( ) }
); };