| 1 |
import { useBlockProps } from "@wordpress/block-editor"; |
| 2 |
import { useEffect, useMemo, useState } from "@wordpress/element"; |
| 3 |
import InspectorControl from "../../components/inspectorControls/inspectorControls"; |
| 4 |
import { panelBodyTitleIcon, panelBodyRightIcon } from "../../icons/icons"; |
| 5 |
import Inspector from "./inspect"; |
| 6 |
import dynamicCssFn from "./dynamicCss"; |
| 7 |
import { jsonStringify } from "../shared/helpFn"; |
| 8 |
import { PresetTemplate } from "./smartImgTemplate"; |
| 9 |
import RenderSmartImage from "./render"; |
| 10 |
import { useSelect } from "@wordpress/data"; |
| 11 |
import preloader from "../../../public/assets/img/preloader.svg"; |
| 12 |
import imgCssDependency from "./imgCssDependency"; |
| 13 |
import { googleFonts } from "../../controls/controls"; |
| 14 |
import "./editor.scss"; |
| 15 |
import { compose } from "@wordpress/compose"; |
| 16 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 17 |
|
| 18 |
const SmartImageEdit = ({ attributes, setAttributes }) => { |
| 19 |
const { |
| 20 |
fontListsEditPage, |
| 21 |
customCss, |
| 22 |
selectImage, |
| 23 |
selectImageSizes, |
| 24 |
selectImageId, |
| 25 |
imageAltText, |
| 26 |
imgTitleLabel, |
| 27 |
imgCaptionLabel, |
| 28 |
additionalCssClass, |
| 29 |
linkBtnTypography, |
| 30 |
imgTitleTypography, |
| 31 |
imgCaptionTypography, |
| 32 |
} = attributes; |
| 33 |
|
| 34 |
const [isPreloader, setIsPreloader] = useState(false); |
| 35 |
const blockProps = useBlockProps({ className: additionalCssClass }); |
| 36 |
|
| 37 |
const blockStyling = useMemo( |
| 38 |
() => dynamicCssFn(attributes, "frontend"), |
| 39 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 40 |
imgCssDependency(attributes) |
| 41 |
); |
| 42 |
const googleFontLists = [linkBtnTypography, imgTitleTypography, imgCaptionTypography]; |
| 43 |
useEffect(() => { |
| 44 |
setAttributes({ |
| 45 |
fontLists: jsonStringify(googleFonts(googleFontLists, "frontend")), |
| 46 |
fontListsEditPage: googleFonts(googleFontLists), |
| 47 |
}); |
| 48 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 49 |
}, googleFontLists); |
| 50 |
|
| 51 |
const imageObject = useSelect( |
| 52 |
(select) => { |
| 53 |
// return select("core").getMedia(selectImageId) || {}; |
| 54 |
return select("core").getEntityRecord("postType", "attachment", selectImageId) || {}; |
| 55 |
}, |
| 56 |
[selectImageId] |
| 57 |
); |
| 58 |
|
| 59 |
useEffect(() => { |
| 60 |
if (!imageObject) { |
| 61 |
return; |
| 62 |
} |
| 63 |
const srcset = imageObject?.media_details?.sizes |
| 64 |
? Object.values(imageObject.media_details.sizes) |
| 65 |
.map((size) => `${size.source_url} ${size.width}w`) |
| 66 |
.join(", ") |
| 67 |
: null; |
| 68 |
const newImgUrl = imageObject.url || imageObject.source_url; |
| 69 |
const data = { |
| 70 |
...imageObject, |
| 71 |
srcset, |
| 72 |
url: newImgUrl || selectImage?.insertUrl, |
| 73 |
insertUrl: selectImage?.insertUrl || "", |
| 74 |
}; |
| 75 |
const imageSizes = imageObject?.media_details?.sizes |
| 76 |
? Object.keys(imageObject?.media_details?.sizes).map((size) => ({ |
| 77 |
label: size, |
| 78 |
value: size, |
| 79 |
})) |
| 80 |
: []; |
| 81 |
setAttributes({ |
| 82 |
selectImage: data, |
| 83 |
selectImageSizes: imageSizes, |
| 84 |
imageAltText: imageAltText ? imageAltText : data?.alt_text, |
| 85 |
imgTitleLabel: imgTitleLabel ? imgTitleLabel : data?.title?.raw, |
| 86 |
imgCaptionLabel: imgCaptionLabel ? imgCaptionLabel : data?.caption?.raw, |
| 87 |
}); |
| 88 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 89 |
}, [imageObject]); |
| 90 |
|
| 91 |
return ( |
| 92 |
<div {...blockProps}> |
| 93 |
<style> |
| 94 |
{fontListsEditPage} |
| 95 |
{blockStyling} |
| 96 |
{customCss} |
| 97 |
</style> |
| 98 |
{isPreloader && ( |
| 99 |
<div className="sp-smart-image-upload-preloader"> |
| 100 |
<img src={preloader} alt="Loading..." /> |
| 101 |
</div> |
| 102 |
)} |
| 103 |
<InspectorControl |
| 104 |
TitleIcon={panelBodyTitleIcon} |
| 105 |
RightIcon={panelBodyRightIcon} |
| 106 |
attributes={attributes} |
| 107 |
setAttributes={setAttributes} |
| 108 |
Inspector={Inspector} |
| 109 |
/> |
| 110 |
{!selectImageId && !selectImage?.insertUrl ? ( |
| 111 |
<PresetTemplate |
| 112 |
attributes={{ |
| 113 |
selectImage, |
| 114 |
selectImageId, |
| 115 |
selectImageSizes, |
| 116 |
}} |
| 117 |
setAttributes={setAttributes} |
| 118 |
setIsPreloader={setIsPreloader} |
| 119 |
isPreloader={isPreloader} |
| 120 |
/> |
| 121 |
) : ( |
| 122 |
<RenderSmartImage |
| 123 |
attributes={attributes} |
| 124 |
setAttributes={setAttributes} |
| 125 |
setIsPreloader={setIsPreloader} |
| 126 |
/> |
| 127 |
)} |
| 128 |
</div> |
| 129 |
); |
| 130 |
}; |
| 131 |
export default compose(addInitialAttr)(SmartImageEdit); |
| 132 |
|