| 1 |
import { RichText } from "@wordpress/block-editor"; |
| 2 |
import { usePanelBodyContext } from "../../context"; |
| 3 |
import { useMemo, memo, useEffect } from "@wordpress/element"; |
| 4 |
import useIconList from "../../components/iconLibrary/useIconList"; |
| 5 |
|
| 6 |
import { |
| 7 |
ArrowRight, |
| 8 |
Square, |
| 9 |
Check, |
| 10 |
CheckCircle, |
| 11 |
ChevronRight, |
| 12 |
Dot, |
| 13 |
SquareBorder, |
| 14 |
RightArrow, |
| 15 |
Hand, |
| 16 |
FillStar, |
| 17 |
NpArrow, |
| 18 |
BulletPoint, |
| 19 |
CircleIcon, |
| 20 |
NumberIcon, |
| 21 |
} from "./icons"; |
| 22 |
|
| 23 |
const ICON_COMPONENTS = { |
| 24 |
CheckCircle, |
| 25 |
Dot, |
| 26 |
Square, |
| 27 |
FillStar, |
| 28 |
NpArrow, |
| 29 |
BulletPoint, |
| 30 |
RightArrow, |
| 31 |
Check, |
| 32 |
CircleIcon, |
| 33 |
SquareBorder, |
| 34 |
Hand, |
| 35 |
ArrowRight, |
| 36 |
ChevronRight, |
| 37 |
NumberIcon, |
| 38 |
}; |
| 39 |
|
| 40 |
const ICON_SOURCES = { |
| 41 |
ICON_SET: "iconSet", |
| 42 |
LIBRARY: "library", |
| 43 |
CUSTOM: "custom", |
| 44 |
}; |
| 45 |
|
| 46 |
const getEffectiveAttr = (childAttr, parentAttr, defaultValue) => childAttr ?? parentAttr ?? defaultValue; |
| 47 |
|
| 48 |
function IconRenderer({ |
| 49 |
sourceOfIcon, |
| 50 |
svgIconName, |
| 51 |
iconsClassName, |
| 52 |
imageSrc, |
| 53 |
imageAlt, |
| 54 |
onClick, |
| 55 |
togglePanelBody, |
| 56 |
currentIcon, |
| 57 |
}) { |
| 58 |
const iconContent = useMemo(() => { |
| 59 |
switch (sourceOfIcon) { |
| 60 |
case ICON_SOURCES.ICON_SET: |
| 61 |
if (svgIconName === "NumberIcon") { |
| 62 |
return null; |
| 63 |
} |
| 64 |
const IconComponent = ICON_COMPONENTS[svgIconName] || ICON_COMPONENTS.Dot; |
| 65 |
return <IconComponent />; |
| 66 |
|
| 67 |
case ICON_SOURCES.LIBRARY: |
| 68 |
return currentIcon ? ( |
| 69 |
<svg width={currentIcon?.width} height={currentIcon?.height} viewBox={currentIcon?.viewBox}> |
| 70 |
<path d={currentIcon?.path} /> |
| 71 |
</svg> |
| 72 |
) : null; |
| 73 |
|
| 74 |
case ICON_SOURCES.CUSTOM: |
| 75 |
return imageSrc ? ( |
| 76 |
<> |
| 77 |
<img src={imageSrc} alt={imageAlt || "Custom icon"} /> |
| 78 |
<div className="sp-smart-post-list-overlay" /> |
| 79 |
</> |
| 80 |
) : null; |
| 81 |
|
| 82 |
default: |
| 83 |
return null; |
| 84 |
} |
| 85 |
}, [sourceOfIcon, svgIconName, iconsClassName, imageSrc, imageAlt, currentIcon]); |
| 86 |
|
| 87 |
const iconContainerClasses = () => { |
| 88 |
let suffix = `-svg ${sourceOfIcon}`; |
| 89 |
if (sourceOfIcon === ICON_SOURCES.CUSTOM) { |
| 90 |
suffix = `-img ${sourceOfIcon}`; |
| 91 |
} |
| 92 |
return `sp-smart-post-list-icon${suffix}`; |
| 93 |
}; |
| 94 |
|
| 95 |
return ( |
| 96 |
<div |
| 97 |
className="sp-smart-post-list-icon-wrapper editor-selector-hover sp-panel-data" |
| 98 |
onClick={togglePanelBody} |
| 99 |
data-component="icon-image" |
| 100 |
> |
| 101 |
<div className="sp-smart-post-list-icon-container"> |
| 102 |
<div className={iconContainerClasses()}>{iconContent}</div> |
| 103 |
</div> |
| 104 |
</div> |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
function TitleRenderer({ titleText, enabled, onTitleChange, togglePanelBody }) { |
| 109 |
if (!enabled) { |
| 110 |
return null; |
| 111 |
} |
| 112 |
|
| 113 |
return ( |
| 114 |
<RichText |
| 115 |
tagName="p" |
| 116 |
value={titleText} |
| 117 |
className="sp-smart-post-list-title editor-selector-hover sp-panel-data" |
| 118 |
onChange={onTitleChange} |
| 119 |
onClick={togglePanelBody} |
| 120 |
data-component="content" |
| 121 |
placeholder="Enter list title..." |
| 122 |
/> |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
function DescriptionRenderer({ descriptionText, enabled, onDescriptionChange, togglePanelBody }) { |
| 127 |
if (!enabled) { |
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
return ( |
| 132 |
<RichText |
| 133 |
tagName="p" |
| 134 |
value={descriptionText} |
| 135 |
className="sp-smart-post-list-description editor-selector-hover sp-panel-data" |
| 136 |
onChange={onDescriptionChange} |
| 137 |
onClick={togglePanelBody} |
| 138 |
data-component="content" |
| 139 |
placeholder="Enter description..." |
| 140 |
/> |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
function BadgeRenderer({ badgeLabel, visible, togglePanelBody }) { |
| 145 |
if (!visible) { |
| 146 |
return null; |
| 147 |
} |
| 148 |
|
| 149 |
return ( |
| 150 |
<span |
| 151 |
className="sp-smart-post-list-badge editor-selector-hover sp-panel-data" |
| 152 |
onClick={togglePanelBody} |
| 153 |
data-component="badge" |
| 154 |
> |
| 155 |
{badgeLabel} |
| 156 |
</span> |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
function RenderHtml({ attributes, setAttributes }) { |
| 161 |
const { uniqueId, titleText, descriptionText, badgeLabel, currentIcon } = attributes; |
| 162 |
const { togglePanelBody } = usePanelBodyContext(); |
| 163 |
const iconList = useIconList(); |
| 164 |
|
| 165 |
const iconProps = { |
| 166 |
sourceOfIcon: getEffectiveAttr(attributes.iconSource, attributes.iconSourceParent, ICON_SOURCES.LIBRARY), |
| 167 |
enabledIcon: getEffectiveAttr(attributes.iconEnable, attributes.iconEnableParent, false), |
| 168 |
svgIconName: getEffectiveAttr(attributes.svgIconName, attributes.parentSvgIconName, "Dot"), |
| 169 |
iconsClassName: getEffectiveAttr(attributes.iconName, attributes.parentIconName, ""), |
| 170 |
imageSrc: getEffectiveAttr(attributes.iconSourceCustom?.url, attributes.parentIconSourceCustom?.url, ""), |
| 171 |
imageAlt: getEffectiveAttr(attributes.iconSourceCustom?.alt, attributes.parentIconSourceCustom?.alt, ""), |
| 172 |
currentIcon: currentIcon, |
| 173 |
}; |
| 174 |
|
| 175 |
const titleProps = { |
| 176 |
titleText, |
| 177 |
enabled: getEffectiveAttr(attributes.listTitleEnable, attributes.parentListTitleEnable, true), |
| 178 |
onTitleChange: handleTitleChange, |
| 179 |
}; |
| 180 |
|
| 181 |
const descriptionProps = { |
| 182 |
descriptionText, |
| 183 |
enabled: getEffectiveAttr(attributes.descriptionEnable, attributes.parentDescriptionEnable, false), |
| 184 |
onDescriptionChange: handleDescriptionChange, |
| 185 |
}; |
| 186 |
|
| 187 |
const badgeProps = { |
| 188 |
badgeLabel, |
| 189 |
visible: (attributes.badgeEnable || false) && badgeLabel && badgeLabel.length > 0, |
| 190 |
}; |
| 191 |
|
| 192 |
function handleTitleChange(newValue) { |
| 193 |
setAttributes({ titleText: newValue }); |
| 194 |
} |
| 195 |
|
| 196 |
function handleDescriptionChange(newValue) { |
| 197 |
setAttributes({ descriptionText: newValue }); |
| 198 |
} |
| 199 |
|
| 200 |
useEffect(() => { |
| 201 |
setAttributes({ currentIcon: iconList[getEffectiveAttr(attributes.iconName, attributes.parentIconName, "")] }); |
| 202 |
}, [iconList, attributes.iconName, attributes.parentIconName]); |
| 203 |
|
| 204 |
return ( |
| 205 |
<li className="sp-smart-post-smart-list-wrapper" id={uniqueId}> |
| 206 |
<div className="sp-smart-post-list-link" data-component="general" onClick={togglePanelBody}> |
| 207 |
{iconProps.enabledIcon && <IconRenderer {...iconProps} togglePanelBody={togglePanelBody} />} |
| 208 |
|
| 209 |
<div className="sp-smart-post-list-text"> |
| 210 |
{(titleProps.enabled || attributes.badgeEnable) && ( |
| 211 |
<> |
| 212 |
<div className="sp-smart-post-list-title-wrapper"> |
| 213 |
<TitleRenderer {...titleProps} togglePanelBody={togglePanelBody} /> |
| 214 |
<BadgeRenderer {...badgeProps} togglePanelBody={togglePanelBody} /> |
| 215 |
</div> |
| 216 |
</> |
| 217 |
)} |
| 218 |
<DescriptionRenderer {...descriptionProps} togglePanelBody={togglePanelBody} /> |
| 219 |
</div> |
| 220 |
</div> |
| 221 |
</li> |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
export default memo(RenderHtml); |
| 226 |
|