UNPKG

@wordpress/block-editor

Version:
152 lines (150 loc) 5.32 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-editor/src/components/image-editor/aspect-ratio-dropdown.js var aspect_ratio_dropdown_exports = {}; __export(aspect_ratio_dropdown_exports, { default: () => AspectRatioDropdown, ratioToNumber: () => ratioToNumber }); module.exports = __toCommonJS(aspect_ratio_dropdown_exports); var import_icons = require("@wordpress/icons"); var import_components = require("@wordpress/components"); var import_i18n = require("@wordpress/i18n"); var import_use_settings = require("../use-settings"); var import_constants = require("./constants"); var import_context = require("./context"); var import_jsx_runtime = require("react/jsx-runtime"); function AspectRatioGroup({ aspectRatios, isDisabled, label, onClick, value }) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.MenuGroup, { label, children: aspectRatios.map(({ name, slug, ratio }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.MenuItem, { disabled: isDisabled, onClick: () => { onClick(ratio); }, role: "menuitemradio", isSelected: ratio === value, icon: ratio === value ? import_icons.check : void 0, children: name }, slug )) }); } function ratioToNumber(str) { const [a, b, ...rest] = str.split("/").map(Number); if (a <= 0 || b <= 0 || Number.isNaN(a) || Number.isNaN(b) || rest.length) { return NaN; } return b ? a / b : a; } function presetRatioAsNumber({ ratio, ...rest }) { return { ratio: ratioToNumber(ratio), ...rest }; } function AspectRatioDropdown({ toggleProps }) { const { isInProgress, aspect, setAspect, defaultAspect } = (0, import_context.useImageEditingContext)(); const [defaultRatios, themeRatios, showDefaultRatios] = (0, import_use_settings.useSettings)( "dimensions.aspectRatios.default", "dimensions.aspectRatios.theme", "dimensions.defaultAspectRatios" ); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.DropdownMenu, { icon: import_icons.aspectRatio, label: (0, import_i18n.__)("Aspect Ratio"), popoverProps: import_constants.POPOVER_PROPS, toggleProps, children: ({ onClose }) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( AspectRatioGroup, { isDisabled: isInProgress, onClick: (newAspect) => { setAspect(newAspect); onClose(); }, value: aspect, aspectRatios: [ // All ratios should be mirrored in AspectRatioTool in @wordpress/block-editor. { slug: "original", name: (0, import_i18n.__)("Original"), ratio: defaultAspect }, ...showDefaultRatios ? defaultRatios.map(presetRatioAsNumber).filter(({ ratio }) => ratio === 1) : [] ] } ), themeRatios?.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( AspectRatioGroup, { label: (0, import_i18n.__)("Theme"), isDisabled: isInProgress, onClick: (newAspect) => { setAspect(newAspect); onClose(); }, value: aspect, aspectRatios: themeRatios } ), showDefaultRatios && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( AspectRatioGroup, { label: (0, import_i18n.__)("Landscape"), isDisabled: isInProgress, onClick: (newAspect) => { setAspect(newAspect); onClose(); }, value: aspect, aspectRatios: defaultRatios.map(presetRatioAsNumber).filter(({ ratio }) => ratio > 1) } ), showDefaultRatios && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( AspectRatioGroup, { label: (0, import_i18n.__)("Portrait"), isDisabled: isInProgress, onClick: (newAspect) => { setAspect(newAspect); onClose(); }, value: aspect, aspectRatios: defaultRatios.map(presetRatioAsNumber).filter(({ ratio }) => ratio < 1) } ) ] }) } ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ratioToNumber }); //# sourceMappingURL=aspect-ratio-dropdown.js.map