index.tsx 3.0 KB

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