@helpscout/hsds-react
Version:
React component library for Help Scout's Design System
75 lines (57 loc) • 2.33 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.calculateSize = exports.getTruncatedText = void 0;
var _MessageCard = require("./MessageCard.css");
var _Truncate = _interopRequireDefault(require("../Truncate"));
var _react = _interopRequireDefault(require("react"));
var _jsxRuntime = require("react/jsx-runtime");
var sizeWithRatio = function sizeWithRatio(recalculatedSide, otherSide, defaultValue) {
return (// Check if other side is smaller than max size to not recalculate unnecessarily this side as it doesn't need any scaling
// other condition checks that the image fits the boundaries
otherSide < _MessageCard.MAX_IMAGE_SIZE ? defaultValue : recalculatedSide / otherSide * _MessageCard.MAX_IMAGE_SIZE
);
};
/**
@param text text to truncate
@param limit limit to truncate to
@return element with text truncated to the given limit.
*/
var getTruncatedText = function getTruncatedText(text, limit) {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Truncate.default, {
limit: limit,
type: "end",
children: text
});
};
/**
Calculate size of image to keep the original aspect ratio, but fit within MAX_IMAGE_SIZExMAX_IMAGE_SIZE square for image
@param image image to calculate size for. Must have width and height defined.
@return size Object of type {width: number, height: number} that defines recalculated image's size. Empty object in case of no width or height in parameter image.
*/
exports.getTruncatedText = getTruncatedText;
var calculateSize = function calculateSize(image) {
if (!image.width || !image.height) {
return {};
}
var width = parseInt(image.width);
var height = parseInt(image.height); // Not necessary to recalculate if it fits within boundaries
if (width < _MessageCard.MAX_IMAGE_SIZE && height < _MessageCard.MAX_IMAGE_SIZE) {
return {
width: width,
height: height
};
}
if (width > height) {
return {
height: sizeWithRatio(height, width, height),
width: Math.min(width, _MessageCard.MAX_IMAGE_SIZE)
};
} else {
return {
width: sizeWithRatio(width, height, _MessageCard.MAX_IMAGE_SIZE),
height: Math.min(height, _MessageCard.MAX_IMAGE_SIZE)
};
}
};
exports.calculateSize = calculateSize;
;