UNPKG

@wordpress/block-editor

Version:
185 lines (183 loc) 6.69 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/hooks/background.js var background_exports = {}; __export(background_exports, { BACKGROUND_BLOCK_DEFAULT_VALUES: () => BACKGROUND_BLOCK_DEFAULT_VALUES, BACKGROUND_SUPPORT_KEY: () => BACKGROUND_SUPPORT_KEY, BackgroundImagePanel: () => BackgroundImagePanel, default: () => background_default, getBackgroundImageClasses: () => getBackgroundImageClasses, hasBackgroundSupport: () => hasBackgroundSupport, setBackgroundStyleDefaults: () => setBackgroundStyleDefaults }); module.exports = __toCommonJS(background_exports); var import_blocks = require("@wordpress/blocks"); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); var import_inspector_controls = __toESM(require("../components/inspector-controls")); var import_utils = require("./utils"); var import_store = require("../store"); var import_background_panel = __toESM(require("../components/global-styles/background-panel")); var import_private_keys = require("../store/private-keys"); var import_jsx_runtime = require("react/jsx-runtime"); var BACKGROUND_SUPPORT_KEY = "background"; var BACKGROUND_BLOCK_DEFAULT_VALUES = { backgroundSize: "cover", backgroundPosition: "50% 50%" // used only when backgroundSize is 'contain'. }; function hasBackgroundSupport(blockName, feature = "any") { const support = (0, import_blocks.getBlockSupport)(blockName, BACKGROUND_SUPPORT_KEY); if (support === true) { return true; } if (feature === "any") { return !!support?.backgroundImage || !!support?.backgroundSize || !!support?.backgroundRepeat; } return !!support?.[feature]; } function setBackgroundStyleDefaults(backgroundStyle) { if (!backgroundStyle || !backgroundStyle?.backgroundImage?.url) { return; } let backgroundStylesWithDefaults; if (!backgroundStyle?.backgroundSize) { backgroundStylesWithDefaults = { backgroundSize: BACKGROUND_BLOCK_DEFAULT_VALUES.backgroundSize }; } if ("contain" === backgroundStyle?.backgroundSize && !backgroundStyle?.backgroundPosition) { backgroundStylesWithDefaults = { backgroundPosition: BACKGROUND_BLOCK_DEFAULT_VALUES.backgroundPosition }; } return backgroundStylesWithDefaults; } function useBlockProps({ name, style }) { if (!hasBackgroundSupport(name) || !style?.background?.backgroundImage) { return; } const backgroundStyles = setBackgroundStyleDefaults(style?.background); if (!backgroundStyles) { return; } return { style: { ...backgroundStyles } }; } function getBackgroundImageClasses(style) { return (0, import_background_panel.hasBackgroundImageValue)(style) ? "has-background" : ""; } function BackgroundInspectorControl({ children }) { const resetAllFilter = (0, import_element.useCallback)((attributes) => { return { ...attributes, style: { ...attributes.style, background: void 0 } }; }, []); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_inspector_controls.default, { group: "background", resetAllFilter, children }); } function BackgroundImagePanel({ clientId, name, setAttributes, settings }) { const { style, inheritedValue } = (0, import_data.useSelect)( (select) => { const { getBlockAttributes, getSettings } = select(import_store.store); const _settings = getSettings(); return { style: getBlockAttributes(clientId)?.style, /* * To ensure we pass down the right inherited values: * @TODO 1. Pass inherited value down to all block style controls, * See: packages/block-editor/src/hooks/style.js * @TODO 2. Add support for block style variations, * See implementation: packages/block-editor/src/hooks/block-style-variation.js */ inheritedValue: _settings[import_private_keys.globalStylesDataKey]?.blocks?.[name] }; }, [clientId, name] ); if (!(0, import_background_panel.useHasBackgroundPanel)(settings) || !hasBackgroundSupport(name, "backgroundImage")) { return null; } const onChange = (newStyle) => { setAttributes({ style: (0, import_utils.cleanEmptyObject)(newStyle) }); }; const updatedSettings = { ...settings, background: { ...settings.background, backgroundSize: settings?.background?.backgroundSize && hasBackgroundSupport(name, "backgroundSize") } }; const defaultControls = (0, import_blocks.getBlockSupport)(name, [ BACKGROUND_SUPPORT_KEY, "defaultControls" ]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_background_panel.default, { inheritedValue, as: BackgroundInspectorControl, panelId: clientId, defaultValues: BACKGROUND_BLOCK_DEFAULT_VALUES, settings: updatedSettings, onChange, defaultControls, value: style } ); } var background_default = { useBlockProps, attributeKeys: ["style"], hasSupport: hasBackgroundSupport }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BACKGROUND_BLOCK_DEFAULT_VALUES, BACKGROUND_SUPPORT_KEY, BackgroundImagePanel, getBackgroundImageClasses, hasBackgroundSupport, setBackgroundStyleDefaults }); //# sourceMappingURL=background.js.map