Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@filerobot/explorer / lib / components / CropPanel / ImageResize.js
Size: Mime:
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 { useState } from 'react';
import { InputGroup, TooltipV2 } from '@scaleflex/ui/core';
import { useTheme } from '@scaleflex/ui/theme/hooks';
import { InfoOutline, LockOutline, UnlockOutline } from '@scaleflex/icons';
import isPdf from '@filerobot/utils/lib/isPdf';
import isSvg from '@filerobot/utils/lib/isSvg';
import isPsd from '@filerobot/utils/lib/isPsd';
import isEps from '@filerobot/utils/lib/isEps';
import { PC } from '@filerobot/common';
import { useExplorer } from '../../hooks';
import { roundNum } from './CropPanel.utils';
import { RESIZE_OPTIONS } from './CropPanel.constants';
import Styled from './CropPanel.styled';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var ImageResize = function ImageResize(_ref) {
  var _getImageResizeInputV;
  var imageParams = _ref.imageParams,
    setImageParams = _ref.setImageParams,
    resizeOptions = _ref.resizeOptions,
    _ref$dpiOptions = _ref.dpiOptions,
    dpiOptions = _ref$dpiOptions === void 0 ? [] : _ref$dpiOptions,
    imageDimensions = _ref.imageDimensions,
    isRatioLocked = _ref.isRatioLocked,
    setIsRatioLocked = _ref.setIsRatioLocked,
    isMultipleImagesSelected = _ref.isMultipleImagesSelected,
    updateTransformations = _ref.updateTransformations,
    ratio = _ref.ratio,
    hideOptions = _ref.hideOptions,
    style = _ref.style,
    preventInputsChange = _ref.preventInputsChange,
    isPdfFile = _ref.isPdfFile,
    file = _ref.file;
  var theme = useTheme();
  var _useExplorer = useExplorer(),
    i18n = _useExplorer.i18n;
  // TODO(Amr): we want to have possibility to change default resize at mount
  // to do that we need to fix an issue between cropOptions and imageOptions
  // as cropOptions overwrite the imageParams as they share the same state
  // check the todo in CropOptions component
  var _useState = useState('original'),
    _useState2 = _slicedToArray(_useState, 2),
    imageSizeType = _useState2[0],
    setImageSizeType = _useState2[1];
  var iconPrimaryColor = theme.palette[PC.IconsPrimary];
  var iconsSecondaryColor = theme.palette[PC.IconsSecondary];
  var showDpiOptions = isPdf(file) || isSvg(file) || isPsd(file) || isEps(file);
  var isDpi = imageSizeType.includes('dpi');
  var isValidValue = function isValidValue(param) {
    return ((imageParams === null || imageParams === void 0 ? void 0 : imageParams[param]) || (imageParams === null || imageParams === void 0 ? void 0 : imageParams[param]) === 0) && !Number.isNaN(imageParams === null || imageParams === void 0 ? void 0 : imageParams[param]);
  };
  var setImageParam = function setImageParam(param, paramValue) {
    var nextParams = {};
    if (paramValue < 0) return;
    if (isRatioLocked) {
      var _objectSpread2;
      var secondParamValue = roundNum(param === 'width' ? paramValue * ratio : paramValue / ratio);
      nextParams = _objectSpread(_objectSpread({}, imageParams), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, param, paramValue), _defineProperty(_objectSpread2, param === 'width' ? 'height' : 'width', secondParamValue), _objectSpread2));
    } else {
      nextParams = _objectSpread(_objectSpread({}, imageParams), {}, _defineProperty({}, param, paramValue));
    }
    if (typeof updateTransformations === 'function') {
      updateTransformations(nextParams);
    }
    setImageParams(nextParams);
  };
  var handleImageSizeChange = function handleImageSizeChange(newImageSizeType) {
    var resizeOptions = RESIZE_OPTIONS;
    var newWidth = newImageSizeType === 'original' ? imageDimensions.width : resizeOptions[newImageSizeType];
    var newHeight = newImageSizeType === 'original' ? imageDimensions.height : resizeOptions[newImageSizeType] * ratio;
    var selectedDpiObject = dpiOptions.find(function (option) {
      return option.label === newImageSizeType;
    });
    var isDpi = newImageSizeType.includes('dpi');
    var newParams = _objectSpread(_objectSpread({}, imageParams), {}, {
      width: newWidth,
      height: Math.round(newHeight),
      org_if_sml: !isPsd(file) ? 1 : '',
      dpi: isDpi ? selectedDpiObject === null || selectedDpiObject === void 0 ? void 0 : selectedDpiObject.value : ''
    });
    setImageSizeType(newImageSizeType);
    if (typeof updateTransformations === 'function') {
      updateTransformations(newParams);
    }
    setImageParams(newParams);
  };
  var getImageResizeInputValue = function getImageResizeInputValue() {
    if (!isDpi) {
      var isValidHeight = isValidValue('height');
      if (isValidHeight) return Math.round(imageParams.height);
      return (imageDimensions === null || imageDimensions === void 0 ? void 0 : imageDimensions.height) || 0;
    }
  };
  var getWidthValue = function getWidthValue() {
    if (!isDpi) {
      var isValidWidth = isValidValue('width');
      if (isValidWidth) {
        return ((imageParams === null || imageParams === void 0 ? void 0 : imageParams.width) || 0).toString();
      }
      return ((imageDimensions === null || imageDimensions === void 0 ? void 0 : imageDimensions.width) || 0).toString();
    }
    return '';
  };
  var renderButton = function renderButton(type) {
    return /*#__PURE__*/_jsx(Styled.ImageOptionButtonButtons, {
      color: "link-secondary",
      size: "sm",
      type: type,
      imageSizeType: imageSizeType,
      onClick: function onClick() {
        return handleImageSizeChange(type);
      },
      children: type
    }, type);
  };
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsxs("div", {
      className: "filerobot-Explorer-ImageOptions-resize-options",
      style: style,
      children: [/*#__PURE__*/_jsx("div", {
        className: isMultipleImagesSelected ? 'filerobot-Explorer-ImageOptions-resize-options-input' : '',
        children: /*#__PURE__*/_jsx(InputGroup, {
          label: isMultipleImagesSelected ? i18n('imageSizeMaxWidthLabel') : i18n('mutualizedWidthLabel'),
          style: {
            width: '100%'
          },
          inputProps: {
            type: 'number',
            min: 0
          },
          iconEnd: "px",
          placeholder: (isMultipleImagesSelected || isPdfFile) && imageSizeType === 'original' ? i18n('imageSizeOriginalWidthPlaceholder') : '',
          value: getWidthValue(),
          onChange: preventInputsChange ? undefined : function (_ref2) {
            var target = _ref2.target;
            return setImageParam('width', Number(target.value));
          },
          disabled: preventInputsChange || isDpi
        })
      }), !isMultipleImagesSelected && /*#__PURE__*/_jsx(Styled.IconButton, {
        color: "basic",
        onClick: preventInputsChange ? undefined : function () {
          return setIsRatioLocked(!isRatioLocked);
        },
        isActive: !isRatioLocked,
        children: isRatioLocked ? /*#__PURE__*/_jsx(LockOutline, {
          color: iconPrimaryColor,
          size: 16
        }) : /*#__PURE__*/_jsx(UnlockOutline, {
          color: iconPrimaryColor,
          size: 16
        })
      }), /*#__PURE__*/_jsx(InputGroup, {
        label: isMultipleImagesSelected ? i18n('imageSizeMaxHeightLabel') : i18n('mutualizedHeightLabel'),
        style: {
          width: '100%'
        },
        inputProps: {
          type: 'number',
          min: 0
        },
        iconEnd: "px",
        placeholder: (isMultipleImagesSelected || isPdfFile) && imageSizeType === 'original' ? i18n('imageSizeOriginalHeightPlaceholder') : '',
        value: (_getImageResizeInputV = getImageResizeInputValue()) === null || _getImageResizeInputV === void 0 ? void 0 : _getImageResizeInputV.toString(),
        onChange: preventInputsChange ? undefined : function (_ref3) {
          var target = _ref3.target;
          return setImageParam('height', Number(target.value));
        },
        disabled: preventInputsChange || isDpi
      })]
    }), !hideOptions && /*#__PURE__*/_jsxs("div", {
      className: "filerobot-Explorer-ImageOptions-size-options",
      children: [/*#__PURE__*/_jsxs(Styled.ImageSizeOptions, {
        children: [renderButton('original'), resizeOptions.map(function (option) {
          return renderButton(option.label);
        }), /*#__PURE__*/_jsx(TooltipV2, {
          arrow: true,
          position: "top",
          size: "md",
          title: i18n('imageOptionImageSizeTooltip'),
          children: /*#__PURE__*/_jsx(InfoOutline, {
            color: iconsSecondaryColor,
            size: 14
          })
        })]
      }), showDpiOptions && /*#__PURE__*/_jsx("div", {
        children: dpiOptions.map(function (option) {
          return renderButton(option.label);
        })
      })]
    })]
  });
};
export default ImageResize;