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' ;
2024-03-20 13:28:56 +00:00
import { memo } from '@wordpress/element' ;
2023-08-28 01:28:05 +00:00
import { BlockInstance } from '@wordpress/blocks' ;
2024-05-28 06:55:14 +00:00
2023-08-28 01:28:05 +00:00
/ * *
* Internal dependencies
* /
import {
AutoHeightBlockPreview ,
ScaledBlockPreviewProps ,
} from './auto-block-preview' ;
2023-10-26 10:15:30 +00:00
import { ChangeHandler } from './hooks/use-editor-blocks' ;
2024-05-28 06:55:14 +00:00
import { Toolbar } from './toolbar/toolbar' ;
2024-05-28 08:40:16 +00:00
import { isFullComposabilityFeatureAndAPIAvailable } from './utils/is-full-composability-enabled' ;
2023-08-28 01:28:05 +00:00
export const BlockPreview = ( {
blocks ,
settings ,
2023-09-05 06:21:19 +00:00
useSubRegistry = true ,
2023-10-26 10:15:30 +00:00
onChange ,
2023-08-28 01:28:05 +00:00
. . . props
} : {
blocks : BlockInstance | BlockInstance [ ] ;
settings : Record < string , unknown > ;
2023-10-26 10:15:30 +00:00
onChange? : ChangeHandler | undefined ;
2023-09-05 06:21:19 +00:00
useSubRegistry? : boolean ;
2023-08-28 01:28:05 +00:00
} & Omit < ScaledBlockPreviewProps , 'containerWidth' > ) = > {
2024-03-20 13:28:56 +00:00
const renderedBlocks = Array . isArray ( blocks ) ? blocks : [ blocks ] ;
2023-09-14 08:24:46 +00:00
2023-08-28 01:28:05 +00:00
return (
2024-05-28 06:55:14 +00:00
< >
< BlockEditorProvider
value = { renderedBlocks }
settings = { settings }
// We need to set onChange for logo to work, but we don't want to trigger the onChange callback when highlighting blocks in the preview. It would persist the highlighted block and cause the opacity to be applied to block permanently.
onChange = { onChange }
useSubRegistry = { useSubRegistry }
>
2024-05-28 08:40:16 +00:00
{ isFullComposabilityFeatureAndAPIAvailable ( ) && < Toolbar / > }
2024-05-28 06:55:14 +00:00
< AutoHeightBlockPreview settings = { settings } { ...props } / >
< / BlockEditorProvider >
< / >
2023-08-28 01:28:05 +00:00
) ;
} ;
export default memo ( BlockPreview ) ;