PdfPage.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import React, { useEffect, useState } from 'react';
  2. import Page from '../components/Page';
  3. import Annotation from './Annotation';
  4. import { getPdfPage } from '../helpers/pdf';
  5. import useStore from '../store';
  6. type Props = {
  7. index: number;
  8. };
  9. const PdfPage: React.FC<Props> = ({ index }: Props) => {
  10. const [renderingState, setRenderingState] = useState<RenderingStateType>(
  11. 'LOADING',
  12. );
  13. const [
  14. {
  15. viewport,
  16. pdf,
  17. rotation,
  18. annotations,
  19. scale,
  20. watermark,
  21. currentPage,
  22. queryString,
  23. matchesMap,
  24. toolState,
  25. },
  26. ] = useStore();
  27. const getAnnotationWithPage = (
  28. arr: AnnotationType[],
  29. pageNum: number,
  30. ): React.ReactNode[] => {
  31. const result: React.ReactNode[] = [];
  32. arr.forEach((ele: AnnotationType, i: number) => {
  33. const page = ele.obj_attr ? ele.obj_attr.page + 1 : -1;
  34. if (page === pageNum) {
  35. result.push(
  36. <Annotation key={ele.id} scale={scale} index={i} {...ele} />,
  37. );
  38. }
  39. });
  40. return result;
  41. };
  42. useEffect(() => {
  43. if (currentPage + 2 >= index && currentPage - 2 <= index) {
  44. if (renderingState !== 'RENDERING') {
  45. setRenderingState('RENDERING');
  46. }
  47. } else if (renderingState === 'RENDERING') {
  48. setRenderingState('LOADING');
  49. }
  50. }, [renderingState, currentPage]);
  51. return (
  52. <Page
  53. pageNum={index}
  54. renderingState={renderingState}
  55. viewport={viewport}
  56. scale={scale}
  57. getPage={() => getPdfPage(pdf, index)}
  58. rotation={rotation}
  59. watermark={watermark}
  60. annotations={getAnnotationWithPage(annotations, index)}
  61. queryString={queryString}
  62. matchesMap={matchesMap}
  63. toolState={toolState}
  64. />
  65. );
  66. };
  67. export default PdfPage;