index.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import Icon from '../Icon';
  4. import Pagination from '../Pagination';
  5. import SelectBox from '../SelectBox';
  6. import Divider from '../Divider';
  7. import { scaleCheck } from '../../helpers/utility';
  8. import { Container, ToggleButton } from './styled';
  9. import dataset 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.FC<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 { t } = useTranslation('toolbar');
  37. const data = dataset(t);
  38. const handleClockwiseRotation = (): void => {
  39. const r = rotation + 90;
  40. changeRotate(r);
  41. };
  42. const handleCounterclockwiseRotation = (): void => {
  43. const r = rotation - 90;
  44. changeRotate(r);
  45. };
  46. const handleScaleSelect = async (
  47. selected: SelectOptionType
  48. ): Promise<any> => {
  49. if (selected.child === 'fit') {
  50. const screenWidth = window.document.body.offsetWidth - 288;
  51. const originPdfWidth = viewport.width / scale;
  52. const rate = screenWidth / originPdfWidth;
  53. changeScale(rate);
  54. } else {
  55. changeScale(scaleCheck(selected.child as number));
  56. }
  57. };
  58. const handleZoomIn = (): void => {
  59. changeScale(scaleCheck(scale * 100 + 10));
  60. };
  61. const handleZoomOut = (): void => {
  62. changeScale(scaleCheck(scale * 100 - 10));
  63. };
  64. return (
  65. <>
  66. <Container isHidden={displayMode === 'full'}>
  67. <Pagination
  68. totalPage={totalPage}
  69. currentPage={currentPage}
  70. onChange={setCurrentPage}
  71. />
  72. <Divider />
  73. <Icon
  74. glyph="hand"
  75. style={{ width: '30px' }}
  76. onClick={handleHandClick}
  77. />
  78. <Icon
  79. glyph="zoom-in"
  80. style={{ width: '30px' }}
  81. onClick={handleZoomIn}
  82. />
  83. <Icon
  84. glyph="zoom-out"
  85. style={{ width: '30px' }}
  86. onClick={handleZoomOut}
  87. />
  88. <SelectBox
  89. options={data.scaleOptions}
  90. style={{ width: '94px' }}
  91. useInput
  92. isDivide
  93. onChange={handleScaleSelect}
  94. defaultValue={`${Math.round(scale * 100)} %`}
  95. />
  96. <Divider />
  97. <Icon
  98. glyph="rotate-left"
  99. style={{ width: '30px' }}
  100. onClick={handleCounterclockwiseRotation}
  101. />
  102. <Icon
  103. glyph="rotate-right"
  104. style={{ width: '30px' }}
  105. onClick={handleClockwiseRotation}
  106. />
  107. </Container>
  108. <ToggleButton>
  109. {displayMode === 'normal' ? (
  110. <Icon
  111. glyph="tool-close"
  112. onClick={(): void => {
  113. toggleDisplayMode('full');
  114. }}
  115. />
  116. ) : (
  117. <Icon
  118. glyph="tool-open"
  119. onClick={(): void => {
  120. toggleDisplayMode('normal');
  121. }}
  122. />
  123. )}
  124. </ToggleButton>
  125. </>
  126. );
  127. };
  128. export default Toolbar;