import { __ } from "@wordpress/i18n";
import "./editor.scss";
import { useSelect } from "@wordpress/data";
import {
InnerBlocks,
useBlockProps,
RichText,
MediaReplaceFlow,
InspectorControls,
__experimentalLinkControl as LinkControl,
MediaPlaceholder,
} from "@wordpress/block-editor";
import {
PanelBody,
TextControl,
Button,
Tip,
ToggleControl,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from "@wordpress/components";
import { lock, trash } from "@wordpress/icons";
export default function Edit({ attributes, setAttributes }) {
const {
hasCta,
cta,
hasCustomImage,
imageId,
imageAlt,
imageUrl,
coverSize,
} = attributes;
const pageIllustration = useSelect((select) => {
const currentPost = select("core/editor").getCurrentPost();
if (currentPost && currentPost.acf && currentPost.acf.page_illustration) {
let Image = select("core").getMedia(currentPost.acf.page_illustration);
return Image;
}
});
function onChangeHasCustomImage(newHasAutoImage) {
setAttributes({ hasCustomImage: !hasCustomImage });
if (!newHasAutoImage) {
removeImageAttributes();
}
}
function setImageAttributes(image) {
setAttributes({
imageId: image.id,
imageAlt: image.alt,
imageUrl: image.url,
});
}
function removeImageAttributes() {
setAttributes({
imageId: null,
imageAlt: null,
imageUrl: null,
});
}
function onChangeHasCta(newHasCta) {
setAttributes({ hasCta: !hasCta });
if (!newHasCta) {
setAttributes({ cta: undefined });
}
}
function onChangeCTA(newCtaValue) {
setAttributes({
cta: {
title: !cta || !cta.title ? newCtaValue.title : cta.title,
id: newCtaValue.id,
kind: newCtaValue.kind,
type: newCtaValue.type,
url: newCtaValue.url,
},
});
}
function onChangeTextControl(newCtaTitle) {
setAttributes({
cta: {
...cta,
title: newCtaTitle,
},
});
}
function onCoverSizeChange(coverSize) {
setAttributes({ coverSize });
}
return (
<>
{!hasCustomImage && (
La couverture est automatiquement généré à partir de
l'illustration de couverture de la page.
)}
{hasCustomImage && (
<>
{imageUrl &&
}
{imageUrl && (
<>
>
)}
>
)}
{hasCta && (
<>
>
)}
{pageIllustration && !hasCustomImage && (
)}
{hasCustomImage && imageUrl && (
)}
{hasCustomImage && !imageUrl && (
)}
>
);
}