UNPKG

@wordpress/block-library

Version:
116 lines (115 loc) • 5.76 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let __constants_js = require("./constants.cjs"); // packages/block-library/src/gallery/responsive-styles.js function isObject(value) { return !!value && typeof value === "object" && !Array.isArray(value); } function isValidGalleryColumns(value) { return Number.isInteger(value) && value >= 1 && value <= __constants_js.MAX_COLUMNS; } const ASPECT_RATIO_PATTERN = /^(auto|\d+(\.\d+)?(\s*\/\s*\d+(\.\d+)?)?)$/; /** * Returns whether a value can be used as a Gallery aspect ratio. * * @param {*} value Value to check. * @return {boolean} Whether the value is a valid aspect ratio. */ function isValidGalleryAspectRatio(value) { return typeof value === "string" && ASPECT_RATIO_PATTERN.test(value.trim()); } /** * Returns Gallery styles for a viewport. * * @param {Object} style Gallery block style attribute. * @param {string} viewport Viewport state. * @return {Object} Gallery viewport styles, or an empty object when unavailable. */ function getViewportGalleryStyle(style, viewport) { if (!isObject(style) || !isObject(style[viewport])) return {}; return style[viewport]; } function cleanObject(value) { if (!isObject(value)) return value; const entries = Object.entries(value).filter(([, entryValue]) => entryValue !== void 0); return entries.length ? Object.fromEntries(entries) : void 0; } /** * Updates Gallery layout settings for a viewport without changing base values. * * @param {Object} options Options. * @param {Object} options.style Gallery block style attribute. * @param {string} options.viewport Selected viewport state. * @param {Object} options.baseSettings Gallery base layout settings. * @param {Object} options.settings Gallery layout settings to update. * @return {Object|undefined} Updated Gallery style attribute. */ function getUpdatedGalleryStyle({ style, viewport, baseSettings, settings }) { const currentStyle = isObject(style) ? style : {}; const nextViewportStyle = { ...getViewportGalleryStyle(currentStyle, viewport) }; Object.entries(settings).forEach(([key, value]) => { if (value === void 0 || key !== "columns" && value === baseSettings[key]) delete nextViewportStyle[key]; else nextViewportStyle[key] = value; }); const cleanedViewportStyle = cleanObject(nextViewportStyle); const nextStyle = { ...currentStyle }; if (cleanedViewportStyle) nextStyle[viewport] = cleanedViewportStyle; else delete nextStyle[viewport]; return cleanObject(nextStyle); } function getGallerySelector(selector) { return `${selector}.wp-block-gallery.has-nested-images`; } function getFlexGallerySelector(selector) { return `${getGallerySelector(selector)}:where(.is-layout-flex)`; } function getImageSelector(selector) { return `${getFlexGallerySelector(selector)} figure.wp-block-image:not(#individual-image)`; } function getColumnsCSS(selector, columns) { const width = columns === 1 ? "100%" : `calc((100% - (var(--wp--style--unstable-gallery-gap, 16px) * ${columns - 1})) / ${columns})`; return `${getImageSelector(selector)}{width:${width} !important;}`; } function getImageCropCSS(selector, imageCrop) { const imageSelector = getImageSelector(selector); const wrapperSelector = `${imageSelector} > div:not(.components-drop-zone)`; const linkSelector = `${imageSelector} > a`; const mediaSelector = `${imageSelector} a,${imageSelector} img`; if (imageCrop) return `${imageSelector}{align-self:inherit !important;margin-bottom:0 !important;}${wrapperSelector},${linkSelector}{display:flex !important;}${mediaSelector}{width:100% !important;flex:1 0 0% !important;height:100% !important;object-fit:cover !important;}`; return `${imageSelector}{align-self:auto !important;margin-top:0 !important;margin-bottom:auto !important;}${wrapperSelector}{display:block !important;}${linkSelector}{display:inline-block !important;}${mediaSelector}{width:auto !important;flex:0 1 auto !important;height:auto !important;object-fit:fill !important;}`; } function getAspectRatioCSS(selector, aspectRatio) { const imageSelector = `${getGallerySelector(selector)} figure.wp-block-image:not(#individual-image) img`; const ratio = aspectRatio.trim(); if (ratio === "auto") return `${imageSelector}{aspect-ratio:revert-layer !important;}`; return `${imageSelector}{aspect-ratio:${ratio} !important;object-fit:cover !important;}`; } /** * Generates Gallery-specific responsive CSS. * * @param {string} selector Gallery block selector. * @param {Object} style Gallery block style attribute. * @param {Object} mediaQueries Map of viewport keys to media queries. * @return {string} Responsive Gallery CSS. */ function getGalleryResponsiveCSS(selector, style, mediaQueries) { if (!isObject(mediaQueries)) return ""; return Object.entries(mediaQueries).map(([viewport, mediaQuery]) => { if (typeof mediaQuery !== "string") return ""; const viewportStyle = getViewportGalleryStyle(style, viewport); let css = ""; if (isValidGalleryColumns(viewportStyle.columns)) css += getColumnsCSS(selector, viewportStyle.columns); if (typeof viewportStyle.imageCrop === "boolean") css += getImageCropCSS(selector, viewportStyle.imageCrop); if (isValidGalleryAspectRatio(viewportStyle.aspectRatio)) css += getAspectRatioCSS(selector, viewportStyle.aspectRatio); return css ? `${mediaQuery}{${css}}` : ""; }).join(""); } exports.getGalleryResponsiveCSS = getGalleryResponsiveCSS; exports.getUpdatedGalleryStyle = getUpdatedGalleryStyle; exports.getViewportGalleryStyle = getViewportGalleryStyle; exports.isValidGalleryAspectRatio = isValidGalleryAspectRatio; exports.isValidGalleryColumns = isValidGalleryColumns; //# sourceMappingURL=responsive-styles.cjs.map