@gechiui/block-editor
Version:
107 lines (95 loc) • 2.8 kB
JavaScript
/**
* GeChiUI dependencies
*/
import { Disabled } from '@gechiui/components';
import { useResizeObserver, pure, useRefEffect } from '@gechiui/compose';
import { useSelect } from '@gechiui/data';
import { useMemo } from '@gechiui/element';
/**
* Internal dependencies
*/
import BlockList from '../block-list';
import Iframe from '../iframe';
import EditorStyles from '../editor-styles';
import { store } from '../../store';
// This is used to avoid rendering the block list if the sizes change.
let MemoizedBlockList;
const MAX_HEIGHT = 2000;
function AutoBlockPreview( { viewportWidth, __experimentalPadding } ) {
const [
containerResizeListener,
{ width: containerWidth },
] = useResizeObserver();
const [
contentResizeListener,
{ height: contentHeight },
] = useResizeObserver();
const styles = useSelect( ( select ) => {
return select( store ).getSettings().styles;
}, [] );
// Avoid scrollbars for pattern previews.
const editorStyles = useMemo( () => {
if ( styles ) {
return [
...styles,
{
css: 'body{height:auto;overflow:hidden;}',
__unstableType: 'presets',
},
];
}
return styles;
}, [ styles ] );
// Initialize on render instead of module top level, to avoid circular dependency issues.
MemoizedBlockList = MemoizedBlockList || pure( BlockList );
const scale = containerWidth / viewportWidth;
return (
<div className="block-editor-block-preview__container">
{ containerResizeListener }
<Disabled
className="block-editor-block-preview__content"
style={ {
transform: `scale(${ scale })`,
height: contentHeight * scale,
maxHeight:
contentHeight > MAX_HEIGHT
? MAX_HEIGHT * scale
: undefined,
} }
>
<Iframe
head={ <EditorStyles styles={ editorStyles } /> }
contentRef={ useRefEffect( ( bodyElement ) => {
const {
ownerDocument: { documentElement },
} = bodyElement;
documentElement.classList.add(
'block-editor-block-preview__content-iframe'
);
documentElement.style.position = 'absolute';
documentElement.style.width = '100%';
bodyElement.style.padding =
__experimentalPadding + 'px';
// necessary for contentResizeListener to work.
bodyElement.style.position = 'relative';
}, [] ) }
aria-hidden
tabIndex={ -1 }
style={ {
position: 'absolute',
width: viewportWidth,
height: contentHeight,
pointerEvents: 'none',
// This is a catch-all max-height for patterns.
// See: https://github.com/GeChiUI/gutenberg/pull/38175.
maxHeight: MAX_HEIGHT,
} }
>
{ contentResizeListener }
<MemoizedBlockList renderAppender={ false } />
</Iframe>
</Disabled>
</div>
);
}
export default AutoBlockPreview;