Repository URL to install this package:
|
Version:
3.12.20 ▾
|
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 { useMemo, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { ArrowRightOutline, ArrowLeftOutline } from '@scaleflex/icons';
import isTransparencySupported from '@filerobot/utils/lib/isTransparencySupported';
import { DEFAULT_ZOOM_RATIO, WHEEL_ZOOM_RATIO_PERCENTAGE, MIN_ZOOM_RATIO, MAX_ZOOM_RATIO } from './Body.constants';
import BodyFile from './BodyFile';
import StyledBody from './Body.styled';
import PreviewPlaceholder from './PreviewPlaceholder';
import { selectCurrentFilesArray } from '../../../slices/files.slice';
import { fileWindowUpdated } from '../../../slices/panels.slice';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var previousTouch;
var Preview = function Preview(_ref) {
var _ref$previewedFile = _ref.previewedFile,
previewedFile = _ref$previewedFile === void 0 ? {} : _ref$previewedFile,
_ref$displayedVariant = _ref.displayedVariant,
displayedVariant = _ref$displayedVariant === void 0 ? {} : _ref$displayedVariant,
isCommentsTabOpened = _ref.isCommentsTabOpened,
zoomRatio = _ref.zoomRatio,
setZoomRatio = _ref.setZoomRatio,
isPdfPreviewSupported = _ref.isPdfPreviewSupported,
videoEl = _ref.videoEl,
setVideoRef = _ref.setVideoRef,
isFileLoading = _ref.isFileLoading,
isFileFailedToLoad = _ref.isFileFailedToLoad,
setFileData = _ref.setFileData,
imgEl = _ref.imgEl,
setImgRef = _ref.setImgRef,
isMediaLoadingFailed = _ref.isMediaLoadingFailed,
setIsMediaLoadingFailed = _ref.setIsMediaLoadingFailed,
alertUnsavedChangesFirst = _ref.alertUnsavedChangesFirst;
var dispatch = useDispatch();
var filesArray = useSelector(selectCurrentFilesArray);
var previewEl = useRef();
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
isPanEnabled = _useState2[0],
setIsPanEnabled = _useState2[1];
var _useState3 = useState({
x: 0,
y: 0
}),
_useState4 = _slicedToArray(_useState3, 2),
panningDimens = _useState4[0],
setPanningDimens = _useState4[1];
var isOneFile = filesArray.length === 1;
var currentIndex = useMemo(function () {
return Array.isArray(filesArray) ? filesArray.findIndex(function (item) {
return item.uuid && previewedFile.uuid ? item.uuid === previewedFile.uuid : item.id === previewedFile.id;
}) : 0;
}, [filesArray, previewedFile]);
var isLastFile = currentIndex === (filesArray === null || filesArray === void 0 ? void 0 : filesArray.length) - 1;
var disablePan = function disablePan() {
setIsPanEnabled(false);
previousTouch = null;
document.removeEventListener('mouseleave', disablePan);
document.removeEventListener('mouseup', disablePan);
document.removeEventListener('touchcancel', disablePan);
document.removeEventListener('touchend', disablePan);
};
var enablePan = function enablePan() {
setIsPanEnabled(true);
// We are setting those events on document to support panning
// even if user went outside element and get back while holding pointer down
document.addEventListener('mouseleave', disablePan, {
once: true
});
document.addEventListener('mouseup', disablePan, {
once: true
});
document.addEventListener('touchcancel', disablePan, {
once: true
});
document.addEventListener('touchend', disablePan, {
once: true
});
};
var handlePanning = function handlePanning(e) {
if (isPanEnabled && imgEl.current && zoomRatio > DEFAULT_ZOOM_RATIO) {
var panningArea = {
x: (imgEl.current.width * zoomRatio - previewEl.current.offsetWidth) / (2 * zoomRatio),
y: (imgEl.current.height * zoomRatio - previewEl.current.offsetHeight) / (2 * zoomRatio)
};
setPanningDimens(function (latestDimens) {
return {
x: Math.min(Math.max(-panningArea.x, latestDimens.x + e.movementX), panningArea.x),
y: Math.min(Math.max(-panningArea.y, latestDimens.y + e.movementY), panningArea.y)
};
});
}
};
var handleTouchPanning = function handleTouchPanning(e) {
var touch = e.touches[0];
if (previousTouch) {
setIsPanEnabled(true);
var event = _objectSpread(_objectSpread({}, e), {}, {
movementX: touch.pageX - previousTouch.pageX,
movementY: touch.pageY - previousTouch.pageY
});
handlePanning(event);
}
previousTouch = touch;
};
var zoomByWheel = function zoomByWheel(e) {
if (isPdfPreviewSupported || isCommentsTabOpened) return; // [TODO]: currently we have a big delay when zoom PDF by wheel
setZoomRatio(function (latestZoomRatio) {
return Math.min(MAX_ZOOM_RATIO, Math.max(MIN_ZOOM_RATIO, latestZoomRatio + (e.deltaY > 1 ? -WHEEL_ZOOM_RATIO_PERCENTAGE : WHEEL_ZOOM_RATIO_PERCENTAGE)));
});
};
var updateFileWindow = function updateFileWindow(fileUuid) {
dispatch(fileWindowUpdated({
"for": fileUuid,
enableEdit: false,
enableRename: false
}));
};
var getNextFile = function getNextFile() {
var nextIndex = currentIndex + 1;
var nextFile = nextIndex < filesArray.length ? filesArray[nextIndex] : filesArray[0];
if (nextFile) {
alertUnsavedChangesFirst(function () {
updateFileWindow(nextFile.uuid || nextFile.id);
});
}
};
var getPreviousFile = function getPreviousFile() {
var previousIndex = currentIndex === 0 ? filesArray.length - 1 : currentIndex - 1;
var previousFile = filesArray[previousIndex];
if (previousFile) {
alertUnsavedChangesFirst(function () {
updateFileWindow(previousFile.uuid || previousFile.id);
});
}
};
var displayedFile = _objectSpread(_objectSpread({}, previewedFile), displayedVariant);
if (isFileFailedToLoad) {
return /*#__PURE__*/_jsx(StyledBody.Body, {
ref: previewEl,
onTouchMove: handleTouchPanning,
onMouseDown: enablePan,
onMouseMove: handlePanning,
onWheel: zoomByWheel,
$fullWidth: isPdfPreviewSupported,
draggable: "false",
$hidePagination: isOneFile,
children: /*#__PURE__*/_jsx(PreviewPlaceholder, {
setFileData: setFileData
})
});
}
if (isFileLoading) {
return /*#__PURE__*/_jsx(StyledBody.Body, {
ref: previewEl,
onTouchMove: handleTouchPanning,
onMouseDown: enablePan,
onMouseMove: handlePanning,
onWheel: zoomByWheel,
$fullWidth: isPdfPreviewSupported,
draggable: "false",
$hidePagination: isOneFile,
isFileLoading: isFileLoading,
children: /*#__PURE__*/_jsx(StyledBody.Skeleton, {
height: 250
})
});
}
return /*#__PURE__*/_jsxs(StyledBody.Body, {
ref: previewEl,
onTouchMove: handleTouchPanning,
onMouseDown: enablePan,
onMouseMove: handlePanning,
onWheel: zoomByWheel,
$fullWidth: isPdfPreviewSupported,
draggable: "false",
$hidePagination: isOneFile,
children: [/*#__PURE__*/_jsx(StyledBody.FilePaginationButtons, {
color: "primary",
left: true,
onClick: getPreviousFile,
disabled: !currentIndex,
children: /*#__PURE__*/_jsx(ArrowLeftOutline, {
size: 20
})
}), /*#__PURE__*/_jsx(BodyFile, {
file: displayedFile,
zoomRatio: zoomRatio,
panningDimens: panningDimens,
setPanningDimens: setPanningDimens,
isPanEnabled: isPanEnabled,
imgEl: imgEl,
setImgRef: setImgRef,
previewEl: previewEl,
videoEl: videoEl,
isPdfPreviewSupported: isPdfPreviewSupported,
isTransparencySupported: isTransparencySupported(displayedFile),
setVideoRef: setVideoRef,
isCommentsTabOpened: isCommentsTabOpened,
isMediaLoadingFailed: isMediaLoadingFailed,
setIsMediaLoadingFailed: setIsMediaLoadingFailed
}), /*#__PURE__*/_jsx(StyledBody.FilePaginationButtons, {
color: "primary",
onClick: getNextFile,
disabled: isLastFile,
children: /*#__PURE__*/_jsx(ArrowRightOutline, {
size: 20
})
})]
});
};
export default Preview;