|
@@ -1,4 +1,4 @@
|
|
|
-import React, { useEffect, useRef } from 'react';
|
|
|
+import React, { useEffect } from 'react';
|
|
|
import queryString from 'query-string';
|
|
|
import config from '../config';
|
|
|
import apiPath from '../constants/apiPath';
|
|
@@ -6,18 +6,17 @@ import apiPath from '../constants/apiPath';
|
|
|
import { initialPdfFile, fetchXfdf } from '../apis';
|
|
|
import PdfPages from './PdfPages';
|
|
|
import { fetchPdf } from '../helpers/pdf';
|
|
|
-import { scrollIntoView } from '../helpers/utility';
|
|
|
-import { parseAnnotationFromXml } from '../helpers/annotation';
|
|
|
+import {
|
|
|
+ parseAnnotationFromXml,
|
|
|
+ parseFormElementFromXml,
|
|
|
+} from '../helpers/annotation';
|
|
|
import { parseWatermarkFromXml } from '../helpers/watermark';
|
|
|
|
|
|
import useActions from '../actions';
|
|
|
import useStore from '../store';
|
|
|
|
|
|
const PdfViewer: React.FC = () => {
|
|
|
- const [
|
|
|
- { viewport, pdf, scale, currentPage, totalPage },
|
|
|
- dispatch,
|
|
|
- ] = useStore();
|
|
|
+ const [{ viewport, pdf, scale }, dispatch] = useStore();
|
|
|
const {
|
|
|
setTotalPage,
|
|
|
setPdf,
|
|
@@ -29,7 +28,6 @@ const PdfViewer: React.FC = () => {
|
|
|
changeScale,
|
|
|
updateWatermark,
|
|
|
} = useActions(dispatch);
|
|
|
- const currentPageRef = useRef(0);
|
|
|
|
|
|
const setLoadingProgress = (totalSize: number) => (
|
|
|
progress: ProgressType
|
|
@@ -53,13 +51,14 @@ const PdfViewer: React.FC = () => {
|
|
|
fetchXfdf(token)
|
|
|
.then(xfdf => {
|
|
|
const annotations = parseAnnotationFromXml(xfdf);
|
|
|
+ const forms = parseFormElementFromXml(xfdf);
|
|
|
const watermark = parseWatermarkFromXml(xfdf);
|
|
|
|
|
|
if (watermark.obj_attr.type) {
|
|
|
- addAnnots([...annotations, watermark], false);
|
|
|
+ addAnnots([...annotations, ...forms, watermark], false);
|
|
|
updateWatermark(watermark.obj_attr);
|
|
|
} else {
|
|
|
- addAnnots(annotations, false);
|
|
|
+ addAnnots([...annotations, ...forms], false);
|
|
|
}
|
|
|
})
|
|
|
.catch(error => {
|
|
@@ -93,30 +92,14 @@ const PdfViewer: React.FC = () => {
|
|
|
|
|
|
iViewport.width = Math.round(iViewport.width);
|
|
|
iViewport.height = Math.round(iViewport.height);
|
|
|
- const screenWidth = window.document.body.offsetWidth - 5;
|
|
|
+ const screenWidth = window.document.body.offsetWidth - 286;
|
|
|
const originPdfWidth = iViewport.width;
|
|
|
- const rate = (screenWidth / originPdfWidth).toFixed(2);
|
|
|
|
|
|
+ const rate = (screenWidth / originPdfWidth).toFixed(2);
|
|
|
changeScale(rate);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- const changePdfContainerScale = (): void => {
|
|
|
- for (let i = 1; i <= totalPage; i += 1) {
|
|
|
- const ele: HTMLDivElement = document.getElementById(
|
|
|
- `page_${i}`
|
|
|
- ) as HTMLDivElement;
|
|
|
- if (ele) {
|
|
|
- ele.style.width = `${viewport.width}px`;
|
|
|
- ele.style.height = `${viewport.height}px`;
|
|
|
-
|
|
|
- if (i === currentPageRef.current) {
|
|
|
- scrollIntoView(ele);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
const scrollToUpdate = (state: ScrollStateType): void => {
|
|
|
const ele: HTMLDivElement = document.getElementById(
|
|
|
'page_1'
|
|
@@ -125,25 +108,13 @@ const PdfViewer: React.FC = () => {
|
|
|
(state.lastY + ele.offsetHeight / 1.4) / (ele.offsetHeight + 50)
|
|
|
);
|
|
|
|
|
|
- if (pageNum !== currentPageRef.current) {
|
|
|
- setCurrentPage(pageNum);
|
|
|
- }
|
|
|
+ setCurrentPage(pageNum);
|
|
|
};
|
|
|
|
|
|
useEffect(() => {
|
|
|
pdfStarter();
|
|
|
}, []);
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- currentPageRef.current = currentPage;
|
|
|
- }, [currentPage]);
|
|
|
-
|
|
|
- useEffect(() => {
|
|
|
- if (pdf) {
|
|
|
- changePdfContainerScale();
|
|
|
- }
|
|
|
- }, [viewport]);
|
|
|
-
|
|
|
useEffect(() => {
|
|
|
if (pdf) {
|
|
|
getViewport(pdf, scale);
|