@uxf/core
Advanced tools
Comparing version 11.20.0 to 11.21.0
{ | ||
"name": "@uxf/core", | ||
"version": "11.20.0", | ||
"version": "11.21.0", | ||
"description": "UXF Core", | ||
@@ -5,0 +5,0 @@ "author": "Petr Vejvoda <vejvoda@uxf.cz>", |
import { Ref, RefCallback } from "react"; | ||
/** | ||
* @deprecated Use import from @uxf/core-react instead. | ||
*/ | ||
export declare function composeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.composeRefs = void 0; | ||
/** | ||
* @deprecated Use import from @uxf/core-react instead. | ||
*/ | ||
function composeRefs(...refs) { | ||
@@ -5,0 +8,0 @@ return (value) => refs.forEach((ref) => { |
@@ -1,2 +0,2 @@ | ||
import { FC } from "react"; | ||
import React from "react"; | ||
import { ImageResponse, ImageSource, ResizerImageProps, StaticImageData } from "./resizer"; | ||
@@ -6,4 +6,5 @@ export type Dimension = number | "auto"; | ||
export type Quality = number | Record<Format, number>; | ||
export declare function toImageResponse(src: any): ImageResponse | undefined; | ||
export declare function toStaticImageData(src: any): StaticImageData | undefined; | ||
export declare function isSrcString(src: ImageSource): src is string; | ||
export declare function isSrcImageResponse(src: ImageSource): src is ImageResponse; | ||
export declare function isSrcStaticImageData(src: ImageSource): src is StaticImageData; | ||
export declare function parseImgExtension(src: string): string | undefined; | ||
@@ -18,3 +19,3 @@ export declare function getSvgImgUrl(src: ImageSource): string | undefined; | ||
}; | ||
export declare function getImgSrcSet(src?: ImageSource, width?: Dimension, height?: Dimension, options?: GetImgSrcSetOptions): string | undefined; | ||
export declare function getImgSrcSet(src: ImageSource, width?: Dimension, height?: Dimension, options?: GetImgSrcSetOptions): string | undefined; | ||
/** | ||
@@ -39,3 +40,3 @@ * @deprecated Use import from @uxf/ui instead. | ||
*/ | ||
export declare const ImgSources: FC<ImageSourcesProps>; | ||
export declare function ImgSources(props: ImageSourcesProps): React.JSX.Element | null; | ||
export {}; |
@@ -6,15 +6,21 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ImgSources = exports.getImgSrcSet = exports.getImgElementWidth = exports.getImgElementHeight = exports.getImgUniqueIdentifier = exports.getImgQuality = exports.getSvgImgUrl = exports.parseImgExtension = exports.toStaticImageData = exports.toImageResponse = void 0; | ||
exports.ImgSources = exports.getImgSrcSet = exports.getImgElementWidth = exports.getImgElementHeight = exports.getImgUniqueIdentifier = exports.getImgQuality = exports.getSvgImgUrl = exports.parseImgExtension = exports.isSrcStaticImageData = exports.isSrcImageResponse = exports.isSrcString = void 0; | ||
const hide_1 = require("@uxf/core-react/components/hide"); | ||
const react_1 = __importDefault(require("react")); | ||
const file_1 = require("./file"); | ||
const is_empty_1 = require("./is-empty"); | ||
const is_not_nil_1 = require("./is-not-nil"); | ||
const resizer_1 = require("./resizer"); | ||
function toImageResponse(src) { | ||
return typeof src !== "string" && "uuid" in src ? src : undefined; | ||
function isSrcString(src) { | ||
return typeof src === "string"; | ||
} | ||
exports.toImageResponse = toImageResponse; | ||
function toStaticImageData(src) { | ||
return typeof src !== "string" && "src" in src ? src : undefined; | ||
exports.isSrcString = isSrcString; | ||
function isSrcImageResponse(src) { | ||
return !isSrcString(src) && "uuid" in src; | ||
} | ||
exports.toStaticImageData = toStaticImageData; | ||
exports.isSrcImageResponse = isSrcImageResponse; | ||
function isSrcStaticImageData(src) { | ||
return !isSrcString(src) && "src" in src; | ||
} | ||
exports.isSrcStaticImageData = isSrcStaticImageData; | ||
function parseImgExtension(src) { | ||
@@ -25,13 +31,14 @@ const ext = src.slice(src.lastIndexOf(".") + 1); | ||
exports.parseImgExtension = parseImgExtension; | ||
function isSvgExtension(src) { | ||
return parseImgExtension(src) === "svg"; | ||
} | ||
function getSvgImgUrl(src) { | ||
if (typeof src === "string" && parseImgExtension(src) === "svg") { | ||
if (isSrcString(src) && isSvgExtension(src)) { | ||
return src; | ||
} | ||
const imageResponse = toImageResponse(src); | ||
if (imageResponse && parseImgExtension(imageResponse.extension) === "svg") { | ||
return (0, file_1.getFileUrl)(imageResponse); | ||
if (isSrcImageResponse(src) && isSvgExtension(src.extension)) { | ||
return (0, file_1.getFileUrl)(src); | ||
} | ||
const staticImageData = toStaticImageData(src); | ||
if (staticImageData && parseImgExtension(staticImageData.src) === "svg") { | ||
return staticImageData.src; | ||
if (isSrcStaticImageData(src) && isSvgExtension(src.src)) { | ||
return src.src; | ||
} | ||
@@ -42,8 +49,16 @@ return undefined; | ||
function getImgQuality(quality, format) { | ||
return typeof quality === "number" ? quality : quality ? quality[format] : undefined; | ||
if (typeof quality === "number") { | ||
return quality; | ||
} | ||
return quality ? quality[format] : undefined; | ||
} | ||
exports.getImgQuality = getImgQuality; | ||
function getImgUniqueIdentifier(src) { | ||
var _a, _b, _c; | ||
return typeof src === "string" ? src : (_b = (_a = toImageResponse(src)) === null || _a === void 0 ? void 0 : _a.uuid) !== null && _b !== void 0 ? _b : (_c = toStaticImageData(src)) === null || _c === void 0 ? void 0 : _c.src; | ||
return isSrcString(src) | ||
? src | ||
: isSrcImageResponse(src) | ||
? src.uuid | ||
: isSrcStaticImageData(src) | ||
? src.src | ||
: undefined; | ||
} | ||
@@ -53,5 +68,7 @@ exports.getImgUniqueIdentifier = getImgUniqueIdentifier; | ||
var _a, _b; | ||
return typeof src === "string" | ||
? height | ||
: height !== null && height !== void 0 ? height : (Math.floor((((_a = src.height) !== null && _a !== void 0 ? _a : 0) / ((_b = src.width) !== null && _b !== void 0 ? _b : 0)) * (width || 0)) || undefined); | ||
if (isSrcString(src) || (0, is_not_nil_1.isNotNil)(height)) { | ||
return height; | ||
} | ||
const calculatedHeight = Math.floor((((_a = src.height) !== null && _a !== void 0 ? _a : 0) / ((_b = src.width) !== null && _b !== void 0 ? _b : 0)) * (width !== null && width !== void 0 ? width : 0)); | ||
return calculatedHeight === 0 ? undefined : calculatedHeight; | ||
} | ||
@@ -61,41 +78,47 @@ exports.getImgElementHeight = getImgElementHeight; | ||
var _a, _b; | ||
return typeof src === "string" | ||
? width | ||
: width !== null && width !== void 0 ? width : (Math.floor(((_a = src.width) !== null && _a !== void 0 ? _a : 0) / ((_b = src.height) !== null && _b !== void 0 ? _b : 0)) * (height || 0) || undefined); | ||
if (isSrcString(src) || (0, is_not_nil_1.isNotNil)(width)) { | ||
return width; | ||
} | ||
const calculatedWidth = Math.floor(((_a = src.width) !== null && _a !== void 0 ? _a : 0) / ((_b = src.height) !== null && _b !== void 0 ? _b : 0)) * (height !== null && height !== void 0 ? height : 0); | ||
return calculatedWidth === 0 ? undefined : calculatedWidth; | ||
} | ||
exports.getImgElementWidth = getImgElementWidth; | ||
function isDimensionNumber(value) { | ||
return typeof value === "number"; | ||
} | ||
function getDimension(dimension, size) { | ||
return isDimensionNumber(dimension) ? dimension * size : "auto"; | ||
} | ||
const HIDPI_SIZES = [3, 2, 1]; | ||
function getImgSrcSet(src, width, height, options = {}) { | ||
var _a, _b; | ||
if (!src) { | ||
return undefined; | ||
} | ||
let toFormat; | ||
let h = height !== null && height !== void 0 ? height : "auto"; | ||
let w = width !== null && width !== void 0 ? width : "auto"; | ||
if (typeof src === "string") { | ||
if (isSrcString(src)) { | ||
if ((0, is_empty_1.isEmpty)(src)) { | ||
return undefined; | ||
} | ||
toFormat = parseImgExtension(src); | ||
} | ||
const imageResponse = toImageResponse(src); | ||
if (imageResponse) { | ||
if (!imageResponse.uuid) { | ||
if (isSrcImageResponse(src)) { | ||
if ((0, is_empty_1.isEmpty)(src.uuid)) { | ||
return undefined; | ||
} | ||
toFormat = parseImgExtension(imageResponse.extension); | ||
h = (_a = (h || imageResponse.height)) !== null && _a !== void 0 ? _a : "auto"; | ||
w = (_b = (w || imageResponse.width)) !== null && _b !== void 0 ? _b : "auto"; | ||
toFormat = parseImgExtension(src.extension); | ||
h = (_a = (h || src.height)) !== null && _a !== void 0 ? _a : "auto"; | ||
w = (_b = (w || src.width)) !== null && _b !== void 0 ? _b : "auto"; | ||
} | ||
const staticImageData = toStaticImageData(src); | ||
if (staticImageData) { | ||
if (!staticImageData.src) { | ||
if (isSrcStaticImageData(src)) { | ||
if ((0, is_empty_1.isEmpty)(src.src)) { | ||
return undefined; | ||
} | ||
toFormat = parseImgExtension(staticImageData.src); | ||
h = h || staticImageData.height; | ||
w = w || staticImageData.width; | ||
toFormat = parseImgExtension(src.src); | ||
h = h || src.height; | ||
w = w || src.width; | ||
} | ||
const { maxHiDpi, ...restOptions } = options; | ||
if (typeof h === "number" || typeof w === "number") { | ||
if (isDimensionNumber(h) || isDimensionNumber(w)) { | ||
return (maxHiDpi ? HIDPI_SIZES.slice(-maxHiDpi) : HIDPI_SIZES) | ||
.map((size) => (0, resizer_1.resizerImageUrl)(src, typeof w === "number" ? w * size : "auto", typeof h === "number" ? h * size : "auto", { | ||
.map((size) => (0, resizer_1.resizerImageUrl)(src, getDimension(w, size), getDimension(h, size), { | ||
toFormat, | ||
@@ -112,19 +135,16 @@ ...restOptions, | ||
*/ | ||
const ImgSources = (props) => { | ||
function ImgSources(props) { | ||
var _a, _b, _c, _d, _e; | ||
if (!props.src) { | ||
if (isSrcString(props.src) && (0, is_empty_1.isEmpty)(props.src)) { | ||
return null; | ||
} | ||
const imageResponse = toImageResponse(props.src); | ||
if (imageResponse && !imageResponse.uuid) { | ||
if (isSrcImageResponse(props.src) && (0, is_empty_1.isEmpty)(props.src.uuid)) { | ||
return null; | ||
} | ||
const staticImageData = toStaticImageData(props.src); | ||
if (staticImageData && !staticImageData.src) { | ||
if (isSrcStaticImageData(props.src) && (0, is_empty_1.isEmpty)(props.src.src)) { | ||
return null; | ||
} | ||
let media; | ||
if (props.breakpoints && props.breakpointIndex) { | ||
media = `(min-width: ${props.breakpoints[props.breakpointIndex]})`; | ||
} | ||
const media = (0, is_not_nil_1.isNotNil)(props.breakpoints) && (0, is_not_nil_1.isNotNil)(props.breakpointIndex) | ||
? `(min-width: ${props.breakpoints[props.breakpointIndex]})` | ||
: undefined; | ||
return (react_1.default.createElement(react_1.default.Fragment, null, | ||
@@ -147,3 +167,3 @@ react_1.default.createElement(hide_1.Hide, { when: Boolean((_a = props.options) === null || _a === void 0 ? void 0 : _a.isAvifDisabled) }, | ||
}) }))); | ||
}; | ||
} | ||
exports.ImgSources = ImgSources; |
109586
2536