| 1 |
import { __ } from "@wordpress/i18n"; |
| 2 |
import { BaseControl, SelectControl } from "@wordpress/components"; |
| 3 |
import { MediaUpload } from "@wordpress/block-editor"; |
| 4 |
import "./editor.scss"; |
| 5 |
|
| 6 |
const MediaPicker = ({ |
| 7 |
backgroundImage, |
| 8 |
imageKey, |
| 9 |
setAttributes, |
| 10 |
imageSizeValue, |
| 11 |
imageSizeKey, |
| 12 |
slug = "image", |
| 13 |
label = __("Image", "post-carousel"), |
| 14 |
disableLabel = false, |
| 15 |
disableRemove = false, |
| 16 |
mediaType = "image", |
| 17 |
enableImageSize = true, |
| 18 |
onSelect = false, |
| 19 |
removeImage = false, |
| 20 |
}) => { |
| 21 |
/* |
| 22 |
* Event to set Image as while adding. |
| 23 |
*/ |
| 24 |
const onSelectImage = (media) => { |
| 25 |
if (!media || !media.url) { |
| 26 |
setAttributes({ [imageKey]: null }); |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// if ( ! media.type || 'image' !== media.type ) { |
| 31 |
// setAttributes( { [imageKey]: null } ); |
| 32 |
// return; |
| 33 |
// } |
| 34 |
|
| 35 |
setAttributes({ [imageKey]: media }); |
| 36 |
}; |
| 37 |
|
| 38 |
const imagesSizeOptions = (image) => { |
| 39 |
const sizeArr = []; |
| 40 |
for (const size in image.sizes) { |
| 41 |
if (image.sizes.hasOwnProperty(size)) { |
| 42 |
const p = { |
| 43 |
value: size, |
| 44 |
label: size.charAt(0).toUpperCase() + size.slice(1), |
| 45 |
}; |
| 46 |
sizeArr.push(p); |
| 47 |
} |
| 48 |
} |
| 49 |
return sizeArr; |
| 50 |
}; |
| 51 |
|
| 52 |
/* |
| 53 |
* Event to set Image as null while removing. |
| 54 |
*/ |
| 55 |
const onRemoveImage = () => { |
| 56 |
setAttributes({ [imageKey]: "" }); |
| 57 |
}; |
| 58 |
|
| 59 |
// This is used to render an icon in place of the background image when needed. |
| 60 |
let placeholderIcon; |
| 61 |
|
| 62 |
// These are the localized texts that will show on the Select / Change Button and Popup. |
| 63 |
let selectMediaLabel, replaceMediaLabel; |
| 64 |
|
| 65 |
switch (slug) { |
| 66 |
case "video": |
| 67 |
selectMediaLabel = __("Select Video", "post-carousel"); |
| 68 |
replaceMediaLabel = __("Change Video", "post-carousel"); |
| 69 |
// placeholderIcon = UAGB_Block_Icons.video_placeholder; |
| 70 |
break; |
| 71 |
case "lottie": |
| 72 |
selectMediaLabel = __("Select Lottie Animation", "post-carousel"); |
| 73 |
replaceMediaLabel = __("Change Lottie Animation", "post-carousel"); |
| 74 |
// placeholderIcon = UAGB_Block_Icons.lottie; |
| 75 |
break; |
| 76 |
default: |
| 77 |
selectMediaLabel = __("Select Image", "post-carousel"); |
| 78 |
replaceMediaLabel = __("Change Image", "post-carousel"); |
| 79 |
} |
| 80 |
|
| 81 |
const renderMediaUploader = (open) => { |
| 82 |
const uploadType = backgroundImage?.url ? "replace" : "add"; |
| 83 |
return ( |
| 84 |
<button |
| 85 |
className={`sp-smart-post-show-media-control__clickable sp-smart-post-show-media-control__clickable--${uploadType}`} |
| 86 |
onClick={open} |
| 87 |
> |
| 88 |
{"add" === uploadType ? ( |
| 89 |
renderButton(uploadType) |
| 90 |
) : ( |
| 91 |
<div className="sp-smart-post-show-control-label">{replaceMediaLabel}</div> |
| 92 |
)} |
| 93 |
</button> |
| 94 |
); |
| 95 |
}; |
| 96 |
|
| 97 |
const renderButton = (buttonType) => ( |
| 98 |
<div |
| 99 |
className={`sp-smart-post-show-media-control__button sp-smart-post-show-media-control__button--${buttonType}`} |
| 100 |
> |
| 101 |
<span>{"close" === buttonType ? "×" : "+"}</span> |
| 102 |
</div> |
| 103 |
); |
| 104 |
|
| 105 |
// This Can Be Deprecated. |
| 106 |
const generateBackground = (media) => { |
| 107 |
const regex = /(?:\.([^.]+))?$/; |
| 108 |
let mediaURL = media; |
| 109 |
switch (regex.exec(String(mediaURL))[1]) { |
| 110 |
// For Lottie JSON Files. |
| 111 |
case "json": |
| 112 |
mediaURL = ""; |
| 113 |
break; |
| 114 |
// For Videos. |
| 115 |
case "avi": |
| 116 |
case "mpg": |
| 117 |
case "mp4": |
| 118 |
case "m4v": |
| 119 |
case "mov": |
| 120 |
case "ogv": |
| 121 |
case "vtt": |
| 122 |
case "wmv": |
| 123 |
case "3gp": |
| 124 |
case "3g2": |
| 125 |
mediaURL = ""; |
| 126 |
break; |
| 127 |
} |
| 128 |
return mediaURL; |
| 129 |
}; |
| 130 |
|
| 131 |
const backgroundImageURL = generateBackground(backgroundImage?.url); |
| 132 |
|
| 133 |
return ( |
| 134 |
<> |
| 135 |
<BaseControl |
| 136 |
className="sp-smart-post-show-media-control" |
| 137 |
id={`sp-smart-post-show-option-selector-${slug}`} |
| 138 |
label={label} |
| 139 |
hideLabelFromVision={disableLabel} |
| 140 |
__nextHasNoMarginBottom |
| 141 |
> |
| 142 |
<div |
| 143 |
className="sp-smart-post-show-media-control__wrapper" |
| 144 |
style={{ |
| 145 |
backgroundImage: !placeholderIcon && backgroundImage?.url && `url("${backgroundImageURL}")`, |
| 146 |
}} |
| 147 |
> |
| 148 |
{"video" === mediaType && backgroundImage?.url && ( |
| 149 |
<div className="sp-smart-post-show-media-video"> |
| 150 |
<i className="fa-regular fa-circle-play"></i> |
| 151 |
</div> |
| 152 |
)} |
| 153 |
|
| 154 |
{placeholderIcon && backgroundImage?.url && ( |
| 155 |
<div className="sp-smart-post-show-media-control__icon sp-smart-post-show-media-control__icon--stroke"> |
| 156 |
{placeholderIcon} |
| 157 |
</div> |
| 158 |
)} |
| 159 |
<MediaUpload |
| 160 |
title={selectMediaLabel} |
| 161 |
onSelect={onSelect ? onSelect : onSelectImage} |
| 162 |
allowedTypes={mediaType} |
| 163 |
value={backgroundImage} |
| 164 |
render={({ open }) => renderMediaUploader(open)} |
| 165 |
/> |
| 166 |
{!disableRemove && backgroundImage?.url && ( |
| 167 |
<button |
| 168 |
className="sp-smart-post-show-media-control__clickable sp-smart-post-show-media-control__clickable--close" |
| 169 |
onClick={removeImage ? removeImage : onRemoveImage} |
| 170 |
> |
| 171 |
{renderButton("close")} |
| 172 |
</button> |
| 173 |
)} |
| 174 |
</div> |
| 175 |
</BaseControl> |
| 176 |
|
| 177 |
{backgroundImage && enableImageSize && backgroundImage.url !== "null" && backgroundImage.url !== "" && ( |
| 178 |
<> |
| 179 |
<SelectControl |
| 180 |
label={__("Image Size", "post-carousel")} |
| 181 |
value={imageSizeValue} |
| 182 |
options={imagesSizeOptions(backgroundImage)} |
| 183 |
onChange={(newSize) => setAttributes({ [imageSizeKey]: newSize })} |
| 184 |
__nextHasNoMarginBottom |
| 185 |
/> |
| 186 |
</> |
| 187 |
)} |
| 188 |
</> |
| 189 |
); |
| 190 |
}; |
| 191 |
|
| 192 |
export default MediaPicker; |
| 193 |
|