| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { BaseControl } from '@wordpress/components'; |
| 3 |
import { MediaUpload } from '@wordpress/block-editor'; |
| 4 |
import React from 'react'; |
| 5 |
import UAGB_Block_Icons from '@Controls/block-icons'; |
| 6 |
|
| 7 |
const UAGImage = ( props ) => { |
| 8 |
|
| 9 |
const { |
| 10 |
onSelectImage, |
| 11 |
backgroundImage, |
| 12 |
onRemoveImage, |
| 13 |
showVideoInput, |
| 14 |
label, |
| 15 |
disableRemove = false, |
| 16 |
allow = [ 'image' ], |
| 17 |
} = props; |
| 18 |
|
| 19 |
// This is used to render an icon in place of the background image when needed. |
| 20 |
let placeholderIcon; |
| 21 |
|
| 22 |
// This is used to determine the way the icon is colored. |
| 23 |
let iconColorType = 'stroke'; |
| 24 |
|
| 25 |
// Need to refactor this code as per multi-image select for more diversity. |
| 26 |
let labelText = __( 'Image', 'ultimate-addons-for-gutenberg' ); |
| 27 |
let selectImageLabel = __( |
| 28 |
'Select Image', |
| 29 |
'ultimate-addons-for-gutenberg' |
| 30 |
); |
| 31 |
let replaceImageLabel = __( |
| 32 |
'Change Image', |
| 33 |
'ultimate-addons-for-gutenberg' |
| 34 |
); |
| 35 |
let allowedTypes = [ 'image' ]; |
| 36 |
|
| 37 |
if ( showVideoInput ) { |
| 38 |
labelText = __( 'Video', 'ultimate-addons-for-gutenberg' ); |
| 39 |
selectImageLabel = __( |
| 40 |
'Select Video', |
| 41 |
'ultimate-addons-for-gutenberg' |
| 42 |
); |
| 43 |
replaceImageLabel = __( |
| 44 |
'Change Video', |
| 45 |
'ultimate-addons-for-gutenberg' |
| 46 |
); |
| 47 |
allowedTypes = [ 'video' ]; |
| 48 |
placeholderIcon = UAGB_Block_Icons.video_placeholder; |
| 49 |
iconColorType = 'fill'; |
| 50 |
} |
| 51 |
labelText = label ? label : labelText; |
| 52 |
labelText = false === label ? label : labelText; |
| 53 |
|
| 54 |
// Newer Dynamic Code here ( Currently used in Lottie Block ) |
| 55 |
|
| 56 |
if ( label === 'Lottie Animation' ){ |
| 57 |
// No Template Literals due to @wordpress/i18n-no-variables |
| 58 |
selectImageLabel = __( |
| 59 |
'Select Lottie Animation', |
| 60 |
'ultimate-addons-for-gutenberg' |
| 61 |
); |
| 62 |
replaceImageLabel = __( |
| 63 |
'Change Lottie Animation', |
| 64 |
'ultimate-addons-for-gutenberg' |
| 65 |
); |
| 66 |
allowedTypes = allow; |
| 67 |
placeholderIcon = UAGB_Block_Icons.lottie; |
| 68 |
} |
| 69 |
|
| 70 |
const renderMediaUploader = ( open ) => { |
| 71 |
const uploadType = backgroundImage?.url ? 'replace' : 'add'; |
| 72 |
return( |
| 73 |
<button |
| 74 |
className={ `spectra-media-control__clickable spectra-media-control__clickable--${ uploadType }` } |
| 75 |
onClick={ open } |
| 76 |
> |
| 77 |
{ ( 'add' === uploadType ) ? ( |
| 78 |
renderButton( uploadType ) |
| 79 |
) : ( |
| 80 |
<div className='uag-control-label'>{ replaceImageLabel }</div> |
| 81 |
) } |
| 82 |
</button> |
| 83 |
) |
| 84 |
}; |
| 85 |
|
| 86 |
const renderButton = ( buttonType ) => ( |
| 87 |
<div className={ `spectra-media-control__button spectra-media-control__button--${ buttonType }` }> |
| 88 |
{ UAGB_Block_Icons[ buttonType ] } |
| 89 |
</div> |
| 90 |
); |
| 91 |
|
| 92 |
// This Can Be Deprecated. |
| 93 |
const generateBackground = ( media ) => { |
| 94 |
const regex = /(?:\.([^.]+))?$/; |
| 95 |
let mediaURL = media; |
| 96 |
switch ( regex.exec( String( mediaURL ) )[1] ){ |
| 97 |
// For Lottie JSON Files. |
| 98 |
case 'json': |
| 99 |
mediaURL = ''; |
| 100 |
break; |
| 101 |
// For Videos. |
| 102 |
case 'avi': |
| 103 |
case 'mpg': |
| 104 |
case 'mp4': |
| 105 |
case 'm4v': |
| 106 |
case 'mov': |
| 107 |
case 'ogv': |
| 108 |
case 'vtt': |
| 109 |
case 'wmv': |
| 110 |
case '3gp': |
| 111 |
case '3g2': |
| 112 |
mediaURL = ''; |
| 113 |
break; |
| 114 |
} |
| 115 |
return mediaURL; |
| 116 |
} |
| 117 |
|
| 118 |
return ( |
| 119 |
<BaseControl |
| 120 |
className="spectra-media-control" |
| 121 |
id={ `uagb-option-selector-${ label }` } |
| 122 |
label={ labelText } |
| 123 |
> |
| 124 |
<div |
| 125 |
className="spectra-media-control__wrapper" |
| 126 |
style={ { |
| 127 |
backgroundImage: ( ! placeholderIcon && backgroundImage?.url ) && ( |
| 128 |
`url("${ generateBackground( backgroundImage?.url ) }")` |
| 129 |
), |
| 130 |
} } |
| 131 |
> |
| 132 |
{ ( placeholderIcon && backgroundImage?.url ) && ( |
| 133 |
<div className={ `spectra-media-control__icon spectra-media-control__icon--${ iconColorType }` }> |
| 134 |
{ placeholderIcon } |
| 135 |
</div> |
| 136 |
) } |
| 137 |
<MediaUpload |
| 138 |
title={ selectImageLabel } |
| 139 |
onSelect={ onSelectImage } |
| 140 |
allowedTypes={ allowedTypes } |
| 141 |
value={ backgroundImage } |
| 142 |
render={ ( { open } ) => renderMediaUploader( open ) } |
| 143 |
/> |
| 144 |
{ ( ! disableRemove && backgroundImage?.url ) && ( |
| 145 |
<button |
| 146 |
className='spectra-media-control__clickable spectra-media-control__clickable--close' |
| 147 |
onClick={ onRemoveImage } |
| 148 |
> |
| 149 |
{ renderButton( 'close' ) } |
| 150 |
</button> |
| 151 |
) } |
| 152 |
</div> |
| 153 |
{ props.help && ( |
| 154 |
<p className="uag-control-help-notice">{ props.help }</p> |
| 155 |
) } |
| 156 |
</BaseControl> |
| 157 |
); |
| 158 |
}; |
| 159 |
|
| 160 |
export default UAGImage; |
| 161 |
|