您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {
  2. useGetChunkHighlights,
  3. useGetDocumentUrl,
  4. } from '@/hooks/documentHooks';
  5. import { IChunk } from '@/interfaces/database/knowledge';
  6. import { Skeleton } from 'antd';
  7. import { useEffect, useRef, useState } from 'react';
  8. import {
  9. AreaHighlight,
  10. Highlight,
  11. IHighlight,
  12. PdfHighlighter,
  13. PdfLoader,
  14. Popup,
  15. } from 'react-pdf-highlighter';
  16. import styles from './index.less';
  17. interface IProps {
  18. chunk: IChunk;
  19. documentId: string;
  20. visible: boolean;
  21. }
  22. const HighlightPopup = ({
  23. comment,
  24. }: {
  25. comment: { text: string; emoji: string };
  26. }) =>
  27. comment.text ? (
  28. <div className="Highlight__popup">
  29. {comment.emoji} {comment.text}
  30. </div>
  31. ) : null;
  32. const DocumentPreviewer = ({ chunk, documentId, visible }: IProps) => {
  33. const url = useGetDocumentUrl(documentId);
  34. const state = useGetChunkHighlights(chunk);
  35. const ref = useRef<(highlight: IHighlight) => void>(() => {});
  36. const [loaded, setLoaded] = useState(false);
  37. const resetHash = () => {};
  38. useEffect(() => {
  39. setLoaded(visible);
  40. }, [visible]);
  41. useEffect(() => {
  42. if (state.length > 0 && loaded) {
  43. setLoaded(false);
  44. ref.current(state[0]);
  45. }
  46. }, [state, loaded]);
  47. return (
  48. <div className={styles.documentContainer}>
  49. <PdfLoader
  50. url={url}
  51. beforeLoad={<Skeleton active />}
  52. workerSrc="/pdfjs-dist/pdf.worker.min.js"
  53. >
  54. {(pdfDocument) => (
  55. <PdfHighlighter
  56. pdfDocument={pdfDocument}
  57. enableAreaSelection={(event) => event.altKey}
  58. onScrollChange={resetHash}
  59. scrollRef={(scrollTo) => {
  60. ref.current = scrollTo;
  61. setLoaded(true);
  62. }}
  63. onSelectionFinished={() => null}
  64. highlightTransform={(
  65. highlight,
  66. index,
  67. setTip,
  68. hideTip,
  69. viewportToScaled,
  70. screenshot,
  71. isScrolledTo,
  72. ) => {
  73. const isTextHighlight = !Boolean(
  74. highlight.content && highlight.content.image,
  75. );
  76. const component = isTextHighlight ? (
  77. <Highlight
  78. isScrolledTo={isScrolledTo}
  79. position={highlight.position}
  80. comment={highlight.comment}
  81. />
  82. ) : (
  83. <AreaHighlight
  84. isScrolledTo={isScrolledTo}
  85. highlight={highlight}
  86. onChange={() => {}}
  87. />
  88. );
  89. return (
  90. <Popup
  91. popupContent={<HighlightPopup {...highlight} />}
  92. onMouseOver={(popupContent) =>
  93. setTip(highlight, () => popupContent)
  94. }
  95. onMouseOut={hideTip}
  96. key={index}
  97. >
  98. {component}
  99. </Popup>
  100. );
  101. }}
  102. highlights={state}
  103. />
  104. )}
  105. </PdfLoader>
  106. </div>
  107. );
  108. };
  109. export default DocumentPreviewer;