| 1 |
import { useEffect, useMemo } from "@wordpress/element"; |
| 2 |
import { useBlockProps } from "@wordpress/block-editor"; |
| 3 |
import InspectorControl from "../../components/inspectorControls/inspectorControls"; |
| 4 |
import { getBlockShortName, jsonStringify } from "../shared/helpFn"; |
| 5 |
import Inspector from "./inspect"; |
| 6 |
import dynamicCssFn from "./dynamicCss"; |
| 7 |
import { googleFonts } from "../../controls/controls"; |
| 8 |
import { TogglePanelBodyProvider } from "../../context"; |
| 9 |
import EditHtml from "./editHtml"; |
| 10 |
import { compose } from "@wordpress/compose"; |
| 11 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 12 |
import dynamicDependency from "./infoBoxDependency"; |
| 13 |
|
| 14 |
const SmartInfoBoxEdit = ({ attributes, setAttributes, clientId, name }) => { |
| 15 |
const { |
| 16 |
ratingTypography, |
| 17 |
titleTypography, |
| 18 |
subTitleTypography, |
| 19 |
descriptionTypography, |
| 20 |
badgeTypography, |
| 21 |
additionalCssClass, |
| 22 |
blockName, |
| 23 |
} = attributes; |
| 24 |
|
| 25 |
const googleFontLists = [ |
| 26 |
ratingTypography, |
| 27 |
titleTypography, |
| 28 |
subTitleTypography, |
| 29 |
descriptionTypography, |
| 30 |
badgeTypography, |
| 31 |
]; |
| 32 |
|
| 33 |
useEffect(() => { |
| 34 |
setAttributes({ |
| 35 |
fontLists: jsonStringify(googleFonts(googleFontLists, "frontend")), |
| 36 |
}); |
| 37 |
}, googleFontLists); |
| 38 |
|
| 39 |
const blockProps = useBlockProps({ |
| 40 |
className: additionalCssClass, |
| 41 |
}); |
| 42 |
|
| 43 |
const googleFontListsEditor = useMemo(() => googleFonts(googleFontLists), googleFontLists); |
| 44 |
|
| 45 |
const blockStyling = useMemo(() => { |
| 46 |
return dynamicCssFn(attributes, "frontend"); |
| 47 |
}, dynamicDependency(attributes)); |
| 48 |
|
| 49 |
// BlockName init |
| 50 |
useEffect(() => { |
| 51 |
if (!blockName) { |
| 52 |
setAttributes({ blockName: getBlockShortName(name) }); |
| 53 |
} |
| 54 |
}, []); |
| 55 |
|
| 56 |
useEffect(() => { |
| 57 |
setAttributes({ |
| 58 |
uniqueId: `sp-smart-post-smart-list-${clientId?.split("-").pop()}`, |
| 59 |
}); |
| 60 |
}, [clientId]); |
| 61 |
|
| 62 |
return ( |
| 63 |
<div {...blockProps}> |
| 64 |
<style> |
| 65 |
{googleFontListsEditor} |
| 66 |
{blockStyling} |
| 67 |
</style> |
| 68 |
|
| 69 |
<TogglePanelBodyProvider> |
| 70 |
<InspectorControl Inspector={Inspector} attributes={attributes} setAttributes={setAttributes} /> |
| 71 |
|
| 72 |
<EditHtml attributes={attributes} setAttributes={setAttributes} /> |
| 73 |
</TogglePanelBodyProvider> |
| 74 |
</div> |
| 75 |
); |
| 76 |
}; |
| 77 |
|
| 78 |
export default compose(addInitialAttr)(SmartInfoBoxEdit); |
| 79 |
|