1- import React , { useEffect , useRef , useState } from "react" ;
1+ import React , { useEffect , useState } from "react" ;
22import Directions from "./Directions" ;
33import Footer from "./Footer" ;
44import Title from "./Title" ;
55import Preview from "./Preview" ;
66import Controls from "./Controls" ;
7- import { Box , Button , Typography } from "@mui/material" ;
7+ import { Box , Typography } from "@mui/material" ;
88import Impose from "../lib/imposify.mjs" ;
9- import { Document , Page , pdfjs } from "react-pdf" ;
9+ import { pdfjs } from "react-pdf" ;
1010import { useAppContext } from '../context/AppContext' ;
1111
12+ // Turns the spinner on/off via CSS. doing it this way instead of
13+ // via react state seems to result in a quicker loading initial
14+ // image than allowing a rerender would.
15+ const setSpinner = ( val ) => {
16+ const docBox = document . getElementById ( "document-box" ) ;
17+ const spinBox = document . getElementById ( "spinner-box" )
18+ if ( val ) {
19+ docBox . classList = "hidden" ;
20+ spinBox . classList = "" ;
21+ }
22+ else {
23+ docBox . classList = "doc-box" ;
24+ spinBox . classList = "hidden" ;
25+ }
26+ } ;
27+
1228const Main = ( ) => {
1329
1430 // Boolean to determine if we are dragging a file
1531 const [ isDragging , setIsDragging ] = useState ( false ) ;
16- const [ mode , setMode ] = useState ( 0 ) ;
32+ const [ mode ] = useState ( 0 ) ;
1733
1834 const { sharedState, setSharedState } = useAppContext ( ) ;
1935 // our pdf manipulation class itself
@@ -22,75 +38,60 @@ const Main = () => {
2238 // Set the path to the PDF.js worker from a CDN
2339 pdfjs . GlobalWorkerOptions . workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${ pdfjs . version } /pdf.worker.min.js` ;
2440
25-
26- // Turns the spinner on/off via CSS. doing it this way instead of
27- // via react state seems to result in a quicker loading initial
28- // image than allowing a rerender would.
29- const setSpinner = ( val ) => {
30- const docBox = document . getElementById ( "document-box" ) ;
31- const spinBox = document . getElementById ( "spinner-box" )
32- if ( val ) {
33- docBox . classList = "hidden" ;
34- spinBox . classList = "" ;
35- }
36- else {
37- docBox . classList = "doc-box" ;
38- spinBox . classList = "hidden" ;
39- }
40- } ;
41-
42- // pdf file passed to imposify via drag and drop or by open
43- // menu. currently this function loads a pdf, imposes it
44- // via a simple two-page spread method, converts it back to
45- // a pdf blob, and prepares it for rendering.
46- const processFile = async ( file ) => {
47- handleResize ( ) ;
48- setSharedState ( { ...sharedState , pageNumberFolded : 1 } ) ;
49- let completedPDF = false ;
50- try {
51- setSpinner ( true ) ;
52- console . log ( "loading" ) ;
53- // load pdf here
54- await impose . loadPDF ( await file . arrayBuffer ( ) ) ;
55- console . log ( "imposing" ) ;
56- // createBooklet() does a lot all at once.
57- const completedPdf = await impose . createBooklet ( { rtl : sharedState . rtl } ) ;
58- console . log ( "converting to binary blob" ) ;
59- // generate blob from pdf
60- if ( completedPdf ) {
61- const pageIndex = sharedState . rtl ? completedPdf . getPages ( ) . length : 1 ;
62- const blob = new Blob ( [ completedPdf ] , { type : "application/pdf" } ) ;
63- console . log ( "setting state for preview rendering" )
64- setSharedState ( { ...sharedState , pageNumberFolded : pageIndex , foldedPDF : blob , loaded : true } ) ;
65- console . log ( sharedState ) ;
66- }
67- else {
68- throw new Error ( `completedPDF was ${ completedPDF } ` ) ;
69- }
70- } catch ( error ) {
71- console . error ( "Error processing file:" , error ) ;
72- }
73- // We're loaded but we should delay unshowing the Spinner just a while
74- setTimeout ( ( ) => setSpinner ( false ) , 1250 ) ;
75- } ;
76-
77- const handleResize = ( ) => {
78- setSharedState ( currentState => {
79- const newPreviewWidth = window . innerWidth > 599 ? window . innerWidth * 0.4 : window . innerWidth * 0.8 ;
80- return { ...currentState , previewWidth : newPreviewWidth } ;
81- } ) ;
82- } ;
83-
8441 useEffect ( ( ) => {
8542 setSharedState ( currentState => {
8643 const newPreviewWidth = window . innerWidth > 599 ? window . innerWidth * 0.4 : window . innerWidth * 0.8 ;
8744 return { ...currentState , previewWidth : newPreviewWidth } ;
8845 } ) ;
89- } , [ ] ) ;
46+ } , [ setSharedState ] ) ;
9047
9148 useEffect ( ( ) => {
49+
50+ const handleResize = ( ) => {
51+ setSharedState ( currentState => {
52+ const newPreviewWidth = window . innerWidth > 599 ? window . innerWidth * 0.4 : window . innerWidth * 0.8 ;
53+ return { ...currentState , previewWidth : newPreviewWidth } ;
54+ } ) ;
55+ } ;
56+
57+ // pdf file passed to imposify via drag and drop or by open
58+ // menu. currently this function loads a pdf, imposes it
59+ // via a simple two-page spread method, converts it back to
60+ // a pdf blob, and prepares it for rendering.
61+ const processFile = async ( file ) => {
62+ handleResize ( ) ;
63+ let completedPDF = false ;
64+ try {
65+ setSpinner ( true ) ;
66+ console . log ( "loading" ) ;
67+ // load pdf here
68+ await impose . loadPDF ( await file . arrayBuffer ( ) ) ;
69+ console . log ( "imposing" ) ;
70+ // createBooklet() does a lot all at once.
71+ const completedPdf = await impose . createBooklet ( { rtl : sharedState . rtl } ) ;
72+ console . log ( "converting to binary blob" ) ;
73+ // generate blob from pdf
74+ if ( completedPdf ) {
75+ const pageIndex = sharedState . rtl ? completedPdf . getPages ( ) . length : 1 ;
76+ const blob = new Blob ( [ completedPdf ] , { type : "application/pdf" } ) ;
77+ console . log ( "setting state for preview rendering" )
78+ setTimeout ( ( ) => {
79+ setSharedState ( { ...sharedState , pageNumberFolded : pageIndex , foldedPDF : blob , loaded : true } ) ;
80+ } , 1000 ) ;
81+ console . log ( sharedState ) ;
82+ }
83+ else {
84+ throw new Error ( `completedPDF was ${ completedPDF } ` ) ;
85+ }
86+ } catch ( error ) {
87+ console . error ( "Error processing file:" , error ) ;
88+ }
89+ // We're loaded but we should delay unshowing the Spinner just a while
90+ setTimeout ( ( ) => setSpinner ( false ) , 1250 ) ;
91+ } ;
9292 processFile ( sharedState . origPDF ) ;
93- } , [ sharedState . origPDF ] ) ;
93+ // eslint-disable-next-line react-hooks/exhaustive-deps
94+ } , [ impose , sharedState . rtl , setSharedState , sharedState . origPDF ] ) ;
9495
9596 useEffect ( ( ) => {
9697 // hurl the imposify instance at the sharedState
@@ -123,11 +124,17 @@ const Main = () => {
123124 } ;
124125
125126 useEffect ( ( ) => {
127+ const handleResize = ( ) => {
128+ setSharedState ( currentState => {
129+ const newPreviewWidth = window . innerWidth > 599 ? window . innerWidth * 0.4 : window . innerWidth * 0.8 ;
130+ return { ...currentState , previewWidth : newPreviewWidth } ;
131+ } ) ;
132+ } ;
126133 window . addEventListener ( 'resize' , handleResize ) ;
127134 return ( ) => {
128135 window . removeEventListener ( 'resize' , handleResize ) ;
129136 } ;
130- } , [ ] ) ;
137+ } , [ setSharedState ] ) ;
131138
132139 return (
133140 < Box id = "main"
0 commit comments