import { __ } from "@wordpress/i18n";
import {
useBlockProps,
InnerBlocks,
useSetting,
InspectorControls,
MediaReplaceFlow,
MediaPlaceholder,
} from "@wordpress/block-editor";
import {
Card,
CardHeader,
CardBody,
Heading,
Text,
} from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import "./editor.scss";
import { isColorLight } from "../../_utilities/utilities";
import {
PanelBody,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
Button,
CheckboxControl,
ColorPalette,
} from "@wordpress/components";
import { filterBgColors, filterTextColors } from "./utilities";
export default function Edit({ attributes, setAttributes, ...props }) {
const colors = useSetting("color.palette.theme");
const {
hasBackgroundColor,
backgroundColor,
textColor,
hasLightBackground,
blockVariant,
} = attributes;
// Détecter le type de post actuel
const postType = useSelect((select) => {
return select("core/editor").getCurrentPostType();
}, []);
const filteredBgColors = filterBgColors(colors, postType);
const filteredTextColors = filterTextColors(
colors,
postType,
backgroundColor,
);
function onBackgroundColorChange(value) {
if (value === undefined) {
setAttributes({ hasBackgroundColor: false });
setAttributes({ backgroundColor: "transparent" });
}
const isLightBackgroundColor = isColorLight(value);
setAttributes({ hasLightBackground: isLightBackgroundColor });
setAttributes({ backgroundColor: value });
if (!isLightBackgroundColor) {
setAttributes({ textColor: "#fff" });
} else {
setAttributes({ textColor: "inherit" });
}
}
function onHasBackgroundColorChange(value) {
setAttributes({ hasBackgroundColor: value });
if (!value) {
setAttributes({ backgroundColor: null });
}
}
function onTextColorChange(value) {
setAttributes({ textColor: value });
}
function onBlockVariantChange(value) {
setAttributes({ blockVariant: value });
}
return (
<>
{blockVariant === "backgrounded" && (
Couleur de fond
<>
>
)}
{blockVariant === "backgrounded" && postType !== "articles" && (
Couleur du texte
)}
>
);
}