/** * External dependencies */ import { BlockInstance, createBlock, registerBlockType, } from '@wordpress/blocks'; import type { BlockEditProps } from '@wordpress/blocks'; import { useBlockProps, BlockPreview, store as blockEditorStore, } from '@wordpress/block-editor'; import { Button, Placeholder, Popover, ExternalLink, TabbableContainer, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { shortcode, 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'; import { findBlock } from '@woocommerce/utils'; /** * Internal dependencies */ import './editor.scss'; import './style.scss'; import { CartPlaceholder, CheckoutPlaceholder } from './placeholder'; import { TEMPLATES, TYPES } from './constants'; import { getTemplateDetailsBySlug } from './utils'; import * as blockifiedCheckout from './checkout'; import * as blockifiedCart from './cart'; import metadata from './block.json'; 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 ConvertTemplate = ( { blockifyConfig, clientId, attributes } ) => { const { getButtonLabel, onClickCallback, getBlockifiedTemplate } = blockifyConfig; const [ isPopoverOpen, setIsPopoverOpen ] = useState( false ); const { replaceBlock, selectBlock } = useDispatch( blockEditorStore ); const { createInfoNotice } = useDispatch( noticesStore ); const { getBlocks } = useSelect( ( sel ) => { return { getBlocks: sel( blockEditorStore ).getBlocks, }; }, [] ); return ( ); }; const Edit = ( { clientId, attributes }: BlockEditProps< Attributes > ) => { const blockProps = useBlockProps(); const templateDetails = getTemplateDetailsBySlug( attributes.shortcode, TEMPLATES ); const templateTitle = attributes.shortcode; const templatePlaceholder = templateDetails?.placeholder ?? 'cart'; const templateType = templateDetails?.type ?? 'fallback'; const { isConversionPossible, getDescription, getTitle, blockifyConfig } = conversionConfig[ templateType ]; const canConvert = isConversionPossible(); const placeholderTitle = getTitle ? getTitle() : __( 'Classic Shortcode Placeholder', 'woocommerce' ); 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.', 'woocommerce' ), { a: ( // Suppress the warning as this will be interpolated into the string with content. // eslint-disable-next-line jsx-a11y/anchor-has-content ), } ); return (
{ templatePlaceholder === 'cart' ? ( ) : ( ) }
{ ' ' } { __( 'WooCommerce', 'woocommerce' ) } { placeholderTitle }

{ learnMoreContent }

{ canConvert && blockifyConfig && ( ) }
); }; const settings = { icon: ( ), edit: ( { attributes, clientId, setAttributes, }: BlockEditProps< Attributes > ) => { return ( ); }, save: () => null, variations: [ { name: 'checkout', title: __( 'Classic Checkout', 'woocommerce' ), attributes: { shortcode: 'checkout', }, isActive: ( blockAttributes, variationAttributes ) => blockAttributes.shortcode === variationAttributes.shortcode, scope: [ 'inserter' ], }, { name: 'cart', title: __( 'Classic Cart', 'woocommerce' ), attributes: { shortcode: 'cart', }, isActive: ( blockAttributes, variationAttributes ) => blockAttributes.shortcode === variationAttributes.shortcode, scope: [ 'inserter' ], isDefault: true, }, ], }; registerBlockType( metadata, settings );