import { MediaUpload } from '@wordpress/block-editor';
import { hasBlockSupport } from '@wordpress/blocks';
import {
Button,
ExternalLink,
PanelBody,
SelectControl,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
import ColorIndicator from '../../components/color-indicator';
import ColorPicker from '../../components/color-picker';
import FocalPointPicker from '../../components/focal-point-picker';
import ToggleGroup from '../../components/toggle-group';
import cleanImgTag from '../../utils/clean-img-tag';
import dashCaseToTitle from '../../utils/dash-case-to-title';
import { maybeEncode } from '../../utils/encode-decode';
/**
* Filters registered block settings, extending attributes to include backgrounds.
*
* @param {Object} blockSettings Original block settings
* @return {Object} Filtered block settings
*/
export function addAttribute(blockSettings) {
if (
blockSettings.name === 'ghostkit/grid' ||
blockSettings.name === 'ghostkit/grid-column'
) {
blockSettings.supports.awb = true;
}
let allow = false;
if (hasBlockSupport(blockSettings, 'awb', false)) {
allow = true;
}
if (allow) {
blockSettings.attributes.awb_type = {
type: 'string',
default: 'color',
};
blockSettings.attributes.awb_align = {
type: 'string',
};
blockSettings.attributes.awb_color = {
type: 'string',
default: '',
};
blockSettings.attributes.awb_image = {
type: 'number',
default: '',
};
blockSettings.attributes.awb_imageTag = {
type: 'string',
default: '',
};
blockSettings.attributes.awb_imageSizes = {
type: 'object',
default: '',
};
blockSettings.attributes.awb_imageSize = {
type: 'string',
default: 'full',
};
blockSettings.attributes.awb_imageBackgroundSize = {
type: 'string',
default: 'cover',
};
blockSettings.attributes.awb_imageBackgroundPosition = {
type: 'string',
default: '50% 50%',
};
}
return blockSettings;
}
/**
* Select image
*
* @param {Object} media - media data.
* @param {Function} setAttributes - function to set attributes on the block.
*/
function onImageSelect(media, setAttributes) {
setAttributes({
image: '',
imageSizes: '',
});
wp.media
.attachment(media.id)
.fetch()
.then((data) => {
if (data && data.sizes) {
const { url } =
data.sizes['post-thumbnail'] ||
data.sizes.medium ||
data.sizes.medium_large ||
data.sizes.full;
if (url) {
setAttributes({
image: media.id,
imageSizes: data.sizes,
});
}
}
});
}
function BackgroundControlsInspector(props) {
const { attributes, setAttributes: wpSetAttributes } = props;
const {
awb_color: color,
awb_type: type,
awb_image: image,
awb_imageTag: imageTag,
awb_imageSizes: imageSizes,
awb_imageSize: imageSize,
awb_imageBackgroundSize: imageBackgroundSize,
awb_imageBackgroundPosition: imageBackgroundPosition,
} = attributes;
function setAttributes(attr) {
const newAttrs = {};
Object.keys(attr).forEach((k) => {
newAttrs[`awb_${k}`] = attr[k];
});
wpSetAttributes(newAttrs);
}
const { fetchedImageTag } = useSelect((select) => {
if (!image) {
return false;
}
const data = {
id: image,
size: imageSize,
attr: {
class: 'jarallax-img',
},
};
// background image with pattern size
if (imageBackgroundSize === 'pattern') {
data.div_tag = true;
}
return {
fetchedImageTag: select('ghostkit/base/images').getImageTagData(
data
),
};
});
// Mounted and updated.
useEffect(() => {
// set image tag to attribute
if (fetchedImageTag && maybeEncode(fetchedImageTag) !== imageTag) {
setAttributes({ imageTag: maybeEncode(fetchedImageTag) });
}
});
return (
{__(
'Install AWB plugin to set video backgrounds and images with parallax support.',
'ghostkit'
)}