Thumbnails.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import React from 'react';
  2. import ThumbnailsComponent from '../components/ThumbnailViewer';
  3. import useActions from '../actions';
  4. import useStore from '../store';
  5. const Thumbnails: React.FC = () => {
  6. const [
  7. { pdf, totalPage, currentPage, navbarState, viewport },
  8. dispatch,
  9. ] = useStore();
  10. const { setNavbar } = useActions(dispatch);
  11. const getPdfImage = async (pageNum: number): Promise<any> => {
  12. let dataUrl = '';
  13. const page = await pdf.getPage(pageNum);
  14. const canvas = document.createElement('canvas');
  15. const context: CanvasRenderingContext2D = canvas.getContext(
  16. '2d'
  17. ) as CanvasRenderingContext2D;
  18. canvas.height = viewport.height;
  19. canvas.width = viewport.width;
  20. const renderContext = {
  21. canvasContext: context,
  22. viewport,
  23. };
  24. const renderTask = page.render(renderContext);
  25. await renderTask.promise;
  26. dataUrl = canvas.toDataURL();
  27. canvas.remove();
  28. return dataUrl;
  29. };
  30. return (
  31. pdf && (
  32. <ThumbnailsComponent
  33. getPdfImage={getPdfImage}
  34. totalPage={totalPage}
  35. currentPage={currentPage}
  36. isActive={navbarState === 'thumbnails'}
  37. close={(): void => {
  38. setNavbar('');
  39. }}
  40. />
  41. )
  42. );
  43. };
  44. export default Thumbnails;