/** * External dependencies */ import { BlockInstance, createBlock, registerBlockType, } from '@wordpress/blocks'; import type { BlockEditProps } from '@wordpress/blocks'; import { WC_BLOCKS_IMAGE_URL } from '@woocommerce/block-settings'; import { useBlockProps, BlockPreview, store as blockEditorStore, } from '@wordpress/block-editor'; import { Button, Placeholder, Popover, ExternalLink, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { box, Icon } from '@wordpress/icons'; import { useDispatch, useSelect } from '@wordpress/data'; import { useState, createInterpolateElement } from '@wordpress/element'; import { store as noticesStore } from '@wordpress/notices'; import { woo } from '@woocommerce/icons'; /** * Internal dependencies */ import './editor.scss'; import './style.scss'; import { TEMPLATES, TYPES } from './constants'; import { getTemplateDetailsBySlug } from './utils'; import * as blockifiedCheckout from './checkout'; import * as blockifiedCart from './cart'; import type { BlockifiedTemplateConfig } from './types'; type Attributes = { shortcode: string; align: string; }; const blockifiedFallbackConfig = { isConversionPossible: () => false, getBlockifiedTemplate: () => [], getDescription: () => '', onClickCallback: () => void 0, }; const conversionConfig: { [ key: string ]: BlockifiedTemplateConfig } = { [ TYPES.cart ]: blockifiedCart, [ TYPES.checkout ]: blockifiedCheckout, fallback: blockifiedFallbackConfig, }; const pickBlockClientIds = ( blocks: Array< BlockInstance > ) => blocks.reduce< Array< string > >( ( acc, block ) => { if ( block.name === 'core/template-part' ) { return acc; } return [ ...acc, block.clientId ]; }, [] ); const ConvertTemplate = ( { blockifyConfig, clientId, attributes } ) => { const { getButtonLabel, onClickCallback, getBlockifiedTemplate } = blockifyConfig; const [ isPopoverOpen, setIsPopoverOpen ] = useState( false ); const { replaceBlock, selectBlock, replaceBlocks } = useDispatch( blockEditorStore ); const { getBlocks } = useSelect( ( sel ) => { return { getBlocks: sel( blockEditorStore ).getBlocks, }; }, [] ); const { createInfoNotice } = useDispatch( noticesStore ); return (
); }; const Edit = ( { clientId, attributes }: BlockEditProps< Attributes > ) => { const blockProps = useBlockProps(); const templateDetails = getTemplateDetailsBySlug( attributes.shortcode, TEMPLATES ); const templateTitle = attributes.shortcode; const templatePlaceholder = templateDetails?.placeholder ?? 'fallback'; const templateType = templateDetails?.type ?? 'fallback'; const { isConversionPossible, getDescription, getTitle, blockifyConfig } = conversionConfig[ templateType ]; const canConvert = isConversionPossible(); const placeholderTitle = getTitle ? getTitle() : __( 'Classic Shortcode Placeholder', 'woo-gutenberg-products-block' ); const placeholderDescription = getDescription( templateTitle, canConvert ); const learnMoreContent = createInterpolateElement( __( 'You can learn more about the benefits of switching to blocks, compatibility with extensions, and how to switch back to shortcodes in our documentation.', 'woo-gutenberg-products-block' ), { a: ( // Suppress the warning as this will be interpolated into the string with content. // eslint-disable-next-line jsx-a11y/anchor-has-content ), } ); return (
{
{ ' ' } { __( 'WooCommerce', 'woo-gutenberg-products-block' ) } { placeholderTitle }

{ learnMoreContent }

{ canConvert && blockifyConfig && ( ) }
); }; registerBlockType( 'woocommerce/classic-shortcode', { title: __( 'Classic Shortcode', 'woo-gutenberg-products-block' ), icon: ( ), category: 'woocommerce', apiVersion: 2, keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ], description: __( 'Renders classic WooCommerce shortcodes.', 'woo-gutenberg-products-block' ), supports: { align: true, html: false, multiple: false, reusable: false, inserter: false, }, attributes: { /** * Shortcode attribute is used to determine which shortcode gets rendered. */ shortcode: { type: 'string', default: 'any', }, align: { type: 'string', default: 'wide', }, }, edit: ( { attributes, clientId, setAttributes, }: BlockEditProps< Attributes > ) => { return ( ); }, save: () => null, } );