index.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React from 'react';
  2. import Icon from '../Icon';
  3. import Pagination from '../Pagination';
  4. import SelectBox from '../SelectBox';
  5. import Divider from '../Divider';
  6. import { SelectOptionType, ViewportType } from '../../constants/type';
  7. import { scaleCheck } from '../../helpers/utility';
  8. import { Wrapper, ToggleButton } from './styled';
  9. import data from './data';
  10. type Props = {
  11. totalPage: number;
  12. currentPage: number;
  13. setCurrentPage: (num: number) => void;
  14. changeScale: (num: number | string) => void;
  15. changeRotate: (num: number) => void;
  16. scale: number;
  17. rotation: number;
  18. viewport: ViewportType;
  19. displayMode: string;
  20. toggleDisplayMode: (state: string) => void;
  21. handleHandClick: () => void;
  22. };
  23. const Toolbar: React.FunctionComponent<Props> = ({
  24. totalPage,
  25. currentPage,
  26. setCurrentPage,
  27. changeScale,
  28. changeRotate,
  29. scale,
  30. rotation,
  31. viewport,
  32. displayMode,
  33. toggleDisplayMode,
  34. handleHandClick,
  35. }: Props) => {
  36. const handleClockwiseRotation = (): void => {
  37. const r = rotation + 90;
  38. changeRotate(r);
  39. };
  40. const handleCounterclockwiseRotation = (): void => {
  41. const r = rotation - 90;
  42. changeRotate(r);
  43. };
  44. const handleScaleSelect = async (selected: SelectOptionType): Promise<any> => {
  45. if (selected.value === 'fit') {
  46. const screenWidth = window.document.body.offsetWidth - 276;
  47. const originPdfWidth = viewport.width / scale;
  48. const rate = screenWidth / originPdfWidth;
  49. changeScale(rate);
  50. } else {
  51. changeScale(scaleCheck(selected.value as number));
  52. }
  53. };
  54. const handleZoomIn = (): void => {
  55. changeScale(scaleCheck(scale * 100 + 10));
  56. };
  57. const handleZoomOut = (): void => {
  58. changeScale(scaleCheck(scale * 100 - 10));
  59. };
  60. return (
  61. <>
  62. <Wrapper isHidden={displayMode === 'full'}>
  63. <Pagination totalPage={totalPage} currentPage={currentPage} onChange={setCurrentPage} />
  64. <Divider />
  65. <Icon glyph="hand" style={{ width: '30px' }} onClick={handleHandClick} />
  66. <Icon glyph="zoom-in" style={{ width: '30px' }} onClick={handleZoomIn} />
  67. <Icon glyph="zoom-out" style={{ width: '30px' }} onClick={handleZoomOut} />
  68. <SelectBox options={data.scaleOptions} width="94px" useInput isDivide onChange={handleScaleSelect} defaultValue={`${Math.round(scale * 100)} %`} />
  69. <Divider />
  70. <Icon glyph="rotate-left" style={{ width: '30px' }} onClick={handleCounterclockwiseRotation} />
  71. <Icon glyph="rotate-right" style={{ width: '30px' }} onClick={handleClockwiseRotation} />
  72. </Wrapper>
  73. <ToggleButton>
  74. {
  75. displayMode === 'normal' ? (
  76. <Icon glyph="tool-close" onClick={(): void => { toggleDisplayMode('full'); }} />
  77. ) : (
  78. <Icon glyph="tool-open" onClick={(): void => { toggleDisplayMode('normal'); }} />
  79. )
  80. }
  81. </ToggleButton>
  82. </>
  83. );
  84. };
  85. export default Toolbar;