| 1 |
import { useEffect, useRef, useState, useMemo } from "@wordpress/element"; |
| 2 |
import InspectorControl from "../../components/inspectorControls/inspectorControls"; |
| 3 |
import { panelBodyRightIcon, panelBodyTitleIcon } from "../../icons/icons"; |
| 4 |
import { useBlockProps } from "@wordpress/block-editor"; |
| 5 |
import Inspector from "./inspect"; |
| 6 |
import dynamicCssFn from "./dynamicCss"; |
| 7 |
import { inArray, useDeviceType } from "../../controls/controls"; |
| 8 |
import { EditorWrapper } from "../shared/wrapper"; |
| 9 |
import Render from "./render"; |
| 10 |
import { TogglePanelBodyProvider } from "../../context"; |
| 11 |
import { cssDependency } from "../shared/cssDependency"; |
| 12 |
import { compose } from "@wordpress/compose"; |
| 13 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 14 |
|
| 15 |
const GridThreeEdit = ({ attributes, setAttributes, isSelected }) => { |
| 16 |
const [posts, setPosts] = useState([]); |
| 17 |
const { |
| 18 |
blockName, |
| 19 |
contentAreaBg, |
| 20 |
postGridLayout, |
| 21 |
gridThreeColumns, |
| 22 |
fontListsEditPage, |
| 23 |
largeItemHeight, |
| 24 |
smallItemHeight, |
| 25 |
customCss, |
| 26 |
} = attributes; |
| 27 |
|
| 28 |
const blockProps = useBlockProps(); |
| 29 |
|
| 30 |
const firstLoad = useRef(true); |
| 31 |
const deviceType = useDeviceType(); |
| 32 |
|
| 33 |
useEffect(() => { |
| 34 |
if (!blockName) { |
| 35 |
setAttributes({ |
| 36 |
// align: 'wide', |
| 37 |
// Set default attributes value for this block. |
| 38 |
postLimit: 5, |
| 39 |
// excerptColor: { ...excerptColor, color: '#EEEEEE' }, |
| 40 |
contentHorizontalPosition: "left", |
| 41 |
contentAreaBg: { |
| 42 |
...contentAreaBg, |
| 43 |
color: { |
| 44 |
...contentAreaBg.color, |
| 45 |
solidColor: "transparent", |
| 46 |
}, |
| 47 |
}, |
| 48 |
// titleColor: { ...titleColor, color: '#ffffff' }, |
| 49 |
// metaColors: { ...metaColors, color: '#ffffff' }, |
| 50 |
showReadMoreButton: false, |
| 51 |
imagePosition: "background", |
| 52 |
imageOverlayColor: "default", |
| 53 |
contentVerticalPosition: "bottom", |
| 54 |
gridThreeColumns: { |
| 55 |
...gridThreeColumns, |
| 56 |
device: { |
| 57 |
...gridThreeColumns.device, |
| 58 |
[deviceType]: "", |
| 59 |
}, |
| 60 |
}, |
| 61 |
largeItemHeight: { |
| 62 |
...largeItemHeight, |
| 63 |
device: { |
| 64 |
...largeItemHeight.device, |
| 65 |
Desktop: "372", |
| 66 |
}, |
| 67 |
}, |
| 68 |
smallItemHeight: { |
| 69 |
...smallItemHeight, |
| 70 |
device: { |
| 71 |
...smallItemHeight.device, |
| 72 |
Desktop: 244, |
| 73 |
Mobile: 210, |
| 74 |
}, |
| 75 |
}, |
| 76 |
// paginationMargin: { |
| 77 |
// ...paginationMargin, |
| 78 |
// device: { |
| 79 |
// ...paginationMargin.device, |
| 80 |
// Desktop: { |
| 81 |
// ...paginationMargin.device.Desktop, |
| 82 |
// top: '36', |
| 83 |
// }, |
| 84 |
// }, |
| 85 |
// }, |
| 86 |
// catTabCategoryBg: { |
| 87 |
// ...catTabCategoryBg, |
| 88 |
// color: { |
| 89 |
// ...catTabCategoryBg.color, |
| 90 |
// solidColor: '#FFFFFF', |
| 91 |
// }, |
| 92 |
// hover: { |
| 93 |
// ...catTabCategoryBg.hover, |
| 94 |
// solidColor: '#EF5D30', |
| 95 |
// }, |
| 96 |
// }, |
| 97 |
// catTabCategoryColor: { |
| 98 |
// ...catTabCategoryColor, |
| 99 |
// color: '#222222', |
| 100 |
// hoverColor: '#FFFFFF', |
| 101 |
// }, |
| 102 |
// titleFontSize: { |
| 103 |
// ...titleFontSize, |
| 104 |
// device: { |
| 105 |
// ...titleFontSize.device, |
| 106 |
// Tablet: 22, |
| 107 |
// Mobile: 18, |
| 108 |
// }, |
| 109 |
// }, |
| 110 |
}); |
| 111 |
} |
| 112 |
}, []); |
| 113 |
|
| 114 |
useEffect(() => { |
| 115 |
if (firstLoad.current) { |
| 116 |
firstLoad.current = false; |
| 117 |
} else { |
| 118 |
if (inArray(["grid-three-layout-five"], postGridLayout)) { |
| 119 |
setAttributes({ equalHeightEnable: false }); |
| 120 |
} |
| 121 |
setAttributes({ blockLayoutName: postGridLayout }); |
| 122 |
} |
| 123 |
}, [blockName, postGridLayout]); |
| 124 |
|
| 125 |
useEffect(() => { |
| 126 |
setAttributes({ currentScreen: deviceType }); |
| 127 |
}, [deviceType]); |
| 128 |
|
| 129 |
const blockStyling = useMemo( |
| 130 |
() => dynamicCssFn(attributes, "frontend", deviceType), |
| 131 |
[...cssDependency(attributes), deviceType, attributes] |
| 132 |
); |
| 133 |
|
| 134 |
return ( |
| 135 |
<div {...blockProps}> |
| 136 |
<style> |
| 137 |
{fontListsEditPage} |
| 138 |
{blockStyling} |
| 139 |
{customCss} |
| 140 |
</style> |
| 141 |
<TogglePanelBodyProvider> |
| 142 |
<InspectorControl |
| 143 |
TitleIcon={panelBodyTitleIcon} |
| 144 |
RightIcon={panelBodyRightIcon} |
| 145 |
Inspector={Inspector} |
| 146 |
attributes={attributes} |
| 147 |
setAttributes={setAttributes} |
| 148 |
isSelected={isSelected} |
| 149 |
/> |
| 150 |
<EditorWrapper setPosts={setPosts} setAttributes={setAttributes}> |
| 151 |
<Render posts={posts} attributes={attributes} page={"editor"} /> |
| 152 |
</EditorWrapper> |
| 153 |
</TogglePanelBodyProvider> |
| 154 |
</div> |
| 155 |
); |
| 156 |
}; |
| 157 |
|
| 158 |
export default compose(addInitialAttr)(GridThreeEdit); |
| 159 |
|