/** * WordPress dependencies */ import { createElement } from '@wordpress/element' import { __ } from 'ct-i18n' import { SelectControl, __experimentalUnitControl as UnitControl, __experimentalToolsPanel as ToolsPanel, __experimentalUseCustomUnits as useCustomUnits, __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components' import { useSetting } from '@wordpress/block-editor' import { useSelect } from '@wordpress/data' import { store as blockEditorStore } from '@wordpress/block-editor' const DEFAULT_SIZE = 'full' const DimensionControls = ({ clientId, attributes: { aspectRatio, width, height, sizeSlug }, setAttributes, }) => { const imageSizes = useSelect( (select) => select(blockEditorStore).getSettings().imageSizes, [] ) const imageSizeOptions = imageSizes.map(({ name, slug }) => ({ value: slug, label: name, })) const defaultUnits = ['px', '%', 'vw', 'em', 'rem'] const units = useCustomUnits({ availableUnits: useSetting('spacing.units') || defaultUnits, }) const onDimensionChange = (dimension, nextValue) => { const parsedValue = parseFloat(nextValue) /** * If we have no value set and we change the unit, * we don't want to set the attribute, as it would * end up having the unit as value without any number. */ if (isNaN(parsedValue) && nextValue) return setAttributes({ [dimension]: parsedValue < 0 ? '0' : nextValue, }) } return ( { setAttributes({ aspectRatio: 'auto', width: undefined, height: undefined, sizeSlug: undefined, }) }}> !!aspectRatio} label={__('Aspect Ratio', 'blocksy')} onDeselect={() => setAttributes({ aspectRatio: undefined })} resetAllFilter={() => ({ aspectRatio: 'auto', })} isShownByDefault key={clientId}> setAttributes({ aspectRatio: nextAspectRatio }) } /> !!width} label={__('Width', 'blocksy')} onDeselect={() => setAttributes({ width: undefined })} resetAllFilter={() => ({ width: undefined, })} isShownByDefault key={clientId}> onDimensionChange('width', nextWidth) } units={units} /> !!height} label={__('Height', 'blocksy')} onDeselect={() => setAttributes({ height: undefined })} resetAllFilter={() => ({ height: undefined, })} isShownByDefault key={clientId}> onDimensionChange('height', nextHeight) } units={units} /> {!!imageSizeOptions.length && ( !!sizeSlug} label={__('Resolution', 'blocksy')} onDeselect={() => setAttributes({ sizeSlug: undefined })} resetAllFilter={() => ({ sizeSlug: undefined, })} isShownByDefault={false} key={clientId}> setAttributes({ sizeSlug: nextSizeSlug }) } help={__( 'Select the size of the source image.', 'blocksy' )} /> )} ) } export default DimensionControls