Repository URL to install this package:
|
Version:
3.12.20 ▾
|
@filerobot/explorer
/
lib
/
components
/
Modals
/
TransformedDownload
/
TransformedDownload.hooks.js
|
|---|
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); }
import { useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { Crop, Download, Settings } from '@scaleflex/icons';
import { useModal } from '@filerobot/core/lib/hooks';
import isPdf from '@filerobot/utils/lib/isPdf';
import { useExplorer, useExplorerI18n } from '../../../hooks';
import { getFileWithNewParams } from './TransformedDownload.utils';
import { cropPanelFileSet } from '../../../slices/panels.slice';
import { downloadFilesOneByOne, downloadItems, emitExportEvent } from '../../../slices/downloads.slice';
import prepareExportedFilesLinks from '../../../utils/prepareExportedFilesLinks';
import PdfModalContent from './PdfModalContent';
import ImageOptions from '../../CropPanel/ImageOptions';
import { selectionsCleared } from '../../../slices/selections.slice';
import { labelsSelectionToggled } from '../../../slices/labels.slice';
import { collectionsDeselected } from '../../../slices/collections.slice';
import { jsx as _jsx } from "react/jsx-runtime";
export var useTransformedDownload = function useTransformedDownload() {
var dispatch = useDispatch();
var _useExplorer = useExplorer(),
info = _useExplorer.info,
i18n = _useExplorer.i18n,
_useExplorer$opts = _useExplorer.opts,
preventDownloadDefaultBehavior = _useExplorer$opts.preventDownloadDefaultBehavior,
forceDownload = _useExplorer$opts.forceDownload,
isDownloadUsageRightsEnabled = _useExplorer$opts.isDownloadUsageRightsEnabled;
var handleClearSelection = function handleClearSelection() {
dispatch(selectionsCleared());
dispatch(labelsSelectionToggled({
uuids: []
}));
dispatch(collectionsDeselected());
};
var triggerTransformedDownload = useCallback(function (filesToBeDownloaded, options) {
var _ref = options || {},
transformations = _ref.transformations,
_ref$isDownloadAsZip = _ref.isDownloadAsZip,
isDownloadAsZip = _ref$isDownloadAsZip === void 0 ? isDownloadUsageRightsEnabled : _ref$isDownloadAsZip;
var transformedFiles = filesToBeDownloaded.map(function (file) {
return getFileWithNewParams(file, transformations);
});
handleClearSelection();
if (!preventDownloadDefaultBehavior) {
info(i18n('mutualizedDownloadStartedInfo'));
if (transformedFiles.length > 1 && isDownloadAsZip || isDownloadUsageRightsEnabled) {
var fileUrls = transformedFiles.map(function (file) {
return file.url.download;
});
dispatch(downloadItems({
urls: fileUrls
}));
dispatch(emitExportEvent(prepareExportedFilesLinks(transformedFiles, 'download', forceDownload)));
} else {
// For solving issue contained in this card [T6929]
dispatch(downloadFilesOneByOne({
files: transformedFiles,
fromExport: true
}));
}
return;
}
dispatch(emitExportEvent(prepareExportedFilesLinks(transformedFiles, 'download', forceDownload)));
}, [i18n, info, preventDownloadDefaultBehavior, forceDownload]);
return triggerTransformedDownload;
};
export var useTransformedDownloadModal = function useTransformedDownloadModal() {
var dispatch = useDispatch();
var toggleModal = useModal();
var i18n = useExplorerI18n();
var triggerTransformedDownload = useTransformedDownload();
var _useExplorer2 = useExplorer(),
_useExplorer2$opts = _useExplorer2.opts,
hideExportCropPanel = _useExplorer2$opts.hideExportCropPanel,
noImgOperationsAndDownload = _useExplorer2$opts.noImgOperationsAndDownload,
hideDownloadButtonIcon = _useExplorer2$opts.hideDownloadButtonIcon,
downloadConsent = _useExplorer2$opts.downloadConsent,
isDownloadUsageRightsEnabled = _useExplorer2$opts.isDownloadUsageRightsEnabled;
var triggerTransformationsModal = useCallback(function (downloadableFiles) {
var isVariant = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var showCropPanel = function showCropPanel() {
toggleModal();
dispatch(cropPanelFileSet(downloadableFiles[0]));
};
var isExportCropPanelDisabled = hideExportCropPanel || noImgOperationsAndDownload || downloadableFiles.length > 1;
var isSelectionPdf = isPdf(downloadableFiles[0]);
var isSecondaryButtonDisabled = isExportCropPanelDisabled || isSelectionPdf;
var getModalContent = function getModalContent(_ref2) {
var setPrimaryButtonDisabled = _ref2.setPrimaryButtonDisabled,
data = _ref2.data,
updateData = _ref2.updateData;
var _ref3 = data || {},
_ref3$transformations = _ref3.transformations,
transformations = _ref3$transformations === void 0 ? {} : _ref3$transformations,
_ref3$isDownloadAsZip = _ref3.isDownloadAsZip,
isDownloadAsZip = _ref3$isDownloadAsZip === void 0 ? isDownloadUsageRightsEnabled : _ref3$isDownloadAsZip;
var setIsDownloadAsZip = function setIsDownloadAsZip(newVal) {
return updateData(function (latest) {
return _objectSpread(_objectSpread({}, latest), {}, {
isDownloadAsZip: newVal
});
});
};
var updateTransformations = function updateTransformations(newTransformation) {
return updateData(function (latest) {
return _objectSpread(_objectSpread({}, latest), {}, {
transformations: _objectSpread(_objectSpread({}, latest.transformations), newTransformation)
});
});
};
var handleDownloadConsentChange = function handleDownloadConsentChange(e) {
return setPrimaryButtonDisabled(!e.target.checked);
};
if (isSelectionPdf) {
return /*#__PURE__*/_jsx(PdfModalContent, {
selections: downloadableFiles || [],
isVariant: isVariant,
setPrimaryButtonDisabled: setPrimaryButtonDisabled,
onDownloadConsentChange: handleDownloadConsentChange,
setIsDownloadAsZip: setIsDownloadAsZip,
isDownloadAsZip: isDownloadAsZip,
transformations: transformations,
updateTransformations: updateTransformations
});
}
return /*#__PURE__*/_jsx(ImageOptions, {
isVariant: isVariant,
selections: downloadableFiles,
onDownloadConsentChange: handleDownloadConsentChange,
setIsDownloadAsZip: setIsDownloadAsZip,
isDownloadAsZip: isDownloadAsZip,
transformations: transformations,
updateTransformations: updateTransformations
});
};
toggleModal({
title: i18n('imageOptionsTransformationLabel'),
icon: /*#__PURE__*/_jsx(Settings, {
size: 29
}),
content: getModalContent,
buttonPrimaryLabel: i18n('mutualizedExportButtonLabel'),
buttonPrimaryIcon: hideDownloadButtonIcon ? null : function (props) {
return /*#__PURE__*/_jsx(Download, _objectSpread({}, props));
},
buttonSecondaryIcon: function buttonSecondaryIcon(props) {
return /*#__PURE__*/_jsx(Crop, _objectSpread({}, props));
},
buttonSecondaryLabel: !isSelectionPdf && !isVariant && i18n('explorerModelCropOptionsButton'),
buttonSecondaryBg: 'secondary',
onButtonSecondaryClick: isSecondaryButtonDisabled ? undefined : showCropPanel,
hideSecondaryButton: isSecondaryButtonDisabled,
onButtonPrimaryClick: function onButtonPrimaryClick(_ref4) {
var data = _ref4.data;
return triggerTransformedDownload(downloadableFiles, data);
},
disablePrimaryButton: Boolean(downloadConsent),
enterKeySubmits: true,
fullWidthButtons: true,
// pdfModalStyle: {
// width: ' 550px',
// height: '446px',
// maxHeight: '100%',
// maxWidth: '100%'
// },
modalStyle: {
width: '350px'
}
});
}, [i18n, triggerTransformedDownload, downloadConsent, hideExportCropPanel, noImgOperationsAndDownload, hideDownloadButtonIcon]);
return triggerTransformationsModal;
};