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