ImageSettingsControl.js
167 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { TextareaControl, TextControl, SelectControl, BaseControl } from '@wordpress/components'; |
| 3 | import UnitControl from '../../../../components/unit-control'; |
| 4 | import getAttribute from '../../../../utils/get-attribute'; |
| 5 | import getMediaUrl from '../../../../utils/get-media-url'; |
| 6 | import { store as coreStore } from '@wordpress/core-data'; |
| 7 | import { useSelect } from '@wordpress/data'; |
| 8 | import { store as blockEditorStore } from '@wordpress/block-editor'; |
| 9 | |
| 10 | export default function ImageSettingsControls( props ) { |
| 11 | const { |
| 12 | attributes, |
| 13 | setAttributes, |
| 14 | media, |
| 15 | deviceType, |
| 16 | } = props; |
| 17 | |
| 18 | const { |
| 19 | useDynamicData, |
| 20 | mediaId, |
| 21 | alt, |
| 22 | title, |
| 23 | sizeSlug, |
| 24 | mediaUrl, |
| 25 | width, |
| 26 | widthTablet, |
| 27 | height, |
| 28 | heightTablet, |
| 29 | } = attributes; |
| 30 | |
| 31 | const mediaData = useSelect( ( select ) => { |
| 32 | const { getMedia } = select( coreStore ); |
| 33 | |
| 34 | return mediaId && getMedia( mediaId, { context: 'view' } ); |
| 35 | }, [ useDynamicData, mediaId ] ); |
| 36 | |
| 37 | const imageSizes = useSelect( ( select ) => { |
| 38 | const { |
| 39 | getSettings, |
| 40 | } = select( blockEditorStore ); |
| 41 | |
| 42 | const sizes = getSettings().imageSizes || []; |
| 43 | return sizes.map( ( size ) => ( { value: size.slug, label: size.name } ) ); |
| 44 | }, [] ); |
| 45 | |
| 46 | return ( |
| 47 | <> |
| 48 | { |
| 49 | 'Desktop' === deviceType && |
| 50 | ( |
| 51 | !! mediaId || |
| 52 | useDynamicData |
| 53 | ) && |
| 54 | <SelectControl |
| 55 | label={ __( 'Size', 'generateblocks' ) } |
| 56 | value={ sizeSlug } |
| 57 | options={ imageSizes } |
| 58 | onChange={ ( value ) => { |
| 59 | const imageUrl = getMediaUrl( mediaData, value ) || mediaUrl; |
| 60 | |
| 61 | setAttributes( { |
| 62 | mediaUrl: imageUrl, |
| 63 | sizeSlug: value, |
| 64 | } ); |
| 65 | } } |
| 66 | /> |
| 67 | } |
| 68 | |
| 69 | <BaseControl |
| 70 | help={ __( 'These fields will resize the image using CSS.', 'generateblocks' ) } |
| 71 | > |
| 72 | <div className="gblocks-image-dimensions__row"> |
| 73 | <UnitControl |
| 74 | label={ __( 'Width', 'generateblocks' ) } |
| 75 | id="gblocks-image-width" |
| 76 | value={ getAttribute( 'width', { attributes, deviceType } ) } |
| 77 | desktopValue={ width } |
| 78 | tabletValue={ widthTablet } |
| 79 | onChange={ ( value ) => { |
| 80 | setAttributes( { |
| 81 | [ getAttribute( 'width', { attributes, deviceType }, true ) ]: value, |
| 82 | } ); |
| 83 | } } |
| 84 | min="1" |
| 85 | units={ [ 'px', '%', 'vw', 'rem' ] } |
| 86 | /> |
| 87 | |
| 88 | <UnitControl |
| 89 | label={ __( 'Height', 'generateblocks' ) } |
| 90 | id="gblocks-image-height" |
| 91 | value={ getAttribute( 'height', { attributes, deviceType } ) } |
| 92 | desktopValue={ height } |
| 93 | tabletValue={ heightTablet } |
| 94 | onChange={ ( value ) => { |
| 95 | setAttributes( { |
| 96 | [ getAttribute( 'height', { attributes, deviceType }, true ) ]: value, |
| 97 | } ); |
| 98 | } } |
| 99 | min="1" |
| 100 | units={ [ 'px', '%', 'vw', 'rem' ] } |
| 101 | /> |
| 102 | </div> |
| 103 | </BaseControl> |
| 104 | |
| 105 | <SelectControl |
| 106 | label={ __( 'Object-fit', 'generateblocks' ) } |
| 107 | value={ getAttribute( 'objectFit', props ) } |
| 108 | options={ [ |
| 109 | { |
| 110 | label: __( 'Select…', 'generateblocks' ), |
| 111 | value: '', |
| 112 | }, |
| 113 | { |
| 114 | label: __( 'Inherit', 'generateblocks' ), |
| 115 | value: 'inherit', |
| 116 | }, |
| 117 | { |
| 118 | label: __( 'Cover', 'generateblocks' ), |
| 119 | value: 'cover', |
| 120 | }, |
| 121 | { |
| 122 | label: __( 'Contain', 'generateblocks' ), |
| 123 | value: 'contain', |
| 124 | }, |
| 125 | { |
| 126 | label: __( 'Fill', 'generateblocks' ), |
| 127 | value: 'fill', |
| 128 | }, |
| 129 | { |
| 130 | label: __( 'None', 'generateblocks' ), |
| 131 | value: 'none', |
| 132 | }, |
| 133 | ] } |
| 134 | onChange={ ( value ) => { |
| 135 | setAttributes( { |
| 136 | [ getAttribute( 'objectFit', props, true ) ]: value, |
| 137 | } ); |
| 138 | } } |
| 139 | /> |
| 140 | |
| 141 | { ! useDynamicData && 'Desktop' === deviceType && |
| 142 | <> |
| 143 | <TextareaControl |
| 144 | label={ __( 'Alt text (alternative text)', 'generateblocks' ) } |
| 145 | help={ __( 'Describe the purpose of the image, leave empty if the image is purely decorative.', 'generateblocks' ) } |
| 146 | value={ useDynamicData ? media?.alt_text : alt } |
| 147 | disabled={ useDynamicData } |
| 148 | onChange={ ( value ) => ( |
| 149 | setAttributes( { alt: value } ) |
| 150 | ) } |
| 151 | /> |
| 152 | |
| 153 | <TextControl |
| 154 | label={ __( 'Title attribute', 'generateblocks' ) } |
| 155 | help={ __( 'Describe the role of this image on the page.', 'generateblocks' ) } |
| 156 | value={ useDynamicData ? media?.title?.rendered : title } |
| 157 | disabled={ useDynamicData } |
| 158 | onChange={ ( value ) => ( |
| 159 | setAttributes( { title: value } ) |
| 160 | ) } |
| 161 | /> |
| 162 | </> |
| 163 | } |
| 164 | </> |
| 165 | ); |
| 166 | } |
| 167 |