| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { |
| 4 |
BlockControls, |
| 5 |
InspectorControls, |
| 6 |
useBlockProps, |
| 7 |
} from '@wordpress/block-editor'; |
| 8 |
import { |
| 9 |
Dropdown, |
| 10 |
PanelBody, |
| 11 |
ToolbarButton, |
| 12 |
ToolbarGroup, |
| 13 |
} from '@wordpress/components'; |
| 14 |
import { useEffect } from '@wordpress/element'; |
| 15 |
import { applyFilters } from '@wordpress/hooks'; |
| 16 |
import { __ } from '@wordpress/i18n'; |
| 17 |
|
| 18 |
import ColorPicker from '../../components/color-picker'; |
| 19 |
import ImagePicker from '../../components/image-picker'; |
| 20 |
import ProNote from '../../components/pro-note'; |
| 21 |
import RangeControl from '../../components/range-control'; |
| 22 |
import ResponsiveToggle from '../../components/responsive-toggle'; |
| 23 |
import useResponsive from '../../hooks/use-responsive'; |
| 24 |
import { maybeDecode, maybeEncode } from '../../utils/encode-decode'; |
| 25 |
import getIcon from '../../utils/get-icon'; |
| 26 |
|
| 27 |
const { GHOSTKIT } = window; |
| 28 |
|
| 29 |
const { version } = window.ghostkitVariables; |
| 30 |
|
| 31 |
const { shapes } = GHOSTKIT; |
| 32 |
|
| 33 |
function getShapeData(svg) { |
| 34 |
let result = { |
| 35 |
allow_flip_vertical: true, |
| 36 |
allow_flip_horizontal: true, |
| 37 |
}; |
| 38 |
let ready = false; |
| 39 |
|
| 40 |
Object.keys(shapes).forEach((k) => { |
| 41 |
const data = shapes[k]; |
| 42 |
|
| 43 |
Object.keys(data.shapes).forEach((i) => { |
| 44 |
const shape = data.shapes[i]; |
| 45 |
|
| 46 |
if (shape.svg && shape.svg === maybeDecode(svg) && !ready) { |
| 47 |
result = shape; |
| 48 |
ready = true; |
| 49 |
} |
| 50 |
}); |
| 51 |
}); |
| 52 |
|
| 53 |
return result; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Block Edit Class. |
| 58 |
* |
| 59 |
* @param props |
| 60 |
*/ |
| 61 |
export default function BlockEdit(props) { |
| 62 |
const { attributes, setAttributes } = props; |
| 63 |
const { svg, flipVertical, flipHorizontal, color } = attributes; |
| 64 |
|
| 65 |
let { className = '' } = props; |
| 66 |
|
| 67 |
const { device } = useResponsive(); |
| 68 |
|
| 69 |
// Mounted. |
| 70 |
useEffect(() => { |
| 71 |
// Block inserted on the page. |
| 72 |
if (!svg) { |
| 73 |
const newAttrs = {}; |
| 74 |
|
| 75 |
// Set default svg. |
| 76 |
Object.keys(shapes).forEach((k) => { |
| 77 |
const data = shapes[k]; |
| 78 |
|
| 79 |
Object.keys(data.shapes).forEach((i) => { |
| 80 |
const shape = data.shapes[i]; |
| 81 |
|
| 82 |
if (shape.svg && !newAttrs.svg) { |
| 83 |
newAttrs.svg = shape.svg; |
| 84 |
} |
| 85 |
}); |
| 86 |
}); |
| 87 |
|
| 88 |
// Remove top and bottom margins. |
| 89 |
newAttrs.ghostkitSpacings = { |
| 90 |
marginTop: '0', |
| 91 |
marginBottom: '0', |
| 92 |
'!important': true, |
| 93 |
}; |
| 94 |
|
| 95 |
setAttributes(newAttrs); |
| 96 |
} |
| 97 |
// eslint-disable-next-line react-hooks/exhaustive-deps |
| 98 |
}, []); |
| 99 |
|
| 100 |
function getShapesPicker() { |
| 101 |
return ( |
| 102 |
<div className="ghostkit-shape-divider-control-styles"> |
| 103 |
{Object.keys(shapes).map((k) => { |
| 104 |
const data = shapes[k]; |
| 105 |
const shapesOptions = []; |
| 106 |
|
| 107 |
Object.keys(data.shapes).forEach((i) => { |
| 108 |
const shape = data.shapes[i]; |
| 109 |
|
| 110 |
shapesOptions.push({ |
| 111 |
label: shape.label, |
| 112 |
value: shape.svg, |
| 113 |
image: ( |
| 114 |
<div |
| 115 |
className="ghostkit-shape-divider" |
| 116 |
style={{ |
| 117 |
'--gkt-shape-divider__color': color, |
| 118 |
}} |
| 119 |
dangerouslySetInnerHTML={{ |
| 120 |
__html: shape.svg, |
| 121 |
}} |
| 122 |
/> |
| 123 |
), |
| 124 |
className: `ghostkit-shape-divider-control-styles-item-${k}-${shape.name}`, |
| 125 |
}); |
| 126 |
}); |
| 127 |
|
| 128 |
return ( |
| 129 |
<div key={k}> |
| 130 |
<h3>{data.name}</h3> |
| 131 |
<ImagePicker |
| 132 |
value={maybeDecode(svg)} |
| 133 |
options={shapesOptions} |
| 134 |
onChange={(value) => { |
| 135 |
const shapeData = getShapeData(value); |
| 136 |
|
| 137 |
setAttributes({ |
| 138 |
svg: maybeEncode(value), |
| 139 |
flipVertical: |
| 140 |
shapeData.allow_flip_vertical |
| 141 |
? flipVertical |
| 142 |
: false, |
| 143 |
flipHorizontal: |
| 144 |
shapeData.allow_flip_horizontal |
| 145 |
? flipHorizontal |
| 146 |
: false, |
| 147 |
}); |
| 148 |
}} |
| 149 |
/> |
| 150 |
<ProNote title={__('Pro Shapes', 'ghostkit')}> |
| 151 |
<p> |
| 152 |
{__( |
| 153 |
'Additional 30 shapes available in the Ghost Kit Pro plugin only', |
| 154 |
'ghostkit' |
| 155 |
)} |
| 156 |
</p> |
| 157 |
<ProNote.Button |
| 158 |
target="_blank" |
| 159 |
rel="noopener noreferrer" |
| 160 |
href={`https://www.ghostkit.io/docs/blocks/shape-divider/?utm_source=plugin&utm_medium=block_settings&utm_campaign=pro_shapes&utm_content=${version}`} |
| 161 |
> |
| 162 |
{__('Read More', 'ghostkit')} |
| 163 |
</ProNote.Button> |
| 164 |
</ProNote> |
| 165 |
</div> |
| 166 |
); |
| 167 |
})} |
| 168 |
</div> |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
const shapeData = getShapeData(svg); |
| 173 |
|
| 174 |
className = classnames( |
| 175 |
'ghostkit-shape-divider', |
| 176 |
{ |
| 177 |
'ghostkit-shape-divider-flip-vertical': |
| 178 |
shapeData.allow_flip_vertical && flipVertical, |
| 179 |
'ghostkit-shape-divider-flip-horizontal': |
| 180 |
shapeData.allow_flip_horizontal && flipHorizontal, |
| 181 |
}, |
| 182 |
className |
| 183 |
); |
| 184 |
|
| 185 |
className = applyFilters('ghostkit.editor.className', className, props); |
| 186 |
|
| 187 |
const blockProps = useBlockProps({ |
| 188 |
className, |
| 189 |
dangerouslySetInnerHTML: { __html: maybeDecode(svg) }, |
| 190 |
}); |
| 191 |
|
| 192 |
let heightName = 'height'; |
| 193 |
let widthName = 'width'; |
| 194 |
|
| 195 |
if (device) { |
| 196 |
heightName = `${device}_${heightName}`; |
| 197 |
widthName = `${device}_${widthName}`; |
| 198 |
} |
| 199 |
|
| 200 |
return ( |
| 201 |
<> |
| 202 |
<BlockControls> |
| 203 |
<ToolbarGroup> |
| 204 |
{shapeData.allow_flip_vertical ? ( |
| 205 |
<ToolbarButton |
| 206 |
icon={getIcon('icon-flip-vertical')} |
| 207 |
title={__('Vertical Flip', 'ghostkit')} |
| 208 |
onClick={() => |
| 209 |
setAttributes({ flipVertical: !flipVertical }) |
| 210 |
} |
| 211 |
isActive={flipVertical} |
| 212 |
/> |
| 213 |
) : null} |
| 214 |
|
| 215 |
{shapeData.allow_flip_horizontal ? ( |
| 216 |
<ToolbarButton |
| 217 |
icon={getIcon('icon-flip-horizontal')} |
| 218 |
title={__('Horizontal Flip', 'ghostkit')} |
| 219 |
onClick={() => |
| 220 |
setAttributes({ |
| 221 |
flipHorizontal: !flipHorizontal, |
| 222 |
}) |
| 223 |
} |
| 224 |
isActive={flipHorizontal} |
| 225 |
/> |
| 226 |
) : null} |
| 227 |
|
| 228 |
<Dropdown |
| 229 |
renderToggle={({ onToggle }) => ( |
| 230 |
<ToolbarButton |
| 231 |
label={__('Shapes', 'ghostkit')} |
| 232 |
icon="edit" |
| 233 |
className="components-toolbar__control" |
| 234 |
onClick={onToggle} |
| 235 |
/> |
| 236 |
)} |
| 237 |
renderContent={() => ( |
| 238 |
<div |
| 239 |
style={{ |
| 240 |
minWidth: 260, |
| 241 |
}} |
| 242 |
> |
| 243 |
{getShapesPicker()} |
| 244 |
</div> |
| 245 |
)} |
| 246 |
/> |
| 247 |
</ToolbarGroup> |
| 248 |
</BlockControls> |
| 249 |
<InspectorControls> |
| 250 |
<PanelBody title={__('Style', 'ghostkit')}> |
| 251 |
{getShapesPicker()} |
| 252 |
</PanelBody> |
| 253 |
<PanelBody title={__('Size', 'ghostkit')}> |
| 254 |
<RangeControl |
| 255 |
label={ |
| 256 |
<> |
| 257 |
{__('Height', 'ghostkit')} |
| 258 |
<ResponsiveToggle |
| 259 |
checkActive={(checkMedia) => { |
| 260 |
return !!attributes[ |
| 261 |
`${checkMedia}_height` |
| 262 |
]; |
| 263 |
}} |
| 264 |
/> |
| 265 |
</> |
| 266 |
} |
| 267 |
value={ |
| 268 |
attributes[heightName] |
| 269 |
? parseInt(attributes[heightName], 10) |
| 270 |
: '' |
| 271 |
} |
| 272 |
onChange={(value) => { |
| 273 |
setAttributes({ |
| 274 |
[heightName]: `${ |
| 275 |
typeof value === 'number' ? value : '' |
| 276 |
}`, |
| 277 |
}); |
| 278 |
}} |
| 279 |
min={1} |
| 280 |
max={700} |
| 281 |
allowCustomMax |
| 282 |
/> |
| 283 |
<RangeControl |
| 284 |
label={ |
| 285 |
<> |
| 286 |
{__('Width', 'ghostkit')} |
| 287 |
<ResponsiveToggle |
| 288 |
checkActive={(checkMedia) => { |
| 289 |
return !!attributes[ |
| 290 |
`${checkMedia}_width` |
| 291 |
]; |
| 292 |
}} |
| 293 |
/> |
| 294 |
</> |
| 295 |
} |
| 296 |
value={ |
| 297 |
attributes[widthName] |
| 298 |
? parseInt(attributes[widthName], 10) |
| 299 |
: '' |
| 300 |
} |
| 301 |
onChange={(value) => { |
| 302 |
setAttributes({ |
| 303 |
[widthName]: `${ |
| 304 |
typeof value === 'number' ? value : '' |
| 305 |
}`, |
| 306 |
}); |
| 307 |
}} |
| 308 |
min={100} |
| 309 |
max={400} |
| 310 |
allowCustomMin |
| 311 |
allowCustomMax |
| 312 |
/> |
| 313 |
</PanelBody> |
| 314 |
<PanelBody> |
| 315 |
<ColorPicker |
| 316 |
label={__('Color', 'ghostkit')} |
| 317 |
value={color} |
| 318 |
onChange={(val) => setAttributes({ color: val })} |
| 319 |
alpha |
| 320 |
/> |
| 321 |
</PanelBody> |
| 322 |
</InspectorControls> |
| 323 |
|
| 324 |
<div {...blockProps} /> |
| 325 |
</> |
| 326 |
); |
| 327 |
} |
| 328 |
|