UNPKG

@wordpress/editor

Version:
94 lines (92 loc) 3.79 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/editor/src/components/media/media-editor-modal.js var media_editor_modal_exports = {}; __export(media_editor_modal_exports, { default: () => MediaEditorModalMount }); module.exports = __toCommonJS(media_editor_modal_exports); var import_block_editor = require("@wordpress/block-editor"); var import_element = require("@wordpress/element"); var import_media_editor = require("@wordpress/media-editor"); var import_lock_unlock = require("../../lock-unlock.cjs"); var import_post_fields = __toESM(require("../post-fields/index.cjs")); var import_jsx_runtime = require("react/jsx-runtime"); var { MediaEditorModal } = (0, import_lock_unlock.unlock)(import_media_editor.privateApis); function ratioToNumber(ratio) { if (ratio === void 0 || ratio === null) { return NaN; } const [a, b, ...rest] = String(ratio).split("/").map(Number); if (a <= 0 || b <= 0 || Number.isNaN(a) || Number.isNaN(b) || rest.length) { return NaN; } return b ? a / b : a; } function aspectRatioPresetFromSettings({ name, ratio } = {}) { const value = ratioToNumber(ratio); if (!name || !Number.isFinite(value) || value <= 0) { return null; } return { label: name, value }; } function MediaEditorModalMount() { const fields = (0, import_post_fields.default)({ postType: "attachment" }); const [defaultRatios, themeRatios, showDefaultRatios] = (0, import_block_editor.useSettings)( "dimensions.aspectRatios.default", "dimensions.aspectRatios.theme", "dimensions.defaultAspectRatios" ); const aspectRatioPresets = (0, import_element.useMemo)(() => { const hasAspectRatioSettings = Array.isArray(defaultRatios) || Array.isArray(themeRatios) || typeof showDefaultRatios === "boolean"; if (!hasAspectRatioSettings) { return void 0; } const candidateRatios = [ ...showDefaultRatios && Array.isArray(defaultRatios) ? defaultRatios : [], ...Array.isArray(themeRatios) ? themeRatios : [] ]; const presets = candidateRatios.map(aspectRatioPresetFromSettings).filter(Boolean); if (presets.length || showDefaultRatios === false) { return presets; } return void 0; }, [defaultRatios, themeRatios, showDefaultRatios]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( MediaEditorModal, { fields, aspectRatioPresets } ); } //# sourceMappingURL=media-editor-modal.cjs.map