| 1 |
import { useEffect, useMemo } from "@wordpress/element"; |
| 2 |
import { RichText, useBlockProps } from "@wordpress/block-editor"; |
| 3 |
import InspectorControl from "../../components/inspectorControls/inspectorControls"; |
| 4 |
import { panelBodyRightIcon, panelBodyTitleIcon, SectionNineteenImage } from "../../icons/icons"; |
| 5 |
import { jsonStringify } from "../shared/helpFn"; |
| 6 |
import dynamicCssFn from "./dynamicCss"; |
| 7 |
import Inspector from "./inspector"; |
| 8 |
import HeadingWrapper from "./headingWrapper"; |
| 9 |
import { googleFonts, useDeviceType } from "../../controls/controls"; |
| 10 |
import classNames from "classnames"; |
| 11 |
import addInitialAttr from "../shared/addInitialAttr"; |
| 12 |
import { compose } from "@wordpress/compose"; |
| 13 |
|
| 14 |
const SectionHeadingEdit = ({ attributes, setAttributes }) => { |
| 15 |
const { |
| 16 |
uniqueId, |
| 17 |
showSectionHeading, |
| 18 |
fontListsEditPage, |
| 19 |
sectionHeadingLabel, |
| 20 |
sectionHeadingLinkUrl, |
| 21 |
sectionHeadingStyle, |
| 22 |
sectionHeadingStyleBackgroundColor, |
| 23 |
sectionHeadingHTMLTag: HeadingTag, |
| 24 |
showSubHeading, |
| 25 |
sectionSubHeadingAliment, |
| 26 |
subHeadingLabel, |
| 27 |
additionalCssClass, |
| 28 |
customCss, |
| 29 |
sectionHeadingTypography, |
| 30 |
sectionSubHeadingTypography, |
| 31 |
sectionHeadingGlobalTypo, |
| 32 |
sectionSubHeadingGlobalTypography, |
| 33 |
} = attributes; |
| 34 |
|
| 35 |
const googleFontLists = [sectionHeadingTypography, sectionSubHeadingTypography]; |
| 36 |
useEffect(() => { |
| 37 |
setAttributes({ |
| 38 |
fontLists: jsonStringify(googleFonts(googleFontLists, "frontend")), |
| 39 |
fontListsEditPage: googleFonts(googleFontLists), |
| 40 |
}); |
| 41 |
}, googleFontLists); |
| 42 |
|
| 43 |
const deviceType = useDeviceType(); |
| 44 |
|
| 45 |
const blockStyling = useMemo(() => dynamicCssFn(attributes, deviceType), [attributes, deviceType]); |
| 46 |
const blockProps = useBlockProps(); |
| 47 |
|
| 48 |
const className = classNames("sp-section-heading-container", sectionHeadingStyle); |
| 49 |
const renderSectionNineteenImage = sectionHeadingStyle === "section-heading-nineteen" && ( |
| 50 |
<SectionNineteenImage sectionHeadingStyleBackgroundColor={sectionHeadingStyleBackgroundColor} /> |
| 51 |
); |
| 52 |
|
| 53 |
return ( |
| 54 |
<div {...blockProps}> |
| 55 |
<style> |
| 56 |
{fontListsEditPage} |
| 57 |
{blockStyling} |
| 58 |
{customCss} |
| 59 |
</style> |
| 60 |
<InspectorControl |
| 61 |
TitleIcon={panelBodyTitleIcon} |
| 62 |
RightIcon={panelBodyRightIcon} |
| 63 |
Inspector={Inspector} |
| 64 |
attributes={attributes} |
| 65 |
setAttributes={setAttributes} |
| 66 |
/> |
| 67 |
|
| 68 |
{showSectionHeading && ( |
| 69 |
<div |
| 70 |
id={uniqueId} |
| 71 |
className={classNames( |
| 72 |
"sp-smart-post-wrapper", |
| 73 |
"sp-section-heading-wrapper", |
| 74 |
additionalCssClass ? additionalCssClass : "" |
| 75 |
)} |
| 76 |
> |
| 77 |
<HeadingWrapper sectionHeadingLinkUrl={sectionHeadingLinkUrl} classNames={className}> |
| 78 |
<div className={"section-heading-tag"}> |
| 79 |
<HeadingTag |
| 80 |
className={classNames( |
| 81 |
"sp-heading-text", |
| 82 |
sectionHeadingGlobalTypo?.class ? sectionHeadingGlobalTypo.class : "" |
| 83 |
)} |
| 84 |
> |
| 85 |
<RichText |
| 86 |
tagName={"span"} |
| 87 |
value={sectionHeadingLabel} |
| 88 |
placeholder={"Section heading"} |
| 89 |
onChange={(newValue) => |
| 90 |
setAttributes({ |
| 91 |
sectionHeadingLabel: newValue, |
| 92 |
}) |
| 93 |
} |
| 94 |
contentEditable={true} |
| 95 |
/> |
| 96 |
{renderSectionNineteenImage} |
| 97 |
</HeadingTag> |
| 98 |
</div> |
| 99 |
</HeadingWrapper> |
| 100 |
{showSubHeading && ( |
| 101 |
<div |
| 102 |
className={classNames( |
| 103 |
"sp-section-subheading-container", |
| 104 |
"sp-d-flex", |
| 105 |
"sp-justify-" + sectionSubHeadingAliment |
| 106 |
)} |
| 107 |
> |
| 108 |
<RichText |
| 109 |
className={classNames( |
| 110 |
"sp-subheading-text", |
| 111 |
sectionSubHeadingGlobalTypography?.class && sectionSubHeadingGlobalTypography.class |
| 112 |
)} |
| 113 |
tagName="span" |
| 114 |
value={subHeadingLabel} |
| 115 |
placeholder={"Here is the subheading"} |
| 116 |
onChange={(newValue) => |
| 117 |
setAttributes({ |
| 118 |
subHeadingLabel: newValue, |
| 119 |
}) |
| 120 |
} |
| 121 |
/> |
| 122 |
</div> |
| 123 |
)} |
| 124 |
</div> |
| 125 |
)} |
| 126 |
</div> |
| 127 |
); |
| 128 |
}; |
| 129 |
|
| 130 |
export default compose(addInitialAttr)(SectionHeadingEdit); |
| 131 |
|