PdfPage.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. },
  25. ] = useStore();
  26. const getAnnotationWithPage = (
  27. arr: AnnotationType[],
  28. pageNum: number,
  29. ): React.ReactNode[] => {
  30. const result: React.ReactNode[] = [];
  31. arr.forEach((ele: AnnotationType, i: number) => {
  32. const page = ele.obj_attr ? ele.obj_attr.page + 1 : -1;
  33. if (page === pageNum) {
  34. result.push(
  35. <Annotation key={ele.id} scale={scale} index={i} {...ele} />,
  36. );
  37. }
  38. });
  39. return result;
  40. };
  41. useEffect(() => {
  42. if (currentPage + 2 >= index && currentPage - 2 <= index) {
  43. if (renderingState !== 'RENDERING') {
  44. setRenderingState('RENDERING');
  45. }
  46. } else if (renderingState === 'RENDERING') {
  47. setRenderingState('LOADING');
  48. }
  49. }, [renderingState, currentPage]);
  50. return (
  51. <Page
  52. pageNum={index}
  53. renderingState={renderingState}
  54. viewport={viewport}
  55. scale={scale}
  56. getPage={() => getPdfPage(pdf, index)}
  57. rotation={rotation}
  58. watermark={watermark}
  59. annotations={getAnnotationWithPage(annotations, index)}
  60. queryString={queryString}
  61. matchesMap={matchesMap}
  62. />
  63. );
  64. };
  65. export default PdfPage;