import classnames from 'classnames/dedupe';
import {
BlockControls,
InspectorControls,
useBlockProps,
} from '@wordpress/block-editor';
import {
Dropdown,
PanelBody,
ToolbarButton,
ToolbarGroup,
} from '@wordpress/components';
import { useEffect } from '@wordpress/element';
import { applyFilters } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
import ColorPicker from '../../components/color-picker';
import ImagePicker from '../../components/image-picker';
import ProNote from '../../components/pro-note';
import RangeControl from '../../components/range-control';
import ResponsiveToggle from '../../components/responsive-toggle';
import useResponsive from '../../hooks/use-responsive';
import { maybeDecode, maybeEncode } from '../../utils/encode-decode';
import getIcon from '../../utils/get-icon';
const { GHOSTKIT } = window;
const { version } = window.ghostkitVariables;
const { shapes } = GHOSTKIT;
function getShapeData(svg) {
let result = {
allow_flip_vertical: true,
allow_flip_horizontal: true,
};
let ready = false;
Object.keys(shapes).forEach((k) => {
const data = shapes[k];
Object.keys(data.shapes).forEach((i) => {
const shape = data.shapes[i];
if (shape.svg && shape.svg === maybeDecode(svg) && !ready) {
result = shape;
ready = true;
}
});
});
return result;
}
/**
* Block Edit Class.
*
* @param props
*/
export default function BlockEdit(props) {
const { attributes, setAttributes } = props;
const { svg, flipVertical, flipHorizontal, color } = attributes;
let { className = '' } = props;
const { device } = useResponsive();
// Mounted.
useEffect(() => {
// Block inserted on the page.
if (!svg) {
const newAttrs = {};
// Set default svg.
Object.keys(shapes).forEach((k) => {
const data = shapes[k];
Object.keys(data.shapes).forEach((i) => {
const shape = data.shapes[i];
if (shape.svg && !newAttrs.svg) {
newAttrs.svg = shape.svg;
}
});
});
// Remove top and bottom margins.
newAttrs.ghostkitSpacings = {
marginTop: '0',
marginBottom: '0',
'!important': true,
};
setAttributes(newAttrs);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
function getShapesPicker() {
return (
{Object.keys(shapes).map((k) => {
const data = shapes[k];
const shapesOptions = [];
Object.keys(data.shapes).forEach((i) => {
const shape = data.shapes[i];
shapesOptions.push({
label: shape.label,
value: shape.svg,
image: (
),
className: `ghostkit-shape-divider-control-styles-item-${k}-${shape.name}`,
});
});
return (
{data.name}
{
const shapeData = getShapeData(value);
setAttributes({
svg: maybeEncode(value),
flipVertical:
shapeData.allow_flip_vertical
? flipVertical
: false,
flipHorizontal:
shapeData.allow_flip_horizontal
? flipHorizontal
: false,
});
}}
/>
{__(
'Additional 30 shapes available in the Ghost Kit Pro plugin only',
'ghostkit'
)}
{__('Read More', 'ghostkit')}
);
})}
);
}
const shapeData = getShapeData(svg);
className = classnames(
'ghostkit-shape-divider',
{
'ghostkit-shape-divider-flip-vertical':
shapeData.allow_flip_vertical && flipVertical,
'ghostkit-shape-divider-flip-horizontal':
shapeData.allow_flip_horizontal && flipHorizontal,
},
className
);
className = applyFilters('ghostkit.editor.className', className, props);
const blockProps = useBlockProps({
className,
dangerouslySetInnerHTML: { __html: maybeDecode(svg) },
});
let heightName = 'height';
let widthName = 'width';
if (device) {
heightName = `${device}_${heightName}`;
widthName = `${device}_${widthName}`;
}
return (
<>
{shapeData.allow_flip_vertical ? (
setAttributes({ flipVertical: !flipVertical })
}
isActive={flipVertical}
/>
) : null}
{shapeData.allow_flip_horizontal ? (
setAttributes({
flipHorizontal: !flipHorizontal,
})
}
isActive={flipHorizontal}
/>
) : null}
(
)}
renderContent={() => (
{getShapesPicker()}
)}
/>
{getShapesPicker()}
{__('Height', 'ghostkit')}
{
return !!attributes[
`${checkMedia}_height`
];
}}
/>
>
}
value={
attributes[heightName]
? parseInt(attributes[heightName], 10)
: ''
}
onChange={(value) => {
setAttributes({
[heightName]: `${
typeof value === 'number' ? value : ''
}`,
});
}}
min={1}
max={700}
allowCustomMax
/>
{__('Width', 'ghostkit')}
{
return !!attributes[
`${checkMedia}_width`
];
}}
/>
>
}
value={
attributes[widthName]
? parseInt(attributes[widthName], 10)
: ''
}
onChange={(value) => {
setAttributes({
[widthName]: `${
typeof value === 'number' ? value : ''
}`,
});
}}
min={100}
max={400}
allowCustomMin
allowCustomMax
/>
setAttributes({ color: val })}
alpha
/>
>
);
}