| 1 |
import React, { useLayoutEffect } from 'react'; |
| 2 |
import { |
| 3 |
TextControl, |
| 4 |
} from '@wordpress/components'; |
| 5 |
import ResponsiveSelectControl from '@Components/responsive-select'; |
| 6 |
import { __ } from '@wordpress/i18n'; |
| 7 |
import styles from './editor.lazy.scss'; |
| 8 |
import useDimensionHandler from './use-dimension-handler'; |
| 9 |
import { useDeviceType } from '@Controls/getPreviewType'; |
| 10 |
import ResponsiveToggle from '../responsive-toggle'; |
| 11 |
|
| 12 |
export default function ImageSizeControl( { |
| 13 |
imageWidth, |
| 14 |
imageHeight, |
| 15 |
imageSizeOptions = [], |
| 16 |
isResizable = true, |
| 17 |
sizeSlug, |
| 18 |
sizeSlugTablet, |
| 19 |
sizeSlugMobile, |
| 20 |
width, |
| 21 |
widthTablet, |
| 22 |
widthMobile, |
| 23 |
height, |
| 24 |
heightTablet, |
| 25 |
heightMobile, |
| 26 |
setAttributes, |
| 27 |
onChange, |
| 28 |
} ) { |
| 29 |
|
| 30 |
// Add and remove the CSS on the drop and remove of the component. |
| 31 |
useLayoutEffect( () => { |
| 32 |
styles.use(); |
| 33 |
return () => { |
| 34 |
styles.unuse(); |
| 35 |
}; |
| 36 |
}, [] ); |
| 37 |
|
| 38 |
const deviceType = useDeviceType(); |
| 39 |
const responsive = true; |
| 40 |
|
| 41 |
const { |
| 42 |
currentHeight, |
| 43 |
currentWidth, |
| 44 |
updateDimension, |
| 45 |
} = useDimensionHandler( height, width, imageHeight, imageWidth, onChange ); |
| 46 |
|
| 47 |
const output = {} |
| 48 |
output.Desktop = ( |
| 49 |
<> |
| 50 |
<TextControl |
| 51 |
type="number" |
| 52 |
className="block-editor-image-size-control__width" |
| 53 |
label={ __( 'Width' ) } |
| 54 |
value={ currentWidth } |
| 55 |
min={ 1 } |
| 56 |
onChange={ ( value ) => |
| 57 |
updateDimension( 'width', value ) |
| 58 |
} |
| 59 |
/> |
| 60 |
<TextControl |
| 61 |
type="number" |
| 62 |
className="block-editor-image-size-control__height" |
| 63 |
label={ __( 'Height' ) } |
| 64 |
value={ currentHeight } |
| 65 |
min={ 1 } |
| 66 |
onChange={ ( value ) => { |
| 67 |
updateDimension( 'height', value ) |
| 68 |
if ( ! isNaN( value ) && '' !== value ) { |
| 69 |
setAttributes( { customHeightSetDesktop: true } ); |
| 70 |
} else { |
| 71 |
setAttributes( { customHeightSetDesktop: false } ); |
| 72 |
} |
| 73 |
} } |
| 74 |
/> |
| 75 |
</> |
| 76 |
); |
| 77 |
|
| 78 |
output.Tablet = ( |
| 79 |
<> |
| 80 |
<TextControl |
| 81 |
type="number" |
| 82 |
className="block-editor-image-size-control__width" |
| 83 |
label={ __( 'Width' ) } |
| 84 |
value={ widthTablet } |
| 85 |
min={ 1 } |
| 86 |
onChange={ ( value ) => |
| 87 |
setAttributes( { widthTablet: value} ) |
| 88 |
} |
| 89 |
/> |
| 90 |
<TextControl |
| 91 |
type="number" |
| 92 |
className="block-editor-image-size-control__height" |
| 93 |
label={ __( 'Height' ) } |
| 94 |
value={ heightTablet } |
| 95 |
min={ 1 } |
| 96 |
onChange={ ( value ) => { |
| 97 |
setAttributes( { heightTablet: value} ); |
| 98 |
if ( ! isNaN( value ) && '' !== value ) { |
| 99 |
setAttributes( { customHeightSetTablet: true } ); |
| 100 |
} else { |
| 101 |
setAttributes( { customHeightSetTablet: false } ); |
| 102 |
} |
| 103 |
}} |
| 104 |
/> |
| 105 |
</> |
| 106 |
); |
| 107 |
|
| 108 |
output.Mobile = ( |
| 109 |
<> |
| 110 |
<TextControl |
| 111 |
type="number" |
| 112 |
className="block-editor-image-size-control__width" |
| 113 |
label={ __( 'Width' ) } |
| 114 |
value={ widthMobile } |
| 115 |
min={ 1 } |
| 116 |
onChange={ ( value ) => |
| 117 |
setAttributes( { widthMobile: value} ) |
| 118 |
} |
| 119 |
/> |
| 120 |
<TextControl |
| 121 |
type="number" |
| 122 |
className="block-editor-image-size-control__height" |
| 123 |
label={ __( 'Height' ) } |
| 124 |
value={ heightMobile } |
| 125 |
min={ 1 } |
| 126 |
onChange={ ( value ) => { |
| 127 |
setAttributes( { heightMobile: value} ); |
| 128 |
if ( ! isNaN( value ) && '' !== value ) { |
| 129 |
setAttributes( { customHeightSetMobile: true } ); |
| 130 |
} else { |
| 131 |
setAttributes( { customHeightSetMobile: false } ); |
| 132 |
} |
| 133 |
}} |
| 134 |
/> |
| 135 |
</> |
| 136 |
); |
| 137 |
|
| 138 |
return ( |
| 139 |
<> |
| 140 |
{ imageSizeOptions.length !== 0 && ( |
| 141 |
<ResponsiveSelectControl |
| 142 |
label={ __( 'Image Size', 'ultimate-addons-for-gutenberg' ) } |
| 143 |
options={ { |
| 144 |
desktop: imageSizeOptions, |
| 145 |
tablet: imageSizeOptions, |
| 146 |
mobile: imageSizeOptions, |
| 147 |
} } |
| 148 |
data={ { |
| 149 |
desktop: { |
| 150 |
value: sizeSlug, |
| 151 |
label: 'sizeSlug' |
| 152 |
}, |
| 153 |
tablet: { |
| 154 |
value: sizeSlugTablet, |
| 155 |
label: 'sizeSlugTablet' |
| 156 |
}, |
| 157 |
mobile: { |
| 158 |
value: sizeSlugMobile, |
| 159 |
label: 'sizeSlugMobile' |
| 160 |
}, |
| 161 |
} } |
| 162 |
setAttributes={ setAttributes } |
| 163 |
/> |
| 164 |
) } |
| 165 |
{ isResizable && ( |
| 166 |
<div className="components-base-control block-editor-image-size-control"> |
| 167 |
<div className='uagb-size-type-field-tabs'> |
| 168 |
<div className='uagb-control__header'> |
| 169 |
<ResponsiveToggle |
| 170 |
label= { __( 'Image Dimensions' ) } |
| 171 |
responsive= { responsive } |
| 172 |
/> |
| 173 |
</div> |
| 174 |
<div className="block-editor-image-size-control__row"> |
| 175 |
{output[deviceType]} |
| 176 |
</div> |
| 177 |
</div> |
| 178 |
</div> |
| 179 |
) } |
| 180 |
</> |
| 181 |
); |
| 182 |
} |
| 183 |
|