okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1import { tr } from '../lib/i18n' ; 2import { useEffect , useRef , useState } from 'react' ; 3import type { PDFDocumentProxy , RenderTask } from 'pdfjs-dist' ; 4import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url' ; 5import { PreviewControls } from './PreviewControls' ; 6/** Imported only when a PDF is opened. One page renders at a time. */ 7export default function PdfPreview ({ url} :{ 8url :string ; 9}) { 10const [ doc , setDoc ] = useState < PDFDocumentProxy | null >( null ), [ page , setPage ] = useState ( 1 ), [ zoom , setZoom ] = useState ( 'fit' ), [ width , setWidth ] = useState ( 600 ), [ error , setError ] = useState ( '' ), [ loading , setLoading ] = useState ( true ); 11const canvas = useRef < HTMLCanvasElement >( null ), container = useRef < HTMLDivElement >( null ); 12useEffect (() => { 13let alive = true ; 14let task :ReturnType < typeof import ( 'pdfjs-dist' ). getDocument > | undefined ; 15setLoading ( true ); 16setError ( '' ); 17setDoc ( null ); 18setPage ( 1 ); 19void import ( 'pdfjs-dist' ). then ( => { 20if ( ! alive ) 21return ; 22. GlobalWorkerOptions . workerSrc = workerUrl ; 23task = . getDocument ({ url}); 24return task . promise ; 25}). then ( => { 26if ( alive && ) 27setDoc ( ); 28}). catch ( e => { 29if ( alive ) { 30setError ( e . message ); 31setLoading ( false ); 32} 33}); 34return () => { alive = false ; void task ?. destroy (); }; 35}, [ url ]); 36useEffect (() => { 37if ( ! container . current ) 38return ; 39const observer = new ResizeObserver ( entries => setWidth ( entries [ 0 ]. contentRect . width )); 40observer . observe ( container . current ); 41return () => observer . disconnect (); 42}, []); 43useEffect (() => { 44if ( ! doc || ! canvas . current ) 45return ; 46let alive = true ; 47let task :RenderTask | undefined ; 48setLoading ( true ); 49setError ( '' ); 50void doc . getPage ( page ). then ( async ( p ) => { 51if ( ! alive || ! canvas . current ) 52return ; 53const viewport = p . getViewport ({ scale :1 }); 54const scale = zoom === 'fit' ?Math . max ( .2 , ( width - 48 ) / viewport . width ) :Number ( zoom ); 55const view = p . getViewport ({ scale}); 56const ratio = Math . min ( devicePixelRatio || 1 , 2 ); 57const el = canvas . current ; 58el . width = Math . floor ( view . width * ratio ); 59el . height = Math . floor ( view . height * ratio ); 60el . style . width = ` ${ view . width } px` ; 61el . style . height = ` ${ view . height } px` ; 62task = p . render ({ canvas :el , viewport :view , transform :ratio === 1 ?undefined :[ ratio , 0 , 0 , ratio , 0 , 0 ] }); 63await task . promise ; 64if ( alive ) 65setLoading ( false ); 66}). catch ( e => { 67if ( alive && e . name !== 'RenderingCancelledException' ) { 68setError ( e . message ); 69setLoading ( false ); 70} 71}); 72return () => { alive = false ; task ?. cancel (); }; 73}, [ doc , page , zoom , width ]); 74return < div className = "pdf-view" >< div className = "pdf-controls" role = "group" aria-label = { tr ( "PDF controls" )}>< PreviewControls label = { tr ( "PDF" )} zoom = { zoom } onZoom = { setZoom } page = { page } pages = { doc ?. numPages } onPage = { setPage }/></ div >< div ref = { container } className = "pdf-canvas-wrap" aria-busy = { loading }>{ tr ( loading ?< p className = "small muted pdf-loading" role = "status" >{ tr ( "Rendering page\u2026" )}</ p > :null )}{ tr ( error ?< p className = "inline-error" role = "alert" >{ tr ( "Couldn\u2019t open this PDF. " )}{ tr ( error )}</ p > :< canvas ref = { canvas } role = "img" aria-label = { tr ( "PDF page {0}" , [ page ])}/>)}</ div ></ div >; 75}