@wordpress/block-library
Version:
Block library for the WordPress editor.
90 lines (89 loc) • 3.51 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let ___image_utils_js = require("../image/utils.cjs");
let __utils_js = require("./utils.cjs");
// packages/block-library/src/gallery/shared.js
function defaultColumnsNumber(imageCount) {
return imageCount ? Math.min(3, imageCount) : 3;
}
/**
* Whether a value is a plain object (and not an array).
*
* @param {*} value The value to check.
* @return {boolean} Whether the value is a plain object.
*/
function isObject(value) {
return !!value && typeof value === "object" && !Array.isArray(value);
}
/**
* Whether the Gallery should use its default Flex layout behavior.
*
* Gallery blocks created before layout variations existed do not have an
* explicit layout attribute. Treat missing and malformed layout data as Flex
* so existing galleries keep their current appearance.
*
* @param {*} layout The Gallery layout attribute.
* @return {boolean} Whether the Gallery uses its Flex layout.
*/
function isGalleryFlexLayout(layout) {
const layoutType = isObject(layout) ? layout.type : void 0;
return typeof layoutType !== "string" || layoutType === "" || layoutType === "flex";
}
const pickRelevantMediaFiles = (image, sizeSlug = "large") => {
const imageProps = Object.fromEntries(Object.entries(image ?? {}).filter(([key]) => [
"alt",
"id",
"link"
].includes(key)));
imageProps.url = image?.sizes?.[sizeSlug]?.url || image?.media_details?.sizes?.[sizeSlug]?.source_url || image?.url || image?.source_url;
const fullUrl = image?.sizes?.full?.url || image?.media_details?.sizes?.full?.source_url;
if (fullUrl) imageProps.fullUrl = fullUrl;
return imageProps;
};
/**
* Returns the image attributes for a gallery aspect ratio.
*
* @param {string} aspectRatio The gallery aspect ratio.
* @return {Object} The `aspectRatio` and `scale` attributes.
*/
function getAspectRatioAttributes(aspectRatio) {
const hasAspectRatio = !!aspectRatio && aspectRatio !== "auto";
return {
aspectRatio: hasAspectRatio ? aspectRatio : void 0,
scale: hasAspectRatio ? "cover" : void 0
};
}
/**
* Returns the attributes that apply the gallery settings to an added image.
*
* @param {Object} gallery The gallery attributes, with the resolved `linkTo`.
* @param {Object} attributes The image block attributes.
* @param {Object} media The image attachment.
* @return {Object} The attributes to set on the image block.
*/
function getImageAttributes(gallery, attributes, media) {
const linkTargetAttributes = attributes.linkTarget || attributes.rel ? {
linkTarget: attributes.linkTarget,
rel: attributes.rel
} : (0, ___image_utils_js.getUpdatedLinkTargetSettings)(gallery.linkTarget, gallery);
return {
...pickRelevantMediaFiles(media, gallery.sizeSlug),
...(0, __utils_js.getHrefAndDestination)(media, gallery.linkTo, attributes.linkDestination),
...linkTargetAttributes,
className: attributes.className || void 0,
sizeSlug: gallery.sizeSlug,
caption: attributes.caption.length > 0 ? attributes.caption : media.caption?.raw,
alt: attributes.alt || media.alt_text,
...getAspectRatioAttributes(gallery.aspectRatio)
};
}
exports.defaultColumnsNumber = defaultColumnsNumber;
exports.getAspectRatioAttributes = getAspectRatioAttributes;
exports.getImageAttributes = getImageAttributes;
exports.isGalleryFlexLayout = isGalleryFlexLayout;
exports.isObject = isObject;
exports.pickRelevantMediaFiles = pickRelevantMediaFiles;
//# sourceMappingURL=shared.cjs.map