PdfPage.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import React from 'react';
  2. import { AnnotationType, RenderingStateType } from '../constants/type';
  3. import Page from '../components/Page';
  4. import Annotation from './Annotation';
  5. import { getPdfPage } from '../helpers/pdf';
  6. import useStore from '../store';
  7. type Props = {
  8. index: number;
  9. renderingState: RenderingStateType;
  10. };
  11. const PdfPage: React.FC<Props> = ({ index, renderingState }: Props) => {
  12. const [
  13. { viewport, pdf, rotation, annotations, scale, watermark },
  14. ] = useStore();
  15. const getAnnotationWithPage = (
  16. arr: AnnotationType[],
  17. pageNum: number
  18. ): React.ReactNode[] => {
  19. const result: React.ReactNode[] = [];
  20. arr.forEach((ele: AnnotationType, i: number) => {
  21. const page = ele.obj_attr ? ele.obj_attr.page + 1 : -1;
  22. if (page === pageNum) {
  23. result.push(
  24. <Annotation
  25. key={`annotations_${pageNum + i}`}
  26. scale={scale}
  27. index={i}
  28. {...ele}
  29. />
  30. );
  31. }
  32. });
  33. return result;
  34. };
  35. return (
  36. <Page
  37. pageNum={index}
  38. renderingState={renderingState}
  39. viewport={viewport}
  40. scale={scale}
  41. getPage={(): Promise<any> => getPdfPage(pdf, index)}
  42. rotation={rotation}
  43. watermark={watermark}
  44. annotations={getAnnotationWithPage(annotations, index)}
  45. />
  46. );
  47. };
  48. export default PdfPage;