PdfPages.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React, { useEffect, useRef } from 'react';
  2. import _ from 'lodash';
  3. import Viewer from '../components/Viewer';
  4. import PdfPage from './PdfPage';
  5. import { watchScroll, scaleCheck } from '../helpers/utility';
  6. import useGestureScale from '../hooks/useGestureScale';
  7. import useActions from '../actions';
  8. import useStore from '../store';
  9. type Props = {
  10. scrollToUpdate: (state: ScrollStateType) => void;
  11. };
  12. let timer = 0;
  13. const PdfPages: React.FC<Props> = ({ scrollToUpdate }: Props) => {
  14. const containerRef = useRef<HTMLDivElement>(null);
  15. const [{ totalPage, scale, viewport, rotation }, dispatch] = useStore();
  16. const { changeScale } = useActions(dispatch);
  17. const [zoom] = useGestureScale(containerRef);
  18. useEffect(() => {
  19. watchScroll(containerRef.current, scrollToUpdate);
  20. }, []);
  21. useEffect(() => {
  22. if (zoom !== 0) {
  23. const viewer = containerRef.current as HTMLElement;
  24. viewer.style.transform = `scale(${1 + zoom})`;
  25. clearTimeout(timer);
  26. timer = setTimeout(() => {
  27. const targetScale = Math.round(scale * 100 + zoom * 100);
  28. changeScale(scaleCheck(targetScale));
  29. viewer.style.transform = `scale(1)`;
  30. }, 500);
  31. }
  32. }, [zoom]);
  33. return (
  34. <Viewer ref={containerRef} viewport={viewport} rotation={rotation}>
  35. {totalPage &&
  36. Array(totalPage)
  37. .fill(1)
  38. .map((x, i) => <PdfPage key={`page-${i + x}`} index={i + x} />)}
  39. </Viewer>
  40. );
  41. };
  42. export default PdfPages;