index.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { useEffect, useState } from 'react';
  2. import Divider from '../Divider';
  3. import Markup from '../Markup';
  4. import { scrollIntoView } from '../../helpers/utility';
  5. import {
  6. PageNumber, AnnotationBox, Content, Inner, Text,
  7. } from './styled';
  8. type Props = {
  9. type: string;
  10. page?: number;
  11. bdcolor: string;
  12. getText: () => Promise<any>;
  13. showPageNum?: boolean;
  14. transparency: number;
  15. }
  16. const AnnotationItem = ({
  17. type,
  18. page,
  19. showPageNum,
  20. bdcolor,
  21. getText,
  22. transparency,
  23. }: Props): React.ReactElement => {
  24. const [content, setContent] = useState([]);
  25. const handleClick = (): void => {
  26. const ele: HTMLElement | null = document.getElementById(`page_${page}`);
  27. if (ele) {
  28. scrollIntoView(ele);
  29. }
  30. };
  31. useEffect(() => {
  32. getText().then((text) => {
  33. let textArray = [];
  34. if (text.includes(' ')) {
  35. textArray = text.split(' ');
  36. } else {
  37. textArray = text.match(/.{1,12}/g);
  38. }
  39. setContent(textArray);
  40. });
  41. }, []);
  42. return (
  43. <>
  44. {showPageNum && <Divider orientation="horizontal" />}
  45. {showPageNum && <PageNumber>{`Page ${page}`}</PageNumber>}
  46. <AnnotationBox onClick={handleClick}>
  47. <Content>
  48. {
  49. content.map((textContent: string, index: number): any => {
  50. const key = `key_${index}`;
  51. return textContent ? (
  52. <Inner key={key}>
  53. <Text>{textContent}</Text>
  54. <Markup
  55. position={{
  56. top: 0, left: 0, width: '100%', height: '100%',
  57. }}
  58. markupType={type}
  59. bdcolor={bdcolor}
  60. opacity={transparency}
  61. />
  62. </Inner>
  63. ) : null;
  64. })
  65. }
  66. </Content>
  67. </AnnotationBox>
  68. </>
  69. );
  70. };
  71. export default AnnotationItem;