123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- 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<Props> = ({
- pageNum,
- getPage,
- viewport,
- renderingState = 'LOADING',
- rotation,
- scale,
- annotations = [],
- watermark = {},
- queryString,
- matchesMap,
- toolState,
- }: Props) => {
- let pdfPage: any = null;
- let renderTask: any = null;
- const rootEle = useRef<HTMLDivElement | null>(null);
- const setRenderTask = (task: any) => {
- renderTask = task;
- };
- const renderPage = async (): Promise<void> => {
- 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 (
- <PageWrapper
- ref={rootEle}
- id={`page_${pageNum}`}
- data-page-num={pageNum}
- width={viewport.width}
- height={viewport.height}
- rotation={rotation}
- >
- {renderingState === 'LOADING' ? (
- <Inner>載入中...</Inner>
- ) : (
- <>
- <PdfCanvas />
- {watermark.text || watermark.imagepath ? (
- <WatermarkLayer>
- <Watermark viewScale={scale} {...watermark} />
- </WatermarkLayer>
- ) : (
- ''
- )}
- <TextLayer
- data-id="text-layer"
- style={{
- display:
- toolState === 'freehand' || toolState === 'shape'
- ? 'none'
- : 'block',
- }}
- />
- <AnnotationLayer data-id="annotation-layer">
- {annotations}
- </AnnotationLayer>
- <Canvas
- className="canvas"
- viewBox={`0 0 ${viewport.width} ${viewport.height}`}
- />
- </>
- )}
- </PageWrapper>
- );
- };
- export default PageView;
|