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/index.js
|
|
|
|
|
|
|
|
/* eslint-disable @woocommerce/dependency-group */
|
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { BlockEditorProvider } from '@wordpress/block-editor';
|
2023-09-14 08:24:46 +00:00
|
|
|
import { memo, useContext, useMemo } from '@wordpress/element';
|
2023-08-28 01:28:05 +00:00
|
|
|
import { BlockInstance } from '@wordpress/blocks';
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
AutoHeightBlockPreview,
|
|
|
|
ScaledBlockPreviewProps,
|
|
|
|
} from './auto-block-preview';
|
2023-09-14 08:24:46 +00:00
|
|
|
import { HighlightedBlockContext } from './context/highlighted-block-context';
|
2023-08-28 01:28:05 +00:00
|
|
|
|
|
|
|
export const BlockPreview = ( {
|
|
|
|
blocks,
|
|
|
|
settings,
|
2023-09-05 06:21:19 +00:00
|
|
|
useSubRegistry = true,
|
2023-09-14 08:24:46 +00:00
|
|
|
additionalStyles,
|
2023-09-18 09:29:29 +00:00
|
|
|
previewOpacity = 0.5,
|
2023-08-28 01:28:05 +00:00
|
|
|
...props
|
|
|
|
}: {
|
|
|
|
blocks: BlockInstance | BlockInstance[];
|
|
|
|
settings: Record< string, unknown >;
|
2023-09-05 06:21:19 +00:00
|
|
|
useSubRegistry?: boolean;
|
2023-09-18 09:29:29 +00:00
|
|
|
previewOpacity?: number;
|
2023-08-28 01:28:05 +00:00
|
|
|
} & Omit< ScaledBlockPreviewProps, 'containerWidth' > ) => {
|
2023-09-14 08:24:46 +00:00
|
|
|
const { highlightedBlockIndex } = useContext( HighlightedBlockContext );
|
2023-09-18 09:29:29 +00:00
|
|
|
const renderedBlocks = useMemo( () => {
|
|
|
|
const _blocks = Array.isArray( blocks ) ? blocks : [ blocks ];
|
|
|
|
|
|
|
|
return _blocks.map( ( block, i ) => {
|
|
|
|
if ( i === highlightedBlockIndex ) {
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...block,
|
|
|
|
attributes: {
|
|
|
|
...block.attributes,
|
|
|
|
className: block.attributes.className + ' preview-opacity',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
}, [ blocks, highlightedBlockIndex ] );
|
2023-09-14 08:24:46 +00:00
|
|
|
|
|
|
|
const opacityStyles =
|
|
|
|
highlightedBlockIndex === -1
|
|
|
|
? ''
|
|
|
|
: `
|
|
|
|
.wp-block.preview-opacity {
|
|
|
|
opacity: ${ previewOpacity };
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2023-08-28 01:28:05 +00:00
|
|
|
return (
|
2023-09-05 06:21:19 +00:00
|
|
|
<BlockEditorProvider
|
2023-09-18 09:29:29 +00:00
|
|
|
value={ renderedBlocks }
|
2023-09-05 06:21:19 +00:00
|
|
|
settings={ settings }
|
|
|
|
useSubRegistry={ useSubRegistry }
|
|
|
|
>
|
2023-09-14 08:24:46 +00:00
|
|
|
<AutoHeightBlockPreview
|
|
|
|
settings={ settings }
|
|
|
|
additionalStyles={ `${ opacityStyles } ${ additionalStyles }` }
|
|
|
|
{ ...props }
|
|
|
|
/>
|
2023-08-28 01:28:05 +00:00
|
|
|
</BlockEditorProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default memo( BlockPreview );
|