UNPKG

@wordpress/block-library

Version:
409 lines (408 loc) • 17.1 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js 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 __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let _wordpress_element = require("@wordpress/element"); let _wordpress_components = require("@wordpress/components"); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_data = require("@wordpress/data"); let _wordpress_core_data = require("@wordpress/core-data"); let _wordpress_url = require("@wordpress/url"); let _wordpress_ui = require("@wordpress/ui"); let ___shared_js = require("../shared.cjs"); let ______lock_unlock_ts = require("../../lock-unlock.cjs"); let ______utils_hooks_js = require("../../utils/hooks.cjs"); let ______utils_style_state_js = require("../../utils/style-state.cjs"); let ___constants_js = require("../constants.cjs"); let ______utils_poster_image_jsx = require("../../utils/poster-image.cjs"); ______utils_poster_image_jsx = __toESM(______utils_poster_image_jsx); let ______utils_media_control_jsx = require("../../utils/media-control.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/cover/edit/inspector-controls.jsx const { cleanEmptyObject, ResolutionTool, HTMLElementControl } = (0, ______lock_unlock_ts.unlock)(_wordpress_block_editor.privateApis); function CoverHeightInput({ onChange, onUnitChange, unit = "px", value = "" }) { const inputId = `block-cover-height-input-${(0, _wordpress_compose.useInstanceId)(_wordpress_components.__experimentalUnitControl)}`; const isPx = unit === "px"; const [availableUnits] = (0, _wordpress_block_editor.useSettings)("spacing.units"); const units = (0, _wordpress_components.__experimentalUseCustomUnits)({ availableUnits: availableUnits || [ "px", "em", "rem", "vw", "vh" ], defaultValues: { px: 430, "%": 20, em: 20, rem: 20, vw: 20, vh: 50 } }); const handleOnChange = (unprocessedValue) => { const inputValue = unprocessedValue !== "" ? parseFloat(unprocessedValue) : void 0; if (isNaN(inputValue) && inputValue !== void 0) return; onChange(inputValue); }; const computedValue = (0, _wordpress_element.useMemo)(() => { const [parsedQuantity] = (0, _wordpress_components.__experimentalParseQuantityAndUnitFromRawValue)(value); return [parsedQuantity, unit].join(""); }, [unit, value]); const min = isPx ? ___shared_js.COVER_MIN_HEIGHT : 0; return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalUnitControl, { label: (0, _wordpress_i18n.__)("Minimum height"), id: inputId, isResetValueOnUnitChange: true, min, onChange: handleOnChange, onUnitChange, units, value: computedValue }); } function CoverInspectorControls({ attributes, setAttributes, clientId, setOverlayColor, coverRef, currentSettings, onSelectMedia, onUploadError, toggleUseFeaturedImage, updateDimRatio, onClearMedia, featuredImage, isSelected }) { const { useFeaturedImage, id, dimRatio, focalPoint, hasParallax, isRepeated, minHeight, minHeightUnit, alt, tagName, poster } = attributes; const { isVideoBackground, isImageBackground, mediaElement, url, overlayColor } = currentSettings; const sizeSlug = attributes.sizeSlug || ___constants_js.DEFAULT_MEDIA_SIZE_SLUG; const { gradientValue, setGradient } = (0, _wordpress_block_editor.__experimentalUseGradient)(); const { imageSizes, selectedStyleState, hasSelectedStyleState } = (0, _wordpress_data.useSelect)((select) => { const { getSettings, getSelectedBlockStyleState, hasSelectedBlockStyleState } = select(_wordpress_block_editor.store); return { imageSizes: getSettings()?.imageSizes, selectedStyleState: getSelectedBlockStyleState(clientId), hasSelectedStyleState: hasSelectedBlockStyleState(clientId) }; }, [clientId]); const selectedStyleStateKey = (0, ______utils_style_state_js.getStyleStateKey)(selectedStyleState); const stateMinHeight = (0, ______utils_style_state_js.getActiveDimensionValue)({ attributes, selectedState: selectedStyleState, hasSelectedStyleState, attributeKey: "minHeight", styleKey: "minHeight", rootValue: void 0 }); const [stateMinHeightValue, stateMinHeightUnit] = (0, _wordpress_components.__experimentalParseQuantityAndUnitFromRawValue)(stateMinHeight || ""); const activeMinHeight = hasSelectedStyleState ? stateMinHeightValue : minHeight; const activeMinHeightUnit = hasSelectedStyleState ? stateMinHeightUnit || minHeightUnit : minHeightUnit; const activeAspectRatio = (0, ______utils_style_state_js.getActiveDimensionValue)({ attributes, selectedState: selectedStyleState, hasSelectedStyleState, attributeKey: "aspectRatio", rootValue: attributes?.style?.dimensions?.aspectRatio }); const image = (0, _wordpress_data.useSelect)((select) => id && isImageBackground ? select(_wordpress_core_data.store).getEntityRecord("postType", "attachment", id, { context: "view" }) : null, [id, isImageBackground]); const currentBackgroundImage = useFeaturedImage ? featuredImage : image; function updateImage(newSizeSlug) { const newUrl = currentBackgroundImage?.media_details?.sizes?.[newSizeSlug]?.source_url; if (!newUrl) return null; setAttributes({ url: newUrl, sizeSlug: newSizeSlug }); } const imageSizeOptions = imageSizes?.filter(({ slug }) => currentBackgroundImage?.media_details?.sizes?.[slug]?.source_url)?.map(({ name, slug }) => ({ value: slug, label: name })); const toggleParallax = () => { setAttributes({ hasParallax: !hasParallax, ...!hasParallax ? { focalPoint: void 0 } : {} }); }; const toggleIsRepeated = () => { setAttributes({ isRepeated: !isRepeated }); }; const showFocalPointPicker = isVideoBackground || isImageBackground; const prefersReducedMotion = (0, _wordpress_compose.useReducedMotion)(); const imperativeFocalPointPreview = (value) => { const [styleOfRef, property] = mediaElement.current ? [mediaElement.current.style, "objectPosition"] : [coverRef.current.style, "backgroundPosition"]; styleOfRef[property] = (0, ___shared_js.mediaPosition)(value); }; const colorGradientSettings = (0, _wordpress_block_editor.__experimentalUseMultipleOriginColorsAndGradients)(); const showOverlayControls = colorGradientSettings.hasColorsOrGradients && !hasSelectedStyleState; const setMinHeightAttributes = (nextMinHeight, nextUnit) => { if (hasSelectedStyleState) { setAttributes((0, ______utils_style_state_js.getDimensionUpdateAttributes)({ style: attributes.style, selectedState: selectedStyleState, hasSelectedStyleState, nextDimensions: { minHeight: nextMinHeight === void 0 ? void 0 : `${nextMinHeight}${nextUnit || activeMinHeightUnit || "px"}`, aspectRatio: void 0 } })); return; } setAttributes({ minHeight: nextMinHeight, style: cleanEmptyObject({ ...attributes?.style, dimensions: { ...attributes?.style?.dimensions, aspectRatio: void 0 } }) }); }; const getResetMinHeightAttributes = (attrs = attributes) => { return (0, ______utils_style_state_js.getDimensionResetAttributes)({ style: attrs.style, selectedState: selectedStyleState, hasSelectedStyleState, keys: ["minHeight"], defaultAttributes: { minHeight: void 0, minHeightUnit: void 0 } }); }; const dropdownMenuProps = (0, ______utils_hooks_js.useToolsPanelDropdownMenuProps)(); const mediaInspectorPanel = isSelected ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { group: "content", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanel, { label: (0, _wordpress_i18n.__)("Media"), resetAll: onClearMedia, dropdownMenuProps, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { label: (0, _wordpress_i18n.__)("Media"), hasValue: () => !!url || !!useFeaturedImage, onDeselect: onClearMedia, isShownByDefault: true, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______utils_media_control_jsx.MediaControl, { mediaId: id, mediaUrl: url, filename: image?.media_details?.sizes?.full?.file || image?.slug || (0, _wordpress_url.getFilename)(url), allowedTypes: ___shared_js.ALLOWED_MEDIA_TYPES, onSelect: onSelectMedia, onError: onUploadError, onReset: onClearMedia, useFeaturedImage, onToggleFeaturedImage: toggleUseFeaturedImage, emptyLabel: (0, _wordpress_i18n.__)("Add media") }) }) }) }) : null; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ mediaInspectorPanel, (!!url || useFeaturedImage) && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalToolsPanel, { label: (0, _wordpress_i18n.__)("Settings"), resetAll: () => { setAttributes({ hasParallax: false, focalPoint: void 0, isRepeated: false, alt: "", poster: void 0 }); updateImage(___constants_js.DEFAULT_MEDIA_SIZE_SLUG); }, dropdownMenuProps, children: [ isImageBackground && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { label: (0, _wordpress_i18n.__)("Fixed background"), isShownByDefault: true, hasValue: () => !!hasParallax, onDeselect: () => setAttributes({ hasParallax: false, focalPoint: void 0 }), children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, { label: (0, _wordpress_i18n.__)("Fixed background"), checked: !!hasParallax, onChange: toggleParallax }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { label: (0, _wordpress_i18n.__)("Repeated background"), isShownByDefault: true, hasValue: () => isRepeated, onDeselect: () => setAttributes({ isRepeated: false }), children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, { label: (0, _wordpress_i18n.__)("Repeated background"), checked: isRepeated, onChange: toggleIsRepeated }) })] }), showFocalPointPicker && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { label: (0, _wordpress_i18n.__)("Focal point"), isShownByDefault: true, hasValue: () => !!focalPoint, onDeselect: () => setAttributes({ focalPoint: void 0 }), children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.FocalPointPicker, { label: (0, _wordpress_i18n.__)("Focal point"), url, autoPlay: !prefersReducedMotion, value: focalPoint, onDragStart: imperativeFocalPointPreview, onDrag: imperativeFocalPointPreview, onChange: (newFocalPoint) => setAttributes({ focalPoint: newFocalPoint }) }, prefersReducedMotion) }), isVideoBackground && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______utils_poster_image_jsx.default, { poster, onChange: (posterImage) => setAttributes({ poster: posterImage?.url }) }), !useFeaturedImage && url && !isVideoBackground && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { label: (0, _wordpress_i18n.__)("Alternative text"), isShownByDefault: true, hasValue: () => !!alt, onDeselect: () => setAttributes({ alt: "" }), children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextareaControl, { label: (0, _wordpress_i18n.__)("Alternative text"), value: alt, onChange: (newAlt) => setAttributes({ alt: newAlt }), help: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Link, { openInNewTab: true, href: (0, _wordpress_i18n.__)("https://www.w3.org/WAI/tutorials/images/decision-tree/"), children: (0, _wordpress_i18n.__)("Describe the purpose of the image.") }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("br", {}), (0, _wordpress_i18n.__)("Leave empty if decorative.") ] }) }) }), !!imageSizeOptions?.length && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ResolutionTool, { value: sizeSlug, onChange: updateImage, options: imageSizeOptions, defaultValue: ___constants_js.DEFAULT_MEDIA_SIZE_SLUG }) ] }) }), showOverlayControls && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_block_editor.InspectorControls, { group: "color", children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.__experimentalColorGradientSettingsDropdown, { __experimentalIsRenderedInSidebar: true, settings: [{ colorValue: overlayColor.color, gradientValue, label: (0, _wordpress_i18n.__)("Overlay"), onColorChange: setOverlayColor, onGradientChange: setGradient, isShownByDefault: true, resetAllFilter: () => ({ overlayColor: void 0, customOverlayColor: void 0, gradient: void 0, customGradient: void 0 }), clearable: true }], panelId: clientId, ...colorGradientSettings }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { hasValue: () => { return dimRatio === void 0 ? false : dimRatio !== (url ? 50 : 100); }, label: (0, _wordpress_i18n.__)("Overlay opacity"), onDeselect: () => updateDimRatio(url ? 50 : 100), resetAllFilter: () => ({ dimRatio: url ? 50 : 100 }), isShownByDefault: true, panelId: clientId, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.RangeControl, { label: (0, _wordpress_i18n.__)("Overlay opacity"), value: dimRatio, onChange: (newDimRatio) => updateDimRatio(newDimRatio), min: 0, max: 100, step: 10, required: true }) })] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { group: "dimensions", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { className: "single-column", hasValue: () => !!activeMinHeight, label: (0, _wordpress_i18n.__)("Minimum height"), onDeselect: () => setAttributes(getResetMinHeightAttributes()), resetAllFilter: getResetMinHeightAttributes, isShownByDefault: true, panelId: clientId, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CoverHeightInput, { value: activeAspectRatio ? "" : activeMinHeight, unit: activeMinHeightUnit, onChange: (newMinHeight) => setMinHeightAttributes(newMinHeight), onUnitChange: (nextUnit) => { if (hasSelectedStyleState) { if (activeMinHeight !== void 0) setMinHeightAttributes(activeMinHeight, nextUnit); return; } setAttributes({ minHeightUnit: nextUnit }); } }) }, selectedStyleStateKey) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { group: "advanced", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(HTMLElementControl, { tagName, onChange: (value) => setAttributes({ tagName: value }), clientId, options: [ { label: (0, _wordpress_i18n.__)("Default (<div>)"), value: "div" }, { label: "<header>", value: "header" }, { label: "<main>", value: "main" }, { label: "<section>", value: "section" }, { label: "<article>", value: "article" }, { label: "<aside>", value: "aside" }, { label: "<footer>", value: "footer" } ] }) }) ] }); } exports.default = CoverInspectorControls; //# sourceMappingURL=inspector-controls.cjs.map