| 1 |
import { InnerBlocks, useBlockProps } from "@wordpress/block-editor"; |
| 2 |
import { InspectorControl } from "../../components"; |
| 3 |
import { panelBodyRightIcon } from "../../icons/icons"; |
| 4 |
import { Inspector } from "./Inspector"; |
| 5 |
import dynamicCssFn from "./dynamicCss"; |
| 6 |
import { useEffect, useMemo, useRef, useState } from "@wordpress/element"; |
| 7 |
import { useSelect } from "@wordpress/data"; |
| 8 |
import { useDeviceType } from "../../controls/controls"; |
| 9 |
import editorStyleDependency from "./editorStyleDependency"; |
| 10 |
import { compose } from "@wordpress/compose"; |
| 11 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 12 |
|
| 13 |
|
| 14 |
const ColumnEdit = ({ attributes, setAttributes, clientId, isSelected }) => { |
| 15 |
const { |
| 16 |
uniqueId, |
| 17 |
columnAdditionalID, |
| 18 |
columnAdditionalClass, |
| 19 |
customCss, |
| 20 |
columnWrapperLink, |
| 21 |
columnWrapperLinkUrl, |
| 22 |
columnWrapperLinkNewTab, |
| 23 |
columnBg, |
| 24 |
columnBgVideo, |
| 25 |
} = attributes; |
| 26 |
|
| 27 |
const blockProps = useBlockProps(); |
| 28 |
const deviceType = useDeviceType(); |
| 29 |
const containerColumnRef = useRef(null); |
| 30 |
const [isHovered, setIsHovered] = useState(false); |
| 31 |
|
| 32 |
const { getBlockOrder, getBlockRootClientId, getBlock } = useSelect((select) => select("core/block-editor"), []); |
| 33 |
|
| 34 |
const hasChildBlocks = getBlockOrder(clientId).length > 0; |
| 35 |
const parentBlockId = getBlockRootClientId(clientId); |
| 36 |
const parentAttr = getBlock(parentBlockId)?.attributes || {}; |
| 37 |
const parentFlexDir = parentAttr?.containerFlexDirection?.device?.[deviceType]; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
useEffect( () => { |
| 42 |
setAttributes( { parentContainerFlexDirection: parentAttr?.containerFlexDirection }) |
| 43 |
}, [ parentFlexDir ]) |
| 44 |
|
| 45 |
const blockStyling = useMemo( |
| 46 |
() => dynamicCssFn(attributes, "editor", deviceType), |
| 47 |
[...editorStyleDependency(attributes), deviceType] |
| 48 |
); |
| 49 |
|
| 50 |
return ( |
| 51 |
<> |
| 52 |
<style> |
| 53 |
{blockStyling} |
| 54 |
{customCss} |
| 55 |
</style> |
| 56 |
<div |
| 57 |
ref={containerColumnRef} |
| 58 |
id={uniqueId} |
| 59 |
className={`${uniqueId} sp-smart-post-container-column${ |
| 60 |
columnAdditionalClass ? " " + columnAdditionalClass : "" |
| 61 |
} ${isHovered ? "sp-hovered" : ""} ${isSelected ? "is-selected" : ""}`} |
| 62 |
{...(columnAdditionalID ? { id: columnAdditionalID } : {})} |
| 63 |
onMouseEnter={() => setIsHovered(true)} |
| 64 |
onMouseLeave={() => setIsHovered(false)} |
| 65 |
> |
| 66 |
<div className="sp-smart-post-column-wrapper"> |
| 67 |
{columnBg?.color?.style === "video" && ( |
| 68 |
<div className="sp-smart-post-column__video-wrap"> |
| 69 |
{columnBgVideo && ( |
| 70 |
<video autoPlay loop muted> |
| 71 |
<source src={columnBgVideo.url} type="video/mp4" /> |
| 72 |
</video> |
| 73 |
)} |
| 74 |
</div> |
| 75 |
)} |
| 76 |
<div {...blockProps}> |
| 77 |
<InspectorControl |
| 78 |
RightIcon={panelBodyRightIcon} |
| 79 |
attributes={attributes} |
| 80 |
setAttributes={setAttributes} |
| 81 |
Inspector={Inspector} |
| 82 |
/> |
| 83 |
<div className="sp-smart-post-show-column"> |
| 84 |
<InnerBlocks |
| 85 |
orientation="horizontal" |
| 86 |
renderAppender={hasChildBlocks ? undefined : InnerBlocks.ButtonBlockAppender} |
| 87 |
/> |
| 88 |
{columnWrapperLink && ( |
| 89 |
<a |
| 90 |
className="sp-smart-post-column-block-wrapper-link" |
| 91 |
href={columnWrapperLinkUrl} |
| 92 |
rel="noopener noreferrer" |
| 93 |
target={columnWrapperLinkNewTab ? "_blank" : "_self"} |
| 94 |
content="" |
| 95 |
> |
| 96 |
hidden wrapper |
| 97 |
</a> |
| 98 |
)} |
| 99 |
</div> |
| 100 |
</div> |
| 101 |
</div> |
| 102 |
</div> |
| 103 |
</> |
| 104 |
); |
| 105 |
}; |
| 106 |
|
| 107 |
export default compose(addInitialAttr)(ColumnEdit); |
| 108 |
|