/** * WordPress dependencies. */ import { useBlockProps, MediaPlaceholder, RichText, } from '@wordpress/block-editor'; import { _x } from '@wordpress/i18n'; import type { BlockEditProps } from '@wordpress/blocks'; /** * External dependencies. */ import classnames from 'clsx'; /** * Internal dependencies. */ import './editor.scss'; import { Attributes } from './types'; import { Inspector } from './inspector'; import { Toolbar } from './toolbar'; const EditBlock = ( props: BlockEditProps< Attributes > ): JSX.Element => ( <> { props.isSelected && } { props.isSelected && } ); export default EditBlock; // ======= // HELPERS // ======= const Images = ( props: BlockEditProps< Attributes > ) => { const { attributes: { beforeId, beforeUrl, afterId, afterUrl, isVertical, dividerLocation, caption, }, className, isSelected, setAttributes, } = props; const blockProps = useBlockProps( { className: classnames( className, 'wp-block-nelio-compare-images' ), } ); return (
setAttributes( { beforeId: id, beforeUrl: url, beforeAlt: alt, } ) } /> setAttributes( { afterId: id, afterUrl: url, afterAlt: alt, } ) } /> { !! beforeId && !! afterId && ( ) }
{ !! beforeId && !! afterId && ( setAttributes( { caption: value } ) } /> ) }
); }; type ImageProps = { readonly mode: 'before' | 'after'; readonly isVertical: boolean; readonly imageId?: number; readonly imageUrl?: string; readonly dividerLocation?: number; readonly onChange: ( id: number, url: string, alt: string ) => void; }; const Image = ( { mode, isVertical, dividerLocation, imageId, imageUrl, onChange, }: ImageProps ) => (
{ ! imageId ? ( <> { /* @ts-expect-error onHTMLDrop property is not mandatory */ } onChange( id, url as string, alt as string ) } value={ imageId } labels={ { title: getTitle( mode ), instructions: _x( 'Select an image…', 'text', 'nelio-compare-images' ), } } /> ) : ( { ) }
); const Handler = ( { isVertical, location, }: { isVertical: boolean; location: number; } ) => ( <>
); const Caption = ( { text, isSelected, onChange, }: { isSelected: boolean; text: string; onChange: ( text: string ) => void; } ) => { if ( RichText.isEmpty( text ) && ! isSelected ) { return null; } //end if return ( ); }; const getTitle = ( mode: 'before' | 'after' ) => 'before' === mode ? _x( 'Before Image', 'text', 'nelio-compare-images' ) : _x( 'After Image', 'text', 'nelio-compare-images' );