UNPKG

@wordpress/block-editor

Version:
150 lines (148 loc) 5.61 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/image-size-control/index.js var image_size_control_exports = {}; __export(image_size_control_exports, { default: () => ImageSizeControl }); module.exports = __toCommonJS(image_size_control_exports); var import_components = require("@wordpress/components"); var import_i18n = require("@wordpress/i18n"); var import_use_dimension_handler = __toESM(require("./use-dimension-handler")); var import_jsx_runtime = require("react/jsx-runtime"); var IMAGE_SIZE_PRESETS = [25, 50, 75, 100]; var noop = () => { }; function getScaledWidthAndHeight(scale, imageWidth, imageHeight) { const scaledWidth = Math.round(imageWidth * (scale / 100)); const scaledHeight = Math.round(imageHeight * (scale / 100)); return { scaledWidth, scaledHeight }; } function ImageSizeControl({ imageSizeHelp, imageWidth, imageHeight, imageSizeOptions = [], isResizable = true, slug, width, height, onChange, onChangeImage = noop }) { const { currentHeight, currentWidth, updateDimension, updateDimensions } = (0, import_use_dimension_handler.default)(height, width, imageHeight, imageWidth, onChange); const handleUpdateDimensions = (scale) => { if (void 0 === scale) { updateDimensions(); return; } const { scaledWidth, scaledHeight } = getScaledWidthAndHeight( scale, imageWidth, imageHeight ); updateDimensions(scaledHeight, scaledWidth); }; const selectedValue = IMAGE_SIZE_PRESETS.find((scale) => { const { scaledWidth, scaledHeight } = getScaledWidthAndHeight( scale, imageWidth, imageHeight ); return currentWidth === scaledWidth && currentHeight === scaledHeight; }); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalVStack, { className: "block-editor-image-size-control", spacing: "4", children: [ imageSizeOptions && imageSizeOptions.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.SelectControl, { __nextHasNoMarginBottom: true, label: (0, import_i18n.__)("Resolution"), value: slug, options: imageSizeOptions, onChange: onChangeImage, help: imageSizeHelp, size: "__unstable-large" } ), isResizable && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { align: "baseline", spacing: "4", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalNumberControl, { label: (0, import_i18n.__)("Width"), value: currentWidth, min: 1, onChange: (value) => updateDimension("width", value), size: "__unstable-large" } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalNumberControl, { label: (0, import_i18n.__)("Height"), value: currentHeight, min: 1, onChange: (value) => updateDimension("height", value), size: "__unstable-large" } ) ] }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToggleGroupControl, { label: (0, import_i18n.__)("Image size presets"), hideLabelFromVision: true, onChange: handleUpdateDimensions, value: selectedValue, isBlock: true, __next40pxDefaultSize: true, __nextHasNoMarginBottom: true, children: IMAGE_SIZE_PRESETS.map((scale) => { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToggleGroupControlOption, { value: scale, label: (0, import_i18n.sprintf)( /* translators: %d: Percentage value. */ (0, import_i18n.__)("%d%%"), scale ) }, scale ); }) } ) ] }) ] }); } //# sourceMappingURL=index.js.map