PdfPage.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import React from 'react';
  2. import Page from '../components/Page';
  3. import Annotation from './Annotation';
  4. import { getPdfPage } from '../helpers/pdf';
  5. import useActions from '../actions';
  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, currentPage },
  14. dispatch,
  15. ] = useStore();
  16. const { setTextDivs } = useActions(dispatch);
  17. const getAnnotationWithPage = (
  18. arr: AnnotationType[],
  19. pageNum: number
  20. ): React.ReactNode[] => {
  21. const result: React.ReactNode[] = [];
  22. arr.forEach((ele: AnnotationType, i: number) => {
  23. const page = ele.obj_attr ? ele.obj_attr.page + 1 : -1;
  24. if (page === pageNum) {
  25. result.push(
  26. <Annotation key={ele.id} scale={scale} index={i} {...ele} />
  27. );
  28. }
  29. });
  30. return result;
  31. };
  32. return (
  33. <Page
  34. pageNum={index}
  35. renderingState={renderingState}
  36. viewport={viewport}
  37. scale={scale}
  38. getPage={(): Promise<any> => getPdfPage(pdf, index)}
  39. rotation={rotation}
  40. watermark={watermark}
  41. annotations={getAnnotationWithPage(annotations, index)}
  42. setTextDivs={setTextDivs}
  43. currentPage={currentPage}
  44. />
  45. );
  46. };
  47. export default PdfPage;