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