import classnames from 'classnames/dedupe';
import {
BlockControls,
InspectorControls,
MediaPlaceholder,
MediaUpload,
RichText,
useBlockProps,
} from '@wordpress/block-editor';
import {
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
BaseControl,
Button,
ExternalLink,
PanelBody,
Placeholder,
SelectControl,
TextareaControl,
ToggleControl,
Toolbar,
ToolbarButton,
ToolbarGroup,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import ColorPicker from '../../components/color-picker';
import RangeControl from '../../components/range-control';
import getIcon from '../../utils/get-icon';
const ALLOWED_MEDIA_TYPES = ['image'];
const DEFAULT_SIZE_SLUG = 'large';
/**
* Block Edit Class.
*
* @param props
*/
export default function BlockEdit(props) {
const { attributes, isSelected, setAttributes } = props;
let { className } = props;
const {
position,
direction,
trigger,
caption,
showLabels,
labelBeforeText,
labelAfterText,
labelAlign,
beforeId,
beforeUrl,
beforeAlt,
beforeWidth,
beforeHeight,
beforeSizeSlug,
afterId,
afterUrl,
afterAlt,
afterWidth,
afterHeight,
afterSizeSlug,
colorDivider,
colorDividerIcon,
} = attributes;
const { editorSettings, beforeImage, afterImage } = useSelect((select) => {
const { getSettings } = select('core/block-editor');
const { getMedia } = select('core');
return {
editorSettings: getSettings(),
beforeImage: beforeId && isSelected ? getMedia(beforeId) : null,
afterImage: afterId && isSelected ? getMedia(afterId) : null,
};
});
const onUploadError = (message) => {
const { noticeOperations } = props;
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice(message);
};
const getImgTag = (type = 'before') => {
return attributes[`${type}Url`] ? (
) : null;
};
const updateImageData = (
type = 'before',
imageData = {},
imageSize = false
) => {
imageSize =
imageSize || attributes[`${type}SizeSlug`] || DEFAULT_SIZE_SLUG;
// Prepare full image data.
const result = {
[`${type}SizeSlug`]: imageSize,
[`${type}Id`]: imageData.id,
[`${type}Url`]: imageData.url || imageData.source_url,
[`${type}Alt`]: imageData.alt || imageData.alt_text,
[`${type}Width`]:
imageData.width ||
(imageData.media_details && imageData.media_details.width
? imageData.media_details.width
: undefined),
[`${type}Height`]:
imageData.height ||
(imageData.media_details && imageData.media_details.height
? imageData.media_details.height
: undefined),
};
let sizes = imageData.sizes && imageData.sizes[imageSize];
if (
!sizes &&
imageData.media_details &&
imageData.media_details.sizes &&
imageData.media_details.sizes[imageSize]
) {
sizes = imageData.media_details.sizes[imageSize];
}
// Prepare image data for selected size.
if (sizes) {
if (sizes.url) {
result[`${type}Url`] = sizes.url;
}
if (sizes.source_url) {
result[`${type}Url`] = sizes.source_url;
}
if (sizes.width) {
result[`${type}Width`] = sizes.width;
}
if (sizes.height) {
result[`${type}Height`] = sizes.height;
}
}
setAttributes(result);
};
const iconStart =
direction === 'vertical'
? getIcon('icon-horizontal-start')
: getIcon('icon-vertical-top');
const iconCenter =
direction === 'vertical'
? getIcon('icon-horizontal-center')
: getIcon('icon-vertical-center');
const iconEnd =
direction === 'vertical'
? getIcon('icon-horizontal-end')
: getIcon('icon-vertical-bottom');
className = classnames(
'ghostkit-image-compare',
direction === 'vertical' ? 'ghostkit-image-compare-vertical' : false,
showLabels && labelAlign
? `ghostkit-image-compare-labels-align-${labelAlign}`
: false,
className
);
const blockProps = useBlockProps({ className });
const baseControlLabel =
direction === 'vertical'
? __('Horizontal Align', 'ghostkit')
: __('Vertical Align', 'ghostkit');
return (
<>
{beforeUrl && afterUrl ? (