"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.__PictureImpl = exports.__SourceImpl = exports.Source = exports.Picture = exports.__ReactImgixImpl = exports.default = void 0;
require("./array-findindex");
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _targetWidths = _interopRequireDefault(require("./targetWidths"));
var _constructUrl = _interopRequireDefault(require("./constructUrl"));
var _extractQueryParams3 = _interopRequireDefault(require("./extractQueryParams"));
var _HOCs = require("./HOCs");
var _common = require("./common");
var _constants = require("./constants");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
var PACKAGE_VERSION = "9.0.0";
var NODE_ENV = process.env.NODE_ENV;
var buildKey = function buildKey(idx) {
return "react-imgix-".concat(idx);
};
var defaultImgixParams = {
auto: ["format"]
};
var defaultAttributeMap = {
src: "src",
srcSet: "srcSet",
sizes: "sizes"
};
var noop = function noop() {};
var COMMON_PROP_TYPES = {
className: _propTypes.default.string,
onMounted: _propTypes.default.func,
htmlAttributes: _propTypes.default.object
};
var SHARED_IMGIX_AND_SOURCE_PROP_TYPES = _extends({}, COMMON_PROP_TYPES, {
disableQualityByDPR: _propTypes.default.bool,
disableSrcSet: _propTypes.default.bool,
disableLibraryParam: _propTypes.default.bool,
imgixParams: _propTypes.default.object,
sizes: _propTypes.default.string,
width: _propTypes.default.number,
height: _propTypes.default.number,
src: _propTypes.default.string.isRequired
});
/**
* Validates that an aspect ratio is in the format w:h. If false is returned, the aspect ratio is in the wrong format.
*/
function aspectRatioIsValid(aspectRatio) {
if (typeof aspectRatio !== "string") {
return false;
}
return /^\d+(\.\d+)?:\d+(\.\d+)?$/.test(aspectRatio);
}
var buildSrcSetPairWithFixedHeight = function buildSrcSetPairWithFixedHeight(url, targetWidth, fixedHeight, _) {
return url + "&h=" + fixedHeight + "&w=" + targetWidth + " " + targetWidth + "w";
};
var buildSrcSetPairWithTargetWidth = function buildSrcSetPairWithTargetWidth(url, targetWidth, _1, _2) {
return url + "&w=" + targetWidth + " " + targetWidth + "w";
};
var buildDprSrcWithQuality = function buildDprSrcWithQuality(url, quality, dpr) {
return url + "&q=" + quality + "&dpr=" + dpr + " " + dpr + "x";
};
var buildDprSrcWithoutQuality = function buildDprSrcWithoutQuality(url, _, dpr) {
return url + "&dpr=" + dpr + " " + dpr + "x";
};
var buildDprSrcWithQualityByDpr = function buildDprSrcWithQualityByDpr(url, quality, dpr) {
return url + "&q=" + quality + "&dpr=" + dpr + " " + dpr + "x";
};
/**
* Build a imgix source url with parameters from a raw url
*/
function buildSrc(_ref) {
var inputSrc = _ref.src,
width = _ref.width,
height = _ref.height,
disableLibraryParam = _ref.disableLibraryParam,
disableSrcSet = _ref.disableSrcSet,
imgixParams = _ref.imgixParams,
disableQualityByDPR = _ref.disableQualityByDPR;
var fixedSize = width != null || height != null;
var _extractQueryParams = (0, _extractQueryParams3.default)(inputSrc),
_extractQueryParams2 = _slicedToArray(_extractQueryParams, 2),
rawSrc = _extractQueryParams2[0],
params = _extractQueryParams2[1];
var srcOptions = _extends({}, params, imgixParams, disableLibraryParam ? {} : {
ixlib: "react-".concat(PACKAGE_VERSION)
}, fixedSize && height ? {
height: height
} : {}, fixedSize && width ? {
width: width
} : {});
var src = (0, _constructUrl.default)(rawSrc, srcOptions);
var srcSet;
if (disableSrcSet) {
srcSet = src;
} else {
if (fixedSize) {
var q = srcOptions.q,
urlParams = _objectWithoutProperties(srcOptions, ["q"]);
var constructedUrl = (0, _constructUrl.default)(rawSrc, urlParams);
var srcFn = buildDprSrcWithQualityByDpr;
if (q) {
srcFn = buildDprSrcWithQuality;
} else if (disableQualityByDPR) {
srcFn = buildDprSrcWithoutQuality;
}
srcSet = "";
var len = _constants.DPR_QUALITY_VALUES.length;
for (var _i2 = 0; _i2 < len; _i2++) {
var quality = _constants.DPR_QUALITY_VALUES[_i2];
srcSet += srcFn(constructedUrl, q || quality, _i2 + 1) + ", ";
}
srcSet = srcSet.slice(0, -2);
} else {
var _width = srcOptions.width,
w = srcOptions.w,
_height = srcOptions.height,
_urlParams = _objectWithoutProperties(srcOptions, ["width", "w", "height"]);
var _constructedUrl = (0, _constructUrl.default)(rawSrc, _urlParams);
var aspectRatio = imgixParams.ar;
var showARWarning = aspectRatio != null && aspectRatioIsValid(aspectRatio) === false;
var _srcFn = buildSrcSetPairWithTargetWidth;
if (_height) {
_srcFn = buildSrcSetPairWithFixedHeight;
}
srcSet = "";
var _len = _targetWidths.default.length;
for (var _i3 = 0; _i3 < _len; _i3++) {
var targetWidth = _targetWidths.default[_i3];
srcSet += _srcFn(_constructedUrl, targetWidth, _height) + ", ";
}
srcSet = srcSet.slice(0, -2);
if (NODE_ENV !== "production" && showARWarning && _common.config.warnings.invalidARFormat) {
console.warn("[Imgix] The aspect ratio passed (\"".concat(aspectRatio, "\") is not in the correct format. The correct format is \"W:H\"."));
}
}
}
return {
src: src,
srcSet: srcSet
};
}
/**
* Combines default imgix params with custom imgix params to make a imgix params config object
*/
function imgixParams(props) {
var params = _extends({}, defaultImgixParams, props.imgixParams);
return _extends({}, params);
}
/**
* React component used to render elements with Imgix
*/
var ReactImgix =
/*#__PURE__*/
function (_Component) {
_inherits(ReactImgix, _Component);
function ReactImgix(props) {
var _this;
_classCallCheck(this, ReactImgix);
_this = _possibleConstructorReturn(this, _getPrototypeOf(ReactImgix).call(this, props));
_this.imgRef = null;
return _this;
}
_createClass(ReactImgix, [{
key: "componentDidMount",
value: function componentDidMount() {
this.props.onMounted(this.imgRef);
}
}, {
key: "render",
value: function render() {
var _this2 = this,
_extends2;
var _this$props = this.props,
disableSrcSet = _this$props.disableSrcSet,
type = _this$props.type,
width = _this$props.width,
height = _this$props.height; // Pre-render checks
if (NODE_ENV !== "production" && _common.config.warnings.sizesAttribute) {
if (this.props.width == null && this.props.height == null && this.props.sizes == null && !this.props._inPicture) {
console.warn("If width and height are not set, a sizes attribute should be passed.");
}
}
var _buildSrc = buildSrc(_extends({}, this.props, {
type: "img",
imgixParams: imgixParams(this.props)
})),
src = _buildSrc.src,
srcSet = _buildSrc.srcSet;
var attributeConfig = _extends({}, defaultAttributeMap, this.props.attributeConfig);
var childProps = _extends({}, this.props.htmlAttributes, (_extends2 = {}, _defineProperty(_extends2, attributeConfig.sizes, this.props.sizes), _defineProperty(_extends2, "className", this.props.className), _defineProperty(_extends2, "width", width <= 1 ? null : width), _defineProperty(_extends2, "height", height <= 1 ? null : height), _defineProperty(_extends2, attributeConfig.src, src), _defineProperty(_extends2, "ref", function ref(el) {
return _this2.imgRef = el;
}), _extends2));
if (!disableSrcSet) {
childProps[attributeConfig.srcSet] = srcSet;
}
return _react.default.createElement("img", childProps);
}
}]);
return ReactImgix;
}(_react.Component);
exports.__ReactImgixImpl = ReactImgix;
_defineProperty(ReactImgix, "propTypes", _extends({}, SHARED_IMGIX_AND_SOURCE_PROP_TYPES));
_defineProperty(ReactImgix, "defaultProps", {
disableSrcSet: false,
onMounted: noop
});
ReactImgix.displayName = "ReactImgix";
/**
* React component used to render