Repository URL to install this package:
|
Version:
3.12.16 ▾
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useRef, useState } from 'react';
import { FILE_TYPES_AND_ICONS as ICONS, FILE_TYPES_ICON_PATH } from '@filerobot/utils/lib/constants';
import LinkHelper from '@filerobot/utils/lib/LinkHelper';
import clsx from '@filerobot/utils/lib/clsx';
import getFormattedPreviewUrl from '@filerobot/utils/lib/getFormattedPreviewUrl';
import Styled from './ImagePreview.styled';
import { Image } from '../index';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var ICON_WIDTH = 500;
var ICON_HEIGHT = 375;
var ICON_MARGIN = 30;
var DEFAULT_IMAGE_SIZE = 250;
var ImagePreview = function ImagePreview(_ref) {
var srcUrl = _ref.srcUrl,
extension = _ref.extension,
size = _ref.size,
onDoubleClick = _ref.onDoubleClick,
innerRef = _ref.innerRef,
parentIsList = _ref.parentIsList,
containerClassName = _ref.containerClassName,
onMouseLeave = _ref.onMouseLeave,
onMouseOver = _ref.onMouseOver,
onMouseEnter = _ref.onMouseEnter,
noImgTransformations = _ref.noImgTransformations,
isDevEnv = _ref.isDevEnv,
isHubMode = _ref.isHubMode,
container = _ref.container,
className = _ref.className,
containerRef = _ref.containerRef,
lazyloading = _ref.lazyloading,
isVersioningItem = _ref.isVersioningItem,
isLargeSizePreview = _ref.isLargeSizePreview,
isTransparencySupported = _ref.isTransparencySupported,
_ref$isFileDetailsPre = _ref.isFileDetailsPreview,
isFileDetailsPreview = _ref$isFileDetailsPre === void 0 ? false : _ref$isFileDetailsPre,
containerHeight = _ref.containerHeight,
thumbnailUrl = _ref.thumbnailUrl,
previewUrl = _ref.previewUrl;
var _containerRef = useRef();
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
isImageLoaded = _useState2[0],
setIsImageLoaded = _useState2[1];
// thumbnailUrl is url to be used with small size images, i.e preview images, if not then there then we shall use previewUrl then src url
var imgPreviewUrl = !isLargeSizePreview && thumbnailUrl || previewUrl || srcUrl;
if (imgPreviewUrl === null) return null;
var isFileTypeIcon = imgPreviewUrl.includes(FILE_TYPES_ICON_PATH);
var getAppendedLink = function getAppendedLink(_ref2) {
var url = _ref2.url,
searchParams = _ref2.searchParams;
return new LinkHelper({
href: url || ''
}).appendSearchParams(searchParams);
};
var getFormattedUrl = function getFormattedUrl(url) {
return getFormattedPreviewUrl({
url: url,
isDevEnv: isDevEnv,
isHubMode: isHubMode,
containerToken: container,
provideContainerToken: Boolean(isVersioningItem)
});
};
// TODO(Dima): check if we can apply transformations by using ultrafast link or some other solutions how we can resize image on the fly from 3ed part sources
var getImgSrc = function getImgSrc() {
var fileTypeIconSearchParam = isFileTypeIcon ? {
func: 'fit',
h: ICON_HEIGHT,
margin: "".concat(ICON_MARGIN, "p")
} : {};
var fileWidthSearchParam = {
w: isFileTypeIcon ? ICON_WIDTH : Math.floor(DEFAULT_IMAGE_SIZE * devicePixelRatio)
};
var largeImgSearchParam = isLargeImagePreview(extension, size) && {
ci_preview: 1
};
if (noImgTransformations) {
return getFormattedUrl(imgPreviewUrl);
}
if (!isLargeSizePreview && thumbnailUrl) {
return getAppendedLink({
url: getFormattedUrl(thumbnailUrl),
searchParams: _objectSpread({}, fileTypeIconSearchParam)
});
}
if (previewUrl) {
return getAppendedLink({
url: getFormattedUrl(previewUrl),
searchParams: _objectSpread(_objectSpread({}, isLargeSizePreview ? {} : fileWidthSearchParam), fileTypeIconSearchParam)
});
}
return getAppendedLink({
url: getFormattedUrl(srcUrl),
searchParams: _objectSpread(_objectSpread(_objectSpread({}, fileWidthSearchParam), fileTypeIconSearchParam), largeImgSearchParam)
});
};
var assetTypeIconFormattedUrl = getAppendedLink({
url: ICONS[(extension || '').toLowerCase()] || ICONS._default || '',
searchParams: {
w: ICON_WIDTH,
func: 'fit',
h: ICON_HEIGHT
}
});
if (localStorage.getItem('debug_previews') === '1') {
var imageSrc = getImgSrc();
var formattedPreviewUrl = getFormattedUrl(imgPreviewUrl);
console.log({
imageSrc: imageSrc,
formattedPreviewUrl: formattedPreviewUrl,
assetTypeIconFormattedUrl: assetTypeIconFormattedUrl
});
}
return /*#__PURE__*/_jsxs(Styled.ImagePreviewWrapper, {
containerHeight: containerHeight,
onMouseOver: onMouseOver,
onMouseLeave: onMouseLeave,
onMouseEnter: onMouseEnter,
isListIcon: Boolean(parentIsList),
className: clsx(_objectSpread({
'filerobot-common-ItemIcon': true
}, containerClassName ? _defineProperty({}, containerClassName, true) : {})),
ref: containerRef || _containerRef,
children: [!isImageLoaded && /*#__PURE__*/_jsx(Styled.AssetTypeIcon, {
src: assetTypeIconFormattedUrl
}), /*#__PURE__*/_jsx(Image, {
loading: lazyloading ? 'lazy' : 'eager',
className: className ? "".concat(className, " filerobot-common-ItemIcon-img") : 'filerobot-common-ItemIcon-img',
src: getImgSrc(),
onLoad: function onLoad() {
return setIsImageLoaded(true);
},
onDoubleClick: onDoubleClick,
style: isTransparencySupported ? {
backgroundImage: "url(".concat(ICONS.chess_bg, ")"),
width: 'auto',
height: 'auto',
margin: isFileDetailsPreview ? 'auto' : ''
} : {},
ref: innerRef
})]
});
};
function isLargeImagePreview(extension, size) {
switch (extension) {
case 'png':
return size > 20971520;
// if png more than 20Mb
case 'jpg':
case 'jpeg':
case 'webp':
return size > 10485760;
// if jpg or webp more than 10Mb
default:
return false;
}
}
export default ImagePreview;