All files / src/components/common/FilePreview/hooks useZoom.ts

64.61% Statements 42/65
37.14% Branches 13/35
63.63% Functions 7/11
67.24% Lines 39/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                            3x 3x 3x 3x   3x 3x   3x 4x 4x 4x           4x     4x 3x 3x 3x                 3x 1x 2x     4x 4x 4x 4x   4x                                                 4x 4x 4x   4x                     4x         4x                         3x 16x 12x     3x 4x     3x 3x 3x 3x 3x        
import { useCallback, useMemo, useState } from "react";
import { ZoomHandler } from "../types";
import { throttle } from "lodash";
import { normalizeBetweenTwoRanges } from "@utils/index";
 
type TZoomParams = {
  step?: number;
  minZoom?: number;
  maxZoom?: number;
  defaultValue?: number;
  maxZoomEqualsDeviceSize?: boolean;
  isPdf?: boolean;
};
 
const DEFAULT_ZOOM_VALUE = 1;
const STEP = 0.1;
const MIN_ZOOM = 0.2;
const MAX_ZOOM = 2;
 
const DEVICE_WIDTH = window.innerWidth;
const DEVICE_HEIGHT = window.innerHeight;
 
const useZoom = (params?: TZoomParams) => {
  const [isLoading, setIsLoading] = useState<boolean>(true);
  const [zoom, setZoom] = useState<number>(DEFAULT_ZOOM_VALUE);
  const [originalSize, setOriginalSize] = useState({
    width: params?.isPdf ? 595 : 0,
    height: 0,
  }); //value is fixed for pdf
 
  const isImageBiggerThanScreen =
    !params?.isPdf &&
    (originalSize.width > DEVICE_WIDTH || originalSize.height > DEVICE_HEIGHT);
 
  const maxZoomUsed = useMemo(() => {
    Iif (!params?.maxZoomEqualsDeviceSize) return params?.maxZoom;
    const { width, height } = originalSize;
    Iif (isImageBiggerThanScreen) {
      if (width - DEVICE_WIDTH > height - DEVICE_HEIGHT) {
        const max = DEVICE_WIDTH / width;
        return Math.ceil(max * 10) / 10;
      } else {
        const max = DEVICE_HEIGHT / height;
        return Math.ceil(max * 10) / 10;
      }
    }
    if (DEVICE_HEIGHT - height > DEVICE_WIDTH - width || params.isPdf) {
      return calculateMaxZoom(width, DEVICE_WIDTH);
    } else return calculateMaxZoom(height, DEVICE_HEIGHT);
  }, [originalSize, params?.maxZoomEqualsDeviceSize, isImageBiggerThanScreen]);
 
  const step = sanitizedValue(STEP, params?.step);
  const minZoom = sanitizedValue(MIN_ZOOM, params?.minZoom);
  const maxZoom = sanitizedValue(MAX_ZOOM, maxZoomUsed);
  const defaultValue = sanitizedValue(DEFAULT_ZOOM_VALUE, params?.defaultValue);
 
  const onZoom: ZoomHandler = useCallback(
    throttle((zoomFactor) => {
      if (typeof zoomFactor === "number") {
        const pinchMinZoom = 0.2;
        const pinchMaxZoom = 5.0;
        const normalizedScale = normalizeBetweenTwoRanges(
          zoomFactor > 2 ? 2 : zoomFactor,
          [0.0, 2.0],
          [pinchMinZoom, pinchMaxZoom],
        );
 
        const roundedValue = roundValue(normalizedScale);
        if (roundedValue > pinchMaxZoom || roundedValue < pinchMinZoom) return;
        setZoom(roundedValue);
      }
      const direction = zoomFactor;
      setZoom((prev) => {
        const newZoom = roundValue(prev + (direction === "in" ? step : -step));
        if (newZoom < MIN_ZOOM) return prev;
        return newZoom;
      });
    }, 200),
    [minZoom, maxZoom],
  );
 
  const resetZoom = () => setZoom(defaultValue);
  const resetOriginalSize = () => setOriginalSize({ width: 0, height: 0 });
  const resetLoading = () => setIsLoading(true);
 
  const onLoadImageSetSize = (
    e: any,
    customSize?: { width: number; height: number },
  ) => {
    setOriginalSize({
      width: e?.target?.width || customSize?.width || 595, //fix number is used for PDF because we always render same size
      height: e?.target?.height || customSize?.height || e?.clientHeight,
    });
    setIsLoading(false);
  };
 
  const disabled = {
    in: roundValue(zoom + step) > maxZoom,
    out: zoom - step < minZoom,
  };
 
  return {
    zoom,
    resetZoom,
    onZoom,
    disabled,
    onLoadImageSetSize,
    resetOriginalSize,
    isImageBiggerThanScreen,
    isLoading,
    resetLoading,
  };
};
 
const sanitizedValue = (defaultValue: number, value?: number) => {
  if (typeof value !== "number") return defaultValue;
  return value;
};
 
const roundValue = (value: number) => {
  return Math.round((value + Number.EPSILON) * 10) / 10;
};
 
const calculateMaxZoom = (value: number, maxValue: number) => {
  const originalSizePercantage = (value * 100) / maxValue; //to understand if the file is closer to the maxScreenWidth or maxScreenHeight
  const maxZoomPercentage = 100 / originalSizePercantage; //get the original file-size's percentage as decimal compared to device size
  const max = Math.ceil(DEFAULT_ZOOM_VALUE * maxZoomPercentage * 10) / 10; //multiply the deafult and round it to the nearest one decimal place to receive the max
  return max;
};
 
export default useZoom;