| 1 |
import { useEffect, useMemo } from "@wordpress/element"; |
| 2 |
import { InnerBlocks, useBlockProps } from "@wordpress/block-editor"; |
| 3 |
import InspectorControl from "../../components/inspectorControls/inspectorControls"; |
| 4 |
import { panelBodyRightIcon, panelBodyTitleIcon } from "../../icons/icons"; |
| 5 |
import Inspector from "./inspect"; |
| 6 |
import dynamicCssFn from "./dynamicCss"; |
| 7 |
import { jsonStringify } from "../shared/helpFn"; |
| 8 |
import { googleFonts, useDeviceType } from "../../controls/controls"; |
| 9 |
import { dispatch, select, useSelect } from "@wordpress/data"; |
| 10 |
import { compose } from "@wordpress/compose"; |
| 11 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 12 |
import { TogglePanelBodyProvider } from "../../context"; |
| 13 |
import Render from "./Render"; |
| 14 |
import useMetaData from "../../hooks/useMetaData"; |
| 15 |
import useUpdateParentAttributes from "../../hooks/updateParentAttr"; |
| 16 |
const Edit = ({ attributes, setAttributes, clientId, isSelected }) => { |
| 17 |
const { |
| 18 |
customCss, |
| 19 |
uniqueId, |
| 20 |
fieldAlignment, |
| 21 |
titleTypography, |
| 22 |
optionTypography, |
| 23 |
additionalCssClass, |
| 24 |
titleGlobalTypography, |
| 25 |
optionGlobalTypography, |
| 26 |
layout, |
| 27 |
blockName, |
| 28 |
headingTypography, |
| 29 |
menuTypography, |
| 30 |
multipleFilterRelation |
| 31 |
} = attributes; |
| 32 |
|
| 33 |
useUpdateParentAttributes( |
| 34 |
clientId, |
| 35 |
multipleFilterRelation |
| 36 |
); |
| 37 |
|
| 38 |
const innerBlocks = select("core/block-editor").getBlocks(clientId); |
| 39 |
const { allTaxonomies } = useMetaData(attributes, "editSite"); |
| 40 |
|
| 41 |
// Get the count |
| 42 |
const innerBlocksWidth = innerBlocks.length > 3 ? "sp-width-33" : ""; |
| 43 |
|
| 44 |
const currentDevice = useDeviceType(); |
| 45 |
|
| 46 |
useEffect(() => { |
| 47 |
setAttributes({ |
| 48 |
innerBlockLength: innerBlocks.length, |
| 49 |
}); |
| 50 |
}, [innerBlocks.length]); |
| 51 |
|
| 52 |
const blockProps = useBlockProps({ |
| 53 |
className: additionalCssClass, |
| 54 |
}); |
| 55 |
|
| 56 |
const parentBlock = useSelect((select) => { |
| 57 |
return select("core/block-editor").getBlock(clientId); |
| 58 |
}, []); |
| 59 |
|
| 60 |
useEffect(() => { |
| 61 |
if (!isSelected) { |
| 62 |
return; |
| 63 |
} |
| 64 |
parentBlock.innerBlocks.forEach((innerBlock) => { |
| 65 |
dispatch("core/block-editor").updateBlockAttributes(innerBlock.clientId, { |
| 66 |
titleGlobalTypography, |
| 67 |
optionGlobalTypography, |
| 68 |
}); |
| 69 |
}); |
| 70 |
}, [parentBlock.innerBlocks, isSelected, titleGlobalTypography, optionGlobalTypography]); |
| 71 |
|
| 72 |
const googleFontLists = [titleTypography, optionTypography, headingTypography, menuTypography]; |
| 73 |
|
| 74 |
useEffect(() => { |
| 75 |
setAttributes({ |
| 76 |
fontLists: jsonStringify(googleFonts(googleFontLists, "frontend")), |
| 77 |
}); |
| 78 |
}, googleFontLists); |
| 79 |
|
| 80 |
const blockStyling = useMemo( |
| 81 |
() => dynamicCssFn(attributes, "frontend", currentDevice), |
| 82 |
[attributes, currentDevice] |
| 83 |
); |
| 84 |
|
| 85 |
attributes.allTaxonomies = allTaxonomies; |
| 86 |
|
| 87 |
return ( |
| 88 |
<div {...blockProps}> |
| 89 |
<TogglePanelBodyProvider blockName={blockName}> |
| 90 |
<style> |
| 91 |
{googleFonts(googleFontLists)} |
| 92 |
{/* {dynamicCss(attributes)} */} |
| 93 |
{blockStyling} |
| 94 |
{customCss} |
| 95 |
</style> |
| 96 |
<InspectorControl |
| 97 |
TitleIcon={panelBodyTitleIcon} |
| 98 |
RightIcon={panelBodyRightIcon} |
| 99 |
Inspector={Inspector} |
| 100 |
attributes={attributes} |
| 101 |
setAttributes={setAttributes} |
| 102 |
/> |
| 103 |
|
| 104 |
<div |
| 105 |
id={uniqueId} |
| 106 |
className={`sp-smart-post-wrapper sp-smart-${fieldAlignment} ${innerBlocksWidth} ${layout}`} |
| 107 |
> |
| 108 |
<div className="sp-smart-post-live-filter-parent"> |
| 109 |
{layout === "layoutOne" ? ( |
| 110 |
<InnerBlocks |
| 111 |
allowedBlocks={[ |
| 112 |
"sp-smart-post-show/taxonomy-filter", |
| 113 |
"sp-smart-post-show/sort-filter", |
| 114 |
"sp-smart-post-show/search-filter", |
| 115 |
"sp-smart-post-show/author-filter", |
| 116 |
]} |
| 117 |
template={[ |
| 118 |
["sp-smart-post-show/taxonomy-filter"], |
| 119 |
["sp-smart-post-show/author-filter"], |
| 120 |
]} |
| 121 |
/> |
| 122 |
) : ( |
| 123 |
<Render attributes={attributes} /> |
| 124 |
)} |
| 125 |
</div> |
| 126 |
</div> |
| 127 |
</TogglePanelBodyProvider> |
| 128 |
</div> |
| 129 |
); |
| 130 |
}; |
| 131 |
|
| 132 |
export default compose(addInitialAttr)(Edit); |
| 133 |
|