UNPKG

@wordpress/block-library

Version:
74 lines (73 loc) • 2.85 kB
import { getBlobTypeByURL, isBlobURL } from "@wordpress/blob"; // packages/block-library/src/cover/shared.js const POSITION_CLASSNAMES = { "top left": "is-position-top-left", "top center": "is-position-top-center", "top right": "is-position-top-right", "center left": "is-position-center-left", "center center": "is-position-center-center", center: "is-position-center-center", "center right": "is-position-center-right", "bottom left": "is-position-bottom-left", "bottom center": "is-position-bottom-center", "bottom right": "is-position-bottom-right" }; const IMAGE_BACKGROUND_TYPE = "image"; const VIDEO_BACKGROUND_TYPE = "video"; const EMBED_VIDEO_BACKGROUND_TYPE = "embed-video"; const COVER_MIN_HEIGHT = 50; const COVER_MAX_HEIGHT = 1e3; const COVER_DEFAULT_HEIGHT = 300; const DEFAULT_FOCAL_POINT = { x: .5, y: .5 }; const ALLOWED_MEDIA_TYPES = ["image", "video"]; function mediaPosition({ x, y } = DEFAULT_FOCAL_POINT) { return `${Math.round(x * 100)}% ${Math.round(y * 100)}%`; } function dimRatioToClass(ratio) { return ratio === 50 || ratio === void 0 ? null : "has-background-dim-" + 10 * Math.round(ratio / 10); } function attributesFromMedia(media) { if (!media || !media.url && !media.src) return { url: void 0, id: void 0 }; if (isBlobURL(media.url)) media.type = getBlobTypeByURL(media.url); let mediaType; if (media.media_type) { if (media.media_type === "image") mediaType = IMAGE_BACKGROUND_TYPE; else mediaType = VIDEO_BACKGROUND_TYPE; } else if (media.type && (media.type === "image" || media.type === "video")) mediaType = media.type; else return; return { url: media.url || media.src, id: media.id, alt: media?.alt, backgroundType: mediaType, ...mediaType === "video" ? { hasParallax: void 0 } : {} }; } /** * Checks of the contentPosition is the center (default) position. * * @param {string} contentPosition The current content position. * @return {boolean} Whether the contentPosition is center. */ function isContentPositionCenter(contentPosition) { return !contentPosition || contentPosition === "center center" || contentPosition === "center"; } /** * Retrieves the className for the current contentPosition. * The default position (center) will not have a className. * * @param {string} contentPosition The current content position. * @return {string} The className assigned to the contentPosition. */ function getPositionClassName(contentPosition) { if (isContentPositionCenter(contentPosition)) return ""; return POSITION_CLASSNAMES[contentPosition]; } export { ALLOWED_MEDIA_TYPES, COVER_DEFAULT_HEIGHT, COVER_MAX_HEIGHT, COVER_MIN_HEIGHT, DEFAULT_FOCAL_POINT, EMBED_VIDEO_BACKGROUND_TYPE, IMAGE_BACKGROUND_TYPE, VIDEO_BACKGROUND_TYPE, attributesFromMedia, dimRatioToClass, getPositionClassName, isContentPositionCenter, mediaPosition }; //# sourceMappingURL=shared.mjs.map