Toolbar.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React, { useEffect, useState } from 'react';
  2. import ToolbarComponent from '../components/Toolbar';
  3. import useActions from '../actions';
  4. import useStore from '../store';
  5. import { scrollIntoView, scaleCheck } from '../helpers/utility';
  6. import { delay } from '../helpers/time';
  7. const Toolbar: React.FC = () => {
  8. const [
  9. { totalPage, currentPage, scale, rotation, viewport, displayMode },
  10. dispatch,
  11. ] = useStore();
  12. const [hideToolbar, setHideToolbar] = useState(false);
  13. let scrollTimeout = 0;
  14. const {
  15. setCurrentPage: setCurrentPageAction,
  16. changeScale,
  17. changeRotate,
  18. setSidebar,
  19. } = useActions(dispatch);
  20. const setCurrentPage = (num: number): void => {
  21. if (num > 0) {
  22. const ele: HTMLElement = document.getElementById(
  23. `page_${num}`,
  24. ) as HTMLElement;
  25. scrollIntoView(ele);
  26. }
  27. setCurrentPageAction(num);
  28. setHideToolbar(false);
  29. };
  30. const handleHandClick = (): void => {
  31. setSidebar('');
  32. };
  33. const timer = () => {
  34. if (scrollTimeout > 0) {
  35. clearTimeout(scrollTimeout);
  36. setHideToolbar(false);
  37. }
  38. scrollTimeout = setTimeout(() => {
  39. setHideToolbar(true);
  40. }, 3000);
  41. };
  42. const handleScroll = () => {
  43. setHideToolbar(false);
  44. };
  45. const handleClockwiseRotation = (): void => {
  46. const r = rotation + 90;
  47. changeRotate(r);
  48. setHideToolbar(false);
  49. };
  50. const handleCounterclockwiseRotation = (): void => {
  51. const r = rotation - 90;
  52. changeRotate(r);
  53. setHideToolbar(false);
  54. };
  55. const handleZoomIn = (): void => {
  56. changeScale(scaleCheck(scale * 100 + 10));
  57. setHideToolbar(false);
  58. };
  59. const handleZoomOut = (): void => {
  60. changeScale(scaleCheck(scale * 100 - 10));
  61. setHideToolbar(false);
  62. };
  63. const handleMouseOver = (): void => {
  64. setHideToolbar(false);
  65. };
  66. useEffect(() => {
  67. const viewer = document.getElementById('pdf_viewer');
  68. if (viewer) {
  69. viewer.addEventListener('scroll', handleScroll);
  70. }
  71. return () => {
  72. if (viewer) {
  73. viewer.removeEventListener('scroll', handleScroll);
  74. }
  75. };
  76. }, [viewport]);
  77. useEffect(() => {
  78. if (!hideToolbar) {
  79. timer();
  80. }
  81. }, [hideToolbar]);
  82. useEffect(() => {
  83. delay(3000).then(() => {
  84. setHideToolbar(true);
  85. });
  86. }, []);
  87. return (
  88. <ToolbarComponent
  89. totalPage={totalPage}
  90. currentPage={currentPage}
  91. setCurrentPage={setCurrentPage}
  92. changeScale={changeScale}
  93. handleClockwiseRotation={handleClockwiseRotation}
  94. handleCounterclockwiseRotation={handleCounterclockwiseRotation}
  95. handleZoomIn={handleZoomIn}
  96. handleZoomOut={handleZoomOut}
  97. handleHandClick={handleHandClick}
  98. handleMouseOver={handleMouseOver}
  99. scale={scale}
  100. viewport={viewport}
  101. displayMode={displayMode}
  102. hidden={hideToolbar}
  103. />
  104. );
  105. };
  106. export default Toolbar;