UNPKG

@wordpress/block-library

Version:
107 lines (106 loc) • 3.43 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let __constants_js = require("./constants.cjs"); // packages/block-library/src/image/utils.js /** * Returns the site's default link destination for newly added images, from the * `image_default_link_type` option. * * @return {string} Block link destination. */ function getDefaultLinkDestination() { switch (window.wp?.media?.view?.settings?.defaultProps?.link) { case "file": return __constants_js.LINK_DESTINATION_MEDIA; case "post": return __constants_js.LINK_DESTINATION_ATTACHMENT; case __constants_js.LINK_DESTINATION_CUSTOM: return __constants_js.LINK_DESTINATION_CUSTOM; default: return __constants_js.LINK_DESTINATION_NONE; } } /** * Evaluates a CSS aspect-ratio property value as a number. * * Degenerate or invalid ratios behave as 'auto'. And 'auto' ratios return NaN. * * @see https://drafts.csswg.org/css-sizing-4/#aspect-ratio * * @param {string} value CSS aspect-ratio property value. * @return {number} Numerical aspect ratio or NaN if invalid. */ function evalAspectRatio(value) { const [width, height = 1] = value.split("/").map(Number); const aspectRatio = width / height; return aspectRatio === Infinity || aspectRatio === 0 ? NaN : aspectRatio; } function removeNewTabRel(currentRel) { let newRel = currentRel; if (currentRel !== void 0 && newRel) { __constants_js.NEW_TAB_REL.forEach((relVal) => { const regExp = new RegExp("\\b" + relVal + "\\b", "gi"); newRel = newRel.replace(regExp, ""); }); if (newRel !== currentRel) newRel = newRel.trim(); if (!newRel) newRel = void 0; } return newRel; } /** * Helper to get the link target settings to be stored. * * @param {boolean} value The new link target value. * @param {Object} attributes Block attributes. * @param {Object} attributes.rel Image block's rel attribute. * * @return {Object} Updated link target settings. */ function getUpdatedLinkTargetSettings(value, { rel }) { const linkTarget = value ? "_blank" : void 0; let updatedRel; if (!linkTarget && !rel) updatedRel = void 0; else updatedRel = removeNewTabRel(rel); return { linkTarget, rel: updatedRel }; } /** * Determines new Image block attributes size selection. * * @param {Object} image Media file object for gallery image. * @param {string} size Selected size slug to apply. */ function getImageSizeAttributes(image, size) { const url = image?.media_details?.sizes?.[size]?.source_url; if (url) return { url, width: void 0, height: void 0, sizeSlug: size }; return {}; } /** * Checks if the file has a valid file type. * * @param {File} file - The file to check. * @return {boolean} - Returns true if the file has a valid file type, otherwise false. */ function isValidFileType(file) { return __constants_js.ALLOWED_MEDIA_TYPES.some((mediaType) => file.type.indexOf(mediaType) === 0); } function mediaPosition({ x, y } = { x: .5, y: .5 }) { return `${Math.round(x * 100)}% ${Math.round(y * 100)}%`; } exports.evalAspectRatio = evalAspectRatio; exports.getDefaultLinkDestination = getDefaultLinkDestination; exports.getImageSizeAttributes = getImageSizeAttributes; exports.getUpdatedLinkTargetSettings = getUpdatedLinkTargetSettings; exports.isValidFileType = isValidFileType; exports.mediaPosition = mediaPosition; exports.removeNewTabRel = removeNewTabRel; //# sourceMappingURL=utils.cjs.map