Thumbnails.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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<string> => {
  12. let dataUrl = '';
  13. if (pdf) {
  14. const page = await pdf.getPage(pageNum);
  15. const canvas = document.createElement('canvas');
  16. const context: CanvasRenderingContext2D = canvas.getContext(
  17. '2d',
  18. ) as CanvasRenderingContext2D;
  19. canvas.height = viewport.height;
  20. canvas.width = viewport.width;
  21. const renderContext = {
  22. canvasContext: context,
  23. viewport,
  24. };
  25. if (page) {
  26. const renderTask = page.render(renderContext);
  27. await renderTask.promise;
  28. }
  29. dataUrl = canvas.toDataURL();
  30. canvas.remove();
  31. }
  32. return dataUrl;
  33. };
  34. return (
  35. pdf && (
  36. <ThumbnailsComponent
  37. getPdfImage={getPdfImage}
  38. totalPage={totalPage}
  39. currentPage={currentPage}
  40. isActive={navbarState === 'thumbnails'}
  41. close={(): void => {
  42. setNavbar('');
  43. }}
  44. />
  45. )
  46. );
  47. };
  48. export default Thumbnails;