import { __ } from "@wordpress/i18n";
import "./editor.scss";
import { useSelect } from "@wordpress/data";
import {
useBlockProps,
RichText,
BlockControls,
MediaReplaceFlow,
InspectorControls,
__experimentalLinkControl as LinkControl,
} from "@wordpress/block-editor";
import {
PanelBody,
TextControl,
ToggleControl,
Button,
Tip,
} from "@wordpress/components";
import { trash } from "@wordpress/icons";
import ImageMediaPlaceholder from "../../_components/ImageMediaPlaceholder";
import Image from "../../_components/Image";
import ImagePanelBody from "../../_components/ImagePanelBody";
import ImagePanelBodyContent from "../../_components/ImagePanelBodyContent";
export default function Edit({ attributes, setAttributes }) {
const {
blockCustomTitle,
hasCustomTitle,
pageHeaderTitle,
pageHeaderDescription,
hasCta,
cta,
hasCustomImage,
imageId,
imageAlt,
imageUrl,
imageProportion,
} = attributes;
const pageTitle = useSelect((select) => {
const { getEditedPostAttribute } = select("core/editor");
return getEditedPostAttribute("title");
});
const pageIcon = useSelect((select) => {
const currentPost = select("core/editor").getCurrentPost();
if (currentPost && currentPost.acf && currentPost.acf.page_icon) {
let Image = select("core").getMedia(currentPost.acf.page_icon);
return Image;
}
});
function onChangeBlockCustomTitle(blockCustomTitle) {
setAttributes({ blockCustomTitle });
}
function onChangeTitle(newTitle) {
setAttributes({ pageHeaderTitle: newTitle });
}
function onChangeHasCustomTitle(newHasAutoTitle) {
setAttributes({ hasCustomTitle: !hasCustomTitle });
if (newHasAutoTitle) {
setAttributes({ blockCustomTitle: undefined });
}
}
function onChangeHasCustomImage(newHasAutoImage) {
setAttributes({ hasCustomImage: !hasCustomImage });
if (!newHasAutoImage) {
console.log("newHasAutoImage", newHasAutoImage);
// setAttributes({ blockCustomTitle: undefined });
}
}
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 onChangeDescription(newDescription) {
setAttributes({ pageHeaderDescription: newDescription });
}
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,
},
});
}
return (
<>
{!hasCustomTitle && (
Le titre est automatiquement généré à partir du titre de la page
)}
{!hasCustomImage && (
La couverture est automatiquement généré à partir de l'icone de la
page
)}
{hasCustomImage && (
<>
{imageUrl &&
}
{imageUrl && (
<>
>
)}
>
)}
{hasCta && (
<>
>
)}
{pageIcon && !hasCustomImage && (
)}
{hasCustomImage && (
)}
>
);
}