PdfPages.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 [
  16. { totalPage, scale, viewport, rotation, displayMode },
  17. dispatch,
  18. ] = useStore();
  19. const { changeScale } = useActions(dispatch);
  20. const [zoom] = useGestureScale(containerRef);
  21. useEffect(() => {
  22. watchScroll(containerRef.current, scrollToUpdate);
  23. }, []);
  24. useEffect(() => {
  25. if (zoom !== 0) {
  26. const viewer = containerRef.current as HTMLElement;
  27. viewer.style.transform = `scale(${1 + zoom})`;
  28. clearTimeout(timer);
  29. timer = setTimeout(() => {
  30. const targetScale = Math.round(scale * 100 + zoom * 100);
  31. changeScale(scaleCheck(targetScale));
  32. viewer.style.transform = `scale(1)`;
  33. }, 500);
  34. }
  35. }, [zoom]);
  36. return (
  37. <Viewer
  38. ref={containerRef}
  39. viewport={viewport}
  40. rotation={rotation}
  41. displayMode={displayMode}
  42. >
  43. {totalPage &&
  44. Array(totalPage)
  45. .fill(1)
  46. .map((x, i) => <PdfPage key={`page-${i + x}`} index={i + x} />)}
  47. </Viewer>
  48. );
  49. };
  50. export default PdfPages;