|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { __ } from '@wordpress/i18n'; |
| 5 | +import { useId, useMemo } from '@wordpress/element'; |
| 6 | +// @ts-expect-error Block Editor not fully typed yet. |
| 7 | +import { BlockPreview, BlockEditorProvider } from '@wordpress/block-editor'; |
| 8 | +import { privateApis as editorPrivateApis } from '@wordpress/editor'; |
| 9 | +// @ts-expect-error Blocks not fully typed yet. |
| 10 | +import { parse } from '@wordpress/blocks'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Internal dependencies |
| 14 | + */ |
| 15 | +import './style.scss'; |
| 16 | +import { unlock } from '../../lock-unlock'; |
| 17 | +import { useEditorAssets } from '../../hooks/use-editor-assets'; |
| 18 | +import { useEditorSettings } from '../../hooks/use-editor-settings'; |
| 19 | +import { useStylesId } from '../../hooks/use-styles-id'; |
| 20 | + |
| 21 | +const { useStyle } = unlock( editorPrivateApis ); |
| 22 | + |
| 23 | +function PreviewContent( { |
| 24 | + item, |
| 25 | + description, |
| 26 | +}: { |
| 27 | + item: { blocks?: any[]; content: { raw: string } }; |
| 28 | + description: string; |
| 29 | +} ) { |
| 30 | + const descriptionId = useId(); |
| 31 | + const backgroundColor = useStyle( 'color.background' ); |
| 32 | + const blocks = useMemo( () => { |
| 33 | + return ( |
| 34 | + item.blocks ?? |
| 35 | + parse( item.content.raw, { |
| 36 | + __unstableSkipMigrationLogs: true, |
| 37 | + } ) |
| 38 | + ); |
| 39 | + }, [ item?.content?.raw, item.blocks ] ); |
| 40 | + const isEmpty = ! blocks?.length; |
| 41 | + |
| 42 | + return ( |
| 43 | + <div |
| 44 | + className="lazy-editor-block-preview__container" |
| 45 | + style={ { backgroundColor } } |
| 46 | + aria-describedby={ !! description ? descriptionId : undefined } |
| 47 | + > |
| 48 | + { isEmpty && __( 'Empty template part' ) } |
| 49 | + { ! isEmpty && ( |
| 50 | + <BlockPreview.Async> |
| 51 | + <BlockPreview blocks={ blocks } /> |
| 52 | + </BlockPreview.Async> |
| 53 | + ) } |
| 54 | + { !! description && ( |
| 55 | + <div hidden id={ descriptionId }> |
| 56 | + { description } |
| 57 | + </div> |
| 58 | + ) } |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +export function Preview( { |
| 64 | + item, |
| 65 | + description, |
| 66 | +}: { |
| 67 | + item: { blocks?: any[]; content: { raw: string } }; |
| 68 | + description: string; |
| 69 | +} ) { |
| 70 | + // Resolve styles ID from template |
| 71 | + const stylesId = useStylesId(); |
| 72 | + |
| 73 | + // Load editor settings and assets |
| 74 | + const { isReady: settingsReady, editorSettings } = useEditorSettings( { |
| 75 | + stylesId, |
| 76 | + } ); |
| 77 | + const { isReady: assetsReady } = useEditorAssets(); |
| 78 | + const finalSettings = useMemo( |
| 79 | + () => ( { |
| 80 | + ...editorSettings, |
| 81 | + isPreviewMode: true, |
| 82 | + } ), |
| 83 | + [ editorSettings ] |
| 84 | + ); |
| 85 | + if ( ! settingsReady || ! assetsReady ) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + return ( |
| 89 | + <BlockEditorProvider settings={ finalSettings }> |
| 90 | + <PreviewContent item={ item } description={ description } /> |
| 91 | + </BlockEditorProvider> |
| 92 | + ); |
| 93 | +} |
0 commit comments