| 1 |
import { Button, ColorPicker, ColorIndicator, Popover, Tooltip } from "@wordpress/components"; |
| 2 |
import "./editor.scss"; |
| 3 |
import { useMemo, memo, useEffect, useState } from "@wordpress/element"; |
| 4 |
import { GlobalIcon, ResetIcon } from "../../icons/icons"; |
| 5 |
import { dispatch, useSelect, select } from "@wordpress/data"; |
| 6 |
import { priceLink } from "../../blocks/shared/helpFn"; |
| 7 |
|
| 8 |
const SpColorPicker = (props) => { |
| 9 |
const { |
| 10 |
setAttributes, |
| 11 |
value, |
| 12 |
attributesKey, |
| 13 |
label, |
| 14 |
colorType = "normal", |
| 15 |
attributes = {}, |
| 16 |
onChange, |
| 17 |
resetButton = true, |
| 18 |
defaultColor = "", |
| 19 |
pro = false |
| 20 |
} = props; |
| 21 |
|
| 22 |
const [isVisible, setIsVisible] = useState(false); |
| 23 |
const [globalColor, setGlobalColor] = useState({}); |
| 24 |
const activeColor = value || attributes?.[colorType] || attributes; |
| 25 |
|
| 26 |
const toggleVisible = () => { |
| 27 |
setIsVisible((state) => !state); |
| 28 |
}; |
| 29 |
|
| 30 |
const themeAllColors = useSelect((_select) => { |
| 31 |
const settings = _select("core/block-editor").getSettings(); |
| 32 |
const colors = settings?.colors || []; |
| 33 |
const palette = settings?.__experimentalFeatures?.color?.palette; |
| 34 |
|
| 35 |
const originalColors = palette?.custom ? palette?.theme : undefined; |
| 36 |
|
| 37 |
return { colors: colors, originalColors: originalColors }; |
| 38 |
}, []); |
| 39 |
|
| 40 |
const mergedThemeColors = useMemo( |
| 41 |
() => [...(themeAllColors.originalColors || []), ...(themeAllColors.colors || [])], |
| 42 |
[themeAllColors.originalColors, themeAllColors.colors] |
| 43 |
); |
| 44 |
|
| 45 |
// Get Global Colors. |
| 46 |
const getPresetColors = useSelect((_select) => _select("smartpost/global-settings")?.getPresetColors(), []); |
| 47 |
const getCustomColors = useSelect((_select) => _select("smartpost/global-settings")?.getCustomColors(), []); |
| 48 |
useEffect(() => { |
| 49 |
const colorMap = {}; |
| 50 |
const presetColors = getPresetColors?.map((item) => ({ |
| 51 |
...item, |
| 52 |
colorRoot: `var(--smart-post-${item.slug.toLowerCase().replaceAll("_", "-")})`, |
| 53 |
})); |
| 54 |
const customColors = getCustomColors?.map((item) => ({ |
| 55 |
...item, |
| 56 |
colorRoot: `var(--smart-post-${item.slug.toLowerCase().replaceAll("_", "-")})`, |
| 57 |
})); |
| 58 |
if (presetColors?.length > 0 || customColors?.length > 0) { |
| 59 |
[...presetColors, ...customColors].forEach((c) => (colorMap[c.colorRoot] = c.color)); |
| 60 |
setGlobalColor({ |
| 61 |
presetColors: presetColors, |
| 62 |
customColors: customColors, |
| 63 |
colorMap: colorMap, |
| 64 |
}); |
| 65 |
} |
| 66 |
}, [getPresetColors, getCustomColors]); |
| 67 |
|
| 68 |
const colorPickerValue = (pickerValue) => { |
| 69 |
if (pickerValue?.startsWith("#")) { |
| 70 |
return pickerValue; |
| 71 |
} |
| 72 |
return globalColor?.colorMap?.[pickerValue] || ""; |
| 73 |
}; |
| 74 |
|
| 75 |
const handleColorChange = (_value) => { |
| 76 |
if (colorType && typeof attributes?.[colorType] !== "undefined") { |
| 77 |
setAttributes({ |
| 78 |
[attributesKey]: { ...attributes, [colorType]: _value }, |
| 79 |
}); |
| 80 |
} else { |
| 81 |
setAttributes({ [attributesKey]: _value }); |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
const setDefault = () => { |
| 86 |
if (onChange) { |
| 87 |
onChange(defaultColor); |
| 88 |
} else { |
| 89 |
handleColorChange(defaultColor); |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
useEffect(() => { |
| 94 |
const clickOutSite = (e) => { |
| 95 |
const target = e.target.closest(".sp-smart-post-picker-pallet-wrapper"); |
| 96 |
const buttonTarget = e.target.closest(".sp-smart-post-color-picker-right-area .sp-color-picker-btn"); |
| 97 |
const familyTarget = e.target.closest(".css-1nmdiq5-menu"); |
| 98 |
if (!target && isVisible && !buttonTarget && !familyTarget) { |
| 99 |
setIsVisible(false); |
| 100 |
} |
| 101 |
}; |
| 102 |
window.addEventListener("click", clickOutSite); |
| 103 |
|
| 104 |
return () => window.removeEventListener("click", clickOutSite); |
| 105 |
}); |
| 106 |
const handleGlobalSettings = () => { |
| 107 |
const sidebarName = "smart-post-show-pro-global-settings/sidebar"; |
| 108 |
if (select("core/edit-post")) { |
| 109 |
dispatch("core/edit-post").openGeneralSidebar(sidebarName); |
| 110 |
} else if (select("core/edit-site")) { |
| 111 |
dispatch("core/edit-site").openGeneralSidebar(sidebarName); |
| 112 |
} else if (select("core/customize-widgets")) { |
| 113 |
dispatch("core/customize-widgets").openGeneralSidebar(sidebarName); |
| 114 |
} else { |
| 115 |
console.log("Could not determine current editor type"); |
| 116 |
} |
| 117 |
}; |
| 118 |
const SpGlobalColors = |
| 119 |
Object?.keys(globalColor).length > 0 ? [...globalColor?.presetColors, ...globalColor?.customColors] : []; |
| 120 |
|
| 121 |
return ( |
| 122 |
<div className={`sp-smart-post-color-picker sp-smart-post-component-mb${pro ? " sp-is-pro" : ""}`}> |
| 123 |
<p className="sp-smart-post-component-title"> |
| 124 |
{label} |
| 125 |
{pro && <a target="_blank" href={priceLink} className="sp-smart-pro-text">(Pro)</a>} |
| 126 |
</p> |
| 127 |
<div className="sp-smart-post-color-picker-right-area"> |
| 128 |
{resetButton && ( |
| 129 |
<Button className="sp-smart-post-header-control-reset" onClick={() => setDefault()}> |
| 130 |
<ResetIcon /> |
| 131 |
</Button> |
| 132 |
)} |
| 133 |
<Button className="sp-color-picker-btn" onClick={() => toggleVisible()}> |
| 134 |
<ColorIndicator colorValue={value} /> |
| 135 |
</Button> |
| 136 |
|
| 137 |
{isVisible && ( |
| 138 |
<Popover shift={true} focusOnMount={false}> |
| 139 |
<div className={`sp-smart-post-picker-pallet-wrapper`}> |
| 140 |
<ColorPicker |
| 141 |
color={colorPickerValue(value)} |
| 142 |
onChange={onChange || handleColorChange} |
| 143 |
enableAlpha |
| 144 |
/> |
| 145 |
<p className="sp-default-color-pallet sp-default-theme-color">Theme Color</p> |
| 146 |
<ul className="sp-smart-post-color-picker-palette"> |
| 147 |
{mergedThemeColors?.map((item, i) => ( |
| 148 |
<Tooltip key={i} text={item?.name}> |
| 149 |
<li |
| 150 |
style={{ |
| 151 |
backgroundColor: item.color, |
| 152 |
}} |
| 153 |
className={`${item.color === activeColor ? "active" : ""}`} |
| 154 |
> |
| 155 |
<Button |
| 156 |
aria-label={item.name} |
| 157 |
onClick={() => |
| 158 |
onChange ? onChange(item.color) : handleColorChange(item.color) |
| 159 |
} |
| 160 |
value={item.color} |
| 161 |
/> |
| 162 |
</li> |
| 163 |
</Tooltip> |
| 164 |
))} |
| 165 |
</ul> |
| 166 |
{SpGlobalColors?.length > 0 && ( |
| 167 |
<> |
| 168 |
<div className="sp-default-color-pallet-header sp-d-flex sp-space-between"> |
| 169 |
<p className="sp-default-color-pallet sp-default-smart-color">Smart Color</p> |
| 170 |
<span |
| 171 |
className="sp-global-settings sp-right sp-pointer" |
| 172 |
onClick={handleGlobalSettings} |
| 173 |
> |
| 174 |
<GlobalIcon /> |
| 175 |
</span> |
| 176 |
</div> |
| 177 |
<ul className="sp-smart-post-color-picker-palette"> |
| 178 |
{SpGlobalColors?.map((item, i) => ( |
| 179 |
<Tooltip key={i} text={item?.name}> |
| 180 |
<li |
| 181 |
style={{ |
| 182 |
backgroundColor: item.colorRoot, |
| 183 |
}} |
| 184 |
className={`${item.colorRoot === activeColor ? "active" : ""}`} |
| 185 |
> |
| 186 |
<Button |
| 187 |
aria-label={item.name} |
| 188 |
onClick={() => |
| 189 |
onChange |
| 190 |
? onChange(item.colorRoot) |
| 191 |
: handleColorChange(item.colorRoot) |
| 192 |
} |
| 193 |
/> |
| 194 |
</li> |
| 195 |
</Tooltip> |
| 196 |
))} |
| 197 |
</ul> |
| 198 |
</> |
| 199 |
)} |
| 200 |
{/* { globalColor?.customColors.length > 0 && ( |
| 201 |
<> |
| 202 |
<p className="sp-default-color-pallet sp-default-smart-color sp-left"> |
| 203 |
Custom Color |
| 204 |
</p> |
| 205 |
<ul className="sp-smart-post-color-picker-palette"> |
| 206 |
{ globalColor?.customColors?.map( |
| 207 |
( item, i ) => ( |
| 208 |
<li |
| 209 |
key={ i } |
| 210 |
style={ { |
| 211 |
backgroundColor: |
| 212 |
item.colorRoot, |
| 213 |
} } |
| 214 |
className={ `${ |
| 215 |
item.colorRoot === |
| 216 |
activeColor |
| 217 |
? 'active' |
| 218 |
: '' |
| 219 |
}` } |
| 220 |
> |
| 221 |
<Button |
| 222 |
aria-label={ |
| 223 |
'smart post custom color' |
| 224 |
} |
| 225 |
onClick={ () => |
| 226 |
onChange |
| 227 |
? onChange( |
| 228 |
item.colorRoot |
| 229 |
) |
| 230 |
: handleColorChange( |
| 231 |
item.colorRoot |
| 232 |
) |
| 233 |
} |
| 234 |
/> |
| 235 |
</li> |
| 236 |
) |
| 237 |
) } |
| 238 |
</ul> |
| 239 |
</> |
| 240 |
) } */} |
| 241 |
</div> |
| 242 |
</Popover> |
| 243 |
)} |
| 244 |
</div> |
| 245 |
</div> |
| 246 |
); |
| 247 |
}; |
| 248 |
|
| 249 |
export default memo(SpColorPicker); |
| 250 |
|