import React, { useEffect, useRef } from 'react'; import Watermark from '../Watermark'; import { renderPdfPage } from '../../helpers/pdf'; import { renderMatches } from '../../helpers/search'; import { PageWrapper, PdfCanvas, AnnotationLayer, TextLayer, WatermarkLayer, Inner, Canvas, } from './styled'; type Props = { pageNum: number; renderingState: RenderingStateType; getPage: GetPageType; viewport: ViewportType; rotation: number; scale: number; annotations: React.ReactNode[]; watermark: WatermarkType; queryString: string; matchesMap: MatchType[]; toolState: ToolType | FormType | ''; }; const PageView: React.FC = ({ pageNum, getPage, viewport, renderingState = 'LOADING', rotation, scale, annotations = [], watermark = {}, queryString, matchesMap, toolState, }: Props) => { let pdfPage: any = null; let renderTask: any = null; const rootEle = useRef(null); const setRenderTask = (task: any) => { renderTask = task; }; const renderPage = async (): Promise => { if (getPage) { getPage().then((obj: PdfPageType) => { pdfPage = obj; const setTextDivs = (elements: HTMLElement[]) => { if (queryString && matchesMap.length) { matchesMap.forEach((item) => { if (pageNum === item.page) { const id = `${item.page}_${item.index}`; renderMatches(elements, getPage, item.index, queryString, id); } }); } }; if (rootEle.current) { renderPdfPage({ rootEle: rootEle.current, pdfPage: obj, viewport, setRenderTask, setTextDivs, }); } }); } }; useEffect(() => { if (renderTask) { renderTask.cancel(); renderTask = null; } if (pdfPage) { pdfPage.cleanup(); pdfPage = null; } if (renderingState === 'RENDERING') { renderPage(); } }, [renderingState, viewport, queryString, matchesMap]); useEffect(() => { if (queryString === '' && !matchesMap.length) { renderPage(); } }, [queryString, matchesMap]); return ( {renderingState === 'LOADING' ? ( 載入中... ) : ( <> {watermark.text || watermark.imagepath ? ( ) : ( '' )} {annotations} )} ); }; export default PageView;