UNPKG

@wordpress/block-editor

Version:
250 lines (248 loc) 9.19 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-editor/src/components/global-styles/background-panel.js var background_panel_exports = {}; __export(background_panel_exports, { default: () => BackgroundImagePanel, hasBackgroundGradientValue: () => hasBackgroundGradientValue, hasBackgroundImageValue: () => hasBackgroundImageValue, hasBackgroundSizeValue: () => hasBackgroundSizeValue, useHasBackgroundControl: () => useHasBackgroundControl, useHasBackgroundPanel: () => useHasBackgroundPanel }); module.exports = __toCommonJS(background_panel_exports); var import_components = require("@wordpress/components"); var import_element = require("@wordpress/element"); var import_i18n = require("@wordpress/i18n"); var import_global_styles_engine = require("@wordpress/global-styles-engine"); var import_background_image_control = __toESM(require("../background-image-control/index.cjs")); var import_color_panel = require("./color-panel.cjs"); var import_hooks = require("./hooks.cjs"); var import_utils = require("./utils.cjs"); var import_object = require("../../utils/object.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); var DEFAULT_CONTROLS = { backgroundImage: true, gradient: true }; function useHasBackgroundControl(settings, feature) { return import_element.Platform.OS === "web" && settings?.background?.[feature]; } function useHasBackgroundPanel(settings) { const { backgroundImage, gradient } = settings?.background || {}; return import_element.Platform.OS === "web" && (backgroundImage || gradient); } function hasBackgroundSizeValue(style) { return style?.background?.backgroundPosition !== void 0 || style?.background?.backgroundSize !== void 0; } function hasBackgroundImageValue(style) { return !!style?.background?.backgroundImage?.id || // Supports url() string values in theme.json. "string" === typeof style?.background?.backgroundImage || !!style?.background?.backgroundImage?.url; } function hasBackgroundGradientValue(style) { return "string" === typeof style?.background?.gradient && style?.background?.gradient !== ""; } function BackgroundToolsPanel({ resetAllFilter, onChange, value, panelId, children, headerLabel }) { const dropdownMenuProps = (0, import_utils.useToolsPanelDropdownMenuProps)(); const resetAll = () => { const updatedValue = resetAllFilter(value); onChange(updatedValue); }; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToolsPanel, { label: headerLabel, resetAll, panelId, hasInnerWrapper: true, className: "background-block-support-panel", __experimentalFirstVisibleItemClass: "first", __experimentalLastVisibleItemClass: "last", dropdownMenuProps, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "background-block-support-panel__inner-wrapper", children }) } ); } function BackgroundImagePanel({ as: Wrapper = BackgroundToolsPanel, value, onChange, inheritedValue, settings, panelId, defaultControls = DEFAULT_CONTROLS, defaultValues = {}, headerLabel = (0, import_i18n.__)("Background") }) { const gradients = (0, import_hooks.useGradientsPerOrigin)(settings); const areCustomGradientsEnabled = settings?.color?.customGradient; const hasGradientColors = gradients.length > 0 || areCustomGradientsEnabled; const hasBackgroundGradientControl = useHasBackgroundControl( settings, "gradient" ); const showBackgroundGradientControl = hasGradientColors && hasBackgroundGradientControl; const showBackgroundImageControl = useHasBackgroundControl( settings, "backgroundImage" ); const resetAllFilter = (0, import_element.useCallback)( (previousValue) => { return { ...previousValue, background: {}, color: hasBackgroundGradientControl ? { ...previousValue?.color, gradient: void 0 } : previousValue?.color }; }, [hasBackgroundGradientControl] ); if (!showBackgroundGradientControl && !showBackgroundImageControl) { return null; } const decodeValue = (rawValue) => (0, import_global_styles_engine.getValueFromVariable)({ settings }, "", rawValue); const encodeGradientValue = (gradientValue) => { const allGradients = gradients.flatMap( ({ gradients: originGradients }) => originGradients ); const gradientObject = allGradients.find( ({ gradient }) => gradient === gradientValue ); return gradientObject ? "var:preset|gradient|" + gradientObject.slug : gradientValue; }; const resetBackground = () => onChange( (0, import_object.setImmutably)( value, ["background", "backgroundImage"], void 0 ) ); const resetGradient = () => { let newValue = (0, import_object.setImmutably)( value, ["background", "gradient"], void 0 ); newValue = (0, import_object.setImmutably)(newValue, ["color", "gradient"], void 0); onChange(newValue); }; const currentGradient = decodeValue( value?.background?.gradient ?? value?.color?.gradient ); const inheritedGradient = decodeValue( inheritedValue?.background?.gradient ?? inheritedValue?.color?.gradient ); const setGradient = (newGradient) => { let newValue = (0, import_object.setImmutably)( value, ["background", "gradient"], encodeGradientValue(newGradient) ); newValue = (0, import_object.setImmutably)(newValue, ["color", "gradient"], void 0); onChange(newValue); }; return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( Wrapper, { resetAllFilter, value, onChange, panelId, headerLabel, children: [ showBackgroundImageControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToolsPanelItem, { className: "block-editor-background-panel__item", hasValue: () => hasBackgroundImageValue(value), label: (0, import_i18n.__)("Image"), onDeselect: resetBackground, isShownByDefault: defaultControls.backgroundImage, panelId, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_background_image_control.default, { value, onChange, settings, inheritedValue, defaultControls, defaultValues } ) } ), showBackgroundGradientControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_color_panel.ColorPanelDropdown, { className: "block-editor-background-panel__item", label: (0, import_i18n.__)("Gradient"), hasValue: () => hasBackgroundGradientValue(value), resetValue: resetGradient, isShownByDefault: defaultControls.gradient, indicators: [currentGradient], tabs: [ { key: "gradient", label: (0, import_i18n.__)("Gradient"), inheritedValue: currentGradient ?? inheritedGradient, setValue: setGradient, userValue: currentGradient, isGradient: true } ], colorGradientControlSettings: { gradients, disableCustomGradients: !areCustomGradientsEnabled }, panelId } ) ] } ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { hasBackgroundGradientValue, hasBackgroundImageValue, hasBackgroundSizeValue, useHasBackgroundControl, useHasBackgroundPanel }); //# sourceMappingURL=background-panel.cjs.map