diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 6e006423a..9afb72a4d 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -5,7 +5,8 @@ "Bash(mkdir:*)", "Bash(./gradlew:*)", "Bash(grep:*)", - "Bash(cat:*)" + "Bash(cat:*)", + "Bash(find:*)" ], "deny": [] } diff --git a/CLAUDE.md b/CLAUDE.md index 05bfb5254..8bdd7c235 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -25,23 +25,54 @@ Set `DOCKER_ENABLE_SECURITY=true` environment variable to enable security featur - **Proxy Configuration**: Vite proxies `/api/*` calls to backend (localhost:8080) - **Build Process**: DO NOT run build scripts manually - builds are handled by CI/CD pipelines - **Package Installation**: DO NOT run npm install commands - package management handled separately +- **Deployment Options**: + - **Desktop App**: `npm run tauri-build` (native desktop application) + - **Web Server**: `npm run build` then serve dist/ folder + - **Development**: `npm run tauri-dev` for desktop dev mode -#### Tailwind CSS Setup (if not already installed) -```bash -cd frontend -npm install -D tailwindcss postcss autoprefixer -npx tailwindcss init -p -``` +#### Multi-Tool Workflow Architecture +Frontend designed for **stateful document processing**: +- Users upload PDFs once, then chain tools (split → merge → compress → view) +- File state and processing results persist across tool switches +- No file reloading between tools - performance critical for large PDFs (up to 100GB+) + +#### FileContext - Central State Management +**Location**: `src/contexts/FileContext.tsx` +- **Active files**: Currently loaded PDFs and their variants +- **Tool navigation**: Current mode (viewer/pageEditor/fileEditor/toolName) +- **Memory management**: PDF document cleanup, blob URL lifecycle, Web Worker management +- **IndexedDB persistence**: File storage with thumbnail caching +- **Preview system**: Tools can preview results (e.g., Split → Viewer → back to Split) without context pollution + +**Critical**: All file operations go through FileContext. Don't bypass with direct file handling. + +#### Processing Services +- **enhancedPDFProcessingService**: Background PDF parsing and manipulation +- **thumbnailGenerationService**: Web Worker-based with main-thread fallback +- **fileStorage**: IndexedDB with LRU cache management + +#### Memory Management Strategy +**Why manual cleanup exists**: Large PDFs (up to 100GB+) through multiple tools accumulate: +- PDF.js documents that need explicit .destroy() calls +- Blob URLs from tool outputs that need revocation +- Web Workers that need termination +Without cleanup: browser crashes with memory leaks. + +#### Tool Development +- **Pattern**: Follow `src/tools/Split.tsx` as reference implementation +- **File Access**: Tools receive `selectedFiles` prop (computed from activeFiles based on user selection) +- **File Selection**: Users select files in FileEditor (tool mode) → stored as IDs → computed to File objects for tools +- **Integration**: All files are part of FileContext ecosystem - automatic memory management and operation tracking +- **Parameters**: Tool parameter handling patterns still being standardized +- **Preview Integration**: Tools can implement preview functionality (see Split tool's thumbnail preview) ## Architecture Overview ### Project Structure - **Backend**: Spring Boot application with Thymeleaf templating -- **Frontend**: React-based SPA in `/frontend` directory (replacing legacy Thymeleaf templates) - - **Current Status**: Active development to replace Thymeleaf UI with modern React SPA +- **Frontend**: React-based SPA in `/frontend` directory (Thymeleaf templates fully replaced) - **File Storage**: IndexedDB for client-side file persistence and thumbnails - **Internationalization**: JSON-based translations (converted from backend .properties) - - **URL Parameters**: Deep linking support for tool states and configurations - **PDF Processing**: PDFBox for core PDF operations, LibreOffice for conversions, PDF.js for client-side rendering - **Security**: Spring Security with optional authentication (controlled by `DOCKER_ENABLE_SECURITY`) - **Configuration**: YAML-based configuration with environment variable overrides @@ -59,9 +90,8 @@ npx tailwindcss init -p - **Pipeline System**: Automated PDF processing workflows via `PipelineController` - **Security Layer**: Authentication, authorization, and user management (when enabled) -### Template System (Legacy + Modern) -- **Legacy Thymeleaf Templates**: Located in `src/main/resources/templates/` (being phased out) -- **Modern React Components**: Located in `frontend/src/components/` and `frontend/src/tools/` +### Component Architecture +- **React Components**: Located in `frontend/src/components/` and `frontend/src/tools/` - **Static Assets**: CSS, JS, and resources in `src/main/resources/static/` (legacy) + `frontend/public/` (modern) - **Internationalization**: - Backend: `messages_*.properties` files @@ -91,13 +121,14 @@ npx tailwindcss init -p - Frontend: Update JSON files in `frontend/public/locales/` or use conversion script 5. **Documentation**: API docs auto-generated and available at `/swagger-ui/index.html` -## Frontend Migration Notes +## Frontend Architecture Status -- **Current Branch**: `feature/react-overhaul` - Active React SPA development -- **Migration Status**: Core tools (Split, Merge, Compress) converted to React with URL parameter support -- **File Management**: Implemented IndexedDB storage with thumbnail generation using PDF.js -- **Tools Architecture**: Each tool receives `params` and `updateParams` for URL state synchronization -- **Remaining Work**: Convert remaining Thymeleaf templates to React components +- **Core Status**: React SPA architecture complete with multi-tool workflow support +- **State Management**: FileContext handles all file operations and tool navigation +- **File Processing**: Production-ready with memory management for large PDF workflows (up to 100GB+) +- **Tool Integration**: Standardized tool interface - see `src/tools/Split.tsx` as reference +- **Preview System**: Tool results can be previewed without polluting file context (Split tool example) +- **Performance**: Web Worker thumbnails, IndexedDB persistence, background processing ## Important Notes @@ -108,6 +139,11 @@ npx tailwindcss init -p - **Backend**: Designed to be stateless - files are processed in memory/temp locations only - **Frontend**: Uses IndexedDB for client-side file storage and caching (with thumbnails) - **Security**: When `DOCKER_ENABLE_SECURITY=false`, security-related classes are excluded from compilation +- **FileContext**: All file operations MUST go through FileContext - never bypass with direct File handling +- **Memory Management**: Manual cleanup required for PDF.js documents and blob URLs - don't remove cleanup code +- **Tool Development**: New tools should follow Split tool pattern (`src/tools/Split.tsx`) +- **Performance Target**: Must handle PDFs up to 100GB+ without browser crashes +- **Preview System**: Tools can preview results without polluting main file context (see Split tool implementation) ## Communication Style - Be direct and to the point diff --git a/common/src/main/java/stirling/software/common/service/TaskManager.java b/common/src/main/java/stirling/software/common/service/TaskManager.java index 219ae4ac4..902b2bfd1 100644 --- a/common/src/main/java/stirling/software/common/service/TaskManager.java +++ b/common/src/main/java/stirling/software/common/service/TaskManager.java @@ -1,6 +1,5 @@ package stirling.software.common.service; -import io.github.pixee.security.ZipSecurity; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -21,6 +20,8 @@ import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import io.github.pixee.security.ZipSecurity; + import jakarta.annotation.PreDestroy; import lombok.extern.slf4j.Slf4j; @@ -361,7 +362,8 @@ public class TaskManager { MultipartFile zipFile = fileStorage.retrieveFile(zipFileId); try (ZipInputStream zipIn = - ZipSecurity.createHardenedInputStream(new ByteArrayInputStream(zipFile.getBytes()))) { + ZipSecurity.createHardenedInputStream( + new ByteArrayInputStream(zipFile.getBytes()))) { ZipEntry entry; while ((entry = zipIn.getNextEntry()) != null) { if (!entry.isDirectory()) { diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 0e33392bc..6c19c7632 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -25,6 +25,7 @@ "i18next": "^25.2.1", "i18next-browser-languagedetector": "^8.1.0", "i18next-http-backend": "^3.0.2", + "jszip": "^3.10.1", "pdf-lib": "^1.17.1", "pdfjs-dist": "^3.11.174", "react": "^19.1.0", @@ -2689,6 +2690,11 @@ "node": ">=18" } }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, "node_modules/cosmiconfig": { "version": "7.1.0", "resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-7.1.0.tgz", @@ -3450,6 +3456,11 @@ "cross-fetch": "4.0.0" } }, + "node_modules/immediate": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz", + "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==" + }, "node_modules/import-fresh": { "version": "3.3.1", "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz", @@ -3491,8 +3502,7 @@ "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "license": "ISC", - "optional": true + "license": "ISC" }, "node_modules/is-arrayish": { "version": "0.2.1", @@ -3571,6 +3581,11 @@ "node": ">=0.12.0" } }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, "node_modules/jiti": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/jiti/-/jiti-2.4.2.tgz", @@ -3630,6 +3645,52 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/jszip": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/jszip/-/jszip-3.10.1.tgz", + "integrity": "sha512-xXDvecyTpGLrqFrvkrUSoxxfJI5AH7U8zxxtVclpsUtMCq4JQ290LY8AW5c7Ggnr/Y/oK+bQMbqK2qmtk3pN4g==", + "dependencies": { + "lie": "~3.3.0", + "pako": "~1.0.2", + "readable-stream": "~2.3.6", + "setimmediate": "^1.0.5" + } + }, + "node_modules/jszip/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/jszip/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "node_modules/jszip/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, + "node_modules/lie": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/lie/-/lie-3.3.0.tgz", + "integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==", + "dependencies": { + "immediate": "~3.0.5" + } + }, "node_modules/lightningcss": { "version": "1.30.1", "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.30.1.tgz", @@ -4729,6 +4790,11 @@ "node": ">= 0.8" } }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, "node_modules/prop-types": { "version": "15.8.1", "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz", @@ -5237,6 +5303,11 @@ "integrity": "sha512-IOc8uWeOZgnb3ptbCURJWNjWUPcO3ZnTTdzsurqERrP6nPyv+paC55vJM0LpOlT2ne+Ix+9+CRG1MNLlyZ4GjQ==", "license": "MIT" }, + "node_modules/setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==" + }, "node_modules/signal-exit": { "version": "3.0.7", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", @@ -5697,7 +5768,6 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", - "devOptional": true, "license": "MIT" }, "node_modules/vite": { diff --git a/frontend/package.json b/frontend/package.json index fa7a0b5d2..38dfbf56e 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -21,6 +21,7 @@ "i18next": "^25.2.1", "i18next-browser-languagedetector": "^8.1.0", "i18next-http-backend": "^3.0.2", + "jszip": "^3.10.1", "pdf-lib": "^1.17.1", "pdfjs-dist": "^3.11.174", "react": "^19.1.0", diff --git a/frontend/public/pdf.js b/frontend/public/pdf.js new file mode 100644 index 000000000..c31b6ab62 --- /dev/null +++ b/frontend/public/pdf.js @@ -0,0 +1,22 @@ +/** + * @licstart The following is the entire license notice for the + * JavaScript code in this page + * + * Copyright 2023 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @licend The above is the entire license notice for the + * JavaScript code in this page + */ +!function webpackUniversalModuleDefinition(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=t.pdfjsLib=e():"function"==typeof define&&define.amd?define("pdfjs-dist/build/pdf",[],(()=>t.pdfjsLib=e())):"object"==typeof exports?exports["pdfjs-dist/build/pdf"]=t.pdfjsLib=e():t["pdfjs-dist/build/pdf"]=t.pdfjsLib=e()}(globalThis,(()=>(()=>{"use strict";var __webpack_modules__=[,(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});e.VerbosityLevel=e.Util=e.UnknownErrorException=e.UnexpectedResponseException=e.TextRenderingMode=e.RenderingIntentFlag=e.PromiseCapability=e.PermissionFlag=e.PasswordResponses=e.PasswordException=e.PageActionEventType=e.OPS=e.MissingPDFException=e.MAX_IMAGE_SIZE_TO_CACHE=e.LINE_FACTOR=e.LINE_DESCENT_FACTOR=e.InvalidPDFException=e.ImageKind=e.IDENTITY_MATRIX=e.FormatError=e.FeatureTest=e.FONT_IDENTITY_MATRIX=e.DocumentActionEventType=e.CMapCompressionType=e.BaseException=e.BASELINE_FACTOR=e.AnnotationType=e.AnnotationReplyType=e.AnnotationPrefix=e.AnnotationMode=e.AnnotationFlag=e.AnnotationFieldFlag=e.AnnotationEditorType=e.AnnotationEditorPrefix=e.AnnotationEditorParamsType=e.AnnotationBorderStyleType=e.AnnotationActionEventType=e.AbortException=void 0;e.assert=function assert(t,e){t||unreachable(e)};e.bytesToString=bytesToString;e.createValidAbsoluteUrl=function createValidAbsoluteUrl(t,e=null,i=null){if(!t)return null;try{if(i&&"string"==typeof t){if(i.addDefaultProtocol&&t.startsWith("www.")){const e=t.match(/\./g);e?.length>=2&&(t=`http://${t}`)}if(i.tryConvertEncoding)try{t=stringToUTF8String(t)}catch{}}const s=e?new URL(t,e):new URL(t);if(function _isValidProtocol(t){switch(t?.protocol){case"http:":case"https:":case"ftp:":case"mailto:":case"tel:":return!0;default:return!1}}(s))return s}catch{}return null};e.getModificationDate=function getModificationDate(t=new Date){return[t.getUTCFullYear().toString(),(t.getUTCMonth()+1).toString().padStart(2,"0"),t.getUTCDate().toString().padStart(2,"0"),t.getUTCHours().toString().padStart(2,"0"),t.getUTCMinutes().toString().padStart(2,"0"),t.getUTCSeconds().toString().padStart(2,"0")].join("")};e.getUuid=function getUuid(){if("undefined"!=typeof crypto&&"function"==typeof crypto?.randomUUID)return crypto.randomUUID();const t=new Uint8Array(32);if("undefined"!=typeof crypto&&"function"==typeof crypto?.getRandomValues)crypto.getRandomValues(t);else for(let e=0;e<32;e++)t[e]=Math.floor(255*Math.random());return bytesToString(t)};e.getVerbosityLevel=function getVerbosityLevel(){return n};e.info=function info(t){n>=s.INFOS&&console.log(`Info: ${t}`)};e.isArrayBuffer=function isArrayBuffer(t){return"object"==typeof t&&void 0!==t?.byteLength};e.isArrayEqual=function isArrayEqual(t,e){if(t.length!==e.length)return!1;for(let i=0,s=t.length;ie?e.normalize("NFKC"):h.get(i)))};e.objectFromMap=function objectFromMap(t){const e=Object.create(null);for(const[i,s]of t)e[i]=s;return e};e.objectSize=function objectSize(t){return Object.keys(t).length};e.setVerbosityLevel=function setVerbosityLevel(t){Number.isInteger(t)&&(n=t)};e.shadow=shadow;e.string32=function string32(t){return String.fromCharCode(t>>24&255,t>>16&255,t>>8&255,255&t)};e.stringToBytes=stringToBytes;e.stringToPDFString=function stringToPDFString(t){if(t[0]>="ï"){let e;"þ"===t[0]&&"ÿ"===t[1]?e="utf-16be":"ÿ"===t[0]&&"þ"===t[1]?e="utf-16le":"ï"===t[0]&&"»"===t[1]&&"¿"===t[2]&&(e="utf-8");if(e)try{const i=new TextDecoder(e,{fatal:!0}),s=stringToBytes(t);return i.decode(s)}catch(t){warn(`stringToPDFString: "${t}".`)}}const e=[];for(let i=0,s=t.length;i=s.WARNINGS&&console.log(`Warning: ${t}`)}function unreachable(t){throw new Error(t)}function shadow(t,e,i,s=!1){Object.defineProperty(t,e,{value:i,enumerable:!s,configurable:!0,writable:!1});return i}const a=function BaseExceptionClosure(){function BaseException(t,e){this.constructor===BaseException&&unreachable("Cannot initialize BaseException.");this.message=t;this.name=e}BaseException.prototype=new Error;BaseException.constructor=BaseException;return BaseException}();e.BaseException=a;e.PasswordException=class PasswordException extends a{constructor(t,e){super(t,"PasswordException");this.code=e}};e.UnknownErrorException=class UnknownErrorException extends a{constructor(t,e){super(t,"UnknownErrorException");this.details=e}};e.InvalidPDFException=class InvalidPDFException extends a{constructor(t){super(t,"InvalidPDFException")}};e.MissingPDFException=class MissingPDFException extends a{constructor(t){super(t,"MissingPDFException")}};e.UnexpectedResponseException=class UnexpectedResponseException extends a{constructor(t,e){super(t,"UnexpectedResponseException");this.status=e}};e.FormatError=class FormatError extends a{constructor(t){super(t,"FormatError")}};e.AbortException=class AbortException extends a{constructor(t){super(t,"AbortException")}};function bytesToString(t){"object"==typeof t&&void 0!==t?.length||unreachable("Invalid argument for bytesToString");const e=t.length,i=8192;if(et.toString(16).padStart(2,"0")));e.Util=class Util{static makeHexColor(t,e,i){return`#${r[t]}${r[e]}${r[i]}`}static scaleMinMax(t,e){let i;if(t[0]){if(t[0]<0){i=e[0];e[0]=e[1];e[1]=i}e[0]*=t[0];e[1]*=t[0];if(t[3]<0){i=e[2];e[2]=e[3];e[3]=i}e[2]*=t[3];e[3]*=t[3]}else{i=e[0];e[0]=e[2];e[2]=i;i=e[1];e[1]=e[3];e[3]=i;if(t[1]<0){i=e[2];e[2]=e[3];e[3]=i}e[2]*=t[1];e[3]*=t[1];if(t[2]<0){i=e[0];e[0]=e[1];e[1]=i}e[0]*=t[2];e[1]*=t[2]}e[0]+=t[4];e[1]+=t[4];e[2]+=t[5];e[3]+=t[5]}static transform(t,e){return[t[0]*e[0]+t[2]*e[1],t[1]*e[0]+t[3]*e[1],t[0]*e[2]+t[2]*e[3],t[1]*e[2]+t[3]*e[3],t[0]*e[4]+t[2]*e[5]+t[4],t[1]*e[4]+t[3]*e[5]+t[5]]}static applyTransform(t,e){return[t[0]*e[0]+t[1]*e[2]+e[4],t[0]*e[1]+t[1]*e[3]+e[5]]}static applyInverseTransform(t,e){const i=e[0]*e[3]-e[1]*e[2];return[(t[0]*e[3]-t[1]*e[2]+e[2]*e[5]-e[4]*e[3])/i,(-t[0]*e[1]+t[1]*e[0]+e[4]*e[1]-e[5]*e[0])/i]}static getAxialAlignedBoundingBox(t,e){const i=this.applyTransform(t,e),s=this.applyTransform(t.slice(2,4),e),n=this.applyTransform([t[0],t[3]],e),a=this.applyTransform([t[2],t[1]],e);return[Math.min(i[0],s[0],n[0],a[0]),Math.min(i[1],s[1],n[1],a[1]),Math.max(i[0],s[0],n[0],a[0]),Math.max(i[1],s[1],n[1],a[1])]}static inverseTransform(t){const e=t[0]*t[3]-t[1]*t[2];return[t[3]/e,-t[1]/e,-t[2]/e,t[0]/e,(t[2]*t[5]-t[4]*t[3])/e,(t[4]*t[1]-t[5]*t[0])/e]}static singularValueDecompose2dScale(t){const e=[t[0],t[2],t[1],t[3]],i=t[0]*e[0]+t[1]*e[2],s=t[0]*e[1]+t[1]*e[3],n=t[2]*e[0]+t[3]*e[2],a=t[2]*e[1]+t[3]*e[3],r=(i+a)/2,o=Math.sqrt((i+a)**2-4*(i*a-n*s))/2,l=r+o||1,h=r-o||1;return[Math.sqrt(l),Math.sqrt(h)]}static normalizeRect(t){const e=t.slice(0);if(t[0]>t[2]){e[0]=t[2];e[2]=t[0]}if(t[1]>t[3]){e[1]=t[3];e[3]=t[1]}return e}static intersect(t,e){const i=Math.max(Math.min(t[0],t[2]),Math.min(e[0],e[2])),s=Math.min(Math.max(t[0],t[2]),Math.max(e[0],e[2]));if(i>s)return null;const n=Math.max(Math.min(t[1],t[3]),Math.min(e[1],e[3])),a=Math.min(Math.max(t[1],t[3]),Math.max(e[1],e[3]));return n>a?null:[i,n,s,a]}static bezierBoundingBox(t,e,i,s,n,a,r,o){const l=[],h=[[],[]];let c,d,u,p,g,m,f,b;for(let h=0;h<2;++h){if(0===h){d=6*t-12*i+6*n;c=-3*t+9*i-9*n+3*r;u=3*i-3*t}else{d=6*e-12*s+6*a;c=-3*e+9*s-9*a+3*o;u=3*s-3*e}if(Math.abs(c)<1e-12){if(Math.abs(d)<1e-12)continue;p=-u/d;0{this.resolve=e=>{this.#t=!0;t(e)};this.reject=t=>{this.#t=!0;e(t)}}))}get settled(){return this.#t}};let l=null,h=null;e.AnnotationPrefix="pdfjs_internal_id_"},(__unused_webpack_module,exports,__w_pdfjs_require__)=>{Object.defineProperty(exports,"__esModule",{value:!0});exports.RenderTask=exports.PDFWorkerUtil=exports.PDFWorker=exports.PDFPageProxy=exports.PDFDocumentProxy=exports.PDFDocumentLoadingTask=exports.PDFDataRangeTransport=exports.LoopbackPort=exports.DefaultStandardFontDataFactory=exports.DefaultFilterFactory=exports.DefaultCanvasFactory=exports.DefaultCMapReaderFactory=void 0;Object.defineProperty(exports,"SVGGraphics",{enumerable:!0,get:function(){return _displaySvg.SVGGraphics}});exports.build=void 0;exports.getDocument=getDocument;exports.version=void 0;var _util=__w_pdfjs_require__(1),_annotation_storage=__w_pdfjs_require__(3),_display_utils=__w_pdfjs_require__(6),_font_loader=__w_pdfjs_require__(9),_displayNode_utils=__w_pdfjs_require__(10),_canvas=__w_pdfjs_require__(11),_worker_options=__w_pdfjs_require__(14),_message_handler=__w_pdfjs_require__(15),_metadata=__w_pdfjs_require__(16),_optional_content_config=__w_pdfjs_require__(17),_transport_stream=__w_pdfjs_require__(18),_displayFetch_stream=__w_pdfjs_require__(19),_displayNetwork=__w_pdfjs_require__(22),_displayNode_stream=__w_pdfjs_require__(23),_displaySvg=__w_pdfjs_require__(24),_xfa_text=__w_pdfjs_require__(25);const DEFAULT_RANGE_CHUNK_SIZE=65536,RENDERING_CANCELLED_TIMEOUT=100,DELAYED_CLEANUP_TIMEOUT=5e3,DefaultCanvasFactory=_util.isNodeJS?_displayNode_utils.NodeCanvasFactory:_display_utils.DOMCanvasFactory;exports.DefaultCanvasFactory=DefaultCanvasFactory;const DefaultCMapReaderFactory=_util.isNodeJS?_displayNode_utils.NodeCMapReaderFactory:_display_utils.DOMCMapReaderFactory;exports.DefaultCMapReaderFactory=DefaultCMapReaderFactory;const DefaultFilterFactory=_util.isNodeJS?_displayNode_utils.NodeFilterFactory:_display_utils.DOMFilterFactory;exports.DefaultFilterFactory=DefaultFilterFactory;const DefaultStandardFontDataFactory=_util.isNodeJS?_displayNode_utils.NodeStandardFontDataFactory:_display_utils.DOMStandardFontDataFactory;exports.DefaultStandardFontDataFactory=DefaultStandardFontDataFactory;function getDocument(t){"string"==typeof t||t instanceof URL?t={url:t}:(0,_util.isArrayBuffer)(t)&&(t={data:t});if("object"!=typeof t)throw new Error("Invalid parameter in getDocument, need parameter object.");if(!t.url&&!t.data&&!t.range)throw new Error("Invalid parameter object: need either .data, .range or .url");const e=new PDFDocumentLoadingTask,{docId:i}=e,s=t.url?getUrlProp(t.url):null,n=t.data?getDataProp(t.data):null,a=t.httpHeaders||null,r=!0===t.withCredentials,o=t.password??null,l=t.range instanceof PDFDataRangeTransport?t.range:null,h=Number.isInteger(t.rangeChunkSize)&&t.rangeChunkSize>0?t.rangeChunkSize:DEFAULT_RANGE_CHUNK_SIZE;let c=t.worker instanceof PDFWorker?t.worker:null;const d=t.verbosity,u="string"!=typeof t.docBaseUrl||(0,_display_utils.isDataScheme)(t.docBaseUrl)?null:t.docBaseUrl,p="string"==typeof t.cMapUrl?t.cMapUrl:null,g=!1!==t.cMapPacked,m=t.CMapReaderFactory||DefaultCMapReaderFactory,f="string"==typeof t.standardFontDataUrl?t.standardFontDataUrl:null,b=t.StandardFontDataFactory||DefaultStandardFontDataFactory,A=!0!==t.stopAtErrors,_=Number.isInteger(t.maxImageSize)&&t.maxImageSize>-1?t.maxImageSize:-1,v=!1!==t.isEvalSupported,y="boolean"==typeof t.isOffscreenCanvasSupported?t.isOffscreenCanvasSupported:!_util.isNodeJS,S=Number.isInteger(t.canvasMaxAreaInBytes)?t.canvasMaxAreaInBytes:-1,E="boolean"==typeof t.disableFontFace?t.disableFontFace:_util.isNodeJS,x=!0===t.fontExtraProperties,w=!0===t.enableXfa,C=t.ownerDocument||globalThis.document,T=!0===t.disableRange,P=!0===t.disableStream,M=!0===t.disableAutoFetch,k=!0===t.pdfBug,F=l?l.length:t.length??NaN,R="boolean"==typeof t.useSystemFonts?t.useSystemFonts:!_util.isNodeJS&&!E,D="boolean"==typeof t.useWorkerFetch?t.useWorkerFetch:m===_display_utils.DOMCMapReaderFactory&&b===_display_utils.DOMStandardFontDataFactory&&p&&f&&(0,_display_utils.isValidFetchUrl)(p,document.baseURI)&&(0,_display_utils.isValidFetchUrl)(f,document.baseURI),I=t.canvasFactory||new DefaultCanvasFactory({ownerDocument:C}),L=t.filterFactory||new DefaultFilterFactory({docId:i,ownerDocument:C});(0,_util.setVerbosityLevel)(d);const O={canvasFactory:I,filterFactory:L};if(!D){O.cMapReaderFactory=new m({baseUrl:p,isCompressed:g});O.standardFontDataFactory=new b({baseUrl:f})}if(!c){const t={verbosity:d,port:_worker_options.GlobalWorkerOptions.workerPort};c=t.port?PDFWorker.fromPort(t):new PDFWorker(t);e._worker=c}const N={docId:i,apiVersion:"3.11.174",data:n,password:o,disableAutoFetch:M,rangeChunkSize:h,length:F,docBaseUrl:u,enableXfa:w,evaluatorOptions:{maxImageSize:_,disableFontFace:E,ignoreErrors:A,isEvalSupported:v,isOffscreenCanvasSupported:y,canvasMaxAreaInBytes:S,fontExtraProperties:x,useSystemFonts:R,cMapUrl:D?p:null,standardFontDataUrl:D?f:null}},B={ignoreErrors:A,isEvalSupported:v,disableFontFace:E,fontExtraProperties:x,enableXfa:w,ownerDocument:C,disableAutoFetch:M,pdfBug:k,styleElement:null};c.promise.then((function(){if(e.destroyed)throw new Error("Loading aborted");const t=_fetchDocument(c,N),o=new Promise((function(t){let e;if(l)e=new _transport_stream.PDFDataTransportStream({length:F,initialData:l.initialData,progressiveDone:l.progressiveDone,contentDispositionFilename:l.contentDispositionFilename,disableRange:T,disableStream:P},l);else if(!n){e=(t=>_util.isNodeJS?new _displayNode_stream.PDFNodeStream(t):(0,_display_utils.isValidFetchUrl)(t.url)?new _displayFetch_stream.PDFFetchStream(t):new _displayNetwork.PDFNetworkStream(t))({url:s,length:F,httpHeaders:a,withCredentials:r,rangeChunkSize:h,disableRange:T,disableStream:P})}t(e)}));return Promise.all([t,o]).then((function([t,s]){if(e.destroyed)throw new Error("Loading aborted");const n=new _message_handler.MessageHandler(i,t,c.port),a=new WorkerTransport(n,e,s,B,O);e._transport=a;n.send("Ready",null)}))})).catch(e._capability.reject);return e}async function _fetchDocument(t,e){if(t.destroyed)throw new Error("Worker was destroyed");const i=await t.messageHandler.sendWithPromise("GetDocRequest",e,e.data?[e.data.buffer]:null);if(t.destroyed)throw new Error("Worker was destroyed");return i}function getUrlProp(t){if(t instanceof URL)return t.href;try{return new URL(t,window.location).href}catch{if(_util.isNodeJS&&"string"==typeof t)return t}throw new Error("Invalid PDF url data: either string or URL-object is expected in the url property.")}function getDataProp(t){if(_util.isNodeJS&&"undefined"!=typeof Buffer&&t instanceof Buffer)throw new Error("Please provide binary data as `Uint8Array`, rather than `Buffer`.");if(t instanceof Uint8Array&&t.byteLength===t.buffer.byteLength)return t;if("string"==typeof t)return(0,_util.stringToBytes)(t);if("object"==typeof t&&!isNaN(t?.length)||(0,_util.isArrayBuffer)(t))return new Uint8Array(t);throw new Error("Invalid PDF binary data: either TypedArray, string, or array-like object is expected in the data property.")}class PDFDocumentLoadingTask{static#e=0;constructor(){this._capability=new _util.PromiseCapability;this._transport=null;this._worker=null;this.docId="d"+PDFDocumentLoadingTask.#e++;this.destroyed=!1;this.onPassword=null;this.onProgress=null}get promise(){return this._capability.promise}async destroy(){this.destroyed=!0;try{this._worker?.port&&(this._worker._pendingDestroy=!0);await(this._transport?.destroy())}catch(t){this._worker?.port&&delete this._worker._pendingDestroy;throw t}this._transport=null;if(this._worker){this._worker.destroy();this._worker=null}}}exports.PDFDocumentLoadingTask=PDFDocumentLoadingTask;class PDFDataRangeTransport{constructor(t,e,i=!1,s=null){this.length=t;this.initialData=e;this.progressiveDone=i;this.contentDispositionFilename=s;this._rangeListeners=[];this._progressListeners=[];this._progressiveReadListeners=[];this._progressiveDoneListeners=[];this._readyCapability=new _util.PromiseCapability}addRangeListener(t){this._rangeListeners.push(t)}addProgressListener(t){this._progressListeners.push(t)}addProgressiveReadListener(t){this._progressiveReadListeners.push(t)}addProgressiveDoneListener(t){this._progressiveDoneListeners.push(t)}onDataRange(t,e){for(const i of this._rangeListeners)i(t,e)}onDataProgress(t,e){this._readyCapability.promise.then((()=>{for(const i of this._progressListeners)i(t,e)}))}onDataProgressiveRead(t){this._readyCapability.promise.then((()=>{for(const e of this._progressiveReadListeners)e(t)}))}onDataProgressiveDone(){this._readyCapability.promise.then((()=>{for(const t of this._progressiveDoneListeners)t()}))}transportReady(){this._readyCapability.resolve()}requestDataRange(t,e){(0,_util.unreachable)("Abstract method PDFDataRangeTransport.requestDataRange")}abort(){}}exports.PDFDataRangeTransport=PDFDataRangeTransport;class PDFDocumentProxy{constructor(t,e){this._pdfInfo=t;this._transport=e;Object.defineProperty(this,"getJavaScript",{value:()=>{(0,_display_utils.deprecated)("`PDFDocumentProxy.getJavaScript`, please use `PDFDocumentProxy.getJSActions` instead.");return this.getJSActions().then((t=>{if(!t)return t;const e=[];for(const i in t)e.push(...t[i]);return e}))}})}get annotationStorage(){return this._transport.annotationStorage}get filterFactory(){return this._transport.filterFactory}get numPages(){return this._pdfInfo.numPages}get fingerprints(){return this._pdfInfo.fingerprints}get isPureXfa(){return(0,_util.shadow)(this,"isPureXfa",!!this._transport._htmlForXfa)}get allXfaHtml(){return this._transport._htmlForXfa}getPage(t){return this._transport.getPage(t)}getPageIndex(t){return this._transport.getPageIndex(t)}getDestinations(){return this._transport.getDestinations()}getDestination(t){return this._transport.getDestination(t)}getPageLabels(){return this._transport.getPageLabels()}getPageLayout(){return this._transport.getPageLayout()}getPageMode(){return this._transport.getPageMode()}getViewerPreferences(){return this._transport.getViewerPreferences()}getOpenAction(){return this._transport.getOpenAction()}getAttachments(){return this._transport.getAttachments()}getJSActions(){return this._transport.getDocJSActions()}getOutline(){return this._transport.getOutline()}getOptionalContentConfig(){return this._transport.getOptionalContentConfig()}getPermissions(){return this._transport.getPermissions()}getMetadata(){return this._transport.getMetadata()}getMarkInfo(){return this._transport.getMarkInfo()}getData(){return this._transport.getData()}saveDocument(){return this._transport.saveDocument()}getDownloadInfo(){return this._transport.downloadInfoCapability.promise}cleanup(t=!1){return this._transport.startCleanup(t||this.isPureXfa)}destroy(){return this.loadingTask.destroy()}get loadingParams(){return this._transport.loadingParams}get loadingTask(){return this._transport.loadingTask}getFieldObjects(){return this._transport.getFieldObjects()}hasJSActions(){return this._transport.hasJSActions()}getCalculationOrderIds(){return this._transport.getCalculationOrderIds()}}exports.PDFDocumentProxy=PDFDocumentProxy;class PDFPageProxy{#i=null;#s=!1;constructor(t,e,i,s=!1){this._pageIndex=t;this._pageInfo=e;this._transport=i;this._stats=s?new _display_utils.StatTimer:null;this._pdfBug=s;this.commonObjs=i.commonObjs;this.objs=new PDFObjects;this._maybeCleanupAfterRender=!1;this._intentStates=new Map;this.destroyed=!1}get pageNumber(){return this._pageIndex+1}get rotate(){return this._pageInfo.rotate}get ref(){return this._pageInfo.ref}get userUnit(){return this._pageInfo.userUnit}get view(){return this._pageInfo.view}getViewport({scale:t,rotation:e=this.rotate,offsetX:i=0,offsetY:s=0,dontFlip:n=!1}={}){return new _display_utils.PageViewport({viewBox:this.view,scale:t,rotation:e,offsetX:i,offsetY:s,dontFlip:n})}getAnnotations({intent:t="display"}={}){const e=this._transport.getRenderingIntent(t);return this._transport.getAnnotations(this._pageIndex,e.renderingIntent)}getJSActions(){return this._transport.getPageJSActions(this._pageIndex)}get filterFactory(){return this._transport.filterFactory}get isPureXfa(){return(0,_util.shadow)(this,"isPureXfa",!!this._transport._htmlForXfa)}async getXfa(){return this._transport._htmlForXfa?.children[this._pageIndex]||null}render({canvasContext:t,viewport:e,intent:i="display",annotationMode:s=_util.AnnotationMode.ENABLE,transform:n=null,background:a=null,optionalContentConfigPromise:r=null,annotationCanvasMap:o=null,pageColors:l=null,printAnnotationStorage:h=null}){this._stats?.time("Overall");const c=this._transport.getRenderingIntent(i,s,h);this.#s=!1;this.#n();r||(r=this._transport.getOptionalContentConfig());let d=this._intentStates.get(c.cacheKey);if(!d){d=Object.create(null);this._intentStates.set(c.cacheKey,d)}if(d.streamReaderCancelTimeout){clearTimeout(d.streamReaderCancelTimeout);d.streamReaderCancelTimeout=null}const u=!!(c.renderingIntent&_util.RenderingIntentFlag.PRINT);if(!d.displayReadyCapability){d.displayReadyCapability=new _util.PromiseCapability;d.operatorList={fnArray:[],argsArray:[],lastChunk:!1,separateAnnots:null};this._stats?.time("Page Request");this._pumpOperatorList(c)}const complete=t=>{d.renderTasks.delete(p);(this._maybeCleanupAfterRender||u)&&(this.#s=!0);this.#a(!u);if(t){p.capability.reject(t);this._abortOperatorList({intentState:d,reason:t instanceof Error?t:new Error(t)})}else p.capability.resolve();this._stats?.timeEnd("Rendering");this._stats?.timeEnd("Overall")},p=new InternalRenderTask({callback:complete,params:{canvasContext:t,viewport:e,transform:n,background:a},objs:this.objs,commonObjs:this.commonObjs,annotationCanvasMap:o,operatorList:d.operatorList,pageIndex:this._pageIndex,canvasFactory:this._transport.canvasFactory,filterFactory:this._transport.filterFactory,useRequestAnimationFrame:!u,pdfBug:this._pdfBug,pageColors:l});(d.renderTasks||=new Set).add(p);const g=p.task;Promise.all([d.displayReadyCapability.promise,r]).then((([t,e])=>{if(this.destroyed)complete();else{this._stats?.time("Rendering");p.initializeGraphics({transparency:t,optionalContentConfig:e});p.operatorListChanged()}})).catch(complete);return g}getOperatorList({intent:t="display",annotationMode:e=_util.AnnotationMode.ENABLE,printAnnotationStorage:i=null}={}){const s=this._transport.getRenderingIntent(t,e,i,!0);let n,a=this._intentStates.get(s.cacheKey);if(!a){a=Object.create(null);this._intentStates.set(s.cacheKey,a)}if(!a.opListReadCapability){n=Object.create(null);n.operatorListChanged=function operatorListChanged(){if(a.operatorList.lastChunk){a.opListReadCapability.resolve(a.operatorList);a.renderTasks.delete(n)}};a.opListReadCapability=new _util.PromiseCapability;(a.renderTasks||=new Set).add(n);a.operatorList={fnArray:[],argsArray:[],lastChunk:!1,separateAnnots:null};this._stats?.time("Page Request");this._pumpOperatorList(s)}return a.opListReadCapability.promise}streamTextContent({includeMarkedContent:t=!1,disableNormalization:e=!1}={}){return this._transport.messageHandler.sendWithStream("GetTextContent",{pageIndex:this._pageIndex,includeMarkedContent:!0===t,disableNormalization:!0===e},{highWaterMark:100,size:t=>t.items.length})}getTextContent(t={}){if(this._transport._htmlForXfa)return this.getXfa().then((t=>_xfa_text.XfaText.textContent(t)));const e=this.streamTextContent(t);return new Promise((function(t,i){const s=e.getReader(),n={items:[],styles:Object.create(null)};!function pump(){s.read().then((function({value:e,done:i}){if(i)t(n);else{Object.assign(n.styles,e.styles);n.items.push(...e.items);pump()}}),i)}()}))}getStructTree(){return this._transport.getStructTree(this._pageIndex)}_destroy(){this.destroyed=!0;const t=[];for(const e of this._intentStates.values()){this._abortOperatorList({intentState:e,reason:new Error("Page was destroyed."),force:!0});if(!e.opListReadCapability)for(const i of e.renderTasks){t.push(i.completed);i.cancel()}}this.objs.clear();this.#s=!1;this.#n();return Promise.all(t)}cleanup(t=!1){this.#s=!0;const e=this.#a(!1);t&&e&&(this._stats&&=new _display_utils.StatTimer);return e}#a(t=!1){this.#n();if(!this.#s||this.destroyed)return!1;if(t){this.#i=setTimeout((()=>{this.#i=null;this.#a(!1)}),DELAYED_CLEANUP_TIMEOUT);return!1}for(const{renderTasks:t,operatorList:e}of this._intentStates.values())if(t.size>0||!e.lastChunk)return!1;this._intentStates.clear();this.objs.clear();this.#s=!1;return!0}#n(){if(this.#i){clearTimeout(this.#i);this.#i=null}}_startRenderPage(t,e){const i=this._intentStates.get(e);if(i){this._stats?.timeEnd("Page Request");i.displayReadyCapability?.resolve(t)}}_renderPageChunk(t,e){for(let i=0,s=t.length;i{a.read().then((({value:t,done:e})=>{if(e)r.streamReader=null;else if(!this._transport.destroyed){this._renderPageChunk(t,r);pump()}}),(t=>{r.streamReader=null;if(!this._transport.destroyed){if(r.operatorList){r.operatorList.lastChunk=!0;for(const t of r.renderTasks)t.operatorListChanged();this.#a(!0)}if(r.displayReadyCapability)r.displayReadyCapability.reject(t);else{if(!r.opListReadCapability)throw t;r.opListReadCapability.reject(t)}}}))};pump()}_abortOperatorList({intentState:t,reason:e,force:i=!1}){if(t.streamReader){if(t.streamReaderCancelTimeout){clearTimeout(t.streamReaderCancelTimeout);t.streamReaderCancelTimeout=null}if(!i){if(t.renderTasks.size>0)return;if(e instanceof _display_utils.RenderingCancelledException){let i=RENDERING_CANCELLED_TIMEOUT;e.extraDelay>0&&e.extraDelay<1e3&&(i+=e.extraDelay);t.streamReaderCancelTimeout=setTimeout((()=>{t.streamReaderCancelTimeout=null;this._abortOperatorList({intentState:t,reason:e,force:!0})}),i);return}}t.streamReader.cancel(new _util.AbortException(e.message)).catch((()=>{}));t.streamReader=null;if(!this._transport.destroyed){for(const[e,i]of this._intentStates)if(i===t){this._intentStates.delete(e);break}this.cleanup()}}}get stats(){return this._stats}}exports.PDFPageProxy=PDFPageProxy;class LoopbackPort{#r=new Set;#o=Promise.resolve();postMessage(t,e){const i={data:structuredClone(t,e?{transfer:e}:null)};this.#o.then((()=>{for(const t of this.#r)t.call(this,i)}))}addEventListener(t,e){this.#r.add(e)}removeEventListener(t,e){this.#r.delete(e)}terminate(){this.#r.clear()}}exports.LoopbackPort=LoopbackPort;const PDFWorkerUtil={isWorkerDisabled:!1,fallbackWorkerSrc:null,fakeWorkerId:0};exports.PDFWorkerUtil=PDFWorkerUtil;if(_util.isNodeJS&&"function"==typeof require){PDFWorkerUtil.isWorkerDisabled=!0;PDFWorkerUtil.fallbackWorkerSrc="./pdf.worker.js"}else if("object"==typeof document){const t=document?.currentScript?.src;t&&(PDFWorkerUtil.fallbackWorkerSrc=t.replace(/(\.(?:min\.)?js)(\?.*)?$/i,".worker$1$2"))}PDFWorkerUtil.isSameOrigin=function(t,e){let i;try{i=new URL(t);if(!i.origin||"null"===i.origin)return!1}catch{return!1}const s=new URL(e,i);return i.origin===s.origin};PDFWorkerUtil.createCDNWrapper=function(t){const e=`importScripts("${t}");`;return URL.createObjectURL(new Blob([e]))};class PDFWorker{static#l;constructor({name:t=null,port:e=null,verbosity:i=(0,_util.getVerbosityLevel)()}={}){this.name=t;this.destroyed=!1;this.verbosity=i;this._readyCapability=new _util.PromiseCapability;this._port=null;this._webWorker=null;this._messageHandler=null;if(e){if(PDFWorker.#l?.has(e))throw new Error("Cannot use more than one PDFWorker per port.");(PDFWorker.#l||=new WeakMap).set(e,this);this._initializeFromPort(e)}else this._initialize()}get promise(){return this._readyCapability.promise}get port(){return this._port}get messageHandler(){return this._messageHandler}_initializeFromPort(t){this._port=t;this._messageHandler=new _message_handler.MessageHandler("main","worker",t);this._messageHandler.on("ready",(function(){}));this._readyCapability.resolve();this._messageHandler.send("configure",{verbosity:this.verbosity})}_initialize(){if(!PDFWorkerUtil.isWorkerDisabled&&!PDFWorker._mainThreadWorkerMessageHandler){let{workerSrc:t}=PDFWorker;try{PDFWorkerUtil.isSameOrigin(window.location.href,t)||(t=PDFWorkerUtil.createCDNWrapper(new URL(t,window.location).href));const e=new Worker(t),i=new _message_handler.MessageHandler("main","worker",e),terminateEarly=()=>{e.removeEventListener("error",onWorkerError);i.destroy();e.terminate();this.destroyed?this._readyCapability.reject(new Error("Worker was destroyed")):this._setupFakeWorker()},onWorkerError=()=>{this._webWorker||terminateEarly()};e.addEventListener("error",onWorkerError);i.on("test",(t=>{e.removeEventListener("error",onWorkerError);if(this.destroyed)terminateEarly();else if(t){this._messageHandler=i;this._port=e;this._webWorker=e;this._readyCapability.resolve();i.send("configure",{verbosity:this.verbosity})}else{this._setupFakeWorker();i.destroy();e.terminate()}}));i.on("ready",(t=>{e.removeEventListener("error",onWorkerError);if(this.destroyed)terminateEarly();else try{sendTest()}catch{this._setupFakeWorker()}}));const sendTest=()=>{const t=new Uint8Array;i.send("test",t,[t.buffer])};sendTest();return}catch{(0,_util.info)("The worker has been disabled.")}}this._setupFakeWorker()}_setupFakeWorker(){if(!PDFWorkerUtil.isWorkerDisabled){(0,_util.warn)("Setting up fake worker.");PDFWorkerUtil.isWorkerDisabled=!0}PDFWorker._setupFakeWorkerGlobal.then((t=>{if(this.destroyed){this._readyCapability.reject(new Error("Worker was destroyed"));return}const e=new LoopbackPort;this._port=e;const i="fake"+PDFWorkerUtil.fakeWorkerId++,s=new _message_handler.MessageHandler(i+"_worker",i,e);t.setup(s,e);const n=new _message_handler.MessageHandler(i,i+"_worker",e);this._messageHandler=n;this._readyCapability.resolve();n.send("configure",{verbosity:this.verbosity})})).catch((t=>{this._readyCapability.reject(new Error(`Setting up fake worker failed: "${t.message}".`))}))}destroy(){this.destroyed=!0;if(this._webWorker){this._webWorker.terminate();this._webWorker=null}PDFWorker.#l?.delete(this._port);this._port=null;if(this._messageHandler){this._messageHandler.destroy();this._messageHandler=null}}static fromPort(t){if(!t?.port)throw new Error("PDFWorker.fromPort - invalid method signature.");const e=this.#l?.get(t.port);if(e){if(e._pendingDestroy)throw new Error("PDFWorker.fromPort - the worker is being destroyed.\nPlease remember to await `PDFDocumentLoadingTask.destroy()`-calls.");return e}return new PDFWorker(t)}static get workerSrc(){if(_worker_options.GlobalWorkerOptions.workerSrc)return _worker_options.GlobalWorkerOptions.workerSrc;if(null!==PDFWorkerUtil.fallbackWorkerSrc){_util.isNodeJS||(0,_display_utils.deprecated)('No "GlobalWorkerOptions.workerSrc" specified.');return PDFWorkerUtil.fallbackWorkerSrc}throw new Error('No "GlobalWorkerOptions.workerSrc" specified.')}static get _mainThreadWorkerMessageHandler(){try{return globalThis.pdfjsWorker?.WorkerMessageHandler||null}catch{return null}}static get _setupFakeWorkerGlobal(){const loader=async()=>{const mainWorkerMessageHandler=this._mainThreadWorkerMessageHandler;if(mainWorkerMessageHandler)return mainWorkerMessageHandler;if(_util.isNodeJS&&"function"==typeof require){const worker=eval("require")(this.workerSrc);return worker.WorkerMessageHandler}await(0,_display_utils.loadScript)(this.workerSrc);return window.pdfjsWorker.WorkerMessageHandler};return(0,_util.shadow)(this,"_setupFakeWorkerGlobal",loader())}}exports.PDFWorker=PDFWorker;class WorkerTransport{#h=new Map;#c=new Map;#d=new Map;#u=null;constructor(t,e,i,s,n){this.messageHandler=t;this.loadingTask=e;this.commonObjs=new PDFObjects;this.fontLoader=new _font_loader.FontLoader({ownerDocument:s.ownerDocument,styleElement:s.styleElement});this._params=s;this.canvasFactory=n.canvasFactory;this.filterFactory=n.filterFactory;this.cMapReaderFactory=n.cMapReaderFactory;this.standardFontDataFactory=n.standardFontDataFactory;this.destroyed=!1;this.destroyCapability=null;this._networkStream=i;this._fullReader=null;this._lastProgress=null;this.downloadInfoCapability=new _util.PromiseCapability;this.setupMessageHandler()}#p(t,e=null){const i=this.#h.get(t);if(i)return i;const s=this.messageHandler.sendWithPromise(t,e);this.#h.set(t,s);return s}get annotationStorage(){return(0,_util.shadow)(this,"annotationStorage",new _annotation_storage.AnnotationStorage)}getRenderingIntent(t,e=_util.AnnotationMode.ENABLE,i=null,s=!1){let n=_util.RenderingIntentFlag.DISPLAY,a=_annotation_storage.SerializableEmpty;switch(t){case"any":n=_util.RenderingIntentFlag.ANY;break;case"display":break;case"print":n=_util.RenderingIntentFlag.PRINT;break;default:(0,_util.warn)(`getRenderingIntent - invalid intent: ${t}`)}switch(e){case _util.AnnotationMode.DISABLE:n+=_util.RenderingIntentFlag.ANNOTATIONS_DISABLE;break;case _util.AnnotationMode.ENABLE:break;case _util.AnnotationMode.ENABLE_FORMS:n+=_util.RenderingIntentFlag.ANNOTATIONS_FORMS;break;case _util.AnnotationMode.ENABLE_STORAGE:n+=_util.RenderingIntentFlag.ANNOTATIONS_STORAGE;a=(n&_util.RenderingIntentFlag.PRINT&&i instanceof _annotation_storage.PrintAnnotationStorage?i:this.annotationStorage).serializable;break;default:(0,_util.warn)(`getRenderingIntent - invalid annotationMode: ${e}`)}s&&(n+=_util.RenderingIntentFlag.OPLIST);return{renderingIntent:n,cacheKey:`${n}_${a.hash}`,annotationStorageSerializable:a}}destroy(){if(this.destroyCapability)return this.destroyCapability.promise;this.destroyed=!0;this.destroyCapability=new _util.PromiseCapability;this.#u?.reject(new Error("Worker was destroyed during onPassword callback"));const t=[];for(const e of this.#c.values())t.push(e._destroy());this.#c.clear();this.#d.clear();this.hasOwnProperty("annotationStorage")&&this.annotationStorage.resetModified();const e=this.messageHandler.sendWithPromise("Terminate",null);t.push(e);Promise.all(t).then((()=>{this.commonObjs.clear();this.fontLoader.clear();this.#h.clear();this.filterFactory.destroy();this._networkStream?.cancelAllRequests(new _util.AbortException("Worker was terminated."));if(this.messageHandler){this.messageHandler.destroy();this.messageHandler=null}this.destroyCapability.resolve()}),this.destroyCapability.reject);return this.destroyCapability.promise}setupMessageHandler(){const{messageHandler:t,loadingTask:e}=this;t.on("GetReader",((t,e)=>{(0,_util.assert)(this._networkStream,"GetReader - no `IPDFStream` instance available.");this._fullReader=this._networkStream.getFullReader();this._fullReader.onProgress=t=>{this._lastProgress={loaded:t.loaded,total:t.total}};e.onPull=()=>{this._fullReader.read().then((function({value:t,done:i}){if(i)e.close();else{(0,_util.assert)(t instanceof ArrayBuffer,"GetReader - expected an ArrayBuffer.");e.enqueue(new Uint8Array(t),1,[t])}})).catch((t=>{e.error(t)}))};e.onCancel=t=>{this._fullReader.cancel(t);e.ready.catch((t=>{if(!this.destroyed)throw t}))}}));t.on("ReaderHeadersReady",(t=>{const i=new _util.PromiseCapability,s=this._fullReader;s.headersReady.then((()=>{if(!s.isStreamingSupported||!s.isRangeSupported){this._lastProgress&&e.onProgress?.(this._lastProgress);s.onProgress=t=>{e.onProgress?.({loaded:t.loaded,total:t.total})}}i.resolve({isStreamingSupported:s.isStreamingSupported,isRangeSupported:s.isRangeSupported,contentLength:s.contentLength})}),i.reject);return i.promise}));t.on("GetRangeReader",((t,e)=>{(0,_util.assert)(this._networkStream,"GetRangeReader - no `IPDFStream` instance available.");const i=this._networkStream.getRangeReader(t.begin,t.end);if(i){e.onPull=()=>{i.read().then((function({value:t,done:i}){if(i)e.close();else{(0,_util.assert)(t instanceof ArrayBuffer,"GetRangeReader - expected an ArrayBuffer.");e.enqueue(new Uint8Array(t),1,[t])}})).catch((t=>{e.error(t)}))};e.onCancel=t=>{i.cancel(t);e.ready.catch((t=>{if(!this.destroyed)throw t}))}}else e.close()}));t.on("GetDoc",(({pdfInfo:t})=>{this._numPages=t.numPages;this._htmlForXfa=t.htmlForXfa;delete t.htmlForXfa;e._capability.resolve(new PDFDocumentProxy(t,this))}));t.on("DocException",(function(t){let i;switch(t.name){case"PasswordException":i=new _util.PasswordException(t.message,t.code);break;case"InvalidPDFException":i=new _util.InvalidPDFException(t.message);break;case"MissingPDFException":i=new _util.MissingPDFException(t.message);break;case"UnexpectedResponseException":i=new _util.UnexpectedResponseException(t.message,t.status);break;case"UnknownErrorException":i=new _util.UnknownErrorException(t.message,t.details);break;default:(0,_util.unreachable)("DocException - expected a valid Error.")}e._capability.reject(i)}));t.on("PasswordRequest",(t=>{this.#u=new _util.PromiseCapability;if(e.onPassword){const updatePassword=t=>{t instanceof Error?this.#u.reject(t):this.#u.resolve({password:t})};try{e.onPassword(updatePassword,t.code)}catch(t){this.#u.reject(t)}}else this.#u.reject(new _util.PasswordException(t.message,t.code));return this.#u.promise}));t.on("DataLoaded",(t=>{e.onProgress?.({loaded:t.length,total:t.length});this.downloadInfoCapability.resolve(t)}));t.on("StartRenderPage",(t=>{if(this.destroyed)return;this.#c.get(t.pageIndex)._startRenderPage(t.transparency,t.cacheKey)}));t.on("commonobj",(([e,i,s])=>{if(!this.destroyed&&!this.commonObjs.has(e))switch(i){case"Font":const n=this._params;if("error"in s){const t=s.error;(0,_util.warn)(`Error during font loading: ${t}`);this.commonObjs.resolve(e,t);break}const a=n.pdfBug&&globalThis.FontInspector?.enabled?(t,e)=>globalThis.FontInspector.fontAdded(t,e):null,r=new _font_loader.FontFaceObject(s,{isEvalSupported:n.isEvalSupported,disableFontFace:n.disableFontFace,ignoreErrors:n.ignoreErrors,inspectFont:a});this.fontLoader.bind(r).catch((i=>t.sendWithPromise("FontFallback",{id:e}))).finally((()=>{!n.fontExtraProperties&&r.data&&(r.data=null);this.commonObjs.resolve(e,r)}));break;case"FontPath":case"Image":case"Pattern":this.commonObjs.resolve(e,s);break;default:throw new Error(`Got unknown common object type ${i}`)}}));t.on("obj",(([t,e,i,s])=>{if(this.destroyed)return;const n=this.#c.get(e);if(!n.objs.has(t))switch(i){case"Image":n.objs.resolve(t,s);if(s){let t;if(s.bitmap){const{width:e,height:i}=s;t=e*i*4}else t=s.data?.length||0;t>_util.MAX_IMAGE_SIZE_TO_CACHE&&(n._maybeCleanupAfterRender=!0)}break;case"Pattern":n.objs.resolve(t,s);break;default:throw new Error(`Got unknown object type ${i}`)}}));t.on("DocProgress",(t=>{this.destroyed||e.onProgress?.({loaded:t.loaded,total:t.total})}));t.on("FetchBuiltInCMap",(t=>this.destroyed?Promise.reject(new Error("Worker was destroyed.")):this.cMapReaderFactory?this.cMapReaderFactory.fetch(t):Promise.reject(new Error("CMapReaderFactory not initialized, see the `useWorkerFetch` parameter."))));t.on("FetchStandardFontData",(t=>this.destroyed?Promise.reject(new Error("Worker was destroyed.")):this.standardFontDataFactory?this.standardFontDataFactory.fetch(t):Promise.reject(new Error("StandardFontDataFactory not initialized, see the `useWorkerFetch` parameter."))))}getData(){return this.messageHandler.sendWithPromise("GetData",null)}saveDocument(){this.annotationStorage.size<=0&&(0,_util.warn)("saveDocument called while `annotationStorage` is empty, please use the getData-method instead.");const{map:t,transfers:e}=this.annotationStorage.serializable;return this.messageHandler.sendWithPromise("SaveDocument",{isPureXfa:!!this._htmlForXfa,numPages:this._numPages,annotationStorage:t,filename:this._fullReader?.filename??null},e).finally((()=>{this.annotationStorage.resetModified()}))}getPage(t){if(!Number.isInteger(t)||t<=0||t>this._numPages)return Promise.reject(new Error("Invalid page request."));const e=t-1,i=this.#d.get(e);if(i)return i;const s=this.messageHandler.sendWithPromise("GetPage",{pageIndex:e}).then((t=>{if(this.destroyed)throw new Error("Transport destroyed");const i=new PDFPageProxy(e,t,this,this._params.pdfBug);this.#c.set(e,i);return i}));this.#d.set(e,s);return s}getPageIndex(t){return"object"!=typeof t||null===t||!Number.isInteger(t.num)||t.num<0||!Number.isInteger(t.gen)||t.gen<0?Promise.reject(new Error("Invalid pageIndex request.")):this.messageHandler.sendWithPromise("GetPageIndex",{num:t.num,gen:t.gen})}getAnnotations(t,e){return this.messageHandler.sendWithPromise("GetAnnotations",{pageIndex:t,intent:e})}getFieldObjects(){return this.#p("GetFieldObjects")}hasJSActions(){return this.#p("HasJSActions")}getCalculationOrderIds(){return this.messageHandler.sendWithPromise("GetCalculationOrderIds",null)}getDestinations(){return this.messageHandler.sendWithPromise("GetDestinations",null)}getDestination(t){return"string"!=typeof t?Promise.reject(new Error("Invalid destination request.")):this.messageHandler.sendWithPromise("GetDestination",{id:t})}getPageLabels(){return this.messageHandler.sendWithPromise("GetPageLabels",null)}getPageLayout(){return this.messageHandler.sendWithPromise("GetPageLayout",null)}getPageMode(){return this.messageHandler.sendWithPromise("GetPageMode",null)}getViewerPreferences(){return this.messageHandler.sendWithPromise("GetViewerPreferences",null)}getOpenAction(){return this.messageHandler.sendWithPromise("GetOpenAction",null)}getAttachments(){return this.messageHandler.sendWithPromise("GetAttachments",null)}getDocJSActions(){return this.#p("GetDocJSActions")}getPageJSActions(t){return this.messageHandler.sendWithPromise("GetPageJSActions",{pageIndex:t})}getStructTree(t){return this.messageHandler.sendWithPromise("GetStructTree",{pageIndex:t})}getOutline(){return this.messageHandler.sendWithPromise("GetOutline",null)}getOptionalContentConfig(){return this.messageHandler.sendWithPromise("GetOptionalContentConfig",null).then((t=>new _optional_content_config.OptionalContentConfig(t)))}getPermissions(){return this.messageHandler.sendWithPromise("GetPermissions",null)}getMetadata(){const t="GetMetadata",e=this.#h.get(t);if(e)return e;const i=this.messageHandler.sendWithPromise(t,null).then((t=>({info:t[0],metadata:t[1]?new _metadata.Metadata(t[1]):null,contentDispositionFilename:this._fullReader?.filename??null,contentLength:this._fullReader?.contentLength??null})));this.#h.set(t,i);return i}getMarkInfo(){return this.messageHandler.sendWithPromise("GetMarkInfo",null)}async startCleanup(t=!1){if(!this.destroyed){await this.messageHandler.sendWithPromise("Cleanup",null);for(const t of this.#c.values()){if(!t.cleanup())throw new Error(`startCleanup: Page ${t.pageNumber} is currently rendering.`)}this.commonObjs.clear();t||this.fontLoader.clear();this.#h.clear();this.filterFactory.destroy(!0)}}get loadingParams(){const{disableAutoFetch:t,enableXfa:e}=this._params;return(0,_util.shadow)(this,"loadingParams",{disableAutoFetch:t,enableXfa:e})}}class PDFObjects{#g=Object.create(null);#m(t){return this.#g[t]||={capability:new _util.PromiseCapability,data:null}}get(t,e=null){if(e){const i=this.#m(t);i.capability.promise.then((()=>e(i.data)));return null}const i=this.#g[t];if(!i?.capability.settled)throw new Error(`Requesting object that isn't resolved yet ${t}.`);return i.data}has(t){const e=this.#g[t];return e?.capability.settled||!1}resolve(t,e=null){const i=this.#m(t);i.data=e;i.capability.resolve()}clear(){for(const t in this.#g){const{data:e}=this.#g[t];e?.bitmap?.close()}this.#g=Object.create(null)}}class RenderTask{#f=null;constructor(t){this.#f=t;this.onContinue=null}get promise(){return this.#f.capability.promise}cancel(t=0){this.#f.cancel(null,t)}get separateAnnots(){const{separateAnnots:t}=this.#f.operatorList;if(!t)return!1;const{annotationCanvasMap:e}=this.#f;return t.form||t.canvas&&e?.size>0}}exports.RenderTask=RenderTask;class InternalRenderTask{static#b=new WeakSet;constructor({callback:t,params:e,objs:i,commonObjs:s,annotationCanvasMap:n,operatorList:a,pageIndex:r,canvasFactory:o,filterFactory:l,useRequestAnimationFrame:h=!1,pdfBug:c=!1,pageColors:d=null}){this.callback=t;this.params=e;this.objs=i;this.commonObjs=s;this.annotationCanvasMap=n;this.operatorListIdx=null;this.operatorList=a;this._pageIndex=r;this.canvasFactory=o;this.filterFactory=l;this._pdfBug=c;this.pageColors=d;this.running=!1;this.graphicsReadyCallback=null;this.graphicsReady=!1;this._useRequestAnimationFrame=!0===h&&"undefined"!=typeof window;this.cancelled=!1;this.capability=new _util.PromiseCapability;this.task=new RenderTask(this);this._cancelBound=this.cancel.bind(this);this._continueBound=this._continue.bind(this);this._scheduleNextBound=this._scheduleNext.bind(this);this._nextBound=this._next.bind(this);this._canvas=e.canvasContext.canvas}get completed(){return this.capability.promise.catch((function(){}))}initializeGraphics({transparency:t=!1,optionalContentConfig:e}){if(this.cancelled)return;if(this._canvas){if(InternalRenderTask.#b.has(this._canvas))throw new Error("Cannot use the same canvas during multiple render() operations. Use different canvas or ensure previous operations were cancelled or completed.");InternalRenderTask.#b.add(this._canvas)}if(this._pdfBug&&globalThis.StepperManager?.enabled){this.stepper=globalThis.StepperManager.create(this._pageIndex);this.stepper.init(this.operatorList);this.stepper.nextBreakPoint=this.stepper.getNextBreakPoint()}const{canvasContext:i,viewport:s,transform:n,background:a}=this.params;this.gfx=new _canvas.CanvasGraphics(i,this.commonObjs,this.objs,this.canvasFactory,this.filterFactory,{optionalContentConfig:e},this.annotationCanvasMap,this.pageColors);this.gfx.beginDrawing({transform:n,viewport:s,transparency:t,background:a});this.operatorListIdx=0;this.graphicsReady=!0;this.graphicsReadyCallback?.()}cancel(t=null,e=0){this.running=!1;this.cancelled=!0;this.gfx?.endDrawing();InternalRenderTask.#b.delete(this._canvas);this.callback(t||new _display_utils.RenderingCancelledException(`Rendering cancelled, page ${this._pageIndex+1}`,e))}operatorListChanged(){if(this.graphicsReady){this.stepper?.updateOperatorList(this.operatorList);this.running||this._continue()}else this.graphicsReadyCallback||=this._continueBound}_continue(){this.running=!0;this.cancelled||(this.task.onContinue?this.task.onContinue(this._scheduleNextBound):this._scheduleNext())}_scheduleNext(){this._useRequestAnimationFrame?window.requestAnimationFrame((()=>{this._nextBound().catch(this._cancelBound)})):Promise.resolve().then(this._nextBound).catch(this._cancelBound)}async _next(){if(!this.cancelled){this.operatorListIdx=this.gfx.executeOperatorList(this.operatorList,this.operatorListIdx,this._continueBound,this.stepper);if(this.operatorListIdx===this.operatorList.argsArray.length){this.running=!1;if(this.operatorList.lastChunk){this.gfx.endDrawing();InternalRenderTask.#b.delete(this._canvas);this.callback()}}}}}const version="3.11.174";exports.version=version;const build="ce8716743";exports.build=build},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.SerializableEmpty=e.PrintAnnotationStorage=e.AnnotationStorage=void 0;var s=i(1),n=i(4),a=i(8);const r=Object.freeze({map:null,hash:"",transfers:void 0});e.SerializableEmpty=r;class AnnotationStorage{#A=!1;#_=new Map;constructor(){this.onSetModified=null;this.onResetModified=null;this.onAnnotationEditor=null}getValue(t,e){const i=this.#_.get(t);return void 0===i?e:Object.assign(e,i)}getRawValue(t){return this.#_.get(t)}remove(t){this.#_.delete(t);0===this.#_.size&&this.resetModified();if("function"==typeof this.onAnnotationEditor){for(const t of this.#_.values())if(t instanceof n.AnnotationEditor)return;this.onAnnotationEditor(null)}}setValue(t,e){const i=this.#_.get(t);let s=!1;if(void 0!==i){for(const[t,n]of Object.entries(e))if(i[t]!==n){s=!0;i[t]=n}}else{s=!0;this.#_.set(t,e)}s&&this.#v();e instanceof n.AnnotationEditor&&"function"==typeof this.onAnnotationEditor&&this.onAnnotationEditor(e.constructor._type)}has(t){return this.#_.has(t)}getAll(){return this.#_.size>0?(0,s.objectFromMap)(this.#_):null}setAll(t){for(const[e,i]of Object.entries(t))this.setValue(e,i)}get size(){return this.#_.size}#v(){if(!this.#A){this.#A=!0;"function"==typeof this.onSetModified&&this.onSetModified()}}resetModified(){if(this.#A){this.#A=!1;"function"==typeof this.onResetModified&&this.onResetModified()}}get print(){return new PrintAnnotationStorage(this)}get serializable(){if(0===this.#_.size)return r;const t=new Map,e=new a.MurmurHash3_64,i=[],s=Object.create(null);let o=!1;for(const[i,a]of this.#_){const r=a instanceof n.AnnotationEditor?a.serialize(!1,s):a;if(r){t.set(i,r);e.update(`${i}:${JSON.stringify(r)}`);o||=!!r.bitmap}}if(o)for(const e of t.values())e.bitmap&&i.push(e.bitmap);return t.size>0?{map:t,hash:e.hexdigest(),transfers:i}:r}}e.AnnotationStorage=AnnotationStorage;class PrintAnnotationStorage extends AnnotationStorage{#y;constructor(t){super();const{map:e,hash:i,transfers:s}=t.serializable,n=structuredClone(e,s?{transfer:s}:null);this.#y={map:n,hash:i,transfers:s}}get print(){(0,s.unreachable)("Should not call PrintAnnotationStorage.print")}get serializable(){return this.#y}}e.PrintAnnotationStorage=PrintAnnotationStorage},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.AnnotationEditor=void 0;var s=i(5),n=i(1),a=i(6);class AnnotationEditor{#S="";#E=!1;#x=null;#w=null;#C=null;#T=!1;#P=null;#M=this.focusin.bind(this);#k=this.focusout.bind(this);#F=!1;#R=!1;#D=!1;_initialOptions=Object.create(null);_uiManager=null;_focusEventsAllowed=!0;_l10nPromise=null;#I=!1;#L=AnnotationEditor._zIndex++;static _borderLineWidth=-1;static _colorManager=new s.ColorManager;static _zIndex=1;static SMALL_EDITOR_SIZE=0;constructor(t){this.constructor===AnnotationEditor&&(0,n.unreachable)("Cannot initialize AnnotationEditor.");this.parent=t.parent;this.id=t.id;this.width=this.height=null;this.pageIndex=t.parent.pageIndex;this.name=t.name;this.div=null;this._uiManager=t.uiManager;this.annotationElementId=null;this._willKeepAspectRatio=!1;this._initialOptions.isCentered=t.isCentered;this._structTreeParentId=null;const{rotation:e,rawDims:{pageWidth:i,pageHeight:s,pageX:a,pageY:r}}=this.parent.viewport;this.rotation=e;this.pageRotation=(360+e-this._uiManager.viewParameters.rotation)%360;this.pageDimensions=[i,s];this.pageTranslation=[a,r];const[o,l]=this.parentDimensions;this.x=t.x/o;this.y=t.y/l;this.isAttachedToDOM=!1;this.deleted=!1}get editorType(){return Object.getPrototypeOf(this).constructor._type}static get _defaultLineColor(){return(0,n.shadow)(this,"_defaultLineColor",this._colorManager.getHexCode("CanvasText"))}static deleteAnnotationElement(t){const e=new FakeEditor({id:t.parent.getNextId(),parent:t.parent,uiManager:t._uiManager});e.annotationElementId=t.annotationElementId;e.deleted=!0;e._uiManager.addToAnnotationStorage(e)}static initialize(t,e=null){AnnotationEditor._l10nPromise||=new Map(["editor_alt_text_button_label","editor_alt_text_edit_button_label","editor_alt_text_decorative_tooltip"].map((e=>[e,t.get(e)])));if(e?.strings)for(const i of e.strings)AnnotationEditor._l10nPromise.set(i,t.get(i));if(-1!==AnnotationEditor._borderLineWidth)return;const i=getComputedStyle(document.documentElement);AnnotationEditor._borderLineWidth=parseFloat(i.getPropertyValue("--outline-width"))||0}static updateDefaultParams(t,e){}static get defaultPropertiesToUpdate(){return[]}static isHandlingMimeForPasting(t){return!1}static paste(t,e){(0,n.unreachable)("Not implemented")}get propertiesToUpdate(){return[]}get _isDraggable(){return this.#I}set _isDraggable(t){this.#I=t;this.div?.classList.toggle("draggable",t)}center(){const[t,e]=this.pageDimensions;switch(this.parentRotation){case 90:this.x-=this.height*e/(2*t);this.y+=this.width*t/(2*e);break;case 180:this.x+=this.width/2;this.y+=this.height/2;break;case 270:this.x+=this.height*e/(2*t);this.y-=this.width*t/(2*e);break;default:this.x-=this.width/2;this.y-=this.height/2}this.fixAndSetPosition()}addCommands(t){this._uiManager.addCommands(t)}get currentLayer(){return this._uiManager.currentLayer}setInBackground(){this.div.style.zIndex=0}setInForeground(){this.div.style.zIndex=this.#L}setParent(t){if(null!==t){this.pageIndex=t.pageIndex;this.pageDimensions=t.pageDimensions}this.parent=t}focusin(t){this._focusEventsAllowed&&(this.#F?this.#F=!1:this.parent.setSelected(this))}focusout(t){if(!this._focusEventsAllowed)return;if(!this.isAttachedToDOM)return;const e=t.relatedTarget;if(!e?.closest(`#${this.id}`)){t.preventDefault();this.parent?.isMultipleSelection||this.commitOrRemove()}}commitOrRemove(){this.isEmpty()?this.remove():this.commit()}commit(){this.addToAnnotationStorage()}addToAnnotationStorage(){this._uiManager.addToAnnotationStorage(this)}setAt(t,e,i,s){const[n,a]=this.parentDimensions;[i,s]=this.screenToPageTranslation(i,s);this.x=(t+i)/n;this.y=(e+s)/a;this.fixAndSetPosition()}#O([t,e],i,s){[i,s]=this.screenToPageTranslation(i,s);this.x+=i/t;this.y+=s/e;this.fixAndSetPosition()}translate(t,e){this.#O(this.parentDimensions,t,e)}translateInPage(t,e){this.#O(this.pageDimensions,t,e);this.div.scrollIntoView({block:"nearest"})}drag(t,e){const[i,s]=this.parentDimensions;this.x+=t/i;this.y+=e/s;if(this.parent&&(this.x<0||this.x>1||this.y<0||this.y>1)){const{x:t,y:e}=this.div.getBoundingClientRect();if(this.parent.findNewParent(this,t,e)){this.x-=Math.floor(this.x);this.y-=Math.floor(this.y)}}let{x:n,y:a}=this;const[r,o]=this.#N();n+=r;a+=o;this.div.style.left=`${(100*n).toFixed(2)}%`;this.div.style.top=`${(100*a).toFixed(2)}%`;this.div.scrollIntoView({block:"nearest"})}#N(){const[t,e]=this.parentDimensions,{_borderLineWidth:i}=AnnotationEditor,s=i/t,n=i/e;switch(this.rotation){case 90:return[-s,n];case 180:return[s,n];case 270:return[s,-n];default:return[-s,-n]}}fixAndSetPosition(){const[t,e]=this.pageDimensions;let{x:i,y:s,width:n,height:a}=this;n*=t;a*=e;i*=t;s*=e;switch(this.rotation){case 0:i=Math.max(0,Math.min(t-n,i));s=Math.max(0,Math.min(e-a,s));break;case 90:i=Math.max(0,Math.min(t-a,i));s=Math.min(e,Math.max(n,s));break;case 180:i=Math.min(t,Math.max(n,i));s=Math.min(e,Math.max(a,s));break;case 270:i=Math.min(t,Math.max(a,i));s=Math.max(0,Math.min(e-n,s))}this.x=i/=t;this.y=s/=e;const[r,o]=this.#N();i+=r;s+=o;const{style:l}=this.div;l.left=`${(100*i).toFixed(2)}%`;l.top=`${(100*s).toFixed(2)}%`;this.moveInDOM()}static#B(t,e,i){switch(i){case 90:return[e,-t];case 180:return[-t,-e];case 270:return[-e,t];default:return[t,e]}}screenToPageTranslation(t,e){return AnnotationEditor.#B(t,e,this.parentRotation)}pageTranslationToScreen(t,e){return AnnotationEditor.#B(t,e,360-this.parentRotation)}#U(t){switch(t){case 90:{const[t,e]=this.pageDimensions;return[0,-t/e,e/t,0]}case 180:return[-1,0,0,-1];case 270:{const[t,e]=this.pageDimensions;return[0,t/e,-e/t,0]}default:return[1,0,0,1]}}get parentScale(){return this._uiManager.viewParameters.realScale}get parentRotation(){return(this._uiManager.viewParameters.rotation+this.pageRotation)%360}get parentDimensions(){const{parentScale:t,pageDimensions:[e,i]}=this,s=e*t,a=i*t;return n.FeatureTest.isCSSRoundSupported?[Math.round(s),Math.round(a)]:[s,a]}setDims(t,e){const[i,s]=this.parentDimensions;this.div.style.width=`${(100*t/i).toFixed(2)}%`;this.#T||(this.div.style.height=`${(100*e/s).toFixed(2)}%`);this.#x?.classList.toggle("small",t{this._isDraggable=a;window.removeEventListener("pointerup",pointerUpCallback);window.removeEventListener("blur",pointerUpCallback);window.removeEventListener("pointermove",s,r);this.parent.div.style.cursor=d;this.div.style.cursor=u;const t=this.x,e=this.y,i=this.width,n=this.height;t===o&&e===l&&i===h&&n===c||this.addCommands({cmd:()=>{this.width=i;this.height=n;this.x=t;this.y=e;const[s,a]=this.parentDimensions;this.setDims(s*i,a*n);this.fixAndSetPosition()},undo:()=>{this.width=h;this.height=c;this.x=o;this.y=l;const[t,e]=this.parentDimensions;this.setDims(t*h,e*c);this.fixAndSetPosition()},mustExec:!0})};window.addEventListener("pointerup",pointerUpCallback);window.addEventListener("blur",pointerUpCallback)}#H(t,e){const[i,s]=this.parentDimensions,n=this.x,a=this.y,r=this.width,o=this.height,l=AnnotationEditor.MIN_SIZE/i,h=AnnotationEditor.MIN_SIZE/s,round=t=>Math.round(1e4*t)/1e4,c=this.#U(this.rotation),transf=(t,e)=>[c[0]*t+c[2]*e,c[1]*t+c[3]*e],d=this.#U(360-this.rotation);let u,p,g=!1,m=!1;switch(t){case"topLeft":g=!0;u=(t,e)=>[0,0];p=(t,e)=>[t,e];break;case"topMiddle":u=(t,e)=>[t/2,0];p=(t,e)=>[t/2,e];break;case"topRight":g=!0;u=(t,e)=>[t,0];p=(t,e)=>[0,e];break;case"middleRight":m=!0;u=(t,e)=>[t,e/2];p=(t,e)=>[0,e/2];break;case"bottomRight":g=!0;u=(t,e)=>[t,e];p=(t,e)=>[0,0];break;case"bottomMiddle":u=(t,e)=>[t/2,e];p=(t,e)=>[t/2,0];break;case"bottomLeft":g=!0;u=(t,e)=>[0,e];p=(t,e)=>[t,0];break;case"middleLeft":m=!0;u=(t,e)=>[0,e/2];p=(t,e)=>[t,e/2]}const f=u(r,o),b=p(r,o);let A=transf(...b);const _=round(n+A[0]),v=round(a+A[1]);let y=1,S=1,[E,x]=this.screenToPageTranslation(e.movementX,e.movementY);[E,x]=(w=E/i,C=x/s,[d[0]*w+d[2]*C,d[1]*w+d[3]*C]);var w,C;if(g){const t=Math.hypot(r,o);y=S=Math.max(Math.min(Math.hypot(b[0]-f[0]-E,b[1]-f[1]-x)/t,1/r,1/o),l/r,h/o)}else m?y=Math.max(l,Math.min(1,Math.abs(b[0]-f[0]-E)))/r:S=Math.max(h,Math.min(1,Math.abs(b[1]-f[1]-x)))/o;const T=round(r*y),P=round(o*S);A=transf(...p(T,P));const M=_-A[0],k=v-A[1];this.width=T;this.height=P;this.x=M;this.y=k;this.setDims(i*T,s*P);this.fixAndSetPosition()}async addAltTextButton(){if(this.#x)return;const t=this.#x=document.createElement("button");t.className="altText";const e=await AnnotationEditor._l10nPromise.get("editor_alt_text_button_label");t.textContent=e;t.setAttribute("aria-label",e);t.tabIndex="0";t.addEventListener("contextmenu",a.noContextMenu);t.addEventListener("pointerdown",(t=>t.stopPropagation()));t.addEventListener("click",(t=>{t.preventDefault();this._uiManager.editAltText(this)}),{capture:!0});t.addEventListener("keydown",(e=>{if(e.target===t&&"Enter"===e.key){e.preventDefault();this._uiManager.editAltText(this)}}));this.#W();this.div.append(t);if(!AnnotationEditor.SMALL_EDITOR_SIZE){const e=40;AnnotationEditor.SMALL_EDITOR_SIZE=Math.min(128,Math.round(t.getBoundingClientRect().width*(1+e/100)))}}async#W(){const t=this.#x;if(!t)return;if(!this.#S&&!this.#E){t.classList.remove("done");this.#w?.remove();return}AnnotationEditor._l10nPromise.get("editor_alt_text_edit_button_label").then((e=>{t.setAttribute("aria-label",e)}));let e=this.#w;if(!e){this.#w=e=document.createElement("span");e.className="tooltip";e.setAttribute("role","tooltip");const i=e.id=`alt-text-tooltip-${this.id}`;t.setAttribute("aria-describedby",i);const s=100;t.addEventListener("mouseenter",(()=>{this.#C=setTimeout((()=>{this.#C=null;this.#w.classList.add("show");this._uiManager._eventBus.dispatch("reporttelemetry",{source:this,details:{type:"editing",subtype:this.editorType,data:{action:"alt_text_tooltip"}}})}),s)}));t.addEventListener("mouseleave",(()=>{clearTimeout(this.#C);this.#C=null;this.#w?.classList.remove("show")}))}t.classList.add("done");e.innerText=this.#E?await AnnotationEditor._l10nPromise.get("editor_alt_text_decorative_tooltip"):this.#S;e.parentNode||t.append(e)}getClientDimensions(){return this.div.getBoundingClientRect()}get altTextData(){return{altText:this.#S,decorative:this.#E}}set altTextData({altText:t,decorative:e}){if(this.#S!==t||this.#E!==e){this.#S=t;this.#E=e;this.#W()}}render(){this.div=document.createElement("div");this.div.setAttribute("data-editor-rotation",(360-this.rotation)%360);this.div.className=this.name;this.div.setAttribute("id",this.id);this.div.setAttribute("tabIndex",0);this.setInForeground();this.div.addEventListener("focusin",this.#M);this.div.addEventListener("focusout",this.#k);const[t,e]=this.parentDimensions;if(this.parentRotation%180!=0){this.div.style.maxWidth=`${(100*e/t).toFixed(2)}%`;this.div.style.maxHeight=`${(100*t/e).toFixed(2)}%`}const[i,n]=this.getInitialTranslation();this.translate(i,n);(0,s.bindEvents)(this,this.div,["pointerdown"]);return this.div}pointerdown(t){const{isMac:e}=n.FeatureTest.platform;if(0!==t.button||t.ctrlKey&&e)t.preventDefault();else{this.#F=!0;this.#G(t)}}#G(t){if(!this._isDraggable)return;const e=this._uiManager.isSelected(this);this._uiManager.setUpDragSession();let i,s;if(e){i={passive:!0,capture:!0};s=t=>{const[e,i]=this.screenToPageTranslation(t.movementX,t.movementY);this._uiManager.dragSelectedEditors(e,i)};window.addEventListener("pointermove",s,i)}const pointerUpCallback=()=>{window.removeEventListener("pointerup",pointerUpCallback);window.removeEventListener("blur",pointerUpCallback);e&&window.removeEventListener("pointermove",s,i);this.#F=!1;if(!this._uiManager.endDragSession()){const{isMac:e}=n.FeatureTest.platform;t.ctrlKey&&!e||t.shiftKey||t.metaKey&&e?this.parent.toggleSelected(this):this.parent.setSelected(this)}};window.addEventListener("pointerup",pointerUpCallback);window.addEventListener("blur",pointerUpCallback)}moveInDOM(){this.parent?.moveEditorInDOM(this)}_setParentAndPosition(t,e,i){t.changeParent(this);this.x=e;this.y=i;this.fixAndSetPosition()}getRect(t,e){const i=this.parentScale,[s,n]=this.pageDimensions,[a,r]=this.pageTranslation,o=t/i,l=e/i,h=this.x*s,c=this.y*n,d=this.width*s,u=this.height*n;switch(this.rotation){case 0:return[h+o+a,n-c-l-u+r,h+o+d+a,n-c-l+r];case 90:return[h+l+a,n-c+o+r,h+l+u+a,n-c+o+d+r];case 180:return[h-o-d+a,n-c+l+r,h-o+a,n-c+l+u+r];case 270:return[h-l-u+a,n-c-o-d+r,h-l+a,n-c-o+r];default:throw new Error("Invalid rotation")}}getRectInCurrentCoords(t,e){const[i,s,n,a]=t,r=n-i,o=a-s;switch(this.rotation){case 0:return[i,e-a,r,o];case 90:return[i,e-s,o,r];case 180:return[n,e-s,r,o];case 270:return[n,e-a,o,r];default:throw new Error("Invalid rotation")}}onceAdded(){}isEmpty(){return!1}enableEditMode(){this.#D=!0}disableEditMode(){this.#D=!1}isInEditMode(){return this.#D}shouldGetKeyboardEvents(){return!1}needsToBeRebuilt(){return this.div&&!this.isAttachedToDOM}rebuild(){this.div?.addEventListener("focusin",this.#M);this.div?.addEventListener("focusout",this.#k)}serialize(t=!1,e=null){(0,n.unreachable)("An editor must be serializable")}static deserialize(t,e,i){const s=new this.prototype.constructor({parent:e,id:e.getNextId(),uiManager:i});s.rotation=t.rotation;const[n,a]=s.pageDimensions,[r,o,l,h]=s.getRectInCurrentCoords(t.rect,a);s.x=r/n;s.y=o/a;s.width=l/n;s.height=h/a;return s}remove(){this.div.removeEventListener("focusin",this.#M);this.div.removeEventListener("focusout",this.#k);this.isEmpty()||this.commit();this.parent?this.parent.remove(this):this._uiManager.removeEditor(this);this.#x?.remove();this.#x=null;this.#w=null}get isResizable(){return!1}makeResizable(){if(this.isResizable){this.#j();this.#P.classList.remove("hidden")}}select(){this.makeResizable();this.div?.classList.add("selectedEditor")}unselect(){this.#P?.classList.add("hidden");this.div?.classList.remove("selectedEditor");this.div?.contains(document.activeElement)&&this._uiManager.currentLayer.div.focus()}updateParams(t,e){}disableEditing(){this.#x&&(this.#x.hidden=!0)}enableEditing(){this.#x&&(this.#x.hidden=!1)}enterInEditMode(){}get contentDiv(){return this.div}get isEditing(){return this.#R}set isEditing(t){this.#R=t;if(this.parent)if(t){this.parent.setSelected(this);this.parent.setActiveEditor(this)}else this.parent.setActiveEditor(null)}setAspectRatio(t,e){this.#T=!0;const i=t/e,{style:s}=this.div;s.aspectRatio=i;s.height="auto"}static get MIN_SIZE(){return 16}}e.AnnotationEditor=AnnotationEditor;class FakeEditor extends AnnotationEditor{constructor(t){super(t);this.annotationElementId=t.annotationElementId;this.deleted=!0}serialize(){return{id:this.annotationElementId,deleted:!0,pageIndex:this.pageIndex}}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.KeyboardManager=e.CommandManager=e.ColorManager=e.AnnotationEditorUIManager=void 0;e.bindEvents=function bindEvents(t,e,i){for(const s of i)e.addEventListener(s,t[s].bind(t))};e.opacityToHex=function opacityToHex(t){return Math.round(Math.min(255,Math.max(1,255*t))).toString(16).padStart(2,"0")};var s=i(1),n=i(6);class IdManager{#q=0;getId(){return`${s.AnnotationEditorPrefix}${this.#q++}`}}class ImageManager{#V=(0,s.getUuid)();#q=0;#$=null;static get _isSVGFittingCanvas(){const t=new OffscreenCanvas(1,3).getContext("2d"),e=new Image;e.src='data:image/svg+xml;charset=UTF-8,';const i=e.decode().then((()=>{t.drawImage(e,0,0,1,1,0,0,1,3);return 0===new Uint32Array(t.getImageData(0,0,1,1).data.buffer)[0]}));return(0,s.shadow)(this,"_isSVGFittingCanvas",i)}async#X(t,e){this.#$||=new Map;let i=this.#$.get(t);if(null===i)return null;if(i?.bitmap){i.refCounter+=1;return i}try{i||={bitmap:null,id:`image_${this.#V}_${this.#q++}`,refCounter:0,isSvg:!1};let t;if("string"==typeof e){i.url=e;const s=await fetch(e);if(!s.ok)throw new Error(s.statusText);t=await s.blob()}else t=i.file=e;if("image/svg+xml"===t.type){const e=ImageManager._isSVGFittingCanvas,s=new FileReader,n=new Image,a=new Promise(((t,a)=>{n.onload=()=>{i.bitmap=n;i.isSvg=!0;t()};s.onload=async()=>{const t=i.svgUrl=s.result;n.src=await e?`${t}#svgView(preserveAspectRatio(none))`:t};n.onerror=s.onerror=a}));s.readAsDataURL(t);await a}else i.bitmap=await createImageBitmap(t);i.refCounter=1}catch(t){console.error(t);i=null}this.#$.set(t,i);i&&this.#$.set(i.id,i);return i}async getFromFile(t){const{lastModified:e,name:i,size:s,type:n}=t;return this.#X(`${e}_${i}_${s}_${n}`,t)}async getFromUrl(t){return this.#X(t,t)}async getFromId(t){this.#$||=new Map;const e=this.#$.get(t);if(!e)return null;if(e.bitmap){e.refCounter+=1;return e}return e.file?this.getFromFile(e.file):this.getFromUrl(e.url)}getSvgUrl(t){const e=this.#$.get(t);return e?.isSvg?e.svgUrl:null}deleteId(t){this.#$||=new Map;const e=this.#$.get(t);if(e){e.refCounter-=1;0===e.refCounter&&(e.bitmap=null)}}isValidId(t){return t.startsWith(`image_${this.#V}_`)}}class CommandManager{#K=[];#Y=!1;#J;#Q=-1;constructor(t=128){this.#J=t}add({cmd:t,undo:e,mustExec:i,type:s=NaN,overwriteIfSameType:n=!1,keepUndo:a=!1}){i&&t();if(this.#Y)return;const r={cmd:t,undo:e,type:s};if(-1===this.#Q){this.#K.length>0&&(this.#K.length=0);this.#Q=0;this.#K.push(r);return}if(n&&this.#K[this.#Q].type===s){a&&(r.undo=this.#K[this.#Q].undo);this.#K[this.#Q]=r;return}const o=this.#Q+1;if(o===this.#J)this.#K.splice(0,1);else{this.#Q=o;ot===e[i])))return ColorManager._colorsMapping.get(t);return e}getHexCode(t){const e=this._colors.get(t);return e?s.Util.makeHexColor(...e):t}}e.ColorManager=ColorManager;class AnnotationEditorUIManager{#tt=null;#et=new Map;#it=new Map;#st=null;#nt=null;#at=new CommandManager;#rt=0;#ot=new Set;#lt=null;#ht=null;#ct=new Set;#dt=null;#ut=new IdManager;#pt=!1;#gt=!1;#mt=null;#ft=s.AnnotationEditorType.NONE;#bt=new Set;#At=null;#_t=this.blur.bind(this);#vt=this.focus.bind(this);#yt=this.copy.bind(this);#St=this.cut.bind(this);#Et=this.paste.bind(this);#xt=this.keydown.bind(this);#wt=this.onEditingAction.bind(this);#Ct=this.onPageChanging.bind(this);#Tt=this.onScaleChanging.bind(this);#Pt=this.onRotationChanging.bind(this);#Mt={isEditing:!1,isEmpty:!0,hasSomethingToUndo:!1,hasSomethingToRedo:!1,hasSelectedEditor:!1};#kt=[0,0];#Ft=null;#Rt=null;#Dt=null;static TRANSLATE_SMALL=1;static TRANSLATE_BIG=10;static get _keyboardManager(){const t=AnnotationEditorUIManager.prototype,arrowChecker=t=>{const{activeElement:e}=document;return e&&t.#Rt.contains(e)&&t.hasSomethingToControl()},e=this.TRANSLATE_SMALL,i=this.TRANSLATE_BIG;return(0,s.shadow)(this,"_keyboardManager",new KeyboardManager([[["ctrl+a","mac+meta+a"],t.selectAll],[["ctrl+z","mac+meta+z"],t.undo],[["ctrl+y","ctrl+shift+z","mac+meta+shift+z","ctrl+shift+Z","mac+meta+shift+Z"],t.redo],[["Backspace","alt+Backspace","ctrl+Backspace","shift+Backspace","mac+Backspace","mac+alt+Backspace","mac+ctrl+Backspace","Delete","ctrl+Delete","shift+Delete","mac+Delete"],t.delete],[["Escape","mac+Escape"],t.unselectAll],[["ArrowLeft","mac+ArrowLeft"],t.translateSelectedEditors,{args:[-e,0],checker:arrowChecker}],[["ctrl+ArrowLeft","mac+shift+ArrowLeft"],t.translateSelectedEditors,{args:[-i,0],checker:arrowChecker}],[["ArrowRight","mac+ArrowRight"],t.translateSelectedEditors,{args:[e,0],checker:arrowChecker}],[["ctrl+ArrowRight","mac+shift+ArrowRight"],t.translateSelectedEditors,{args:[i,0],checker:arrowChecker}],[["ArrowUp","mac+ArrowUp"],t.translateSelectedEditors,{args:[0,-e],checker:arrowChecker}],[["ctrl+ArrowUp","mac+shift+ArrowUp"],t.translateSelectedEditors,{args:[0,-i],checker:arrowChecker}],[["ArrowDown","mac+ArrowDown"],t.translateSelectedEditors,{args:[0,e],checker:arrowChecker}],[["ctrl+ArrowDown","mac+shift+ArrowDown"],t.translateSelectedEditors,{args:[0,i],checker:arrowChecker}]]))}constructor(t,e,i,s,a,r){this.#Rt=t;this.#Dt=e;this.#st=i;this._eventBus=s;this._eventBus._on("editingaction",this.#wt);this._eventBus._on("pagechanging",this.#Ct);this._eventBus._on("scalechanging",this.#Tt);this._eventBus._on("rotationchanging",this.#Pt);this.#nt=a.annotationStorage;this.#dt=a.filterFactory;this.#At=r;this.viewParameters={realScale:n.PixelsPerInch.PDF_TO_CSS_UNITS,rotation:0}}destroy(){this.#It();this.#Lt();this._eventBus._off("editingaction",this.#wt);this._eventBus._off("pagechanging",this.#Ct);this._eventBus._off("scalechanging",this.#Tt);this._eventBus._off("rotationchanging",this.#Pt);for(const t of this.#it.values())t.destroy();this.#it.clear();this.#et.clear();this.#ct.clear();this.#tt=null;this.#bt.clear();this.#at.destroy();this.#st.destroy()}get hcmFilter(){return(0,s.shadow)(this,"hcmFilter",this.#At?this.#dt.addHCMFilter(this.#At.foreground,this.#At.background):"none")}get direction(){return(0,s.shadow)(this,"direction",getComputedStyle(this.#Rt).direction)}editAltText(t){this.#st?.editAltText(this,t)}onPageChanging({pageNumber:t}){this.#rt=t-1}focusMainContainer(){this.#Rt.focus()}findParent(t,e){for(const i of this.#it.values()){const{x:s,y:n,width:a,height:r}=i.div.getBoundingClientRect();if(t>=s&&t<=s+a&&e>=n&&e<=n+r)return i}return null}disableUserSelect(t=!1){this.#Dt.classList.toggle("noUserSelect",t)}addShouldRescale(t){this.#ct.add(t)}removeShouldRescale(t){this.#ct.delete(t)}onScaleChanging({scale:t}){this.commitOrRemove();this.viewParameters.realScale=t*n.PixelsPerInch.PDF_TO_CSS_UNITS;for(const t of this.#ct)t.onScaleChanging()}onRotationChanging({pagesRotation:t}){this.commitOrRemove();this.viewParameters.rotation=t}addToAnnotationStorage(t){t.isEmpty()||!this.#nt||this.#nt.has(t.id)||this.#nt.setValue(t.id,t)}#Ot(){window.addEventListener("focus",this.#vt);window.addEventListener("blur",this.#_t)}#Lt(){window.removeEventListener("focus",this.#vt);window.removeEventListener("blur",this.#_t)}blur(){if(!this.hasSelection)return;const{activeElement:t}=document;for(const e of this.#bt)if(e.div.contains(t)){this.#mt=[e,t];e._focusEventsAllowed=!1;break}}focus(){if(!this.#mt)return;const[t,e]=this.#mt;this.#mt=null;e.addEventListener("focusin",(()=>{t._focusEventsAllowed=!0}),{once:!0});e.focus()}#Nt(){window.addEventListener("keydown",this.#xt,{capture:!0})}#It(){window.removeEventListener("keydown",this.#xt,{capture:!0})}#Bt(){document.addEventListener("copy",this.#yt);document.addEventListener("cut",this.#St);document.addEventListener("paste",this.#Et)}#Ut(){document.removeEventListener("copy",this.#yt);document.removeEventListener("cut",this.#St);document.removeEventListener("paste",this.#Et)}addEditListeners(){this.#Nt();this.#Bt()}removeEditListeners(){this.#It();this.#Ut()}copy(t){t.preventDefault();this.#tt?.commitOrRemove();if(!this.hasSelection)return;const e=[];for(const t of this.#bt){const i=t.serialize(!0);i&&e.push(i)}0!==e.length&&t.clipboardData.setData("application/pdfjs",JSON.stringify(e))}cut(t){this.copy(t);this.delete()}paste(t){t.preventDefault();const{clipboardData:e}=t;for(const t of e.items)for(const e of this.#ht)if(e.isHandlingMimeForPasting(t.type)){e.paste(t,this.currentLayer);return}let i=e.getData("application/pdfjs");if(!i)return;try{i=JSON.parse(i)}catch(t){(0,s.warn)(`paste: "${t.message}".`);return}if(!Array.isArray(i))return;this.unselectAll();const n=this.currentLayer;try{const t=[];for(const e of i){const i=n.deserialize(e);if(!i)return;t.push(i)}const cmd=()=>{for(const e of t)this.#jt(e);this.#zt(t)},undo=()=>{for(const e of t)e.remove()};this.addCommands({cmd:cmd,undo:undo,mustExec:!0})}catch(t){(0,s.warn)(`paste: "${t.message}".`)}}keydown(t){this.getActive()?.shouldGetKeyboardEvents()||AnnotationEditorUIManager._keyboardManager.exec(this,t)}onEditingAction(t){["undo","redo","delete","selectAll"].includes(t.name)&&this[t.name]()}#Ht(t){Object.entries(t).some((([t,e])=>this.#Mt[t]!==e))&&this._eventBus.dispatch("annotationeditorstateschanged",{source:this,details:Object.assign(this.#Mt,t)})}#Wt(t){this._eventBus.dispatch("annotationeditorparamschanged",{source:this,details:t})}setEditingState(t){if(t){this.#Ot();this.#Nt();this.#Bt();this.#Ht({isEditing:this.#ft!==s.AnnotationEditorType.NONE,isEmpty:this.#Gt(),hasSomethingToUndo:this.#at.hasSomethingToUndo(),hasSomethingToRedo:this.#at.hasSomethingToRedo(),hasSelectedEditor:!1})}else{this.#Lt();this.#It();this.#Ut();this.#Ht({isEditing:!1});this.disableUserSelect(!1)}}registerEditorTypes(t){if(!this.#ht){this.#ht=t;for(const t of this.#ht)this.#Wt(t.defaultPropertiesToUpdate)}}getId(){return this.#ut.getId()}get currentLayer(){return this.#it.get(this.#rt)}getLayer(t){return this.#it.get(t)}get currentPageIndex(){return this.#rt}addLayer(t){this.#it.set(t.pageIndex,t);this.#pt?t.enable():t.disable()}removeLayer(t){this.#it.delete(t.pageIndex)}updateMode(t,e=null){if(this.#ft!==t){this.#ft=t;if(t!==s.AnnotationEditorType.NONE){this.setEditingState(!0);this.#qt();this.unselectAll();for(const e of this.#it.values())e.updateMode(t);if(e)for(const t of this.#et.values())if(t.annotationElementId===e){this.setSelected(t);t.enterInEditMode();break}}else{this.setEditingState(!1);this.#Vt()}}}updateToolbar(t){t!==this.#ft&&this._eventBus.dispatch("switchannotationeditormode",{source:this,mode:t})}updateParams(t,e){if(this.#ht)if(t!==s.AnnotationEditorParamsType.CREATE){for(const i of this.#bt)i.updateParams(t,e);for(const i of this.#ht)i.updateDefaultParams(t,e)}else this.currentLayer.addNewEditor(t)}enableWaiting(t=!1){if(this.#gt!==t){this.#gt=t;for(const e of this.#it.values()){t?e.disableClick():e.enableClick();e.div.classList.toggle("waiting",t)}}}#qt(){if(!this.#pt){this.#pt=!0;for(const t of this.#it.values())t.enable()}}#Vt(){this.unselectAll();if(this.#pt){this.#pt=!1;for(const t of this.#it.values())t.disable()}}getEditors(t){const e=[];for(const i of this.#et.values())i.pageIndex===t&&e.push(i);return e}getEditor(t){return this.#et.get(t)}addEditor(t){this.#et.set(t.id,t)}removeEditor(t){this.#et.delete(t.id);this.unselect(t);t.annotationElementId&&this.#ot.has(t.annotationElementId)||this.#nt?.remove(t.id)}addDeletedAnnotationElement(t){this.#ot.add(t.annotationElementId);t.deleted=!0}isDeletedAnnotationElement(t){return this.#ot.has(t)}removeDeletedAnnotationElement(t){this.#ot.delete(t.annotationElementId);t.deleted=!1}#jt(t){const e=this.#it.get(t.pageIndex);e?e.addOrRebuild(t):this.addEditor(t)}setActiveEditor(t){if(this.#tt!==t){this.#tt=t;t&&this.#Wt(t.propertiesToUpdate)}}toggleSelected(t){if(this.#bt.has(t)){this.#bt.delete(t);t.unselect();this.#Ht({hasSelectedEditor:this.hasSelection})}else{this.#bt.add(t);t.select();this.#Wt(t.propertiesToUpdate);this.#Ht({hasSelectedEditor:!0})}}setSelected(t){for(const e of this.#bt)e!==t&&e.unselect();this.#bt.clear();this.#bt.add(t);t.select();this.#Wt(t.propertiesToUpdate);this.#Ht({hasSelectedEditor:!0})}isSelected(t){return this.#bt.has(t)}unselect(t){t.unselect();this.#bt.delete(t);this.#Ht({hasSelectedEditor:this.hasSelection})}get hasSelection(){return 0!==this.#bt.size}undo(){this.#at.undo();this.#Ht({hasSomethingToUndo:this.#at.hasSomethingToUndo(),hasSomethingToRedo:!0,isEmpty:this.#Gt()})}redo(){this.#at.redo();this.#Ht({hasSomethingToUndo:!0,hasSomethingToRedo:this.#at.hasSomethingToRedo(),isEmpty:this.#Gt()})}addCommands(t){this.#at.add(t);this.#Ht({hasSomethingToUndo:!0,hasSomethingToRedo:!1,isEmpty:this.#Gt()})}#Gt(){if(0===this.#et.size)return!0;if(1===this.#et.size)for(const t of this.#et.values())return t.isEmpty();return!1}delete(){this.commitOrRemove();if(!this.hasSelection)return;const t=[...this.#bt];this.addCommands({cmd:()=>{for(const e of t)e.remove()},undo:()=>{for(const e of t)this.#jt(e)},mustExec:!0})}commitOrRemove(){this.#tt?.commitOrRemove()}hasSomethingToControl(){return this.#tt||this.hasSelection}#zt(t){this.#bt.clear();for(const e of t)if(!e.isEmpty()){this.#bt.add(e);e.select()}this.#Ht({hasSelectedEditor:!0})}selectAll(){for(const t of this.#bt)t.commit();this.#zt(this.#et.values())}unselectAll(){if(this.#tt)this.#tt.commitOrRemove();else if(this.hasSelection){for(const t of this.#bt)t.unselect();this.#bt.clear();this.#Ht({hasSelectedEditor:!1})}}translateSelectedEditors(t,e,i=!1){i||this.commitOrRemove();if(!this.hasSelection)return;this.#kt[0]+=t;this.#kt[1]+=e;const[s,n]=this.#kt,a=[...this.#bt];this.#Ft&&clearTimeout(this.#Ft);this.#Ft=setTimeout((()=>{this.#Ft=null;this.#kt[0]=this.#kt[1]=0;this.addCommands({cmd:()=>{for(const t of a)this.#et.has(t.id)&&t.translateInPage(s,n)},undo:()=>{for(const t of a)this.#et.has(t.id)&&t.translateInPage(-s,-n)},mustExec:!1})}),1e3);for(const i of a)i.translateInPage(t,e)}setUpDragSession(){if(this.hasSelection){this.disableUserSelect(!0);this.#lt=new Map;for(const t of this.#bt)this.#lt.set(t,{savedX:t.x,savedY:t.y,savedPageIndex:t.pageIndex,newX:0,newY:0,newPageIndex:-1})}}endDragSession(){if(!this.#lt)return!1;this.disableUserSelect(!1);const t=this.#lt;this.#lt=null;let e=!1;for(const[{x:i,y:s,pageIndex:n},a]of t){a.newX=i;a.newY=s;a.newPageIndex=n;e||=i!==a.savedX||s!==a.savedY||n!==a.savedPageIndex}if(!e)return!1;const move=(t,e,i,s)=>{if(this.#et.has(t.id)){const n=this.#it.get(s);if(n)t._setParentAndPosition(n,e,i);else{t.pageIndex=s;t.x=e;t.y=i}}};this.addCommands({cmd:()=>{for(const[e,{newX:i,newY:s,newPageIndex:n}]of t)move(e,i,s,n)},undo:()=>{for(const[e,{savedX:i,savedY:s,savedPageIndex:n}]of t)move(e,i,s,n)},mustExec:!0});return!0}dragSelectedEditors(t,e){if(this.#lt)for(const i of this.#lt.keys())i.drag(t,e)}rebuild(t){if(null===t.parent){const e=this.getLayer(t.pageIndex);if(e){e.changeParent(t);e.addOrRebuild(t)}else{this.addEditor(t);this.addToAnnotationStorage(t);t.rebuild()}}else t.parent.addOrRebuild(t)}isActive(t){return this.#tt===t}getActive(){return this.#tt}getMode(){return this.#ft}get imageManager(){return(0,s.shadow)(this,"imageManager",new ImageManager)}}e.AnnotationEditorUIManager=AnnotationEditorUIManager},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.StatTimer=e.RenderingCancelledException=e.PixelsPerInch=e.PageViewport=e.PDFDateString=e.DOMStandardFontDataFactory=e.DOMSVGFactory=e.DOMFilterFactory=e.DOMCanvasFactory=e.DOMCMapReaderFactory=void 0;e.deprecated=function deprecated(t){console.log("Deprecated API usage: "+t)};e.getColorValues=function getColorValues(t){const e=document.createElement("span");e.style.visibility="hidden";document.body.append(e);for(const i of t.keys()){e.style.color=i;const s=window.getComputedStyle(e).color;t.set(i,getRGB(s))}e.remove()};e.getCurrentTransform=function getCurrentTransform(t){const{a:e,b:i,c:s,d:n,e:a,f:r}=t.getTransform();return[e,i,s,n,a,r]};e.getCurrentTransformInverse=function getCurrentTransformInverse(t){const{a:e,b:i,c:s,d:n,e:a,f:r}=t.getTransform().invertSelf();return[e,i,s,n,a,r]};e.getFilenameFromUrl=function getFilenameFromUrl(t,e=!1){e||([t]=t.split(/[#?]/,1));return t.substring(t.lastIndexOf("/")+1)};e.getPdfFilenameFromUrl=function getPdfFilenameFromUrl(t,e="document.pdf"){if("string"!=typeof t)return e;if(isDataScheme(t)){(0,n.warn)('getPdfFilenameFromUrl: ignore "data:"-URL for performance reasons.');return e}const i=/[^/?#=]+\.pdf\b(?!.*\.pdf\b)/i,s=/^(?:(?:[^:]+:)?\/\/[^/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/.exec(t);let a=i.exec(s[1])||i.exec(s[2])||i.exec(s[3]);if(a){a=a[0];if(a.includes("%"))try{a=i.exec(decodeURIComponent(a))[0]}catch{}}return a||e};e.getRGB=getRGB;e.getXfaPageViewport=function getXfaPageViewport(t,{scale:e=1,rotation:i=0}){const{width:s,height:n}=t.attributes.style,a=[0,0,parseInt(s),parseInt(n)];return new PageViewport({viewBox:a,scale:e,rotation:i})};e.isDataScheme=isDataScheme;e.isPdfFile=function isPdfFile(t){return"string"==typeof t&&/\.pdf$/i.test(t)};e.isValidFetchUrl=isValidFetchUrl;e.loadScript=function loadScript(t,e=!1){return new Promise(((i,s)=>{const n=document.createElement("script");n.src=t;n.onload=function(t){e&&n.remove();i(t)};n.onerror=function(){s(new Error(`Cannot load script at: ${n.src}`))};(document.head||document.documentElement).append(n)}))};e.noContextMenu=function noContextMenu(t){t.preventDefault()};e.setLayerDimensions=function setLayerDimensions(t,e,i=!1,s=!0){if(e instanceof PageViewport){const{pageWidth:s,pageHeight:a}=e.rawDims,{style:r}=t,o=n.FeatureTest.isCSSRoundSupported,l=`var(--scale-factor) * ${s}px`,h=`var(--scale-factor) * ${a}px`,c=o?`round(${l}, 1px)`:`calc(${l})`,d=o?`round(${h}, 1px)`:`calc(${h})`;if(i&&e.rotation%180!=0){r.width=d;r.height=c}else{r.width=c;r.height=d}}s&&t.setAttribute("data-main-rotation",e.rotation)};var s=i(7),n=i(1);const a="http://www.w3.org/2000/svg";class PixelsPerInch{static CSS=96;static PDF=72;static PDF_TO_CSS_UNITS=this.CSS/this.PDF}e.PixelsPerInch=PixelsPerInch;class DOMFilterFactory extends s.BaseFilterFactory{#$t;#Xt;#e;#Kt;#Yt;#Jt;#Qt;#Zt;#te;#ee;#q=0;constructor({docId:t,ownerDocument:e=globalThis.document}={}){super();this.#e=t;this.#Kt=e}get#$(){return this.#$t||=new Map}get#ie(){if(!this.#Xt){const t=this.#Kt.createElement("div"),{style:e}=t;e.visibility="hidden";e.contain="strict";e.width=e.height=0;e.position="absolute";e.top=e.left=0;e.zIndex=-1;const i=this.#Kt.createElementNS(a,"svg");i.setAttribute("width",0);i.setAttribute("height",0);this.#Xt=this.#Kt.createElementNS(a,"defs");t.append(i);i.append(this.#Xt);this.#Kt.body.append(t)}return this.#Xt}addFilter(t){if(!t)return"none";let e,i,s,n,a=this.#$.get(t);if(a)return a;if(1===t.length){const a=t[0],r=new Array(256);for(let t=0;t<256;t++)r[t]=a[t]/255;n=e=i=s=r.join(",")}else{const[a,r,o]=t,l=new Array(256),h=new Array(256),c=new Array(256);for(let t=0;t<256;t++){l[t]=a[t]/255;h[t]=r[t]/255;c[t]=o[t]/255}e=l.join(",");i=h.join(",");s=c.join(",");n=`${e}${i}${s}`}a=this.#$.get(n);if(a){this.#$.set(t,a);return a}const r=`g_${this.#e}_transfer_map_${this.#q++}`,o=`url(#${r})`;this.#$.set(t,o);this.#$.set(n,o);const l=this.#se(r);this.#ne(e,i,s,l);return o}addHCMFilter(t,e){const i=`${t}-${e}`;if(this.#Jt===i)return this.#Qt;this.#Jt=i;this.#Qt="none";this.#Yt?.remove();if(!t||!e)return this.#Qt;const s=this.#ae(t);t=n.Util.makeHexColor(...s);const a=this.#ae(e);e=n.Util.makeHexColor(...a);this.#ie.style.color="";if("#000000"===t&&"#ffffff"===e||t===e)return this.#Qt;const r=new Array(256);for(let t=0;t<=255;t++){const e=t/255;r[t]=e<=.03928?e/12.92:((e+.055)/1.055)**2.4}const o=r.join(","),l=`g_${this.#e}_hcm_filter`,h=this.#Zt=this.#se(l);this.#ne(o,o,o,h);this.#re(h);const getSteps=(t,e)=>{const i=s[t]/255,n=a[t]/255,r=new Array(e+1);for(let t=0;t<=e;t++)r[t]=i+t/e*(n-i);return r.join(",")};this.#ne(getSteps(0,5),getSteps(1,5),getSteps(2,5),h);this.#Qt=`url(#${l})`;return this.#Qt}addHighlightHCMFilter(t,e,i,s){const n=`${t}-${e}-${i}-${s}`;if(this.#te===n)return this.#ee;this.#te=n;this.#ee="none";this.#Zt?.remove();if(!t||!e)return this.#ee;const[a,r]=[t,e].map(this.#ae.bind(this));let o=Math.round(.2126*a[0]+.7152*a[1]+.0722*a[2]),l=Math.round(.2126*r[0]+.7152*r[1]+.0722*r[2]),[h,c]=[i,s].map(this.#ae.bind(this));l{const s=new Array(256),n=(l-o)/i,a=t/255,r=(e-t)/(255*i);let h=0;for(let t=0;t<=i;t++){const e=Math.round(o+t*n),i=a+t*r;for(let t=h;t<=e;t++)s[t]=i;h=e+1}for(let t=h;t<256;t++)s[t]=s[h-1];return s.join(",")},d=`g_${this.#e}_hcm_highlight_filter`,u=this.#Zt=this.#se(d);this.#re(u);this.#ne(getSteps(h[0],c[0],5),getSteps(h[1],c[1],5),getSteps(h[2],c[2],5),u);this.#ee=`url(#${d})`;return this.#ee}destroy(t=!1){if(!t||!this.#Qt&&!this.#ee){if(this.#Xt){this.#Xt.parentNode.parentNode.remove();this.#Xt=null}if(this.#$t){this.#$t.clear();this.#$t=null}this.#q=0}}#re(t){const e=this.#Kt.createElementNS(a,"feColorMatrix");e.setAttribute("type","matrix");e.setAttribute("values","0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0 0 0 1 0");t.append(e)}#se(t){const e=this.#Kt.createElementNS(a,"filter");e.setAttribute("color-interpolation-filters","sRGB");e.setAttribute("id",t);this.#ie.append(e);return e}#oe(t,e,i){const s=this.#Kt.createElementNS(a,e);s.setAttribute("type","discrete");s.setAttribute("tableValues",i);t.append(s)}#ne(t,e,i,s){const n=this.#Kt.createElementNS(a,"feComponentTransfer");s.append(n);this.#oe(n,"feFuncR",t);this.#oe(n,"feFuncG",e);this.#oe(n,"feFuncB",i)}#ae(t){this.#ie.style.color=t;return getRGB(getComputedStyle(this.#ie).getPropertyValue("color"))}}e.DOMFilterFactory=DOMFilterFactory;class DOMCanvasFactory extends s.BaseCanvasFactory{constructor({ownerDocument:t=globalThis.document}={}){super();this._document=t}_createCanvas(t,e){const i=this._document.createElement("canvas");i.width=t;i.height=e;return i}}e.DOMCanvasFactory=DOMCanvasFactory;async function fetchData(t,e=!1){if(isValidFetchUrl(t,document.baseURI)){const i=await fetch(t);if(!i.ok)throw new Error(i.statusText);return e?new Uint8Array(await i.arrayBuffer()):(0,n.stringToBytes)(await i.text())}return new Promise(((i,s)=>{const a=new XMLHttpRequest;a.open("GET",t,!0);e&&(a.responseType="arraybuffer");a.onreadystatechange=()=>{if(a.readyState===XMLHttpRequest.DONE){if(200===a.status||0===a.status){let t;e&&a.response?t=new Uint8Array(a.response):!e&&a.responseText&&(t=(0,n.stringToBytes)(a.responseText));if(t){i(t);return}}s(new Error(a.statusText))}};a.send(null)}))}class DOMCMapReaderFactory extends s.BaseCMapReaderFactory{_fetchData(t,e){return fetchData(t,this.isCompressed).then((t=>({cMapData:t,compressionType:e})))}}e.DOMCMapReaderFactory=DOMCMapReaderFactory;class DOMStandardFontDataFactory extends s.BaseStandardFontDataFactory{_fetchData(t){return fetchData(t,!0)}}e.DOMStandardFontDataFactory=DOMStandardFontDataFactory;class DOMSVGFactory extends s.BaseSVGFactory{_createSVG(t){return document.createElementNS(a,t)}}e.DOMSVGFactory=DOMSVGFactory;class PageViewport{constructor({viewBox:t,scale:e,rotation:i,offsetX:s=0,offsetY:n=0,dontFlip:a=!1}){this.viewBox=t;this.scale=e;this.rotation=i;this.offsetX=s;this.offsetY=n;const r=(t[2]+t[0])/2,o=(t[3]+t[1])/2;let l,h,c,d,u,p,g,m;(i%=360)<0&&(i+=360);switch(i){case 180:l=-1;h=0;c=0;d=1;break;case 90:l=0;h=1;c=1;d=0;break;case 270:l=0;h=-1;c=-1;d=0;break;case 0:l=1;h=0;c=0;d=-1;break;default:throw new Error("PageViewport: Invalid rotation, must be a multiple of 90 degrees.")}if(a){c=-c;d=-d}if(0===l){u=Math.abs(o-t[1])*e+s;p=Math.abs(r-t[0])*e+n;g=(t[3]-t[1])*e;m=(t[2]-t[0])*e}else{u=Math.abs(r-t[0])*e+s;p=Math.abs(o-t[1])*e+n;g=(t[2]-t[0])*e;m=(t[3]-t[1])*e}this.transform=[l*e,h*e,c*e,d*e,u-l*e*r-c*e*o,p-h*e*r-d*e*o];this.width=g;this.height=m}get rawDims(){const{viewBox:t}=this;return(0,n.shadow)(this,"rawDims",{pageWidth:t[2]-t[0],pageHeight:t[3]-t[1],pageX:t[0],pageY:t[1]})}clone({scale:t=this.scale,rotation:e=this.rotation,offsetX:i=this.offsetX,offsetY:s=this.offsetY,dontFlip:n=!1}={}){return new PageViewport({viewBox:this.viewBox.slice(),scale:t,rotation:e,offsetX:i,offsetY:s,dontFlip:n})}convertToViewportPoint(t,e){return n.Util.applyTransform([t,e],this.transform)}convertToViewportRectangle(t){const e=n.Util.applyTransform([t[0],t[1]],this.transform),i=n.Util.applyTransform([t[2],t[3]],this.transform);return[e[0],e[1],i[0],i[1]]}convertToPdfPoint(t,e){return n.Util.applyInverseTransform([t,e],this.transform)}}e.PageViewport=PageViewport;class RenderingCancelledException extends n.BaseException{constructor(t,e=0){super(t,"RenderingCancelledException");this.extraDelay=e}}e.RenderingCancelledException=RenderingCancelledException;function isDataScheme(t){const e=t.length;let i=0;for(;i=1&&s<=12?s-1:0;let n=parseInt(e[3],10);n=n>=1&&n<=31?n:1;let a=parseInt(e[4],10);a=a>=0&&a<=23?a:0;let o=parseInt(e[5],10);o=o>=0&&o<=59?o:0;let l=parseInt(e[6],10);l=l>=0&&l<=59?l:0;const h=e[7]||"Z";let c=parseInt(e[8],10);c=c>=0&&c<=23?c:0;let d=parseInt(e[9],10)||0;d=d>=0&&d<=59?d:0;if("-"===h){a+=c;o+=d}else if("+"===h){a-=c;o-=d}return new Date(Date.UTC(i,s,n,a,o,l))}};function getRGB(t){if(t.startsWith("#")){const e=parseInt(t.slice(1),16);return[(16711680&e)>>16,(65280&e)>>8,255&e]}if(t.startsWith("rgb("))return t.slice(4,-1).split(",").map((t=>parseInt(t)));if(t.startsWith("rgba("))return t.slice(5,-1).split(",").map((t=>parseInt(t))).slice(0,3);(0,n.warn)(`Not a valid color format: "${t}"`);return[0,0,0]}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.BaseStandardFontDataFactory=e.BaseSVGFactory=e.BaseFilterFactory=e.BaseCanvasFactory=e.BaseCMapReaderFactory=void 0;var s=i(1);class BaseFilterFactory{constructor(){this.constructor===BaseFilterFactory&&(0,s.unreachable)("Cannot initialize BaseFilterFactory.")}addFilter(t){return"none"}addHCMFilter(t,e){return"none"}addHighlightHCMFilter(t,e,i,s){return"none"}destroy(t=!1){}}e.BaseFilterFactory=BaseFilterFactory;class BaseCanvasFactory{constructor(){this.constructor===BaseCanvasFactory&&(0,s.unreachable)("Cannot initialize BaseCanvasFactory.")}create(t,e){if(t<=0||e<=0)throw new Error("Invalid canvas size");const i=this._createCanvas(t,e);return{canvas:i,context:i.getContext("2d")}}reset(t,e,i){if(!t.canvas)throw new Error("Canvas is not specified");if(e<=0||i<=0)throw new Error("Invalid canvas size");t.canvas.width=e;t.canvas.height=i}destroy(t){if(!t.canvas)throw new Error("Canvas is not specified");t.canvas.width=0;t.canvas.height=0;t.canvas=null;t.context=null}_createCanvas(t,e){(0,s.unreachable)("Abstract method `_createCanvas` called.")}}e.BaseCanvasFactory=BaseCanvasFactory;class BaseCMapReaderFactory{constructor({baseUrl:t=null,isCompressed:e=!0}){this.constructor===BaseCMapReaderFactory&&(0,s.unreachable)("Cannot initialize BaseCMapReaderFactory.");this.baseUrl=t;this.isCompressed=e}async fetch({name:t}){if(!this.baseUrl)throw new Error('The CMap "baseUrl" parameter must be specified, ensure that the "cMapUrl" and "cMapPacked" API parameters are provided.');if(!t)throw new Error("CMap name must be specified.");const e=this.baseUrl+t+(this.isCompressed?".bcmap":""),i=this.isCompressed?s.CMapCompressionType.BINARY:s.CMapCompressionType.NONE;return this._fetchData(e,i).catch((t=>{throw new Error(`Unable to load ${this.isCompressed?"binary ":""}CMap at: ${e}`)}))}_fetchData(t,e){(0,s.unreachable)("Abstract method `_fetchData` called.")}}e.BaseCMapReaderFactory=BaseCMapReaderFactory;class BaseStandardFontDataFactory{constructor({baseUrl:t=null}){this.constructor===BaseStandardFontDataFactory&&(0,s.unreachable)("Cannot initialize BaseStandardFontDataFactory.");this.baseUrl=t}async fetch({filename:t}){if(!this.baseUrl)throw new Error('The standard font "baseUrl" parameter must be specified, ensure that the "standardFontDataUrl" API parameter is provided.');if(!t)throw new Error("Font filename must be specified.");const e=`${this.baseUrl}${t}`;return this._fetchData(e).catch((t=>{throw new Error(`Unable to load font data at: ${e}`)}))}_fetchData(t){(0,s.unreachable)("Abstract method `_fetchData` called.")}}e.BaseStandardFontDataFactory=BaseStandardFontDataFactory;class BaseSVGFactory{constructor(){this.constructor===BaseSVGFactory&&(0,s.unreachable)("Cannot initialize BaseSVGFactory.")}create(t,e,i=!1){if(t<=0||e<=0)throw new Error("Invalid SVG dimensions");const s=this._createSVG("svg:svg");s.setAttribute("version","1.1");if(!i){s.setAttribute("width",`${t}px`);s.setAttribute("height",`${e}px`)}s.setAttribute("preserveAspectRatio","none");s.setAttribute("viewBox",`0 0 ${t} ${e}`);return s}createElement(t){if("string"!=typeof t)throw new Error("Invalid SVG element type");return this._createSVG(t)}_createSVG(t){(0,s.unreachable)("Abstract method `_createSVG` called.")}}e.BaseSVGFactory=BaseSVGFactory},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.MurmurHash3_64=void 0;var s=i(1);const n=3285377520,a=4294901760,r=65535;e.MurmurHash3_64=class MurmurHash3_64{constructor(t){this.h1=t?4294967295&t:n;this.h2=t?4294967295&t:n}update(t){let e,i;if("string"==typeof t){e=new Uint8Array(2*t.length);i=0;for(let s=0,n=t.length;s>>8;e[i++]=255&n}}}else{if(!(0,s.isArrayBuffer)(t))throw new Error("Wrong data format in MurmurHash3_64_update. Input must be a string or array.");e=t.slice();i=e.byteLength}const n=i>>2,o=i-4*n,l=new Uint32Array(e.buffer,0,n);let h=0,c=0,d=this.h1,u=this.h2;const p=3432918353,g=461845907,m=11601,f=13715;for(let t=0;t>>17;h=h*g&a|h*f&r;d^=h;d=d<<13|d>>>19;d=5*d+3864292196}else{c=l[t];c=c*p&a|c*m&r;c=c<<15|c>>>17;c=c*g&a|c*f&r;u^=c;u=u<<13|u>>>19;u=5*u+3864292196}h=0;switch(o){case 3:h^=e[4*n+2]<<16;case 2:h^=e[4*n+1]<<8;case 1:h^=e[4*n];h=h*p&a|h*m&r;h=h<<15|h>>>17;h=h*g&a|h*f&r;1&n?d^=h:u^=h}this.h1=d;this.h2=u}hexdigest(){let t=this.h1,e=this.h2;t^=e>>>1;t=3981806797*t&a|36045*t&r;e=4283543511*e&a|(2950163797*(e<<16|t>>>16)&a)>>>16;t^=e>>>1;t=444984403*t&a|60499*t&r;e=3301882366*e&a|(3120437893*(e<<16|t>>>16)&a)>>>16;t^=e>>>1;return(t>>>0).toString(16).padStart(8,"0")+(e>>>0).toString(16).padStart(8,"0")}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.FontLoader=e.FontFaceObject=void 0;var s=i(1);e.FontLoader=class FontLoader{#le=new Set;constructor({ownerDocument:t=globalThis.document,styleElement:e=null}){this._document=t;this.nativeFontFaces=new Set;this.styleElement=null;this.loadingRequests=[];this.loadTestFontId=0}addNativeFontFace(t){this.nativeFontFaces.add(t);this._document.fonts.add(t)}removeNativeFontFace(t){this.nativeFontFaces.delete(t);this._document.fonts.delete(t)}insertRule(t){if(!this.styleElement){this.styleElement=this._document.createElement("style");this._document.documentElement.getElementsByTagName("head")[0].append(this.styleElement)}const e=this.styleElement.sheet;e.insertRule(t,e.cssRules.length)}clear(){for(const t of this.nativeFontFaces)this._document.fonts.delete(t);this.nativeFontFaces.clear();this.#le.clear();if(this.styleElement){this.styleElement.remove();this.styleElement=null}}async loadSystemFont(t){if(t&&!this.#le.has(t.loadedName)){(0,s.assert)(!this.disableFontFace,"loadSystemFont shouldn't be called when `disableFontFace` is set.");if(this.isFontLoadingAPISupported){const{loadedName:e,src:i,style:n}=t,a=new FontFace(e,i,n);this.addNativeFontFace(a);try{await a.load();this.#le.add(e)}catch{(0,s.warn)(`Cannot load system font: ${t.baseFontName}, installing it could help to improve PDF rendering.`);this.removeNativeFontFace(a)}}else(0,s.unreachable)("Not implemented: loadSystemFont without the Font Loading API.")}}async bind(t){if(t.attached||t.missingFile&&!t.systemFontInfo)return;t.attached=!0;if(t.systemFontInfo){await this.loadSystemFont(t.systemFontInfo);return}if(this.isFontLoadingAPISupported){const e=t.createNativeFontFace();if(e){this.addNativeFontFace(e);try{await e.loaded}catch(i){(0,s.warn)(`Failed to load font '${e.family}': '${i}'.`);t.disableFontFace=!0;throw i}}return}const e=t.createFontFaceRule();if(e){this.insertRule(e);if(this.isSyncFontLoadingSupported)return;await new Promise((e=>{const i=this._queueLoadingCallback(e);this._prepareFontLoadEvent(t,i)}))}}get isFontLoadingAPISupported(){const t=!!this._document?.fonts;return(0,s.shadow)(this,"isFontLoadingAPISupported",t)}get isSyncFontLoadingSupported(){let t=!1;(s.isNodeJS||"undefined"!=typeof navigator&&/Mozilla\/5.0.*?rv:\d+.*? Gecko/.test(navigator.userAgent))&&(t=!0);return(0,s.shadow)(this,"isSyncFontLoadingSupported",t)}_queueLoadingCallback(t){const{loadingRequests:e}=this,i={done:!1,complete:function completeRequest(){(0,s.assert)(!i.done,"completeRequest() cannot be called twice.");i.done=!0;for(;e.length>0&&e[0].done;){const t=e.shift();setTimeout(t.callback,0)}},callback:t};e.push(i);return i}get _loadTestFont(){const t=atob("T1RUTwALAIAAAwAwQ0ZGIDHtZg4AAAOYAAAAgUZGVE1lkzZwAAAEHAAAABxHREVGABQAFQAABDgAAAAeT1MvMlYNYwkAAAEgAAAAYGNtYXABDQLUAAACNAAAAUJoZWFk/xVFDQAAALwAAAA2aGhlYQdkA+oAAAD0AAAAJGhtdHgD6AAAAAAEWAAAAAZtYXhwAAJQAAAAARgAAAAGbmFtZVjmdH4AAAGAAAAAsXBvc3T/hgAzAAADeAAAACAAAQAAAAEAALZRFsRfDzz1AAsD6AAAAADOBOTLAAAAAM4KHDwAAAAAA+gDIQAAAAgAAgAAAAAAAAABAAADIQAAAFoD6AAAAAAD6AABAAAAAAAAAAAAAAAAAAAAAQAAUAAAAgAAAAQD6AH0AAUAAAKKArwAAACMAooCvAAAAeAAMQECAAACAAYJAAAAAAAAAAAAAQAAAAAAAAAAAAAAAFBmRWQAwAAuAC4DIP84AFoDIQAAAAAAAQAAAAAAAAAAACAAIAABAAAADgCuAAEAAAAAAAAAAQAAAAEAAAAAAAEAAQAAAAEAAAAAAAIAAQAAAAEAAAAAAAMAAQAAAAEAAAAAAAQAAQAAAAEAAAAAAAUAAQAAAAEAAAAAAAYAAQAAAAMAAQQJAAAAAgABAAMAAQQJAAEAAgABAAMAAQQJAAIAAgABAAMAAQQJAAMAAgABAAMAAQQJAAQAAgABAAMAAQQJAAUAAgABAAMAAQQJAAYAAgABWABYAAAAAAAAAwAAAAMAAAAcAAEAAAAAADwAAwABAAAAHAAEACAAAAAEAAQAAQAAAC7//wAAAC7////TAAEAAAAAAAABBgAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAD/gwAyAAAAAQAAAAAAAAAAAAAAAAAAAAABAAQEAAEBAQJYAAEBASH4DwD4GwHEAvgcA/gXBIwMAYuL+nz5tQXkD5j3CBLnEQACAQEBIVhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYAAABAQAADwACAQEEE/t3Dov6fAH6fAT+fPp8+nwHDosMCvm1Cvm1DAz6fBQAAAAAAAABAAAAAMmJbzEAAAAAzgTjFQAAAADOBOQpAAEAAAAAAAAADAAUAAQAAAABAAAAAgABAAAAAAAAAAAD6AAAAAAAAA==");return(0,s.shadow)(this,"_loadTestFont",t)}_prepareFontLoadEvent(t,e){function int32(t,e){return t.charCodeAt(e)<<24|t.charCodeAt(e+1)<<16|t.charCodeAt(e+2)<<8|255&t.charCodeAt(e+3)}function spliceString(t,e,i,s){return t.substring(0,e)+s+t.substring(e+i)}let i,n;const a=this._document.createElement("canvas");a.width=1;a.height=1;const r=a.getContext("2d");let o=0;const l=`lt${Date.now()}${this.loadTestFontId++}`;let h=this._loadTestFont;h=spliceString(h,976,l.length,l);const c=1482184792;let d=int32(h,16);for(i=0,n=l.length-3;i30){(0,s.warn)("Load test font never loaded.");e();return}r.font="30px "+t;r.fillText(".",0,20);r.getImageData(0,0,1,1).data[3]>0?e():setTimeout(isFontReady.bind(null,t,e))}(l,(()=>{p.remove();e.complete()}))}};e.FontFaceObject=class FontFaceObject{constructor(t,{isEvalSupported:e=!0,disableFontFace:i=!1,ignoreErrors:s=!1,inspectFont:n=null}){this.compiledGlyphs=Object.create(null);for(const e in t)this[e]=t[e];this.isEvalSupported=!1!==e;this.disableFontFace=!0===i;this.ignoreErrors=!0===s;this._inspectFont=n}createNativeFontFace(){if(!this.data||this.disableFontFace)return null;let t;if(this.cssFontInfo){const e={weight:this.cssFontInfo.fontWeight};this.cssFontInfo.italicAngle&&(e.style=`oblique ${this.cssFontInfo.italicAngle}deg`);t=new FontFace(this.cssFontInfo.fontFamily,this.data,e)}else t=new FontFace(this.loadedName,this.data,{});this._inspectFont?.(this);return t}createFontFaceRule(){if(!this.data||this.disableFontFace)return null;const t=(0,s.bytesToString)(this.data),e=`url(data:${this.mimetype};base64,${btoa(t)});`;let i;if(this.cssFontInfo){let t=`font-weight: ${this.cssFontInfo.fontWeight};`;this.cssFontInfo.italicAngle&&(t+=`font-style: oblique ${this.cssFontInfo.italicAngle}deg;`);i=`@font-face {font-family:"${this.cssFontInfo.fontFamily}";${t}src:${e}}`}else i=`@font-face {font-family:"${this.loadedName}";src:${e}}`;this._inspectFont?.(this,e);return i}getPathGenerator(t,e){if(void 0!==this.compiledGlyphs[e])return this.compiledGlyphs[e];let i;try{i=t.get(this.loadedName+"_path_"+e)}catch(t){if(!this.ignoreErrors)throw t;(0,s.warn)(`getPathGenerator - ignoring character: "${t}".`);return this.compiledGlyphs[e]=function(t,e){}}if(this.isEvalSupported&&s.FeatureTest.isEvalSupported){const t=[];for(const e of i){const i=void 0!==e.args?e.args.join(","):"";t.push("c.",e.cmd,"(",i,");\n")}return this.compiledGlyphs[e]=new Function("c","size",t.join(""))}return this.compiledGlyphs[e]=function(t,e){for(const s of i){"scale"===s.cmd&&(s.args=[e,-e]);t[s.cmd].apply(t,s.args)}}}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.NodeStandardFontDataFactory=e.NodeFilterFactory=e.NodeCanvasFactory=e.NodeCMapReaderFactory=void 0;var s=i(7);i(1);const fetchData=function(t){return new Promise(((e,i)=>{require("fs").readFile(t,((t,s)=>{!t&&s?e(new Uint8Array(s)):i(new Error(t))}))}))};class NodeFilterFactory extends s.BaseFilterFactory{}e.NodeFilterFactory=NodeFilterFactory;class NodeCanvasFactory extends s.BaseCanvasFactory{_createCanvas(t,e){return require("canvas").createCanvas(t,e)}}e.NodeCanvasFactory=NodeCanvasFactory;class NodeCMapReaderFactory extends s.BaseCMapReaderFactory{_fetchData(t,e){return fetchData(t).then((t=>({cMapData:t,compressionType:e})))}}e.NodeCMapReaderFactory=NodeCMapReaderFactory;class NodeStandardFontDataFactory extends s.BaseStandardFontDataFactory{_fetchData(t){return fetchData(t)}}e.NodeStandardFontDataFactory=NodeStandardFontDataFactory},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.CanvasGraphics=void 0;var s=i(1),n=i(6),a=i(12),r=i(13);const o=4096,l=16;class CachedCanvases{constructor(t){this.canvasFactory=t;this.cache=Object.create(null)}getCanvas(t,e,i){let s;if(void 0!==this.cache[t]){s=this.cache[t];this.canvasFactory.reset(s,e,i)}else{s=this.canvasFactory.create(e,i);this.cache[t]=s}return s}delete(t){delete this.cache[t]}clear(){for(const t in this.cache){const e=this.cache[t];this.canvasFactory.destroy(e);delete this.cache[t]}}}function drawImageAtIntegerCoords(t,e,i,s,a,r,o,l,h,c){const[d,u,p,g,m,f]=(0,n.getCurrentTransform)(t);if(0===u&&0===p){const n=o*d+m,b=Math.round(n),A=l*g+f,_=Math.round(A),v=(o+h)*d+m,y=Math.abs(Math.round(v)-b)||1,S=(l+c)*g+f,E=Math.abs(Math.round(S)-_)||1;t.setTransform(Math.sign(d),0,0,Math.sign(g),b,_);t.drawImage(e,i,s,a,r,0,0,y,E);t.setTransform(d,u,p,g,m,f);return[y,E]}if(0===d&&0===g){const n=l*p+m,b=Math.round(n),A=o*u+f,_=Math.round(A),v=(l+c)*p+m,y=Math.abs(Math.round(v)-b)||1,S=(o+h)*u+f,E=Math.abs(Math.round(S)-_)||1;t.setTransform(0,Math.sign(u),Math.sign(p),0,b,_);t.drawImage(e,i,s,a,r,0,0,E,y);t.setTransform(d,u,p,g,m,f);return[E,y]}t.drawImage(e,i,s,a,r,o,l,h,c);return[Math.hypot(d,u)*h,Math.hypot(p,g)*c]}class CanvasExtraState{constructor(t,e){this.alphaIsShape=!1;this.fontSize=0;this.fontSizeScale=1;this.textMatrix=s.IDENTITY_MATRIX;this.textMatrixScale=1;this.fontMatrix=s.FONT_IDENTITY_MATRIX;this.leading=0;this.x=0;this.y=0;this.lineX=0;this.lineY=0;this.charSpacing=0;this.wordSpacing=0;this.textHScale=1;this.textRenderingMode=s.TextRenderingMode.FILL;this.textRise=0;this.fillColor="#000000";this.strokeColor="#000000";this.patternFill=!1;this.fillAlpha=1;this.strokeAlpha=1;this.lineWidth=1;this.activeSMask=null;this.transferMaps="none";this.startNewPathAndClipBox([0,0,t,e])}clone(){const t=Object.create(this);t.clipBox=this.clipBox.slice();return t}setCurrentPoint(t,e){this.x=t;this.y=e}updatePathMinMax(t,e,i){[e,i]=s.Util.applyTransform([e,i],t);this.minX=Math.min(this.minX,e);this.minY=Math.min(this.minY,i);this.maxX=Math.max(this.maxX,e);this.maxY=Math.max(this.maxY,i)}updateRectMinMax(t,e){const i=s.Util.applyTransform(e,t),n=s.Util.applyTransform(e.slice(2),t);this.minX=Math.min(this.minX,i[0],n[0]);this.minY=Math.min(this.minY,i[1],n[1]);this.maxX=Math.max(this.maxX,i[0],n[0]);this.maxY=Math.max(this.maxY,i[1],n[1])}updateScalingPathMinMax(t,e){s.Util.scaleMinMax(t,e);this.minX=Math.min(this.minX,e[0]);this.maxX=Math.max(this.maxX,e[1]);this.minY=Math.min(this.minY,e[2]);this.maxY=Math.max(this.maxY,e[3])}updateCurvePathMinMax(t,e,i,n,a,r,o,l,h,c){const d=s.Util.bezierBoundingBox(e,i,n,a,r,o,l,h);if(c){c[0]=Math.min(c[0],d[0],d[2]);c[1]=Math.max(c[1],d[0],d[2]);c[2]=Math.min(c[2],d[1],d[3]);c[3]=Math.max(c[3],d[1],d[3])}else this.updateRectMinMax(t,d)}getPathBoundingBox(t=a.PathType.FILL,e=null){const i=[this.minX,this.minY,this.maxX,this.maxY];if(t===a.PathType.STROKE){e||(0,s.unreachable)("Stroke bounding box must include transform.");const t=s.Util.singularValueDecompose2dScale(e),n=t[0]*this.lineWidth/2,a=t[1]*this.lineWidth/2;i[0]-=n;i[1]-=a;i[2]+=n;i[3]+=a}return i}updateClipFromPath(){const t=s.Util.intersect(this.clipBox,this.getPathBoundingBox());this.startNewPathAndClipBox(t||[0,0,0,0])}isEmptyClip(){return this.minX===1/0}startNewPathAndClipBox(t){this.clipBox=t;this.minX=1/0;this.minY=1/0;this.maxX=0;this.maxY=0}getClippedPathBoundingBox(t=a.PathType.FILL,e=null){return s.Util.intersect(this.clipBox,this.getPathBoundingBox(t,e))}}function putBinaryImageData(t,e){if("undefined"!=typeof ImageData&&e instanceof ImageData){t.putImageData(e,0,0);return}const i=e.height,n=e.width,a=i%l,r=(i-a)/l,o=0===a?r:r+1,h=t.createImageData(n,l);let c,d=0;const u=e.data,p=h.data;let g,m,f,b;if(e.kind===s.ImageKind.GRAYSCALE_1BPP){const e=u.byteLength,i=new Uint32Array(p.buffer,0,p.byteLength>>2),b=i.length,A=n+7>>3,_=4294967295,v=s.FeatureTest.isLittleEndian?4278190080:255;for(g=0;gA?n:8*t-7,r=-8&a;let o=0,l=0;for(;s>=1}}for(;c=r){f=a;b=n*f}c=0;for(m=b;m--;){p[c++]=u[d++];p[c++]=u[d++];p[c++]=u[d++];p[c++]=255}t.putImageData(h,0,g*l)}}}function putBinaryImageMask(t,e){if(e.bitmap){t.drawImage(e.bitmap,0,0);return}const i=e.height,s=e.width,n=i%l,a=(i-n)/l,o=0===n?a:a+1,h=t.createImageData(s,l);let c=0;const d=e.data,u=h.data;for(let e=0;e>8;t[a-2]=t[a-2]*n+i*r>>8;t[a-1]=t[a-1]*n+s*r>>8}}}function composeSMaskAlpha(t,e,i){const s=t.length;for(let n=3;n>8]>>8:e[n]*s>>16}}function composeSMask(t,e,i,s){const n=s[0],a=s[1],r=s[2]-n,o=s[3]-a;if(0!==r&&0!==o){!function genericComposeSMask(t,e,i,s,n,a,r,o,l,h,c){const d=!!a,u=d?a[0]:0,p=d?a[1]:0,g=d?a[2]:0,m="Luminosity"===n?composeSMaskLuminosity:composeSMaskAlpha,f=Math.min(s,Math.ceil(1048576/i));for(let n=0;n10&&"function"==typeof i,c=h?Date.now()+15:0;let d=0;const u=this.commonObjs,p=this.objs;let g;for(;;){if(void 0!==n&&o===n.nextBreakPoint){n.breakIt(o,i);return o}g=r[o];if(g!==s.OPS.dependency)this[g].apply(this,a[o]);else for(const t of a[o]){const e=t.startsWith("g_")?u:p;if(!e.has(t)){e.get(t,i);return o}}o++;if(o===l)return o;if(h&&++d>10){if(Date.now()>c){i();return o}d=0}}}#he(){for(;this.stateStack.length||this.inSMaskMode;)this.restore();this.ctx.restore();if(this.transparentCanvas){this.ctx=this.compositeCtx;this.ctx.save();this.ctx.setTransform(1,0,0,1,0,0);this.ctx.drawImage(this.transparentCanvas,0,0);this.ctx.restore();this.transparentCanvas=null}}endDrawing(){this.#he();this.cachedCanvases.clear();this.cachedPatterns.clear();for(const t of this._cachedBitmapsMap.values()){for(const e of t.values())"undefined"!=typeof HTMLCanvasElement&&e instanceof HTMLCanvasElement&&(e.width=e.height=0);t.clear()}this._cachedBitmapsMap.clear();this.#ce()}#ce(){if(this.pageColors){const t=this.filterFactory.addHCMFilter(this.pageColors.foreground,this.pageColors.background);if("none"!==t){const e=this.ctx.filter;this.ctx.filter=t;this.ctx.drawImage(this.ctx.canvas,0,0);this.ctx.filter=e}}}_scaleImage(t,e){const i=t.width,s=t.height;let n,a,r=Math.max(Math.hypot(e[0],e[1]),1),o=Math.max(Math.hypot(e[2],e[3]),1),l=i,h=s,c="prescale1";for(;r>2&&l>1||o>2&&h>1;){let e=l,i=h;if(r>2&&l>1){e=l>=16384?Math.floor(l/2)-1||1:Math.ceil(l/2);r/=l/e}if(o>2&&h>1){i=h>=16384?Math.floor(h/2)-1||1:Math.ceil(h)/2;o/=h/i}n=this.cachedCanvases.getCanvas(c,e,i);a=n.context;a.clearRect(0,0,e,i);a.drawImage(t,0,0,l,h,0,0,e,i);t=n.canvas;l=e;h=i;c="prescale1"===c?"prescale2":"prescale1"}return{img:t,paintWidth:l,paintHeight:h}}_createMaskCanvas(t){const e=this.ctx,{width:i,height:r}=t,o=this.current.fillColor,l=this.current.patternFill,h=(0,n.getCurrentTransform)(e);let c,d,u,p;if((t.bitmap||t.data)&&t.count>1){const e=t.bitmap||t.data.buffer;d=JSON.stringify(l?h:[h.slice(0,4),o]);c=this._cachedBitmapsMap.get(e);if(!c){c=new Map;this._cachedBitmapsMap.set(e,c)}const i=c.get(d);if(i&&!l){return{canvas:i,offsetX:Math.round(Math.min(h[0],h[2])+h[4]),offsetY:Math.round(Math.min(h[1],h[3])+h[5])}}u=i}if(!u){p=this.cachedCanvases.getCanvas("maskCanvas",i,r);putBinaryImageMask(p.context,t)}let g=s.Util.transform(h,[1/i,0,0,-1/r,0,0]);g=s.Util.transform(g,[1,0,0,1,0,-r]);const m=s.Util.applyTransform([0,0],g),f=s.Util.applyTransform([i,r],g),b=s.Util.normalizeRect([m[0],m[1],f[0],f[1]]),A=Math.round(b[2]-b[0])||1,_=Math.round(b[3]-b[1])||1,v=this.cachedCanvases.getCanvas("fillCanvas",A,_),y=v.context,S=Math.min(m[0],f[0]),E=Math.min(m[1],f[1]);y.translate(-S,-E);y.transform(...g);if(!u){u=this._scaleImage(p.canvas,(0,n.getCurrentTransformInverse)(y));u=u.img;c&&l&&c.set(d,u)}y.imageSmoothingEnabled=getImageSmoothingEnabled((0,n.getCurrentTransform)(y),t.interpolate);drawImageAtIntegerCoords(y,u,0,0,u.width,u.height,0,0,i,r);y.globalCompositeOperation="source-in";const x=s.Util.transform((0,n.getCurrentTransformInverse)(y),[1,0,0,1,-S,-E]);y.fillStyle=l?o.getPattern(e,this,x,a.PathType.FILL):o;y.fillRect(0,0,i,r);if(c&&!l){this.cachedCanvases.delete("fillCanvas");c.set(d,v.canvas)}return{canvas:v.canvas,offsetX:Math.round(S),offsetY:Math.round(E)}}setLineWidth(t){t!==this.current.lineWidth&&(this._cachedScaleForStroking[0]=-1);this.current.lineWidth=t;this.ctx.lineWidth=t}setLineCap(t){this.ctx.lineCap=h[t]}setLineJoin(t){this.ctx.lineJoin=c[t]}setMiterLimit(t){this.ctx.miterLimit=t}setDash(t,e){const i=this.ctx;if(void 0!==i.setLineDash){i.setLineDash(t);i.lineDashOffset=e}}setRenderingIntent(t){}setFlatness(t){}setGState(t){for(const[e,i]of t)switch(e){case"LW":this.setLineWidth(i);break;case"LC":this.setLineCap(i);break;case"LJ":this.setLineJoin(i);break;case"ML":this.setMiterLimit(i);break;case"D":this.setDash(i[0],i[1]);break;case"RI":this.setRenderingIntent(i);break;case"FL":this.setFlatness(i);break;case"Font":this.setFont(i[0],i[1]);break;case"CA":this.current.strokeAlpha=i;break;case"ca":this.current.fillAlpha=i;this.ctx.globalAlpha=i;break;case"BM":this.ctx.globalCompositeOperation=i;break;case"SMask":this.current.activeSMask=i?this.tempSMask:null;this.tempSMask=null;this.checkSMaskState();break;case"TR":this.ctx.filter=this.current.transferMaps=this.filterFactory.addFilter(i)}}get inSMaskMode(){return!!this.suspendedCtx}checkSMaskState(){const t=this.inSMaskMode;this.current.activeSMask&&!t?this.beginSMaskMode():!this.current.activeSMask&&t&&this.endSMaskMode()}beginSMaskMode(){if(this.inSMaskMode)throw new Error("beginSMaskMode called while already in smask mode");const t=this.ctx.canvas.width,e=this.ctx.canvas.height,i="smaskGroupAt"+this.groupLevel,s=this.cachedCanvases.getCanvas(i,t,e);this.suspendedCtx=this.ctx;this.ctx=s.context;const a=this.ctx;a.setTransform(...(0,n.getCurrentTransform)(this.suspendedCtx));copyCtxState(this.suspendedCtx,a);!function mirrorContextOperations(t,e){if(t._removeMirroring)throw new Error("Context is already forwarding operations.");t.__originalSave=t.save;t.__originalRestore=t.restore;t.__originalRotate=t.rotate;t.__originalScale=t.scale;t.__originalTranslate=t.translate;t.__originalTransform=t.transform;t.__originalSetTransform=t.setTransform;t.__originalResetTransform=t.resetTransform;t.__originalClip=t.clip;t.__originalMoveTo=t.moveTo;t.__originalLineTo=t.lineTo;t.__originalBezierCurveTo=t.bezierCurveTo;t.__originalRect=t.rect;t.__originalClosePath=t.closePath;t.__originalBeginPath=t.beginPath;t._removeMirroring=()=>{t.save=t.__originalSave;t.restore=t.__originalRestore;t.rotate=t.__originalRotate;t.scale=t.__originalScale;t.translate=t.__originalTranslate;t.transform=t.__originalTransform;t.setTransform=t.__originalSetTransform;t.resetTransform=t.__originalResetTransform;t.clip=t.__originalClip;t.moveTo=t.__originalMoveTo;t.lineTo=t.__originalLineTo;t.bezierCurveTo=t.__originalBezierCurveTo;t.rect=t.__originalRect;t.closePath=t.__originalClosePath;t.beginPath=t.__originalBeginPath;delete t._removeMirroring};t.save=function ctxSave(){e.save();this.__originalSave()};t.restore=function ctxRestore(){e.restore();this.__originalRestore()};t.translate=function ctxTranslate(t,i){e.translate(t,i);this.__originalTranslate(t,i)};t.scale=function ctxScale(t,i){e.scale(t,i);this.__originalScale(t,i)};t.transform=function ctxTransform(t,i,s,n,a,r){e.transform(t,i,s,n,a,r);this.__originalTransform(t,i,s,n,a,r)};t.setTransform=function ctxSetTransform(t,i,s,n,a,r){e.setTransform(t,i,s,n,a,r);this.__originalSetTransform(t,i,s,n,a,r)};t.resetTransform=function ctxResetTransform(){e.resetTransform();this.__originalResetTransform()};t.rotate=function ctxRotate(t){e.rotate(t);this.__originalRotate(t)};t.clip=function ctxRotate(t){e.clip(t);this.__originalClip(t)};t.moveTo=function(t,i){e.moveTo(t,i);this.__originalMoveTo(t,i)};t.lineTo=function(t,i){e.lineTo(t,i);this.__originalLineTo(t,i)};t.bezierCurveTo=function(t,i,s,n,a,r){e.bezierCurveTo(t,i,s,n,a,r);this.__originalBezierCurveTo(t,i,s,n,a,r)};t.rect=function(t,i,s,n){e.rect(t,i,s,n);this.__originalRect(t,i,s,n)};t.closePath=function(){e.closePath();this.__originalClosePath()};t.beginPath=function(){e.beginPath();this.__originalBeginPath()}}(a,this.suspendedCtx);this.setGState([["BM","source-over"],["ca",1],["CA",1]])}endSMaskMode(){if(!this.inSMaskMode)throw new Error("endSMaskMode called while not in smask mode");this.ctx._removeMirroring();copyCtxState(this.ctx,this.suspendedCtx);this.ctx=this.suspendedCtx;this.suspendedCtx=null}compose(t){if(!this.current.activeSMask)return;if(t){t[0]=Math.floor(t[0]);t[1]=Math.floor(t[1]);t[2]=Math.ceil(t[2]);t[3]=Math.ceil(t[3])}else t=[0,0,this.ctx.canvas.width,this.ctx.canvas.height];const e=this.current.activeSMask;composeSMask(this.suspendedCtx,e,this.ctx,t);this.ctx.save();this.ctx.setTransform(1,0,0,1,0,0);this.ctx.clearRect(0,0,this.ctx.canvas.width,this.ctx.canvas.height);this.ctx.restore()}save(){if(this.inSMaskMode){copyCtxState(this.ctx,this.suspendedCtx);this.suspendedCtx.save()}else this.ctx.save();const t=this.current;this.stateStack.push(t);this.current=t.clone()}restore(){0===this.stateStack.length&&this.inSMaskMode&&this.endSMaskMode();if(0!==this.stateStack.length){this.current=this.stateStack.pop();if(this.inSMaskMode){this.suspendedCtx.restore();copyCtxState(this.suspendedCtx,this.ctx)}else this.ctx.restore();this.checkSMaskState();this.pendingClip=null;this._cachedScaleForStroking[0]=-1;this._cachedGetSinglePixelWidth=null}}transform(t,e,i,s,n,a){this.ctx.transform(t,e,i,s,n,a);this._cachedScaleForStroking[0]=-1;this._cachedGetSinglePixelWidth=null}constructPath(t,e,i){const a=this.ctx,r=this.current;let o,l,h=r.x,c=r.y;const d=(0,n.getCurrentTransform)(a),u=0===d[0]&&0===d[3]||0===d[1]&&0===d[2],p=u?i.slice(0):null;for(let i=0,n=0,g=t.length;i100&&(h=100);this.current.fontSizeScale=e/h;this.ctx.font=`${l} ${o} ${h}px ${r}`}setTextRenderingMode(t){this.current.textRenderingMode=t}setTextRise(t){this.current.textRise=t}moveText(t,e){this.current.x=this.current.lineX+=t;this.current.y=this.current.lineY+=e}setLeadingMoveText(t,e){this.setLeading(-e);this.moveText(t,e)}setTextMatrix(t,e,i,s,n,a){this.current.textMatrix=[t,e,i,s,n,a];this.current.textMatrixScale=Math.hypot(t,e);this.current.x=this.current.lineX=0;this.current.y=this.current.lineY=0}nextLine(){this.moveText(0,this.current.leading)}paintChar(t,e,i,a){const r=this.ctx,o=this.current,l=o.font,h=o.textRenderingMode,c=o.fontSize/o.fontSizeScale,d=h&s.TextRenderingMode.FILL_STROKE_MASK,u=!!(h&s.TextRenderingMode.ADD_TO_PATH_FLAG),p=o.patternFill&&!l.missingFile;let g;(l.disableFontFace||u||p)&&(g=l.getPathGenerator(this.commonObjs,t));if(l.disableFontFace||p){r.save();r.translate(e,i);r.beginPath();g(r,c);a&&r.setTransform(...a);d!==s.TextRenderingMode.FILL&&d!==s.TextRenderingMode.FILL_STROKE||r.fill();d!==s.TextRenderingMode.STROKE&&d!==s.TextRenderingMode.FILL_STROKE||r.stroke();r.restore()}else{d!==s.TextRenderingMode.FILL&&d!==s.TextRenderingMode.FILL_STROKE||r.fillText(t,e,i);d!==s.TextRenderingMode.STROKE&&d!==s.TextRenderingMode.FILL_STROKE||r.strokeText(t,e,i)}if(u){(this.pendingTextPaths||=[]).push({transform:(0,n.getCurrentTransform)(r),x:e,y:i,fontSize:c,addToPath:g})}}get isFontSubpixelAAEnabled(){const{context:t}=this.cachedCanvases.getCanvas("isFontSubpixelAAEnabled",10,10);t.scale(1.5,1);t.fillText("I",0,10);const e=t.getImageData(0,0,10,10).data;let i=!1;for(let t=3;t0&&e[t]<255){i=!0;break}return(0,s.shadow)(this,"isFontSubpixelAAEnabled",i)}showText(t){const e=this.current,i=e.font;if(i.isType3Font)return this.showType3Text(t);const r=e.fontSize;if(0===r)return;const o=this.ctx,l=e.fontSizeScale,h=e.charSpacing,c=e.wordSpacing,d=e.fontDirection,u=e.textHScale*d,p=t.length,g=i.vertical,m=g?1:-1,f=i.defaultVMetrics,b=r*e.fontMatrix[0],A=e.textRenderingMode===s.TextRenderingMode.FILL&&!i.disableFontFace&&!e.patternFill;o.save();o.transform(...e.textMatrix);o.translate(e.x,e.y+e.textRise);d>0?o.scale(u,-1):o.scale(u,1);let _;if(e.patternFill){o.save();const t=e.fillColor.getPattern(o,this,(0,n.getCurrentTransformInverse)(o),a.PathType.FILL);_=(0,n.getCurrentTransform)(o);o.restore();o.fillStyle=t}let v=e.lineWidth;const y=e.textMatrixScale;if(0===y||0===v){const t=e.textRenderingMode&s.TextRenderingMode.FILL_STROKE_MASK;t!==s.TextRenderingMode.STROKE&&t!==s.TextRenderingMode.FILL_STROKE||(v=this.getSinglePixelWidth())}else v/=y;if(1!==l){o.scale(l,l);v/=l}o.lineWidth=v;if(i.isInvalidPDFjsFont){const i=[];let s=0;for(const e of t){i.push(e.unicode);s+=e.width}o.fillText(i.join(""),0,0);e.x+=s*b*u;o.restore();this.compose();return}let S,E=0;for(S=0;S0){const t=1e3*o.measureText(a).width/r*l;if(ynew CanvasGraphics(t,this.commonObjs,this.objs,this.canvasFactory,this.filterFactory,{optionalContentConfig:this.optionalContentConfig,markedContentStack:this.markedContentStack})};e=new a.TilingPattern(t,i,this.ctx,r,s)}else e=this._getPattern(t[1],t[2]);return e}setStrokeColorN(){this.current.strokeColor=this.getColorN_Pattern(arguments)}setFillColorN(){this.current.fillColor=this.getColorN_Pattern(arguments);this.current.patternFill=!0}setStrokeRGBColor(t,e,i){const n=s.Util.makeHexColor(t,e,i);this.ctx.strokeStyle=n;this.current.strokeColor=n}setFillRGBColor(t,e,i){const n=s.Util.makeHexColor(t,e,i);this.ctx.fillStyle=n;this.current.fillColor=n;this.current.patternFill=!1}_getPattern(t,e=null){let i;if(this.cachedPatterns.has(t))i=this.cachedPatterns.get(t);else{i=(0,a.getShadingPattern)(this.getObject(t));this.cachedPatterns.set(t,i)}e&&(i.matrix=e);return i}shadingFill(t){if(!this.contentVisible)return;const e=this.ctx;this.save();const i=this._getPattern(t);e.fillStyle=i.getPattern(e,this,(0,n.getCurrentTransformInverse)(e),a.PathType.SHADING);const r=(0,n.getCurrentTransformInverse)(e);if(r){const{width:t,height:i}=e.canvas,[n,a,o,l]=s.Util.getAxialAlignedBoundingBox([0,0,t,i],r);this.ctx.fillRect(n,a,o-n,l-a)}else this.ctx.fillRect(-1e10,-1e10,2e10,2e10);this.compose(this.current.getClippedPathBoundingBox());this.restore()}beginInlineImage(){(0,s.unreachable)("Should not call beginInlineImage")}beginImageData(){(0,s.unreachable)("Should not call beginImageData")}paintFormXObjectBegin(t,e){if(this.contentVisible){this.save();this.baseTransformStack.push(this.baseTransform);Array.isArray(t)&&6===t.length&&this.transform(...t);this.baseTransform=(0,n.getCurrentTransform)(this.ctx);if(e){const t=e[2]-e[0],i=e[3]-e[1];this.ctx.rect(e[0],e[1],t,i);this.current.updateRectMinMax((0,n.getCurrentTransform)(this.ctx),e);this.clip();this.endPath()}}}paintFormXObjectEnd(){if(this.contentVisible){this.restore();this.baseTransform=this.baseTransformStack.pop()}}beginGroup(t){if(!this.contentVisible)return;this.save();if(this.inSMaskMode){this.endSMaskMode();this.current.activeSMask=null}const e=this.ctx;t.isolated||(0,s.info)("TODO: Support non-isolated groups.");t.knockout&&(0,s.warn)("Knockout groups not supported.");const i=(0,n.getCurrentTransform)(e);t.matrix&&e.transform(...t.matrix);if(!t.bbox)throw new Error("Bounding box is required.");let a=s.Util.getAxialAlignedBoundingBox(t.bbox,(0,n.getCurrentTransform)(e));const r=[0,0,e.canvas.width,e.canvas.height];a=s.Util.intersect(a,r)||[0,0,0,0];const l=Math.floor(a[0]),h=Math.floor(a[1]);let c=Math.max(Math.ceil(a[2])-l,1),d=Math.max(Math.ceil(a[3])-h,1),u=1,p=1;if(c>o){u=c/o;c=o}if(d>o){p=d/o;d=o}this.current.startNewPathAndClipBox([0,0,c,d]);let g="groupAt"+this.groupLevel;t.smask&&(g+="_smask_"+this.smaskCounter++%2);const m=this.cachedCanvases.getCanvas(g,c,d),f=m.context;f.scale(1/u,1/p);f.translate(-l,-h);f.transform(...i);if(t.smask)this.smaskStack.push({canvas:m.canvas,context:f,offsetX:l,offsetY:h,scaleX:u,scaleY:p,subtype:t.smask.subtype,backdrop:t.smask.backdrop,transferMap:t.smask.transferMap||null,startTransformInverse:null});else{e.setTransform(1,0,0,1,0,0);e.translate(l,h);e.scale(u,p);e.save()}copyCtxState(e,f);this.ctx=f;this.setGState([["BM","source-over"],["ca",1],["CA",1]]);this.groupStack.push(e);this.groupLevel++}endGroup(t){if(!this.contentVisible)return;this.groupLevel--;const e=this.ctx,i=this.groupStack.pop();this.ctx=i;this.ctx.imageSmoothingEnabled=!1;if(t.smask){this.tempSMask=this.smaskStack.pop();this.restore()}else{this.ctx.restore();const t=(0,n.getCurrentTransform)(this.ctx);this.restore();this.ctx.save();this.ctx.setTransform(...t);const i=s.Util.getAxialAlignedBoundingBox([0,0,e.canvas.width,e.canvas.height],t);this.ctx.drawImage(e.canvas,0,0);this.ctx.restore();this.compose(i)}}beginAnnotation(t,e,i,a,r){this.#he();resetCtxToDefault(this.ctx);this.ctx.save();this.save();this.baseTransform&&this.ctx.setTransform(...this.baseTransform);if(Array.isArray(e)&&4===e.length){const a=e[2]-e[0],o=e[3]-e[1];if(r&&this.annotationCanvasMap){(i=i.slice())[4]-=e[0];i[5]-=e[1];(e=e.slice())[0]=e[1]=0;e[2]=a;e[3]=o;const[r,l]=s.Util.singularValueDecompose2dScale((0,n.getCurrentTransform)(this.ctx)),{viewportScale:h}=this,c=Math.ceil(a*this.outputScaleX*h),d=Math.ceil(o*this.outputScaleY*h);this.annotationCanvas=this.canvasFactory.create(c,d);const{canvas:u,context:p}=this.annotationCanvas;this.annotationCanvasMap.set(t,u);this.annotationCanvas.savedCtx=this.ctx;this.ctx=p;this.ctx.save();this.ctx.setTransform(r,0,0,-l,0,o*l);resetCtxToDefault(this.ctx)}else{resetCtxToDefault(this.ctx);this.ctx.rect(e[0],e[1],a,o);this.ctx.clip();this.endPath()}}this.current=new CanvasExtraState(this.ctx.canvas.width,this.ctx.canvas.height);this.transform(...i);this.transform(...a)}endAnnotation(){if(this.annotationCanvas){this.ctx.restore();this.#ce();this.ctx=this.annotationCanvas.savedCtx;delete this.annotationCanvas.savedCtx;delete this.annotationCanvas}}paintImageMaskXObject(t){if(!this.contentVisible)return;const e=t.count;(t=this.getObject(t.data,t)).count=e;const i=this.ctx,s=this.processingType3;if(s){void 0===s.compiled&&(s.compiled=function compileType3Glyph(t){const{width:e,height:i}=t;if(e>1e3||i>1e3)return null;const s=new Uint8Array([0,2,4,0,1,0,5,4,8,10,0,8,0,2,1,0]),n=e+1;let a,r,o,l=new Uint8Array(n*(i+1));const h=e+7&-8;let c=new Uint8Array(h*i),d=0;for(const e of t.data){let t=128;for(;t>0;){c[d++]=e&t?0:255;t>>=1}}let u=0;d=0;if(0!==c[d]){l[0]=1;++u}for(r=1;r>2)+(c[d+1]?4:0)+(c[d-h+1]?8:0);if(s[t]){l[o+r]=s[t];++u}d++}if(c[d-h]!==c[d]){l[o+r]=c[d]?2:4;++u}if(u>1e3)return null}d=h*(i-1);o=a*n;if(0!==c[d]){l[o]=8;++u}for(r=1;r1e3)return null;const p=new Int32Array([0,n,-1,0,-n,0,0,0,1]),g=new Path2D;for(a=0;u&&a<=i;a++){let t=a*n;const i=t+e;for(;t>4;l[t]&=r>>2|r<<2}g.lineTo(t%n,t/n|0);l[t]||--u}while(s!==t);--a}c=null;l=null;return function(t){t.save();t.scale(1/e,-1/i);t.translate(0,-i);t.fill(g);t.beginPath();t.restore()}}(t));if(s.compiled){s.compiled(i);return}}const n=this._createMaskCanvas(t),a=n.canvas;i.save();i.setTransform(1,0,0,1,0,0);i.drawImage(a,n.offsetX,n.offsetY);i.restore();this.compose()}paintImageMaskXObjectRepeat(t,e,i=0,a=0,r,o){if(!this.contentVisible)return;t=this.getObject(t.data,t);const l=this.ctx;l.save();const h=(0,n.getCurrentTransform)(l);l.transform(e,i,a,r,0,0);const c=this._createMaskCanvas(t);l.setTransform(1,0,0,1,c.offsetX-h[4],c.offsetY-h[5]);for(let t=0,n=o.length;te?h/e:1;r=l>e?l/e:1}}this._cachedScaleForStroking[0]=a;this._cachedScaleForStroking[1]=r}return this._cachedScaleForStroking}rescaleAndStroke(t){const{ctx:e}=this,{lineWidth:i}=this.current,[s,n]=this.getScaleForStroking();e.lineWidth=i||1;if(1===s&&1===n){e.stroke();return}const a=e.getLineDash();t&&e.save();e.scale(s,n);if(a.length>0){const t=Math.max(s,n);e.setLineDash(a.map((e=>e/t)));e.lineDashOffset/=t}e.stroke();t&&e.restore()}isContentVisible(){for(let t=this.markedContentStack.length-1;t>=0;t--)if(!this.markedContentStack[t].visible)return!1;return!0}}e.CanvasGraphics=CanvasGraphics;for(const t in s.OPS)void 0!==CanvasGraphics.prototype[t]&&(CanvasGraphics.prototype[s.OPS[t]]=CanvasGraphics.prototype[t])},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.TilingPattern=e.PathType=void 0;e.getShadingPattern=function getShadingPattern(t){switch(t[0]){case"RadialAxial":return new RadialAxialShadingPattern(t);case"Mesh":return new MeshShadingPattern(t);case"Dummy":return new DummyShadingPattern}throw new Error(`Unknown IR type: ${t[0]}`)};var s=i(1),n=i(6);const a={FILL:"Fill",STROKE:"Stroke",SHADING:"Shading"};e.PathType=a;function applyBoundingBox(t,e){if(!e)return;const i=e[2]-e[0],s=e[3]-e[1],n=new Path2D;n.rect(e[0],e[1],i,s);t.clip(n)}class BaseShadingPattern{constructor(){this.constructor===BaseShadingPattern&&(0,s.unreachable)("Cannot initialize BaseShadingPattern.")}getPattern(){(0,s.unreachable)("Abstract method `getPattern` called.")}}class RadialAxialShadingPattern extends BaseShadingPattern{constructor(t){super();this._type=t[1];this._bbox=t[2];this._colorStops=t[3];this._p0=t[4];this._p1=t[5];this._r0=t[6];this._r1=t[7];this.matrix=null}_createGradient(t){let e;"axial"===this._type?e=t.createLinearGradient(this._p0[0],this._p0[1],this._p1[0],this._p1[1]):"radial"===this._type&&(e=t.createRadialGradient(this._p0[0],this._p0[1],this._r0,this._p1[0],this._p1[1],this._r1));for(const t of this._colorStops)e.addColorStop(t[0],t[1]);return e}getPattern(t,e,i,r){let o;if(r===a.STROKE||r===a.FILL){const a=e.current.getClippedPathBoundingBox(r,(0,n.getCurrentTransform)(t))||[0,0,0,0],l=Math.ceil(a[2]-a[0])||1,h=Math.ceil(a[3]-a[1])||1,c=e.cachedCanvases.getCanvas("pattern",l,h,!0),d=c.context;d.clearRect(0,0,d.canvas.width,d.canvas.height);d.beginPath();d.rect(0,0,d.canvas.width,d.canvas.height);d.translate(-a[0],-a[1]);i=s.Util.transform(i,[1,0,0,1,a[0],a[1]]);d.transform(...e.baseTransform);this.matrix&&d.transform(...this.matrix);applyBoundingBox(d,this._bbox);d.fillStyle=this._createGradient(d);d.fill();o=t.createPattern(c.canvas,"no-repeat");const u=new DOMMatrix(i);o.setTransform(u)}else{applyBoundingBox(t,this._bbox);o=this._createGradient(t)}return o}}function drawTriangle(t,e,i,s,n,a,r,o){const l=e.coords,h=e.colors,c=t.data,d=4*t.width;let u;if(l[i+1]>l[s+1]){u=i;i=s;s=u;u=a;a=r;r=u}if(l[s+1]>l[n+1]){u=s;s=n;n=u;u=r;r=o;o=u}if(l[i+1]>l[s+1]){u=i;i=s;s=u;u=a;a=r;r=u}const p=(l[i]+e.offsetX)*e.scaleX,g=(l[i+1]+e.offsetY)*e.scaleY,m=(l[s]+e.offsetX)*e.scaleX,f=(l[s+1]+e.offsetY)*e.scaleY,b=(l[n]+e.offsetX)*e.scaleX,A=(l[n+1]+e.offsetY)*e.scaleY;if(g>=A)return;const _=h[a],v=h[a+1],y=h[a+2],S=h[r],E=h[r+1],x=h[r+2],w=h[o],C=h[o+1],T=h[o+2],P=Math.round(g),M=Math.round(A);let k,F,R,D,I,L,O,N;for(let t=P;t<=M;t++){if(tA?1:f===A?0:(f-t)/(f-A);k=m-(m-b)*e;F=S-(S-w)*e;R=E-(E-C)*e;D=x-(x-T)*e}let e;e=tA?1:(g-t)/(g-A);I=p-(p-b)*e;L=_-(_-w)*e;O=v-(v-C)*e;N=y-(y-T)*e;const i=Math.round(Math.min(k,I)),s=Math.round(Math.max(k,I));let n=d*t+4*i;for(let t=i;t<=s;t++){e=(k-t)/(k-I);e<0?e=0:e>1&&(e=1);c[n++]=F-(F-L)*e|0;c[n++]=R-(R-O)*e|0;c[n++]=D-(D-N)*e|0;c[n++]=255}}}function drawFigure(t,e,i){const s=e.coords,n=e.colors;let a,r;switch(e.type){case"lattice":const o=e.verticesPerRow,l=Math.floor(s.length/o)-1,h=o-1;for(a=0;a=s?n=s:i=n/t;return{scale:i,size:n}}clipBbox(t,e,i,s,a){const r=s-e,o=a-i;t.ctx.rect(e,i,r,o);t.current.updateRectMinMax((0,n.getCurrentTransform)(t.ctx),[e,i,s,a]);t.clip();t.endPath()}setFillAndStrokeStyleToContext(t,e,i){const n=t.ctx,a=t.current;switch(e){case r:const t=this.ctx;n.fillStyle=t.fillStyle;n.strokeStyle=t.strokeStyle;a.fillColor=t.fillStyle;a.strokeColor=t.strokeStyle;break;case o:const l=s.Util.makeHexColor(i[0],i[1],i[2]);n.fillStyle=l;n.strokeStyle=l;a.fillColor=l;a.strokeColor=l;break;default:throw new s.FormatError(`Unsupported paint type: ${e}`)}}getPattern(t,e,i,n){let r=i;if(n!==a.SHADING){r=s.Util.transform(r,e.baseTransform);this.matrix&&(r=s.Util.transform(r,this.matrix))}const o=this.createPatternCanvas(e);let l=new DOMMatrix(r);l=l.translate(o.offsetX,o.offsetY);l=l.scale(1/o.scaleX,1/o.scaleY);const h=t.createPattern(o.canvas,"repeat");h.setTransform(l);return h}}e.TilingPattern=TilingPattern},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.convertBlackAndWhiteToRGBA=convertBlackAndWhiteToRGBA;e.convertToRGBA=function convertToRGBA(t){switch(t.kind){case s.ImageKind.GRAYSCALE_1BPP:return convertBlackAndWhiteToRGBA(t);case s.ImageKind.RGB_24BPP:return function convertRGBToRGBA({src:t,srcPos:e=0,dest:i,destPos:n=0,width:a,height:r}){let o=0;const l=t.length>>2,h=new Uint32Array(t.buffer,e,l);if(s.FeatureTest.isLittleEndian){for(;o>>24|e<<8|4278190080;i[n+2]=e>>>16|s<<16|4278190080;i[n+3]=s>>>8|4278190080}for(let e=4*o,s=t.length;e>>8|255;i[n+2]=e<<16|s>>>16|255;i[n+3]=s<<8|255}for(let e=4*o,s=t.length;e>3,u=7&n,p=t.length;i=new Uint32Array(i.buffer);let g=0;for(let s=0;s{Object.defineProperty(e,"__esModule",{value:!0});e.GlobalWorkerOptions=void 0;const i=Object.create(null);e.GlobalWorkerOptions=i;i.workerPort=null;i.workerSrc=""},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.MessageHandler=void 0;var s=i(1);const n=1,a=2,r=1,o=2,l=3,h=4,c=5,d=6,u=7,p=8;function wrapReason(t){t instanceof Error||"object"==typeof t&&null!==t||(0,s.unreachable)('wrapReason: Expected "reason" to be a (possibly cloned) Error.');switch(t.name){case"AbortException":return new s.AbortException(t.message);case"MissingPDFException":return new s.MissingPDFException(t.message);case"PasswordException":return new s.PasswordException(t.message,t.code);case"UnexpectedResponseException":return new s.UnexpectedResponseException(t.message,t.status);case"UnknownErrorException":return new s.UnknownErrorException(t.message,t.details);default:return new s.UnknownErrorException(t.message,t.toString())}}e.MessageHandler=class MessageHandler{constructor(t,e,i){this.sourceName=t;this.targetName=e;this.comObj=i;this.callbackId=1;this.streamId=1;this.streamSinks=Object.create(null);this.streamControllers=Object.create(null);this.callbackCapabilities=Object.create(null);this.actionHandler=Object.create(null);this._onComObjOnMessage=t=>{const e=t.data;if(e.targetName!==this.sourceName)return;if(e.stream){this.#de(e);return}if(e.callback){const t=e.callbackId,i=this.callbackCapabilities[t];if(!i)throw new Error(`Cannot resolve callback ${t}`);delete this.callbackCapabilities[t];if(e.callback===n)i.resolve(e.data);else{if(e.callback!==a)throw new Error("Unexpected callback case");i.reject(wrapReason(e.reason))}return}const s=this.actionHandler[e.action];if(!s)throw new Error(`Unknown action from worker: ${e.action}`);if(e.callbackId){const t=this.sourceName,r=e.sourceName;new Promise((function(t){t(s(e.data))})).then((function(s){i.postMessage({sourceName:t,targetName:r,callback:n,callbackId:e.callbackId,data:s})}),(function(s){i.postMessage({sourceName:t,targetName:r,callback:a,callbackId:e.callbackId,reason:wrapReason(s)})}))}else e.streamId?this.#ue(e):s(e.data)};i.addEventListener("message",this._onComObjOnMessage)}on(t,e){const i=this.actionHandler;if(i[t])throw new Error(`There is already an actionName called "${t}"`);i[t]=e}send(t,e,i){this.comObj.postMessage({sourceName:this.sourceName,targetName:this.targetName,action:t,data:e},i)}sendWithPromise(t,e,i){const n=this.callbackId++,a=new s.PromiseCapability;this.callbackCapabilities[n]=a;try{this.comObj.postMessage({sourceName:this.sourceName,targetName:this.targetName,action:t,callbackId:n,data:e},i)}catch(t){a.reject(t)}return a.promise}sendWithStream(t,e,i,n){const a=this.streamId++,o=this.sourceName,l=this.targetName,h=this.comObj;return new ReadableStream({start:i=>{const r=new s.PromiseCapability;this.streamControllers[a]={controller:i,startCall:r,pullCall:null,cancelCall:null,isClosed:!1};h.postMessage({sourceName:o,targetName:l,action:t,streamId:a,data:e,desiredSize:i.desiredSize},n);return r.promise},pull:t=>{const e=new s.PromiseCapability;this.streamControllers[a].pullCall=e;h.postMessage({sourceName:o,targetName:l,stream:d,streamId:a,desiredSize:t.desiredSize});return e.promise},cancel:t=>{(0,s.assert)(t instanceof Error,"cancel must have a valid reason");const e=new s.PromiseCapability;this.streamControllers[a].cancelCall=e;this.streamControllers[a].isClosed=!0;h.postMessage({sourceName:o,targetName:l,stream:r,streamId:a,reason:wrapReason(t)});return e.promise}},i)}#ue(t){const e=t.streamId,i=this.sourceName,n=t.sourceName,a=this.comObj,r=this,o=this.actionHandler[t.action],d={enqueue(t,r=1,o){if(this.isCancelled)return;const l=this.desiredSize;this.desiredSize-=r;if(l>0&&this.desiredSize<=0){this.sinkCapability=new s.PromiseCapability;this.ready=this.sinkCapability.promise}a.postMessage({sourceName:i,targetName:n,stream:h,streamId:e,chunk:t},o)},close(){if(!this.isCancelled){this.isCancelled=!0;a.postMessage({sourceName:i,targetName:n,stream:l,streamId:e});delete r.streamSinks[e]}},error(t){(0,s.assert)(t instanceof Error,"error must have a valid reason");if(!this.isCancelled){this.isCancelled=!0;a.postMessage({sourceName:i,targetName:n,stream:c,streamId:e,reason:wrapReason(t)})}},sinkCapability:new s.PromiseCapability,onPull:null,onCancel:null,isCancelled:!1,desiredSize:t.desiredSize,ready:null};d.sinkCapability.resolve();d.ready=d.sinkCapability.promise;this.streamSinks[e]=d;new Promise((function(e){e(o(t.data,d))})).then((function(){a.postMessage({sourceName:i,targetName:n,stream:p,streamId:e,success:!0})}),(function(t){a.postMessage({sourceName:i,targetName:n,stream:p,streamId:e,reason:wrapReason(t)})}))}#de(t){const e=t.streamId,i=this.sourceName,n=t.sourceName,a=this.comObj,g=this.streamControllers[e],m=this.streamSinks[e];switch(t.stream){case p:t.success?g.startCall.resolve():g.startCall.reject(wrapReason(t.reason));break;case u:t.success?g.pullCall.resolve():g.pullCall.reject(wrapReason(t.reason));break;case d:if(!m){a.postMessage({sourceName:i,targetName:n,stream:u,streamId:e,success:!0});break}m.desiredSize<=0&&t.desiredSize>0&&m.sinkCapability.resolve();m.desiredSize=t.desiredSize;new Promise((function(t){t(m.onPull?.())})).then((function(){a.postMessage({sourceName:i,targetName:n,stream:u,streamId:e,success:!0})}),(function(t){a.postMessage({sourceName:i,targetName:n,stream:u,streamId:e,reason:wrapReason(t)})}));break;case h:(0,s.assert)(g,"enqueue should have stream controller");if(g.isClosed)break;g.controller.enqueue(t.chunk);break;case l:(0,s.assert)(g,"close should have stream controller");if(g.isClosed)break;g.isClosed=!0;g.controller.close();this.#pe(g,e);break;case c:(0,s.assert)(g,"error should have stream controller");g.controller.error(wrapReason(t.reason));this.#pe(g,e);break;case o:t.success?g.cancelCall.resolve():g.cancelCall.reject(wrapReason(t.reason));this.#pe(g,e);break;case r:if(!m)break;new Promise((function(e){e(m.onCancel?.(wrapReason(t.reason)))})).then((function(){a.postMessage({sourceName:i,targetName:n,stream:o,streamId:e,success:!0})}),(function(t){a.postMessage({sourceName:i,targetName:n,stream:o,streamId:e,reason:wrapReason(t)})}));m.sinkCapability.reject(wrapReason(t.reason));m.isCancelled=!0;delete this.streamSinks[e];break;default:throw new Error("Unexpected stream case")}}async#pe(t,e){await Promise.allSettled([t.startCall?.promise,t.pullCall?.promise,t.cancelCall?.promise]);delete this.streamControllers[e]}destroy(){this.comObj.removeEventListener("message",this._onComObjOnMessage)}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.Metadata=void 0;var s=i(1);e.Metadata=class Metadata{#ge;#me;constructor({parsedData:t,rawData:e}){this.#ge=t;this.#me=e}getRaw(){return this.#me}get(t){return this.#ge.get(t)??null}getAll(){return(0,s.objectFromMap)(this.#ge)}has(t){return this.#ge.has(t)}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.OptionalContentConfig=void 0;var s=i(1),n=i(8);const a=Symbol("INTERNAL");class OptionalContentGroup{#fe=!0;constructor(t,e){this.name=t;this.intent=e}get visible(){return this.#fe}_setVisible(t,e){t!==a&&(0,s.unreachable)("Internal method `_setVisible` called.");this.#fe=e}}e.OptionalContentConfig=class OptionalContentConfig{#be=null;#Ae=new Map;#_e=null;#ve=null;constructor(t){this.name=null;this.creator=null;if(null!==t){this.name=t.name;this.creator=t.creator;this.#ve=t.order;for(const e of t.groups)this.#Ae.set(e.id,new OptionalContentGroup(e.name,e.intent));if("OFF"===t.baseState)for(const t of this.#Ae.values())t._setVisible(a,!1);for(const e of t.on)this.#Ae.get(e)._setVisible(a,!0);for(const e of t.off)this.#Ae.get(e)._setVisible(a,!1);this.#_e=this.getHash()}}#ye(t){const e=t.length;if(e<2)return!0;const i=t[0];for(let n=1;n0?(0,s.objectFromMap)(this.#Ae):null}getGroup(t){return this.#Ae.get(t)||null}getHash(){if(null!==this.#be)return this.#be;const t=new n.MurmurHash3_64;for(const[e,i]of this.#Ae)t.update(`${e}:${i.visible}`);return this.#be=t.hexdigest()}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.PDFDataTransportStream=void 0;var s=i(1),n=i(6);e.PDFDataTransportStream=class PDFDataTransportStream{constructor({length:t,initialData:e,progressiveDone:i=!1,contentDispositionFilename:n=null,disableRange:a=!1,disableStream:r=!1},o){(0,s.assert)(o,'PDFDataTransportStream - missing required "pdfDataRangeTransport" argument.');this._queuedChunks=[];this._progressiveDone=i;this._contentDispositionFilename=n;if(e?.length>0){const t=e instanceof Uint8Array&&e.byteLength===e.buffer.byteLength?e.buffer:new Uint8Array(e).buffer;this._queuedChunks.push(t)}this._pdfDataRangeTransport=o;this._isStreamingSupported=!r;this._isRangeSupported=!a;this._contentLength=t;this._fullRequestReader=null;this._rangeReaders=[];this._pdfDataRangeTransport.addRangeListener(((t,e)=>{this._onReceiveData({begin:t,chunk:e})}));this._pdfDataRangeTransport.addProgressListener(((t,e)=>{this._onProgress({loaded:t,total:e})}));this._pdfDataRangeTransport.addProgressiveReadListener((t=>{this._onReceiveData({chunk:t})}));this._pdfDataRangeTransport.addProgressiveDoneListener((()=>{this._onProgressiveDone()}));this._pdfDataRangeTransport.transportReady()}_onReceiveData({begin:t,chunk:e}){const i=e instanceof Uint8Array&&e.byteLength===e.buffer.byteLength?e.buffer:new Uint8Array(e).buffer;if(void 0===t)this._fullRequestReader?this._fullRequestReader._enqueue(i):this._queuedChunks.push(i);else{const e=this._rangeReaders.some((function(e){if(e._begin!==t)return!1;e._enqueue(i);return!0}));(0,s.assert)(e,"_onReceiveData - no `PDFDataTransportStreamRangeReader` instance found.")}}get _progressiveDataLength(){return this._fullRequestReader?._loaded??0}_onProgress(t){void 0===t.total?this._rangeReaders[0]?.onProgress?.({loaded:t.loaded}):this._fullRequestReader?.onProgress?.({loaded:t.loaded,total:t.total})}_onProgressiveDone(){this._fullRequestReader?.progressiveDone();this._progressiveDone=!0}_removeRangeReader(t){const e=this._rangeReaders.indexOf(t);e>=0&&this._rangeReaders.splice(e,1)}getFullReader(){(0,s.assert)(!this._fullRequestReader,"PDFDataTransportStream.getFullReader can only be called once.");const t=this._queuedChunks;this._queuedChunks=null;return new PDFDataTransportStreamReader(this,t,this._progressiveDone,this._contentDispositionFilename)}getRangeReader(t,e){if(e<=this._progressiveDataLength)return null;const i=new PDFDataTransportStreamRangeReader(this,t,e);this._pdfDataRangeTransport.requestDataRange(t,e);this._rangeReaders.push(i);return i}cancelAllRequests(t){this._fullRequestReader?.cancel(t);for(const e of this._rangeReaders.slice(0))e.cancel(t);this._pdfDataRangeTransport.abort()}};class PDFDataTransportStreamReader{constructor(t,e,i=!1,s=null){this._stream=t;this._done=i||!1;this._filename=(0,n.isPdfFile)(s)?s:null;this._queuedChunks=e||[];this._loaded=0;for(const t of this._queuedChunks)this._loaded+=t.byteLength;this._requests=[];this._headersReady=Promise.resolve();t._fullRequestReader=this;this.onProgress=null}_enqueue(t){if(!this._done){if(this._requests.length>0){this._requests.shift().resolve({value:t,done:!1})}else this._queuedChunks.push(t);this._loaded+=t.byteLength}}get headersReady(){return this._headersReady}get filename(){return this._filename}get isRangeSupported(){return this._stream._isRangeSupported}get isStreamingSupported(){return this._stream._isStreamingSupported}get contentLength(){return this._stream._contentLength}async read(){if(this._queuedChunks.length>0){return{value:this._queuedChunks.shift(),done:!1}}if(this._done)return{value:void 0,done:!0};const t=new s.PromiseCapability;this._requests.push(t);return t.promise}cancel(t){this._done=!0;for(const t of this._requests)t.resolve({value:void 0,done:!0});this._requests.length=0}progressiveDone(){this._done||(this._done=!0)}}class PDFDataTransportStreamRangeReader{constructor(t,e,i){this._stream=t;this._begin=e;this._end=i;this._queuedChunk=null;this._requests=[];this._done=!1;this.onProgress=null}_enqueue(t){if(!this._done){if(0===this._requests.length)this._queuedChunk=t;else{this._requests.shift().resolve({value:t,done:!1});for(const t of this._requests)t.resolve({value:void 0,done:!0});this._requests.length=0}this._done=!0;this._stream._removeRangeReader(this)}}get isStreamingSupported(){return!1}async read(){if(this._queuedChunk){const t=this._queuedChunk;this._queuedChunk=null;return{value:t,done:!1}}if(this._done)return{value:void 0,done:!0};const t=new s.PromiseCapability;this._requests.push(t);return t.promise}cancel(t){this._done=!0;for(const t of this._requests)t.resolve({value:void 0,done:!0});this._requests.length=0;this._stream._removeRangeReader(this)}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.PDFFetchStream=void 0;var s=i(1),n=i(20);function createFetchOptions(t,e,i){return{method:"GET",headers:t,signal:i.signal,mode:"cors",credentials:e?"include":"same-origin",redirect:"follow"}}function createHeaders(t){const e=new Headers;for(const i in t){const s=t[i];void 0!==s&&e.append(i,s)}return e}function getArrayBuffer(t){if(t instanceof Uint8Array)return t.buffer;if(t instanceof ArrayBuffer)return t;(0,s.warn)(`getArrayBuffer - unexpected data format: ${t}`);return new Uint8Array(t).buffer}e.PDFFetchStream=class PDFFetchStream{constructor(t){this.source=t;this.isHttp=/^https?:/i.test(t.url);this.httpHeaders=this.isHttp&&t.httpHeaders||{};this._fullRequestReader=null;this._rangeRequestReaders=[]}get _progressiveDataLength(){return this._fullRequestReader?._loaded??0}getFullReader(){(0,s.assert)(!this._fullRequestReader,"PDFFetchStream.getFullReader can only be called once.");this._fullRequestReader=new PDFFetchStreamReader(this);return this._fullRequestReader}getRangeReader(t,e){if(e<=this._progressiveDataLength)return null;const i=new PDFFetchStreamRangeReader(this,t,e);this._rangeRequestReaders.push(i);return i}cancelAllRequests(t){this._fullRequestReader?.cancel(t);for(const e of this._rangeRequestReaders.slice(0))e.cancel(t)}};class PDFFetchStreamReader{constructor(t){this._stream=t;this._reader=null;this._loaded=0;this._filename=null;const e=t.source;this._withCredentials=e.withCredentials||!1;this._contentLength=e.length;this._headersCapability=new s.PromiseCapability;this._disableRange=e.disableRange||!1;this._rangeChunkSize=e.rangeChunkSize;this._rangeChunkSize||this._disableRange||(this._disableRange=!0);this._abortController=new AbortController;this._isStreamingSupported=!e.disableStream;this._isRangeSupported=!e.disableRange;this._headers=createHeaders(this._stream.httpHeaders);const i=e.url;fetch(i,createFetchOptions(this._headers,this._withCredentials,this._abortController)).then((t=>{if(!(0,n.validateResponseStatus)(t.status))throw(0,n.createResponseStatusError)(t.status,i);this._reader=t.body.getReader();this._headersCapability.resolve();const getResponseHeader=e=>t.headers.get(e),{allowRangeRequests:e,suggestedLength:a}=(0,n.validateRangeRequestCapabilities)({getResponseHeader:getResponseHeader,isHttp:this._stream.isHttp,rangeChunkSize:this._rangeChunkSize,disableRange:this._disableRange});this._isRangeSupported=e;this._contentLength=a||this._contentLength;this._filename=(0,n.extractFilenameFromHeader)(getResponseHeader);!this._isStreamingSupported&&this._isRangeSupported&&this.cancel(new s.AbortException("Streaming is disabled."))})).catch(this._headersCapability.reject);this.onProgress=null}get headersReady(){return this._headersCapability.promise}get filename(){return this._filename}get contentLength(){return this._contentLength}get isRangeSupported(){return this._isRangeSupported}get isStreamingSupported(){return this._isStreamingSupported}async read(){await this._headersCapability.promise;const{value:t,done:e}=await this._reader.read();if(e)return{value:t,done:e};this._loaded+=t.byteLength;this.onProgress?.({loaded:this._loaded,total:this._contentLength});return{value:getArrayBuffer(t),done:!1}}cancel(t){this._reader?.cancel(t);this._abortController.abort()}}class PDFFetchStreamRangeReader{constructor(t,e,i){this._stream=t;this._reader=null;this._loaded=0;const a=t.source;this._withCredentials=a.withCredentials||!1;this._readCapability=new s.PromiseCapability;this._isStreamingSupported=!a.disableStream;this._abortController=new AbortController;this._headers=createHeaders(this._stream.httpHeaders);this._headers.append("Range",`bytes=${e}-${i-1}`);const r=a.url;fetch(r,createFetchOptions(this._headers,this._withCredentials,this._abortController)).then((t=>{if(!(0,n.validateResponseStatus)(t.status))throw(0,n.createResponseStatusError)(t.status,r);this._readCapability.resolve();this._reader=t.body.getReader()})).catch(this._readCapability.reject);this.onProgress=null}get isStreamingSupported(){return this._isStreamingSupported}async read(){await this._readCapability.promise;const{value:t,done:e}=await this._reader.read();if(e)return{value:t,done:e};this._loaded+=t.byteLength;this.onProgress?.({loaded:this._loaded});return{value:getArrayBuffer(t),done:!1}}cancel(t){this._reader?.cancel(t);this._abortController.abort()}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.createResponseStatusError=function createResponseStatusError(t,e){if(404===t||0===t&&e.startsWith("file:"))return new s.MissingPDFException('Missing PDF "'+e+'".');return new s.UnexpectedResponseException(`Unexpected server response (${t}) while retrieving PDF "${e}".`,t)};e.extractFilenameFromHeader=function extractFilenameFromHeader(t){const e=t("Content-Disposition");if(e){let t=(0,n.getFilenameFromContentDispositionHeader)(e);if(t.includes("%"))try{t=decodeURIComponent(t)}catch{}if((0,a.isPdfFile)(t))return t}return null};e.validateRangeRequestCapabilities=function validateRangeRequestCapabilities({getResponseHeader:t,isHttp:e,rangeChunkSize:i,disableRange:s}){const n={allowRangeRequests:!1,suggestedLength:void 0},a=parseInt(t("Content-Length"),10);if(!Number.isInteger(a))return n;n.suggestedLength=a;if(a<=2*i)return n;if(s||!e)return n;if("bytes"!==t("Accept-Ranges"))return n;if("identity"!==(t("Content-Encoding")||"identity"))return n;n.allowRangeRequests=!0;return n};e.validateResponseStatus=function validateResponseStatus(t){return 200===t||206===t};var s=i(1),n=i(21),a=i(6)},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.getFilenameFromContentDispositionHeader=function getFilenameFromContentDispositionHeader(t){let e=!0,i=toParamRegExp("filename\\*","i").exec(t);if(i){i=i[1];let t=rfc2616unquote(i);t=unescape(t);t=rfc5987decode(t);t=rfc2047decode(t);return fixupEncoding(t)}i=function rfc2231getparam(t){const e=[];let i;const s=toParamRegExp("filename\\*((?!0\\d)\\d+)(\\*?)","ig");for(;null!==(i=s.exec(t));){let[,t,s,n]=i;t=parseInt(t,10);if(t in e){if(0===t)break}else e[t]=[s,n]}const n=[];for(let t=0;t{Object.defineProperty(e,"__esModule",{value:!0});e.PDFNetworkStream=void 0;var s=i(1),n=i(20);class NetworkManager{constructor(t,e={}){this.url=t;this.isHttp=/^https?:/i.test(t);this.httpHeaders=this.isHttp&&e.httpHeaders||Object.create(null);this.withCredentials=e.withCredentials||!1;this.currXhrId=0;this.pendingRequests=Object.create(null)}requestRange(t,e,i){const s={begin:t,end:e};for(const t in i)s[t]=i[t];return this.request(s)}requestFull(t){return this.request(t)}request(t){const e=new XMLHttpRequest,i=this.currXhrId++,s=this.pendingRequests[i]={xhr:e};e.open("GET",this.url);e.withCredentials=this.withCredentials;for(const t in this.httpHeaders){const i=this.httpHeaders[t];void 0!==i&&e.setRequestHeader(t,i)}if(this.isHttp&&"begin"in t&&"end"in t){e.setRequestHeader("Range",`bytes=${t.begin}-${t.end-1}`);s.expectedStatus=206}else s.expectedStatus=200;e.responseType="arraybuffer";t.onError&&(e.onerror=function(i){t.onError(e.status)});e.onreadystatechange=this.onStateChange.bind(this,i);e.onprogress=this.onProgress.bind(this,i);s.onHeadersReceived=t.onHeadersReceived;s.onDone=t.onDone;s.onError=t.onError;s.onProgress=t.onProgress;e.send(null);return i}onProgress(t,e){const i=this.pendingRequests[t];i&&i.onProgress?.(e)}onStateChange(t,e){const i=this.pendingRequests[t];if(!i)return;const n=i.xhr;if(n.readyState>=2&&i.onHeadersReceived){i.onHeadersReceived();delete i.onHeadersReceived}if(4!==n.readyState)return;if(!(t in this.pendingRequests))return;delete this.pendingRequests[t];if(0===n.status&&this.isHttp){i.onError?.(n.status);return}const a=n.status||200;if(!(200===a&&206===i.expectedStatus)&&a!==i.expectedStatus){i.onError?.(n.status);return}const r=function getArrayBuffer(t){const e=t.response;return"string"!=typeof e?e:(0,s.stringToBytes)(e).buffer}(n);if(206===a){const t=n.getResponseHeader("Content-Range"),e=/bytes (\d+)-(\d+)\/(\d+)/.exec(t);i.onDone({begin:parseInt(e[1],10),chunk:r})}else r?i.onDone({begin:0,chunk:r}):i.onError?.(n.status)}getRequestXhr(t){return this.pendingRequests[t].xhr}isPendingRequest(t){return t in this.pendingRequests}abortRequest(t){const e=this.pendingRequests[t].xhr;delete this.pendingRequests[t];e.abort()}}e.PDFNetworkStream=class PDFNetworkStream{constructor(t){this._source=t;this._manager=new NetworkManager(t.url,{httpHeaders:t.httpHeaders,withCredentials:t.withCredentials});this._rangeChunkSize=t.rangeChunkSize;this._fullRequestReader=null;this._rangeRequestReaders=[]}_onRangeRequestReaderClosed(t){const e=this._rangeRequestReaders.indexOf(t);e>=0&&this._rangeRequestReaders.splice(e,1)}getFullReader(){(0,s.assert)(!this._fullRequestReader,"PDFNetworkStream.getFullReader can only be called once.");this._fullRequestReader=new PDFNetworkStreamFullRequestReader(this._manager,this._source);return this._fullRequestReader}getRangeReader(t,e){const i=new PDFNetworkStreamRangeRequestReader(this._manager,t,e);i.onClosed=this._onRangeRequestReaderClosed.bind(this);this._rangeRequestReaders.push(i);return i}cancelAllRequests(t){this._fullRequestReader?.cancel(t);for(const e of this._rangeRequestReaders.slice(0))e.cancel(t)}};class PDFNetworkStreamFullRequestReader{constructor(t,e){this._manager=t;const i={onHeadersReceived:this._onHeadersReceived.bind(this),onDone:this._onDone.bind(this),onError:this._onError.bind(this),onProgress:this._onProgress.bind(this)};this._url=e.url;this._fullRequestId=t.requestFull(i);this._headersReceivedCapability=new s.PromiseCapability;this._disableRange=e.disableRange||!1;this._contentLength=e.length;this._rangeChunkSize=e.rangeChunkSize;this._rangeChunkSize||this._disableRange||(this._disableRange=!0);this._isStreamingSupported=!1;this._isRangeSupported=!1;this._cachedChunks=[];this._requests=[];this._done=!1;this._storedError=void 0;this._filename=null;this.onProgress=null}_onHeadersReceived(){const t=this._fullRequestId,e=this._manager.getRequestXhr(t),getResponseHeader=t=>e.getResponseHeader(t),{allowRangeRequests:i,suggestedLength:s}=(0,n.validateRangeRequestCapabilities)({getResponseHeader:getResponseHeader,isHttp:this._manager.isHttp,rangeChunkSize:this._rangeChunkSize,disableRange:this._disableRange});i&&(this._isRangeSupported=!0);this._contentLength=s||this._contentLength;this._filename=(0,n.extractFilenameFromHeader)(getResponseHeader);this._isRangeSupported&&this._manager.abortRequest(t);this._headersReceivedCapability.resolve()}_onDone(t){if(t)if(this._requests.length>0){this._requests.shift().resolve({value:t.chunk,done:!1})}else this._cachedChunks.push(t.chunk);this._done=!0;if(!(this._cachedChunks.length>0)){for(const t of this._requests)t.resolve({value:void 0,done:!0});this._requests.length=0}}_onError(t){this._storedError=(0,n.createResponseStatusError)(t,this._url);this._headersReceivedCapability.reject(this._storedError);for(const t of this._requests)t.reject(this._storedError);this._requests.length=0;this._cachedChunks.length=0}_onProgress(t){this.onProgress?.({loaded:t.loaded,total:t.lengthComputable?t.total:this._contentLength})}get filename(){return this._filename}get isRangeSupported(){return this._isRangeSupported}get isStreamingSupported(){return this._isStreamingSupported}get contentLength(){return this._contentLength}get headersReady(){return this._headersReceivedCapability.promise}async read(){if(this._storedError)throw this._storedError;if(this._cachedChunks.length>0){return{value:this._cachedChunks.shift(),done:!1}}if(this._done)return{value:void 0,done:!0};const t=new s.PromiseCapability;this._requests.push(t);return t.promise}cancel(t){this._done=!0;this._headersReceivedCapability.reject(t);for(const t of this._requests)t.resolve({value:void 0,done:!0});this._requests.length=0;this._manager.isPendingRequest(this._fullRequestId)&&this._manager.abortRequest(this._fullRequestId);this._fullRequestReader=null}}class PDFNetworkStreamRangeRequestReader{constructor(t,e,i){this._manager=t;const s={onDone:this._onDone.bind(this),onError:this._onError.bind(this),onProgress:this._onProgress.bind(this)};this._url=t.url;this._requestId=t.requestRange(e,i,s);this._requests=[];this._queuedChunk=null;this._done=!1;this._storedError=void 0;this.onProgress=null;this.onClosed=null}_close(){this.onClosed?.(this)}_onDone(t){const e=t.chunk;if(this._requests.length>0){this._requests.shift().resolve({value:e,done:!1})}else this._queuedChunk=e;this._done=!0;for(const t of this._requests)t.resolve({value:void 0,done:!0});this._requests.length=0;this._close()}_onError(t){this._storedError=(0,n.createResponseStatusError)(t,this._url);for(const t of this._requests)t.reject(this._storedError);this._requests.length=0;this._queuedChunk=null}_onProgress(t){this.isStreamingSupported||this.onProgress?.({loaded:t.loaded})}get isStreamingSupported(){return!1}async read(){if(this._storedError)throw this._storedError;if(null!==this._queuedChunk){const t=this._queuedChunk;this._queuedChunk=null;return{value:t,done:!1}}if(this._done)return{value:void 0,done:!0};const t=new s.PromiseCapability;this._requests.push(t);return t.promise}cancel(t){this._done=!0;for(const t of this._requests)t.resolve({value:void 0,done:!0});this._requests.length=0;this._manager.isPendingRequest(this._requestId)&&this._manager.abortRequest(this._requestId);this._close()}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.PDFNodeStream=void 0;var s=i(1),n=i(20);const a=/^file:\/\/\/[a-zA-Z]:\//;e.PDFNodeStream=class PDFNodeStream{constructor(t){this.source=t;this.url=function parseUrl(t){const e=require("url"),i=e.parse(t);if("file:"===i.protocol||i.host)return i;if(/^[a-z]:[/\\]/i.test(t))return e.parse(`file:///${t}`);i.host||(i.protocol="file:");return i}(t.url);this.isHttp="http:"===this.url.protocol||"https:"===this.url.protocol;this.isFsUrl="file:"===this.url.protocol;this.httpHeaders=this.isHttp&&t.httpHeaders||{};this._fullRequestReader=null;this._rangeRequestReaders=[]}get _progressiveDataLength(){return this._fullRequestReader?._loaded??0}getFullReader(){(0,s.assert)(!this._fullRequestReader,"PDFNodeStream.getFullReader can only be called once.");this._fullRequestReader=this.isFsUrl?new PDFNodeStreamFsFullReader(this):new PDFNodeStreamFullReader(this);return this._fullRequestReader}getRangeReader(t,e){if(e<=this._progressiveDataLength)return null;const i=this.isFsUrl?new PDFNodeStreamFsRangeReader(this,t,e):new PDFNodeStreamRangeReader(this,t,e);this._rangeRequestReaders.push(i);return i}cancelAllRequests(t){this._fullRequestReader?.cancel(t);for(const e of this._rangeRequestReaders.slice(0))e.cancel(t)}};class BaseFullReader{constructor(t){this._url=t.url;this._done=!1;this._storedError=null;this.onProgress=null;const e=t.source;this._contentLength=e.length;this._loaded=0;this._filename=null;this._disableRange=e.disableRange||!1;this._rangeChunkSize=e.rangeChunkSize;this._rangeChunkSize||this._disableRange||(this._disableRange=!0);this._isStreamingSupported=!e.disableStream;this._isRangeSupported=!e.disableRange;this._readableStream=null;this._readCapability=new s.PromiseCapability;this._headersCapability=new s.PromiseCapability}get headersReady(){return this._headersCapability.promise}get filename(){return this._filename}get contentLength(){return this._contentLength}get isRangeSupported(){return this._isRangeSupported}get isStreamingSupported(){return this._isStreamingSupported}async read(){await this._readCapability.promise;if(this._done)return{value:void 0,done:!0};if(this._storedError)throw this._storedError;const t=this._readableStream.read();if(null===t){this._readCapability=new s.PromiseCapability;return this.read()}this._loaded+=t.length;this.onProgress?.({loaded:this._loaded,total:this._contentLength});return{value:new Uint8Array(t).buffer,done:!1}}cancel(t){this._readableStream?this._readableStream.destroy(t):this._error(t)}_error(t){this._storedError=t;this._readCapability.resolve()}_setReadableStream(t){this._readableStream=t;t.on("readable",(()=>{this._readCapability.resolve()}));t.on("end",(()=>{t.destroy();this._done=!0;this._readCapability.resolve()}));t.on("error",(t=>{this._error(t)}));!this._isStreamingSupported&&this._isRangeSupported&&this._error(new s.AbortException("streaming is disabled"));this._storedError&&this._readableStream.destroy(this._storedError)}}class BaseRangeReader{constructor(t){this._url=t.url;this._done=!1;this._storedError=null;this.onProgress=null;this._loaded=0;this._readableStream=null;this._readCapability=new s.PromiseCapability;const e=t.source;this._isStreamingSupported=!e.disableStream}get isStreamingSupported(){return this._isStreamingSupported}async read(){await this._readCapability.promise;if(this._done)return{value:void 0,done:!0};if(this._storedError)throw this._storedError;const t=this._readableStream.read();if(null===t){this._readCapability=new s.PromiseCapability;return this.read()}this._loaded+=t.length;this.onProgress?.({loaded:this._loaded});return{value:new Uint8Array(t).buffer,done:!1}}cancel(t){this._readableStream?this._readableStream.destroy(t):this._error(t)}_error(t){this._storedError=t;this._readCapability.resolve()}_setReadableStream(t){this._readableStream=t;t.on("readable",(()=>{this._readCapability.resolve()}));t.on("end",(()=>{t.destroy();this._done=!0;this._readCapability.resolve()}));t.on("error",(t=>{this._error(t)}));this._storedError&&this._readableStream.destroy(this._storedError)}}function createRequestOptions(t,e){return{protocol:t.protocol,auth:t.auth,host:t.hostname,port:t.port,path:t.path,method:"GET",headers:e}}class PDFNodeStreamFullReader extends BaseFullReader{constructor(t){super(t);const handleResponse=e=>{if(404===e.statusCode){const t=new s.MissingPDFException(`Missing PDF "${this._url}".`);this._storedError=t;this._headersCapability.reject(t);return}this._headersCapability.resolve();this._setReadableStream(e);const getResponseHeader=t=>this._readableStream.headers[t.toLowerCase()],{allowRangeRequests:i,suggestedLength:a}=(0,n.validateRangeRequestCapabilities)({getResponseHeader:getResponseHeader,isHttp:t.isHttp,rangeChunkSize:this._rangeChunkSize,disableRange:this._disableRange});this._isRangeSupported=i;this._contentLength=a||this._contentLength;this._filename=(0,n.extractFilenameFromHeader)(getResponseHeader)};this._request=null;if("http:"===this._url.protocol){const e=require("http");this._request=e.request(createRequestOptions(this._url,t.httpHeaders),handleResponse)}else{const e=require("https");this._request=e.request(createRequestOptions(this._url,t.httpHeaders),handleResponse)}this._request.on("error",(t=>{this._storedError=t;this._headersCapability.reject(t)}));this._request.end()}}class PDFNodeStreamRangeReader extends BaseRangeReader{constructor(t,e,i){super(t);this._httpHeaders={};for(const e in t.httpHeaders){const i=t.httpHeaders[e];void 0!==i&&(this._httpHeaders[e]=i)}this._httpHeaders.Range=`bytes=${e}-${i-1}`;const handleResponse=t=>{if(404!==t.statusCode)this._setReadableStream(t);else{const t=new s.MissingPDFException(`Missing PDF "${this._url}".`);this._storedError=t}};this._request=null;if("http:"===this._url.protocol){const t=require("http");this._request=t.request(createRequestOptions(this._url,this._httpHeaders),handleResponse)}else{const t=require("https");this._request=t.request(createRequestOptions(this._url,this._httpHeaders),handleResponse)}this._request.on("error",(t=>{this._storedError=t}));this._request.end()}}class PDFNodeStreamFsFullReader extends BaseFullReader{constructor(t){super(t);let e=decodeURIComponent(this._url.path);a.test(this._url.href)&&(e=e.replace(/^\//,""));const i=require("fs");i.lstat(e,((t,n)=>{if(t){"ENOENT"===t.code&&(t=new s.MissingPDFException(`Missing PDF "${e}".`));this._storedError=t;this._headersCapability.reject(t)}else{this._contentLength=n.size;this._setReadableStream(i.createReadStream(e));this._headersCapability.resolve()}}))}}class PDFNodeStreamFsRangeReader extends BaseRangeReader{constructor(t,e,i){super(t);let s=decodeURIComponent(this._url.path);a.test(this._url.href)&&(s=s.replace(/^\//,""));const n=require("fs");this._setReadableStream(n.createReadStream(s,{start:e,end:i-1}))}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.SVGGraphics=void 0;var s=i(6),n=i(1);const a="normal",r="normal",o="#000000",l=["butt","round","square"],h=["miter","round","bevel"],createObjectURL=function(t,e="",i=!1){if(URL.createObjectURL&&"undefined"!=typeof Blob&&!i)return URL.createObjectURL(new Blob([t],{type:e}));const s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";let n=`data:${e};base64,`;for(let e=0,i=t.length;e>2]+s[(3&a)<<4|r>>4]+s[e+1>6:64]+s[e+2>1&2147483647:i>>1&2147483647;e[t]=i}function writePngChunk(t,i,s,n){let a=n;const r=i.length;s[a]=r>>24&255;s[a+1]=r>>16&255;s[a+2]=r>>8&255;s[a+3]=255&r;a+=4;s[a]=255&t.charCodeAt(0);s[a+1]=255&t.charCodeAt(1);s[a+2]=255&t.charCodeAt(2);s[a+3]=255&t.charCodeAt(3);a+=4;s.set(i,a);a+=i.length;const o=function crc32(t,i,s){let n=-1;for(let a=i;a>>8^e[i]}return-1^n}(s,n+4,a);s[a]=o>>24&255;s[a+1]=o>>16&255;s[a+2]=o>>8&255;s[a+3]=255&o}function deflateSyncUncompressed(t){let e=t.length;const i=65535,s=Math.ceil(e/i),n=new Uint8Array(2+e+5*s+4);let a=0;n[a++]=120;n[a++]=156;let r=0;for(;e>i;){n[a++]=0;n[a++]=255;n[a++]=255;n[a++]=0;n[a++]=0;n.set(t.subarray(r,r+i),a);a+=i;r+=i;e-=i}n[a++]=1;n[a++]=255&e;n[a++]=e>>8&255;n[a++]=255&~e;n[a++]=(65535&~e)>>8&255;n.set(t.subarray(r),a);a+=t.length-r;const o=function adler32(t,e,i){let s=1,n=0;for(let a=e;a>24&255;n[a++]=o>>16&255;n[a++]=o>>8&255;n[a++]=255&o;return n}function encode(e,i,s,a){const r=e.width,o=e.height;let l,h,c;const d=e.data;switch(i){case n.ImageKind.GRAYSCALE_1BPP:h=0;l=1;c=r+7>>3;break;case n.ImageKind.RGB_24BPP:h=2;l=8;c=3*r;break;case n.ImageKind.RGBA_32BPP:h=6;l=8;c=4*r;break;default:throw new Error("invalid format")}const u=new Uint8Array((1+c)*o);let p=0,g=0;for(let t=0;t>24&255,r>>16&255,r>>8&255,255&r,o>>24&255,o>>16&255,o>>8&255,255&o,l,h,0,0,0]),f=function deflateSync(t){if(!n.isNodeJS)return deflateSyncUncompressed(t);try{const e=parseInt(process.versions.node)>=8?t:Buffer.from(t),i=require("zlib").deflateSync(e,{level:9});return i instanceof Uint8Array?i:new Uint8Array(i)}catch(t){(0,n.warn)("Not compressing PNG because zlib.deflateSync is unavailable: "+t)}return deflateSyncUncompressed(t)}(u),b=t.length+36+m.length+f.length,A=new Uint8Array(b);let _=0;A.set(t,_);_+=t.length;writePngChunk("IHDR",m,A,_);_+=12+m.length;writePngChunk("IDATA",f,A,_);_+=12+f.length;writePngChunk("IEND",new Uint8Array(0),A,_);return createObjectURL(A,"image/png",s)}return function convertImgDataToPng(t,e,i){return encode(t,void 0===t.kind?n.ImageKind.GRAYSCALE_1BPP:t.kind,e,i)}}();class SVGExtraState{constructor(){this.fontSizeScale=1;this.fontWeight=r;this.fontSize=0;this.textMatrix=n.IDENTITY_MATRIX;this.fontMatrix=n.FONT_IDENTITY_MATRIX;this.leading=0;this.textRenderingMode=n.TextRenderingMode.FILL;this.textMatrixScale=1;this.x=0;this.y=0;this.lineX=0;this.lineY=0;this.charSpacing=0;this.wordSpacing=0;this.textHScale=1;this.textRise=0;this.fillColor=o;this.strokeColor="#000000";this.fillAlpha=1;this.strokeAlpha=1;this.lineWidth=1;this.lineJoin="";this.lineCap="";this.miterLimit=0;this.dashArray=[];this.dashPhase=0;this.dependencies=[];this.activeClipUrl=null;this.clipGroup=null;this.maskId=""}clone(){return Object.create(this)}setCurrentPoint(t,e){this.x=t;this.y=e}}function pf(t){if(Number.isInteger(t))return t.toString();const e=t.toFixed(10);let i=e.length-1;if("0"!==e[i])return e;do{i--}while("0"===e[i]);return e.substring(0,"."===e[i]?i:i+1)}function pm(t){if(0===t[4]&&0===t[5]){if(0===t[1]&&0===t[2])return 1===t[0]&&1===t[3]?"":`scale(${pf(t[0])} ${pf(t[3])})`;if(t[0]===t[3]&&t[1]===-t[2]){return`rotate(${pf(180*Math.acos(t[0])/Math.PI)})`}}else if(1===t[0]&&0===t[1]&&0===t[2]&&1===t[3])return`translate(${pf(t[4])} ${pf(t[5])})`;return`matrix(${pf(t[0])} ${pf(t[1])} ${pf(t[2])} ${pf(t[3])} ${pf(t[4])} ${pf(t[5])})`}let d=0,u=0,p=0;e.SVGGraphics=class SVGGraphics{constructor(t,e,i=!1){(0,s.deprecated)("The SVG back-end is no longer maintained and *may* be removed in the future.");this.svgFactory=new s.DOMSVGFactory;this.current=new SVGExtraState;this.transformMatrix=n.IDENTITY_MATRIX;this.transformStack=[];this.extraStack=[];this.commonObjs=t;this.objs=e;this.pendingClip=null;this.pendingEOFill=!1;this.embedFonts=!1;this.embeddedFonts=Object.create(null);this.cssStyle=null;this.forceDataSchema=!!i;this._operatorIdMapping=[];for(const t in n.OPS)this._operatorIdMapping[n.OPS[t]]=t}getObject(t,e=null){return"string"==typeof t?t.startsWith("g_")?this.commonObjs.get(t):this.objs.get(t):e}save(){this.transformStack.push(this.transformMatrix);const t=this.current;this.extraStack.push(t);this.current=t.clone()}restore(){this.transformMatrix=this.transformStack.pop();this.current=this.extraStack.pop();this.pendingClip=null;this.tgrp=null}group(t){this.save();this.executeOpTree(t);this.restore()}loadDependencies(t){const e=t.fnArray,i=t.argsArray;for(let t=0,s=e.length;t{t.get(e,i)}));this.current.dependencies.push(i)}return Promise.all(this.current.dependencies)}transform(t,e,i,s,a,r){const o=[t,e,i,s,a,r];this.transformMatrix=n.Util.transform(this.transformMatrix,o);this.tgrp=null}getSVG(t,e){this.viewport=e;const i=this._initialize(e);return this.loadDependencies(t).then((()=>{this.transformMatrix=n.IDENTITY_MATRIX;this.executeOpTree(this.convertOpList(t));return i}))}convertOpList(t){const e=this._operatorIdMapping,i=t.argsArray,s=t.fnArray,n=[];for(let t=0,a=s.length;t0&&(this.current.lineWidth=t)}setLineCap(t){this.current.lineCap=l[t]}setLineJoin(t){this.current.lineJoin=h[t]}setMiterLimit(t){this.current.miterLimit=t}setStrokeAlpha(t){this.current.strokeAlpha=t}setStrokeRGBColor(t,e,i){this.current.strokeColor=n.Util.makeHexColor(t,e,i)}setFillAlpha(t){this.current.fillAlpha=t}setFillRGBColor(t,e,i){this.current.fillColor=n.Util.makeHexColor(t,e,i);this.current.tspan=this.svgFactory.createElement("svg:tspan");this.current.xcoords=[];this.current.ycoords=[]}setStrokeColorN(t){this.current.strokeColor=this._makeColorN_Pattern(t)}setFillColorN(t){this.current.fillColor=this._makeColorN_Pattern(t)}shadingFill(t){const{width:e,height:i}=this.viewport,s=n.Util.inverseTransform(this.transformMatrix),[a,r,o,l]=n.Util.getAxialAlignedBoundingBox([0,0,e,i],s),h=this.svgFactory.createElement("svg:rect");h.setAttributeNS(null,"x",a);h.setAttributeNS(null,"y",r);h.setAttributeNS(null,"width",o-a);h.setAttributeNS(null,"height",l-r);h.setAttributeNS(null,"fill",this._makeShadingPattern(t));this.current.fillAlpha<1&&h.setAttributeNS(null,"fill-opacity",this.current.fillAlpha);this._ensureTransformGroup().append(h)}_makeColorN_Pattern(t){return"TilingPattern"===t[0]?this._makeTilingPattern(t):this._makeShadingPattern(t)}_makeTilingPattern(t){const e=t[1],i=t[2],s=t[3]||n.IDENTITY_MATRIX,[a,r,o,l]=t[4],h=t[5],c=t[6],d=t[7],u="shading"+p++,[g,m,f,b]=n.Util.normalizeRect([...n.Util.applyTransform([a,r],s),...n.Util.applyTransform([o,l],s)]),[A,_]=n.Util.singularValueDecompose2dScale(s),v=h*A,y=c*_,S=this.svgFactory.createElement("svg:pattern");S.setAttributeNS(null,"id",u);S.setAttributeNS(null,"patternUnits","userSpaceOnUse");S.setAttributeNS(null,"width",v);S.setAttributeNS(null,"height",y);S.setAttributeNS(null,"x",`${g}`);S.setAttributeNS(null,"y",`${m}`);const E=this.svg,x=this.transformMatrix,w=this.current.fillColor,C=this.current.strokeColor,T=this.svgFactory.create(f-g,b-m);this.svg=T;this.transformMatrix=s;if(2===d){const t=n.Util.makeHexColor(...e);this.current.fillColor=t;this.current.strokeColor=t}this.executeOpTree(this.convertOpList(i));this.svg=E;this.transformMatrix=x;this.current.fillColor=w;this.current.strokeColor=C;S.append(T.childNodes[0]);this.defs.append(S);return`url(#${u})`}_makeShadingPattern(t){"string"==typeof t&&(t=this.objs.get(t));switch(t[0]){case"RadialAxial":const e="shading"+p++,i=t[3];let s;switch(t[1]){case"axial":const i=t[4],n=t[5];s=this.svgFactory.createElement("svg:linearGradient");s.setAttributeNS(null,"id",e);s.setAttributeNS(null,"gradientUnits","userSpaceOnUse");s.setAttributeNS(null,"x1",i[0]);s.setAttributeNS(null,"y1",i[1]);s.setAttributeNS(null,"x2",n[0]);s.setAttributeNS(null,"y2",n[1]);break;case"radial":const a=t[4],r=t[5],o=t[6],l=t[7];s=this.svgFactory.createElement("svg:radialGradient");s.setAttributeNS(null,"id",e);s.setAttributeNS(null,"gradientUnits","userSpaceOnUse");s.setAttributeNS(null,"cx",r[0]);s.setAttributeNS(null,"cy",r[1]);s.setAttributeNS(null,"r",l);s.setAttributeNS(null,"fx",a[0]);s.setAttributeNS(null,"fy",a[1]);s.setAttributeNS(null,"fr",o);break;default:throw new Error(`Unknown RadialAxial type: ${t[1]}`)}for(const t of i){const e=this.svgFactory.createElement("svg:stop");e.setAttributeNS(null,"offset",t[0]);e.setAttributeNS(null,"stop-color",t[1]);s.append(e)}this.defs.append(s);return`url(#${e})`;case"Mesh":(0,n.warn)("Unimplemented pattern Mesh");return null;case"Dummy":return"hotpink";default:throw new Error(`Unknown IR type: ${t[0]}`)}}setDash(t,e){this.current.dashArray=t;this.current.dashPhase=e}constructPath(t,e){const i=this.current;let s=i.x,a=i.y,r=[],o=0;for(const i of t)switch(0|i){case n.OPS.rectangle:s=e[o++];a=e[o++];const t=s+e[o++],i=a+e[o++];r.push("M",pf(s),pf(a),"L",pf(t),pf(a),"L",pf(t),pf(i),"L",pf(s),pf(i),"Z");break;case n.OPS.moveTo:s=e[o++];a=e[o++];r.push("M",pf(s),pf(a));break;case n.OPS.lineTo:s=e[o++];a=e[o++];r.push("L",pf(s),pf(a));break;case n.OPS.curveTo:s=e[o+4];a=e[o+5];r.push("C",pf(e[o]),pf(e[o+1]),pf(e[o+2]),pf(e[o+3]),pf(s),pf(a));o+=6;break;case n.OPS.curveTo2:r.push("C",pf(s),pf(a),pf(e[o]),pf(e[o+1]),pf(e[o+2]),pf(e[o+3]));s=e[o+2];a=e[o+3];o+=4;break;case n.OPS.curveTo3:s=e[o+2];a=e[o+3];r.push("C",pf(e[o]),pf(e[o+1]),pf(s),pf(a),pf(s),pf(a));o+=4;break;case n.OPS.closePath:r.push("Z")}r=r.join(" ");if(i.path&&t.length>0&&t[0]!==n.OPS.rectangle&&t[0]!==n.OPS.moveTo)r=i.path.getAttributeNS(null,"d")+r;else{i.path=this.svgFactory.createElement("svg:path");this._ensureTransformGroup().append(i.path)}i.path.setAttributeNS(null,"d",r);i.path.setAttributeNS(null,"fill","none");i.element=i.path;i.setCurrentPoint(s,a)}endPath(){const t=this.current;t.path=null;if(!this.pendingClip)return;if(!t.element){this.pendingClip=null;return}const e="clippath"+d++,i=this.svgFactory.createElement("svg:clipPath");i.setAttributeNS(null,"id",e);i.setAttributeNS(null,"transform",pm(this.transformMatrix));const s=t.element.cloneNode(!0);"evenodd"===this.pendingClip?s.setAttributeNS(null,"clip-rule","evenodd"):s.setAttributeNS(null,"clip-rule","nonzero");this.pendingClip=null;i.append(s);this.defs.append(i);if(t.activeClipUrl){t.clipGroup=null;for(const t of this.extraStack)t.clipGroup=null;i.setAttributeNS(null,"clip-path",t.activeClipUrl)}t.activeClipUrl=`url(#${e})`;this.tgrp=null}clip(t){this.pendingClip=t}closePath(){const t=this.current;if(t.path){const e=`${t.path.getAttributeNS(null,"d")}Z`;t.path.setAttributeNS(null,"d",e)}}setLeading(t){this.current.leading=-t}setTextRise(t){this.current.textRise=t}setTextRenderingMode(t){this.current.textRenderingMode=t}setHScale(t){this.current.textHScale=t/100}setRenderingIntent(t){}setFlatness(t){}setGState(t){for(const[e,i]of t)switch(e){case"LW":this.setLineWidth(i);break;case"LC":this.setLineCap(i);break;case"LJ":this.setLineJoin(i);break;case"ML":this.setMiterLimit(i);break;case"D":this.setDash(i[0],i[1]);break;case"RI":this.setRenderingIntent(i);break;case"FL":this.setFlatness(i);break;case"Font":this.setFont(i);break;case"CA":this.setStrokeAlpha(i);break;case"ca":this.setFillAlpha(i);break;default:(0,n.warn)(`Unimplemented graphic state operator ${e}`)}}fill(){const t=this.current;if(t.element){t.element.setAttributeNS(null,"fill",t.fillColor);t.element.setAttributeNS(null,"fill-opacity",t.fillAlpha);this.endPath()}}stroke(){const t=this.current;if(t.element){this._setStrokeAttributes(t.element);t.element.setAttributeNS(null,"fill","none");this.endPath()}}_setStrokeAttributes(t,e=1){const i=this.current;let s=i.dashArray;1!==e&&s.length>0&&(s=s.map((function(t){return e*t})));t.setAttributeNS(null,"stroke",i.strokeColor);t.setAttributeNS(null,"stroke-opacity",i.strokeAlpha);t.setAttributeNS(null,"stroke-miterlimit",pf(i.miterLimit));t.setAttributeNS(null,"stroke-linecap",i.lineCap);t.setAttributeNS(null,"stroke-linejoin",i.lineJoin);t.setAttributeNS(null,"stroke-width",pf(e*i.lineWidth)+"px");t.setAttributeNS(null,"stroke-dasharray",s.map(pf).join(" "));t.setAttributeNS(null,"stroke-dashoffset",pf(e*i.dashPhase)+"px")}eoFill(){this.current.element?.setAttributeNS(null,"fill-rule","evenodd");this.fill()}fillStroke(){this.stroke();this.fill()}eoFillStroke(){this.current.element?.setAttributeNS(null,"fill-rule","evenodd");this.fillStroke()}closeStroke(){this.closePath();this.stroke()}closeFillStroke(){this.closePath();this.fillStroke()}closeEOFillStroke(){this.closePath();this.eoFillStroke()}paintSolidColorImageMask(){const t=this.svgFactory.createElement("svg:rect");t.setAttributeNS(null,"x","0");t.setAttributeNS(null,"y","0");t.setAttributeNS(null,"width","1px");t.setAttributeNS(null,"height","1px");t.setAttributeNS(null,"fill",this.current.fillColor);this._ensureTransformGroup().append(t)}paintImageXObject(t){const e=this.getObject(t);e?this.paintInlineImageXObject(e):(0,n.warn)(`Dependent image with object ID ${t} is not ready yet`)}paintInlineImageXObject(t,e){const i=t.width,s=t.height,n=c(t,this.forceDataSchema,!!e),a=this.svgFactory.createElement("svg:rect");a.setAttributeNS(null,"x","0");a.setAttributeNS(null,"y","0");a.setAttributeNS(null,"width",pf(i));a.setAttributeNS(null,"height",pf(s));this.current.element=a;this.clip("nonzero");const r=this.svgFactory.createElement("svg:image");r.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",n);r.setAttributeNS(null,"x","0");r.setAttributeNS(null,"y",pf(-s));r.setAttributeNS(null,"width",pf(i)+"px");r.setAttributeNS(null,"height",pf(s)+"px");r.setAttributeNS(null,"transform",`scale(${pf(1/i)} ${pf(-1/s)})`);e?e.append(r):this._ensureTransformGroup().append(r)}paintImageMaskXObject(t){const e=this.getObject(t.data,t);if(e.bitmap){(0,n.warn)("paintImageMaskXObject: ImageBitmap support is not implemented, ensure that the `isOffscreenCanvasSupported` API parameter is disabled.");return}const i=this.current,s=e.width,a=e.height,r=i.fillColor;i.maskId="mask"+u++;const o=this.svgFactory.createElement("svg:mask");o.setAttributeNS(null,"id",i.maskId);const l=this.svgFactory.createElement("svg:rect");l.setAttributeNS(null,"x","0");l.setAttributeNS(null,"y","0");l.setAttributeNS(null,"width",pf(s));l.setAttributeNS(null,"height",pf(a));l.setAttributeNS(null,"fill",r);l.setAttributeNS(null,"mask",`url(#${i.maskId})`);this.defs.append(o);this._ensureTransformGroup().append(l);this.paintInlineImageXObject(e,o)}paintFormXObjectBegin(t,e){Array.isArray(t)&&6===t.length&&this.transform(t[0],t[1],t[2],t[3],t[4],t[5]);if(e){const t=e[2]-e[0],i=e[3]-e[1],s=this.svgFactory.createElement("svg:rect");s.setAttributeNS(null,"x",e[0]);s.setAttributeNS(null,"y",e[1]);s.setAttributeNS(null,"width",pf(t));s.setAttributeNS(null,"height",pf(i));this.current.element=s;this.clip("nonzero");this.endPath()}}paintFormXObjectEnd(){}_initialize(t){const e=this.svgFactory.create(t.width,t.height),i=this.svgFactory.createElement("svg:defs");e.append(i);this.defs=i;const s=this.svgFactory.createElement("svg:g");s.setAttributeNS(null,"transform",pm(t.transform));e.append(s);this.svg=s;return e}_ensureClipGroup(){if(!this.current.clipGroup){const t=this.svgFactory.createElement("svg:g");t.setAttributeNS(null,"clip-path",this.current.activeClipUrl);this.svg.append(t);this.current.clipGroup=t}return this.current.clipGroup}_ensureTransformGroup(){if(!this.tgrp){this.tgrp=this.svgFactory.createElement("svg:g");this.tgrp.setAttributeNS(null,"transform",pm(this.transformMatrix));this.current.activeClipUrl?this._ensureClipGroup().append(this.tgrp):this.svg.append(this.tgrp)}return this.tgrp}}},(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});e.XfaText=void 0;class XfaText{static textContent(t){const e=[],i={items:e,styles:Object.create(null)};!function walk(t){if(!t)return;let i=null;const s=t.name;if("#text"===s)i=t.value;else{if(!XfaText.shouldBuildText(s))return;t?.attributes?.textContent?i=t.attributes.textContent:t.value&&(i=t.value)}null!==i&&e.push({str:i});if(t.children)for(const e of t.children)walk(e)}(t);return i}static shouldBuildText(t){return!("textarea"===t||"input"===t||"option"===t||"select"===t)}}e.XfaText=XfaText},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.TextLayerRenderTask=void 0;e.renderTextLayer=function renderTextLayer(t){if(!t.textContentSource&&(t.textContent||t.textContentStream)){(0,n.deprecated)("The TextLayerRender `textContent`/`textContentStream` parameters will be removed in the future, please use `textContentSource` instead.");t.textContentSource=t.textContent||t.textContentStream}const{container:e,viewport:i}=t,s=getComputedStyle(e),a=s.getPropertyValue("visibility"),r=parseFloat(s.getPropertyValue("--scale-factor"));"visible"===a&&(!r||Math.abs(r-i.scale)>1e-5)&&console.error("The `--scale-factor` CSS-variable must be set, to the same value as `viewport.scale`, either on the `container`-element itself or higher up in the DOM.");const o=new TextLayerRenderTask(t);o._render();return o};e.updateTextLayer=function updateTextLayer({container:t,viewport:e,textDivs:i,textDivProperties:s,isOffscreenCanvasSupported:a,mustRotate:r=!0,mustRescale:o=!0}){r&&(0,n.setLayerDimensions)(t,{rotation:e.rotation});if(o){const t=getCtx(0,a),n={prevFontSize:null,prevFontFamily:null,div:null,scale:e.scale*(globalThis.devicePixelRatio||1),properties:null,ctx:t};for(const t of i){n.properties=s.get(t);n.div=t;layout(n)}}};var s=i(1),n=i(6);const a=30,r=.8,o=new Map;function getCtx(t,e){let i;if(e&&s.FeatureTest.isOffscreenCanvasSupported)i=new OffscreenCanvas(t,t).getContext("2d",{alpha:!1});else{const e=document.createElement("canvas");e.width=e.height=t;i=e.getContext("2d",{alpha:!1})}return i}function appendText(t,e,i){const n=document.createElement("span"),l={angle:0,canvasWidth:0,hasText:""!==e.str,hasEOL:e.hasEOL,fontSize:0};t._textDivs.push(n);const h=s.Util.transform(t._transform,e.transform);let c=Math.atan2(h[1],h[0]);const d=i[e.fontName];d.vertical&&(c+=Math.PI/2);const u=Math.hypot(h[2],h[3]),p=u*function getAscent(t,e){const i=o.get(t);if(i)return i;const s=getCtx(a,e);s.font=`${a}px ${t}`;const n=s.measureText("");let l=n.fontBoundingBoxAscent,h=Math.abs(n.fontBoundingBoxDescent);if(l){const e=l/(l+h);o.set(t,e);s.canvas.width=s.canvas.height=0;return e}s.strokeStyle="red";s.clearRect(0,0,a,a);s.strokeText("g",0,0);let c=s.getImageData(0,0,a,a).data;h=0;for(let t=c.length-1-3;t>=0;t-=4)if(c[t]>0){h=Math.ceil(t/4/a);break}s.clearRect(0,0,a,a);s.strokeText("A",0,a);c=s.getImageData(0,0,a,a).data;l=0;for(let t=0,e=c.length;t0){l=a-Math.floor(t/4/a);break}s.canvas.width=s.canvas.height=0;if(l){const e=l/(l+h);o.set(t,e);return e}o.set(t,r);return r}(d.fontFamily,t._isOffscreenCanvasSupported);let g,m;if(0===c){g=h[4];m=h[5]-p}else{g=h[4]+p*Math.sin(c);m=h[5]-p*Math.cos(c)}const f="calc(var(--scale-factor)*",b=n.style;if(t._container===t._rootContainer){b.left=`${(100*g/t._pageWidth).toFixed(2)}%`;b.top=`${(100*m/t._pageHeight).toFixed(2)}%`}else{b.left=`${f}${g.toFixed(2)}px)`;b.top=`${f}${m.toFixed(2)}px)`}b.fontSize=`${f}${u.toFixed(2)}px)`;b.fontFamily=d.fontFamily;l.fontSize=u;n.setAttribute("role","presentation");n.textContent=e.str;n.dir=e.dir;t._fontInspectorEnabled&&(n.dataset.fontName=e.fontName);0!==c&&(l.angle=c*(180/Math.PI));let A=!1;if(e.str.length>1)A=!0;else if(" "!==e.str&&e.transform[0]!==e.transform[3]){const t=Math.abs(e.transform[0]),i=Math.abs(e.transform[3]);t!==i&&Math.max(t,i)/Math.min(t,i)>1.5&&(A=!0)}A&&(l.canvasWidth=d.vertical?e.height:e.width);t._textDivProperties.set(n,l);t._isReadableStream&&t._layoutText(n)}function layout(t){const{div:e,scale:i,properties:s,ctx:n,prevFontSize:a,prevFontFamily:r}=t,{style:o}=e;let l="";if(0!==s.canvasWidth&&s.hasText){const{fontFamily:h}=o,{canvasWidth:c,fontSize:d}=s;if(a!==d||r!==h){n.font=`${d*i}px ${h}`;t.prevFontSize=d;t.prevFontFamily=h}const{width:u}=n.measureText(e.textContent);u>0&&(l=`scaleX(${c*i/u})`)}0!==s.angle&&(l=`rotate(${s.angle}deg) ${l}`);l.length>0&&(o.transform=l)}class TextLayerRenderTask{constructor({textContentSource:t,container:e,viewport:i,textDivs:a,textDivProperties:r,textContentItemsStr:o,isOffscreenCanvasSupported:l}){this._textContentSource=t;this._isReadableStream=t instanceof ReadableStream;this._container=this._rootContainer=e;this._textDivs=a||[];this._textContentItemsStr=o||[];this._isOffscreenCanvasSupported=l;this._fontInspectorEnabled=!!globalThis.FontInspector?.enabled;this._reader=null;this._textDivProperties=r||new WeakMap;this._canceled=!1;this._capability=new s.PromiseCapability;this._layoutTextParams={prevFontSize:null,prevFontFamily:null,div:null,scale:i.scale*(globalThis.devicePixelRatio||1),properties:null,ctx:getCtx(0,l)};const{pageWidth:h,pageHeight:c,pageX:d,pageY:u}=i.rawDims;this._transform=[1,0,0,-1,-d,u+c];this._pageWidth=h;this._pageHeight=c;(0,n.setLayerDimensions)(e,i);this._capability.promise.finally((()=>{this._layoutTextParams=null})).catch((()=>{}))}get promise(){return this._capability.promise}cancel(){this._canceled=!0;if(this._reader){this._reader.cancel(new s.AbortException("TextLayer task cancelled.")).catch((()=>{}));this._reader=null}this._capability.reject(new s.AbortException("TextLayer task cancelled."))}_processItems(t,e){for(const i of t)if(void 0!==i.str){this._textContentItemsStr.push(i.str);appendText(this,i,e)}else if("beginMarkedContentProps"===i.type||"beginMarkedContent"===i.type){const t=this._container;this._container=document.createElement("span");this._container.classList.add("markedContent");null!==i.id&&this._container.setAttribute("id",`${i.id}`);t.append(this._container)}else"endMarkedContent"===i.type&&(this._container=this._container.parentNode)}_layoutText(t){const e=this._layoutTextParams.properties=this._textDivProperties.get(t);this._layoutTextParams.div=t;layout(this._layoutTextParams);e.hasText&&this._container.append(t);if(e.hasEOL){const t=document.createElement("br");t.setAttribute("role","presentation");this._container.append(t)}}_render(){const t=new s.PromiseCapability;let e=Object.create(null);if(this._isReadableStream){const pump=()=>{this._reader.read().then((({value:i,done:s})=>{if(s)t.resolve();else{Object.assign(e,i.styles);this._processItems(i.items,e);pump()}}),t.reject)};this._reader=this._textContentSource.getReader();pump()}else{if(!this._textContentSource)throw new Error('No "textContentSource" parameter specified.');{const{items:e,styles:i}=this._textContentSource;this._processItems(e,i);t.resolve()}}t.promise.then((()=>{e=null;!function render(t){if(t._canceled)return;const e=t._textDivs,i=t._capability;if(e.length>1e5)i.resolve();else{if(!t._isReadableStream)for(const i of e)t._layoutText(i);i.resolve()}}(this)}),this._capability.reject)}}e.TextLayerRenderTask=TextLayerRenderTask},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.AnnotationEditorLayer=void 0;var s=i(1),n=i(4),a=i(28),r=i(33),o=i(6),l=i(34);class AnnotationEditorLayer{#Se;#Ee=!1;#xe=null;#we=this.pointerup.bind(this);#Ce=this.pointerdown.bind(this);#Te=new Map;#Pe=!1;#Me=!1;#ke=!1;#Fe;static _initialized=!1;constructor({uiManager:t,pageIndex:e,div:i,accessibilityManager:s,annotationLayer:n,viewport:o,l10n:h}){const c=[a.FreeTextEditor,r.InkEditor,l.StampEditor];if(!AnnotationEditorLayer._initialized){AnnotationEditorLayer._initialized=!0;for(const t of c)t.initialize(h)}t.registerEditorTypes(c);this.#Fe=t;this.pageIndex=e;this.div=i;this.#Se=s;this.#xe=n;this.viewport=o;this.#Fe.addLayer(this)}get isEmpty(){return 0===this.#Te.size}updateToolbar(t){this.#Fe.updateToolbar(t)}updateMode(t=this.#Fe.getMode()){this.#Re();if(t===s.AnnotationEditorType.INK){this.addInkEditorIfNeeded(!1);this.disableClick()}else this.enableClick();if(t!==s.AnnotationEditorType.NONE){this.div.classList.toggle("freeTextEditing",t===s.AnnotationEditorType.FREETEXT);this.div.classList.toggle("inkEditing",t===s.AnnotationEditorType.INK);this.div.classList.toggle("stampEditing",t===s.AnnotationEditorType.STAMP);this.div.hidden=!1}}addInkEditorIfNeeded(t){if(!t&&this.#Fe.getMode()!==s.AnnotationEditorType.INK)return;if(!t)for(const t of this.#Te.values())if(t.isEmpty()){t.setInBackground();return}this.#De({offsetX:0,offsetY:0},!1).setInBackground()}setEditingState(t){this.#Fe.setEditingState(t)}addCommands(t){this.#Fe.addCommands(t)}enable(){this.div.style.pointerEvents="auto";const t=new Set;for(const e of this.#Te.values()){e.enableEditing();e.annotationElementId&&t.add(e.annotationElementId)}if(!this.#xe)return;const e=this.#xe.getEditableAnnotations();for(const i of e){i.hide();if(this.#Fe.isDeletedAnnotationElement(i.data.id))continue;if(t.has(i.data.id))continue;const e=this.deserialize(i);if(e){this.addOrRebuild(e);e.enableEditing()}}}disable(){this.#ke=!0;this.div.style.pointerEvents="none";const t=new Set;for(const e of this.#Te.values()){e.disableEditing();if(e.annotationElementId&&null===e.serialize()){this.getEditableAnnotation(e.annotationElementId)?.show();e.remove()}else t.add(e.annotationElementId)}if(this.#xe){const e=this.#xe.getEditableAnnotations();for(const i of e){const{id:e}=i.data;t.has(e)||this.#Fe.isDeletedAnnotationElement(e)||i.show()}}this.#Re();this.isEmpty&&(this.div.hidden=!0);this.#ke=!1}getEditableAnnotation(t){return this.#xe?.getEditableAnnotation(t)||null}setActiveEditor(t){this.#Fe.getActive()!==t&&this.#Fe.setActiveEditor(t)}enableClick(){this.div.addEventListener("pointerdown",this.#Ce);this.div.addEventListener("pointerup",this.#we)}disableClick(){this.div.removeEventListener("pointerdown",this.#Ce);this.div.removeEventListener("pointerup",this.#we)}attach(t){this.#Te.set(t.id,t);const{annotationElementId:e}=t;e&&this.#Fe.isDeletedAnnotationElement(e)&&this.#Fe.removeDeletedAnnotationElement(t)}detach(t){this.#Te.delete(t.id);this.#Se?.removePointerInTextLayer(t.contentDiv);!this.#ke&&t.annotationElementId&&this.#Fe.addDeletedAnnotationElement(t)}remove(t){this.detach(t);this.#Fe.removeEditor(t);t.div.contains(document.activeElement)&&setTimeout((()=>{this.#Fe.focusMainContainer()}),0);t.div.remove();t.isAttachedToDOM=!1;this.#Me||this.addInkEditorIfNeeded(!1)}changeParent(t){if(t.parent!==this){if(t.annotationElementId){this.#Fe.addDeletedAnnotationElement(t.annotationElementId);n.AnnotationEditor.deleteAnnotationElement(t);t.annotationElementId=null}this.attach(t);t.parent?.detach(t);t.setParent(this);if(t.div&&t.isAttachedToDOM){t.div.remove();this.div.append(t.div)}}}add(t){this.changeParent(t);this.#Fe.addEditor(t);this.attach(t);if(!t.isAttachedToDOM){const e=t.render();this.div.append(e);t.isAttachedToDOM=!0}t.fixAndSetPosition();t.onceAdded();this.#Fe.addToAnnotationStorage(t)}moveEditorInDOM(t){if(!t.isAttachedToDOM)return;const{activeElement:e}=document;if(t.div.contains(e)){t._focusEventsAllowed=!1;setTimeout((()=>{if(t.div.contains(document.activeElement))t._focusEventsAllowed=!0;else{t.div.addEventListener("focusin",(()=>{t._focusEventsAllowed=!0}),{once:!0});e.focus()}}),0)}t._structTreeParentId=this.#Se?.moveElementInDOM(this.div,t.div,t.contentDiv,!0)}addOrRebuild(t){t.needsToBeRebuilt()?t.rebuild():this.add(t)}addUndoableEditor(t){this.addCommands({cmd:()=>t._uiManager.rebuild(t),undo:()=>{t.remove()},mustExec:!1})}getNextId(){return this.#Fe.getId()}#Ie(t){switch(this.#Fe.getMode()){case s.AnnotationEditorType.FREETEXT:return new a.FreeTextEditor(t);case s.AnnotationEditorType.INK:return new r.InkEditor(t);case s.AnnotationEditorType.STAMP:return new l.StampEditor(t)}return null}pasteEditor(t,e){this.#Fe.updateToolbar(t);this.#Fe.updateMode(t);const{offsetX:i,offsetY:s}=this.#Le(),n=this.getNextId(),a=this.#Ie({parent:this,id:n,x:i,y:s,uiManager:this.#Fe,isCentered:!0,...e});a&&this.add(a)}deserialize(t){switch(t.annotationType??t.annotationEditorType){case s.AnnotationEditorType.FREETEXT:return a.FreeTextEditor.deserialize(t,this,this.#Fe);case s.AnnotationEditorType.INK:return r.InkEditor.deserialize(t,this,this.#Fe);case s.AnnotationEditorType.STAMP:return l.StampEditor.deserialize(t,this,this.#Fe)}return null}#De(t,e){const i=this.getNextId(),s=this.#Ie({parent:this,id:i,x:t.offsetX,y:t.offsetY,uiManager:this.#Fe,isCentered:e});s&&this.add(s);return s}#Le(){const{x:t,y:e,width:i,height:s}=this.div.getBoundingClientRect(),n=Math.max(0,t),a=Math.max(0,e),r=(n+Math.min(window.innerWidth,t+i))/2-t,o=(a+Math.min(window.innerHeight,e+s))/2-e,[l,h]=this.viewport.rotation%180==0?[r,o]:[o,r];return{offsetX:l,offsetY:h}}addNewEditor(){this.#De(this.#Le(),!0)}setSelected(t){this.#Fe.setSelected(t)}toggleSelected(t){this.#Fe.toggleSelected(t)}isSelected(t){return this.#Fe.isSelected(t)}unselect(t){this.#Fe.unselect(t)}pointerup(t){const{isMac:e}=s.FeatureTest.platform;if(!(0!==t.button||t.ctrlKey&&e)&&t.target===this.div&&this.#Pe){this.#Pe=!1;this.#Ee?this.#Fe.getMode()!==s.AnnotationEditorType.STAMP?this.#De(t,!1):this.#Fe.unselectAll():this.#Ee=!0}}pointerdown(t){if(this.#Pe){this.#Pe=!1;return}const{isMac:e}=s.FeatureTest.platform;if(0!==t.button||t.ctrlKey&&e)return;if(t.target!==this.div)return;this.#Pe=!0;const i=this.#Fe.getActive();this.#Ee=!i||i.isEmpty()}findNewParent(t,e,i){const s=this.#Fe.findParent(e,i);if(null===s||s===this)return!1;s.changeParent(t);return!0}destroy(){if(this.#Fe.getActive()?.parent===this){this.#Fe.commitOrRemove();this.#Fe.setActiveEditor(null)}for(const t of this.#Te.values()){this.#Se?.removePointerInTextLayer(t.contentDiv);t.setParent(null);t.isAttachedToDOM=!1;t.div.remove()}this.div=null;this.#Te.clear();this.#Fe.removeLayer(this)}#Re(){this.#Me=!0;for(const t of this.#Te.values())t.isEmpty()&&t.remove();this.#Me=!1}render({viewport:t}){this.viewport=t;(0,o.setLayerDimensions)(this.div,t);for(const t of this.#Fe.getEditors(this.pageIndex))this.add(t);this.updateMode()}update({viewport:t}){this.#Fe.commitOrRemove();this.viewport=t;(0,o.setLayerDimensions)(this.div,{rotation:t.rotation});this.updateMode()}get pageDimensions(){const{pageWidth:t,pageHeight:e}=this.viewport.rawDims;return[t,e]}}e.AnnotationEditorLayer=AnnotationEditorLayer},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.FreeTextEditor=void 0;var s=i(1),n=i(5),a=i(4),r=i(29);class FreeTextEditor extends a.AnnotationEditor{#Oe=this.editorDivBlur.bind(this);#Ne=this.editorDivFocus.bind(this);#Be=this.editorDivInput.bind(this);#Ue=this.editorDivKeydown.bind(this);#je;#ze="";#He=`${this.id}-editor`;#We;#Ge=null;static _freeTextDefaultContent="";static _internalPadding=0;static _defaultColor=null;static _defaultFontSize=10;static get _keyboardManager(){const t=FreeTextEditor.prototype,arrowChecker=t=>t.isEmpty(),e=n.AnnotationEditorUIManager.TRANSLATE_SMALL,i=n.AnnotationEditorUIManager.TRANSLATE_BIG;return(0,s.shadow)(this,"_keyboardManager",new n.KeyboardManager([[["ctrl+s","mac+meta+s","ctrl+p","mac+meta+p"],t.commitOrRemove,{bubbles:!0}],[["ctrl+Enter","mac+meta+Enter","Escape","mac+Escape"],t.commitOrRemove],[["ArrowLeft","mac+ArrowLeft"],t._translateEmpty,{args:[-e,0],checker:arrowChecker}],[["ctrl+ArrowLeft","mac+shift+ArrowLeft"],t._translateEmpty,{args:[-i,0],checker:arrowChecker}],[["ArrowRight","mac+ArrowRight"],t._translateEmpty,{args:[e,0],checker:arrowChecker}],[["ctrl+ArrowRight","mac+shift+ArrowRight"],t._translateEmpty,{args:[i,0],checker:arrowChecker}],[["ArrowUp","mac+ArrowUp"],t._translateEmpty,{args:[0,-e],checker:arrowChecker}],[["ctrl+ArrowUp","mac+shift+ArrowUp"],t._translateEmpty,{args:[0,-i],checker:arrowChecker}],[["ArrowDown","mac+ArrowDown"],t._translateEmpty,{args:[0,e],checker:arrowChecker}],[["ctrl+ArrowDown","mac+shift+ArrowDown"],t._translateEmpty,{args:[0,i],checker:arrowChecker}]]))}static _type="freetext";constructor(t){super({...t,name:"freeTextEditor"});this.#je=t.color||FreeTextEditor._defaultColor||a.AnnotationEditor._defaultLineColor;this.#We=t.fontSize||FreeTextEditor._defaultFontSize}static initialize(t){a.AnnotationEditor.initialize(t,{strings:["free_text2_default_content","editor_free_text2_aria_label"]});const e=getComputedStyle(document.documentElement);this._internalPadding=parseFloat(e.getPropertyValue("--freetext-padding"))}static updateDefaultParams(t,e){switch(t){case s.AnnotationEditorParamsType.FREETEXT_SIZE:FreeTextEditor._defaultFontSize=e;break;case s.AnnotationEditorParamsType.FREETEXT_COLOR:FreeTextEditor._defaultColor=e}}updateParams(t,e){switch(t){case s.AnnotationEditorParamsType.FREETEXT_SIZE:this.#qe(e);break;case s.AnnotationEditorParamsType.FREETEXT_COLOR:this.#Ve(e)}}static get defaultPropertiesToUpdate(){return[[s.AnnotationEditorParamsType.FREETEXT_SIZE,FreeTextEditor._defaultFontSize],[s.AnnotationEditorParamsType.FREETEXT_COLOR,FreeTextEditor._defaultColor||a.AnnotationEditor._defaultLineColor]]}get propertiesToUpdate(){return[[s.AnnotationEditorParamsType.FREETEXT_SIZE,this.#We],[s.AnnotationEditorParamsType.FREETEXT_COLOR,this.#je]]}#qe(t){const setFontsize=t=>{this.editorDiv.style.fontSize=`calc(${t}px * var(--scale-factor))`;this.translate(0,-(t-this.#We)*this.parentScale);this.#We=t;this.#$e()},e=this.#We;this.addCommands({cmd:()=>{setFontsize(t)},undo:()=>{setFontsize(e)},mustExec:!0,type:s.AnnotationEditorParamsType.FREETEXT_SIZE,overwriteIfSameType:!0,keepUndo:!0})}#Ve(t){const e=this.#je;this.addCommands({cmd:()=>{this.#je=this.editorDiv.style.color=t},undo:()=>{this.#je=this.editorDiv.style.color=e},mustExec:!0,type:s.AnnotationEditorParamsType.FREETEXT_COLOR,overwriteIfSameType:!0,keepUndo:!0})}_translateEmpty(t,e){this._uiManager.translateSelectedEditors(t,e,!0)}getInitialTranslation(){const t=this.parentScale;return[-FreeTextEditor._internalPadding*t,-(FreeTextEditor._internalPadding+this.#We)*t]}rebuild(){if(this.parent){super.rebuild();null!==this.div&&(this.isAttachedToDOM||this.parent.add(this))}}enableEditMode(){if(!this.isInEditMode()){this.parent.setEditingState(!1);this.parent.updateToolbar(s.AnnotationEditorType.FREETEXT);super.enableEditMode();this.overlayDiv.classList.remove("enabled");this.editorDiv.contentEditable=!0;this._isDraggable=!1;this.div.removeAttribute("aria-activedescendant");this.editorDiv.addEventListener("keydown",this.#Ue);this.editorDiv.addEventListener("focus",this.#Ne);this.editorDiv.addEventListener("blur",this.#Oe);this.editorDiv.addEventListener("input",this.#Be)}}disableEditMode(){if(this.isInEditMode()){this.parent.setEditingState(!0);super.disableEditMode();this.overlayDiv.classList.add("enabled");this.editorDiv.contentEditable=!1;this.div.setAttribute("aria-activedescendant",this.#He);this._isDraggable=!0;this.editorDiv.removeEventListener("keydown",this.#Ue);this.editorDiv.removeEventListener("focus",this.#Ne);this.editorDiv.removeEventListener("blur",this.#Oe);this.editorDiv.removeEventListener("input",this.#Be);this.div.focus({preventScroll:!0});this.isEditing=!1;this.parent.div.classList.add("freeTextEditing")}}focusin(t){if(this._focusEventsAllowed){super.focusin(t);t.target!==this.editorDiv&&this.editorDiv.focus()}}onceAdded(){if(this.width)this.#Xe();else{this.enableEditMode();this.editorDiv.focus();this._initialOptions?.isCentered&&this.center();this._initialOptions=null}}isEmpty(){return!this.editorDiv||""===this.editorDiv.innerText.trim()}remove(){this.isEditing=!1;if(this.parent){this.parent.setEditingState(!0);this.parent.div.classList.add("freeTextEditing")}super.remove()}#Ke(){const t=this.editorDiv.getElementsByTagName("div");if(0===t.length)return this.editorDiv.innerText;const e=[];for(const i of t)e.push(i.innerText.replace(/\r\n?|\n/,""));return e.join("\n")}#$e(){const[t,e]=this.parentDimensions;let i;if(this.isAttachedToDOM)i=this.div.getBoundingClientRect();else{const{currentLayer:t,div:e}=this,s=e.style.display;e.style.display="hidden";t.div.append(this.div);i=e.getBoundingClientRect();e.remove();e.style.display=s}if(this.rotation%180==this.parentRotation%180){this.width=i.width/t;this.height=i.height/e}else{this.width=i.height/t;this.height=i.width/e}this.fixAndSetPosition()}commit(){if(!this.isInEditMode())return;super.commit();this.disableEditMode();const t=this.#ze,e=this.#ze=this.#Ke().trimEnd();if(t===e)return;const setText=t=>{this.#ze=t;if(t){this.#Ye();this._uiManager.rebuild(this);this.#$e()}else this.remove()};this.addCommands({cmd:()=>{setText(e)},undo:()=>{setText(t)},mustExec:!1});this.#$e()}shouldGetKeyboardEvents(){return this.isInEditMode()}enterInEditMode(){this.enableEditMode();this.editorDiv.focus()}dblclick(t){this.enterInEditMode()}keydown(t){if(t.target===this.div&&"Enter"===t.key){this.enterInEditMode();t.preventDefault()}}editorDivKeydown(t){FreeTextEditor._keyboardManager.exec(this,t)}editorDivFocus(t){this.isEditing=!0}editorDivBlur(t){this.isEditing=!1}editorDivInput(t){this.parent.div.classList.toggle("freeTextEditing",this.isEmpty())}disableEditing(){this.editorDiv.setAttribute("role","comment");this.editorDiv.removeAttribute("aria-multiline")}enableEditing(){this.editorDiv.setAttribute("role","textbox");this.editorDiv.setAttribute("aria-multiline",!0)}render(){if(this.div)return this.div;let t,e;if(this.width){t=this.x;e=this.y}super.render();this.editorDiv=document.createElement("div");this.editorDiv.className="internal";this.editorDiv.setAttribute("id",this.#He);this.enableEditing();a.AnnotationEditor._l10nPromise.get("editor_free_text2_aria_label").then((t=>this.editorDiv?.setAttribute("aria-label",t)));a.AnnotationEditor._l10nPromise.get("free_text2_default_content").then((t=>this.editorDiv?.setAttribute("default-content",t)));this.editorDiv.contentEditable=!0;const{style:i}=this.editorDiv;i.fontSize=`calc(${this.#We}px * var(--scale-factor))`;i.color=this.#je;this.div.append(this.editorDiv);this.overlayDiv=document.createElement("div");this.overlayDiv.classList.add("overlay","enabled");this.div.append(this.overlayDiv);(0,n.bindEvents)(this,this.div,["dblclick","keydown"]);if(this.width){const[i,s]=this.parentDimensions;if(this.annotationElementId){const{position:n}=this.#Ge;let[a,r]=this.getInitialTranslation();[a,r]=this.pageTranslationToScreen(a,r);const[o,l]=this.pageDimensions,[h,c]=this.pageTranslation;let d,u;switch(this.rotation){case 0:d=t+(n[0]-h)/o;u=e+this.height-(n[1]-c)/l;break;case 90:d=t+(n[0]-h)/o;u=e-(n[1]-c)/l;[a,r]=[r,-a];break;case 180:d=t-this.width+(n[0]-h)/o;u=e-(n[1]-c)/l;[a,r]=[-a,-r];break;case 270:d=t+(n[0]-h-this.height*l)/o;u=e+(n[1]-c-this.width*o)/l;[a,r]=[-r,a]}this.setAt(d*i,u*s,a,r)}else this.setAt(t*i,e*s,this.width*i,this.height*s);this.#Ye();this._isDraggable=!0;this.editorDiv.contentEditable=!1}else{this._isDraggable=!1;this.editorDiv.contentEditable=!0}return this.div}#Ye(){this.editorDiv.replaceChildren();if(this.#ze)for(const t of this.#ze.split("\n")){const e=document.createElement("div");e.append(t?document.createTextNode(t):document.createElement("br"));this.editorDiv.append(e)}}get contentDiv(){return this.editorDiv}static deserialize(t,e,i){let n=null;if(t instanceof r.FreeTextAnnotationElement){const{data:{defaultAppearanceData:{fontSize:e,fontColor:i},rect:a,rotation:r,id:o},textContent:l,textPosition:h,parent:{page:{pageNumber:c}}}=t;if(!l||0===l.length)return null;n=t={annotationType:s.AnnotationEditorType.FREETEXT,color:Array.from(i),fontSize:e,value:l.join("\n"),position:h,pageIndex:c-1,rect:a,rotation:r,id:o,deleted:!1}}const a=super.deserialize(t,e,i);a.#We=t.fontSize;a.#je=s.Util.makeHexColor(...t.color);a.#ze=t.value;a.annotationElementId=t.id||null;a.#Ge=n;return a}serialize(t=!1){if(this.isEmpty())return null;if(this.deleted)return{pageIndex:this.pageIndex,id:this.annotationElementId,deleted:!0};const e=FreeTextEditor._internalPadding*this.parentScale,i=this.getRect(e,e),n=a.AnnotationEditor._colorManager.convert(this.isAttachedToDOM?getComputedStyle(this.editorDiv).color:this.#je),r={annotationType:s.AnnotationEditorType.FREETEXT,color:n,fontSize:this.#We,value:this.#ze,pageIndex:this.pageIndex,rect:i,rotation:this.rotation,structTreeParentId:this._structTreeParentId};if(t)return r;if(this.annotationElementId&&!this.#Je(r))return null;r.id=this.annotationElementId;return r}#Je(t){const{value:e,fontSize:i,color:s,rect:n,pageIndex:a}=this.#Ge;return t.value!==e||t.fontSize!==i||t.rect.some(((t,e)=>Math.abs(t-n[e])>=1))||t.color.some(((t,e)=>t!==s[e]))||t.pageIndex!==a}#Xe(t=!1){if(!this.annotationElementId)return;this.#$e();if(!t&&(0===this.width||0===this.height)){setTimeout((()=>this.#Xe(!0)),0);return}const e=FreeTextEditor._internalPadding*this.parentScale;this.#Ge.rect=this.getRect(e,e)}}e.FreeTextEditor=FreeTextEditor},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.StampAnnotationElement=e.InkAnnotationElement=e.FreeTextAnnotationElement=e.AnnotationLayer=void 0;var s=i(1),n=i(6),a=i(3),r=i(30),o=i(31),l=i(32);const h=1e3,c=new WeakSet;function getRectDims(t){return{width:t[2]-t[0],height:t[3]-t[1]}}class AnnotationElementFactory{static create(t){switch(t.data.annotationType){case s.AnnotationType.LINK:return new LinkAnnotationElement(t);case s.AnnotationType.TEXT:return new TextAnnotationElement(t);case s.AnnotationType.WIDGET:switch(t.data.fieldType){case"Tx":return new TextWidgetAnnotationElement(t);case"Btn":return t.data.radioButton?new RadioButtonWidgetAnnotationElement(t):t.data.checkBox?new CheckboxWidgetAnnotationElement(t):new PushButtonWidgetAnnotationElement(t);case"Ch":return new ChoiceWidgetAnnotationElement(t);case"Sig":return new SignatureWidgetAnnotationElement(t)}return new WidgetAnnotationElement(t);case s.AnnotationType.POPUP:return new PopupAnnotationElement(t);case s.AnnotationType.FREETEXT:return new FreeTextAnnotationElement(t);case s.AnnotationType.LINE:return new LineAnnotationElement(t);case s.AnnotationType.SQUARE:return new SquareAnnotationElement(t);case s.AnnotationType.CIRCLE:return new CircleAnnotationElement(t);case s.AnnotationType.POLYLINE:return new PolylineAnnotationElement(t);case s.AnnotationType.CARET:return new CaretAnnotationElement(t);case s.AnnotationType.INK:return new InkAnnotationElement(t);case s.AnnotationType.POLYGON:return new PolygonAnnotationElement(t);case s.AnnotationType.HIGHLIGHT:return new HighlightAnnotationElement(t);case s.AnnotationType.UNDERLINE:return new UnderlineAnnotationElement(t);case s.AnnotationType.SQUIGGLY:return new SquigglyAnnotationElement(t);case s.AnnotationType.STRIKEOUT:return new StrikeOutAnnotationElement(t);case s.AnnotationType.STAMP:return new StampAnnotationElement(t);case s.AnnotationType.FILEATTACHMENT:return new FileAttachmentAnnotationElement(t);default:return new AnnotationElement(t)}}}class AnnotationElement{#Qe=!1;constructor(t,{isRenderable:e=!1,ignoreBorder:i=!1,createQuadrilaterals:s=!1}={}){this.isRenderable=e;this.data=t.data;this.layer=t.layer;this.linkService=t.linkService;this.downloadManager=t.downloadManager;this.imageResourcesPath=t.imageResourcesPath;this.renderForms=t.renderForms;this.svgFactory=t.svgFactory;this.annotationStorage=t.annotationStorage;this.enableScripting=t.enableScripting;this.hasJSActions=t.hasJSActions;this._fieldObjects=t.fieldObjects;this.parent=t.parent;e&&(this.container=this._createContainer(i));s&&this._createQuadrilaterals()}static _hasPopupData({titleObj:t,contentsObj:e,richText:i}){return!!(t?.str||e?.str||i?.str)}get hasPopupData(){return AnnotationElement._hasPopupData(this.data)}_createContainer(t){const{data:e,parent:{page:i,viewport:n}}=this,a=document.createElement("section");a.setAttribute("data-annotation-id",e.id);this instanceof WidgetAnnotationElement||(a.tabIndex=h);a.style.zIndex=this.parent.zIndex++;this.data.popupRef&&a.setAttribute("aria-haspopup","dialog");e.noRotate&&a.classList.add("norotate");const{pageWidth:r,pageHeight:o,pageX:l,pageY:c}=n.rawDims;if(!e.rect||this instanceof PopupAnnotationElement){const{rotation:t}=e;e.hasOwnCanvas||0===t||this.setRotation(t,a);return a}const{width:d,height:u}=getRectDims(e.rect),p=s.Util.normalizeRect([e.rect[0],i.view[3]-e.rect[1]+i.view[1],e.rect[2],i.view[3]-e.rect[3]+i.view[1]]);if(!t&&e.borderStyle.width>0){a.style.borderWidth=`${e.borderStyle.width}px`;const t=e.borderStyle.horizontalCornerRadius,i=e.borderStyle.verticalCornerRadius;if(t>0||i>0){const e=`calc(${t}px * var(--scale-factor)) / calc(${i}px * var(--scale-factor))`;a.style.borderRadius=e}else if(this instanceof RadioButtonWidgetAnnotationElement){const t=`calc(${d}px * var(--scale-factor)) / calc(${u}px * var(--scale-factor))`;a.style.borderRadius=t}switch(e.borderStyle.style){case s.AnnotationBorderStyleType.SOLID:a.style.borderStyle="solid";break;case s.AnnotationBorderStyleType.DASHED:a.style.borderStyle="dashed";break;case s.AnnotationBorderStyleType.BEVELED:(0,s.warn)("Unimplemented border style: beveled");break;case s.AnnotationBorderStyleType.INSET:(0,s.warn)("Unimplemented border style: inset");break;case s.AnnotationBorderStyleType.UNDERLINE:a.style.borderBottomStyle="solid"}const n=e.borderColor||null;if(n){this.#Qe=!0;a.style.borderColor=s.Util.makeHexColor(0|n[0],0|n[1],0|n[2])}else a.style.borderWidth=0}a.style.left=100*(p[0]-l)/r+"%";a.style.top=100*(p[1]-c)/o+"%";const{rotation:g}=e;if(e.hasOwnCanvas||0===g){a.style.width=100*d/r+"%";a.style.height=100*u/o+"%"}else this.setRotation(g,a);return a}setRotation(t,e=this.container){if(!this.data.rect)return;const{pageWidth:i,pageHeight:s}=this.parent.viewport.rawDims,{width:n,height:a}=getRectDims(this.data.rect);let r,o;if(t%180==0){r=100*n/i;o=100*a/s}else{r=100*a/i;o=100*n/s}e.style.width=`${r}%`;e.style.height=`${o}%`;e.setAttribute("data-main-rotation",(360-t)%360)}get _commonActions(){const setColor=(t,e,i)=>{const s=i.detail[t],n=s[0],a=s.slice(1);i.target.style[e]=r.ColorConverters[`${n}_HTML`](a);this.annotationStorage.setValue(this.data.id,{[e]:r.ColorConverters[`${n}_rgb`](a)})};return(0,s.shadow)(this,"_commonActions",{display:t=>{const{display:e}=t.detail,i=e%2==1;this.container.style.visibility=i?"hidden":"visible";this.annotationStorage.setValue(this.data.id,{noView:i,noPrint:1===e||2===e})},print:t=>{this.annotationStorage.setValue(this.data.id,{noPrint:!t.detail.print})},hidden:t=>{const{hidden:e}=t.detail;this.container.style.visibility=e?"hidden":"visible";this.annotationStorage.setValue(this.data.id,{noPrint:e,noView:e})},focus:t=>{setTimeout((()=>t.target.focus({preventScroll:!1})),0)},userName:t=>{t.target.title=t.detail.userName},readonly:t=>{t.target.disabled=t.detail.readonly},required:t=>{this._setRequired(t.target,t.detail.required)},bgColor:t=>{setColor("bgColor","backgroundColor",t)},fillColor:t=>{setColor("fillColor","backgroundColor",t)},fgColor:t=>{setColor("fgColor","color",t)},textColor:t=>{setColor("textColor","color",t)},borderColor:t=>{setColor("borderColor","borderColor",t)},strokeColor:t=>{setColor("strokeColor","borderColor",t)},rotation:t=>{const e=t.detail.rotation;this.setRotation(e);this.annotationStorage.setValue(this.data.id,{rotation:e})}})}_dispatchEventFromSandbox(t,e){const i=this._commonActions;for(const s of Object.keys(e.detail)){const n=t[s]||i[s];n?.(e)}}_setDefaultPropertiesFromJS(t){if(!this.enableScripting)return;const e=this.annotationStorage.getRawValue(this.data.id);if(!e)return;const i=this._commonActions;for(const[s,n]of Object.entries(e)){const a=i[s];if(a){a({detail:{[s]:n},target:t});delete e[s]}}}_createQuadrilaterals(){if(!this.container)return;const{quadPoints:t}=this.data;if(!t)return;const[e,i,s,n]=this.data.rect;if(1===t.length){const[,{x:a,y:r},{x:o,y:l}]=t[0];if(s===a&&n===r&&e===o&&i===l)return}const{style:a}=this.container;let r;if(this.#Qe){const{borderColor:t,borderWidth:e}=a;a.borderWidth=0;r=["url('data:image/svg+xml;utf8,",'',``];this.container.classList.add("hasBorder")}const o=s-e,l=n-i,{svgFactory:h}=this,c=h.createElement("svg");c.classList.add("quadrilateralsContainer");c.setAttribute("width",0);c.setAttribute("height",0);const d=h.createElement("defs");c.append(d);const u=h.createElement("clipPath"),p=`clippath_${this.data.id}`;u.setAttribute("id",p);u.setAttribute("clipPathUnits","objectBoundingBox");d.append(u);for(const[,{x:i,y:s},{x:a,y:c}]of t){const t=h.createElement("rect"),d=(a-e)/o,p=(n-s)/l,g=(i-a)/o,m=(s-c)/l;t.setAttribute("x",d);t.setAttribute("y",p);t.setAttribute("width",g);t.setAttribute("height",m);u.append(t);r?.push(``)}if(this.#Qe){r.push("')");a.backgroundImage=r.join("")}this.container.append(c);this.container.style.clipPath=`url(#${p})`}_createPopup(){const{container:t,data:e}=this;t.setAttribute("aria-haspopup","dialog");const i=new PopupAnnotationElement({data:{color:e.color,titleObj:e.titleObj,modificationDate:e.modificationDate,contentsObj:e.contentsObj,richText:e.richText,parentRect:e.rect,borderStyle:0,id:`popup_${e.id}`,rotation:e.rotation},parent:this.parent,elements:[this]});this.parent.div.append(i.render())}render(){(0,s.unreachable)("Abstract method `AnnotationElement.render` called")}_getElementsByName(t,e=null){const i=[];if(this._fieldObjects){const n=this._fieldObjects[t];if(n)for(const{page:t,id:a,exportValues:r}of n){if(-1===t)continue;if(a===e)continue;const n="string"==typeof r?r:null,o=document.querySelector(`[data-element-id="${a}"]`);!o||c.has(o)?i.push({id:a,exportValue:n,domElement:o}):(0,s.warn)(`_getElementsByName - element not allowed: ${a}`)}return i}for(const s of document.getElementsByName(t)){const{exportValue:t}=s,n=s.getAttribute("data-element-id");n!==e&&(c.has(s)&&i.push({id:n,exportValue:t,domElement:s}))}return i}show(){this.container&&(this.container.hidden=!1);this.popup?.maybeShow()}hide(){this.container&&(this.container.hidden=!0);this.popup?.forceHide()}getElementsToTriggerPopup(){return this.container}addHighlightArea(){const t=this.getElementsToTriggerPopup();if(Array.isArray(t))for(const e of t)e.classList.add("highlightArea");else t.classList.add("highlightArea")}_editOnDoubleClick(){const{annotationEditorType:t,data:{id:e}}=this;this.container.addEventListener("dblclick",(()=>{this.linkService.eventBus?.dispatch("switchannotationeditormode",{source:this,mode:t,editId:e})}))}}class LinkAnnotationElement extends AnnotationElement{constructor(t,e=null){super(t,{isRenderable:!0,ignoreBorder:!!e?.ignoreBorder,createQuadrilaterals:!0});this.isTooltipOnly=t.data.isTooltipOnly}render(){const{data:t,linkService:e}=this,i=document.createElement("a");i.setAttribute("data-element-id",t.id);let s=!1;if(t.url){e.addLinkAttributes(i,t.url,t.newWindow);s=!0}else if(t.action){this._bindNamedAction(i,t.action);s=!0}else if(t.attachment){this._bindAttachment(i,t.attachment);s=!0}else if(t.setOCGState){this.#Ze(i,t.setOCGState);s=!0}else if(t.dest){this._bindLink(i,t.dest);s=!0}else{if(t.actions&&(t.actions.Action||t.actions["Mouse Up"]||t.actions["Mouse Down"])&&this.enableScripting&&this.hasJSActions){this._bindJSAction(i,t);s=!0}if(t.resetForm){this._bindResetFormAction(i,t.resetForm);s=!0}else if(this.isTooltipOnly&&!s){this._bindLink(i,"");s=!0}}this.container.classList.add("linkAnnotation");s&&this.container.append(i);return this.container}#ti(){this.container.setAttribute("data-internal-link","")}_bindLink(t,e){t.href=this.linkService.getDestinationHash(e);t.onclick=()=>{e&&this.linkService.goToDestination(e);return!1};(e||""===e)&&this.#ti()}_bindNamedAction(t,e){t.href=this.linkService.getAnchorUrl("");t.onclick=()=>{this.linkService.executeNamedAction(e);return!1};this.#ti()}_bindAttachment(t,e){t.href=this.linkService.getAnchorUrl("");t.onclick=()=>{this.downloadManager?.openOrDownloadData(this.container,e.content,e.filename);return!1};this.#ti()}#Ze(t,e){t.href=this.linkService.getAnchorUrl("");t.onclick=()=>{this.linkService.executeSetOCGState(e);return!1};this.#ti()}_bindJSAction(t,e){t.href=this.linkService.getAnchorUrl("");const i=new Map([["Action","onclick"],["Mouse Up","onmouseup"],["Mouse Down","onmousedown"]]);for(const s of Object.keys(e.actions)){const n=i.get(s);n&&(t[n]=()=>{this.linkService.eventBus?.dispatch("dispatcheventinsandbox",{source:this,detail:{id:e.id,name:s}});return!1})}t.onclick||(t.onclick=()=>!1);this.#ti()}_bindResetFormAction(t,e){const i=t.onclick;i||(t.href=this.linkService.getAnchorUrl(""));this.#ti();if(this._fieldObjects)t.onclick=()=>{i?.();const{fields:t,refs:n,include:a}=e,r=[];if(0!==t.length||0!==n.length){const e=new Set(n);for(const i of t){const t=this._fieldObjects[i]||[];for(const{id:i}of t)e.add(i)}for(const t of Object.values(this._fieldObjects))for(const i of t)e.has(i.id)===a&&r.push(i)}else for(const t of Object.values(this._fieldObjects))r.push(...t);const o=this.annotationStorage,l=[];for(const t of r){const{id:e}=t;l.push(e);switch(t.type){case"text":{const i=t.defaultValue||"";o.setValue(e,{value:i});break}case"checkbox":case"radiobutton":{const i=t.defaultValue===t.exportValues;o.setValue(e,{value:i});break}case"combobox":case"listbox":{const i=t.defaultValue||"";o.setValue(e,{value:i});break}default:continue}const i=document.querySelector(`[data-element-id="${e}"]`);i&&(c.has(i)?i.dispatchEvent(new Event("resetform")):(0,s.warn)(`_bindResetFormAction - element not allowed: ${e}`))}this.enableScripting&&this.linkService.eventBus?.dispatch("dispatcheventinsandbox",{source:this,detail:{id:"app",ids:l,name:"ResetForm"}});return!1};else{(0,s.warn)('_bindResetFormAction - "resetForm" action not supported, ensure that the `fieldObjects` parameter is provided.');i||(t.onclick=()=>!1)}}}class TextAnnotationElement extends AnnotationElement{constructor(t){super(t,{isRenderable:!0})}render(){this.container.classList.add("textAnnotation");const t=document.createElement("img");t.src=this.imageResourcesPath+"annotation-"+this.data.name.toLowerCase()+".svg";t.alt="[{{type}} Annotation]";t.dataset.l10nId="text_annotation_type";t.dataset.l10nArgs=JSON.stringify({type:this.data.name});!this.data.popupRef&&this.hasPopupData&&this._createPopup();this.container.append(t);return this.container}}class WidgetAnnotationElement extends AnnotationElement{render(){this.data.alternativeText&&(this.container.title=this.data.alternativeText);return this.container}showElementAndHideCanvas(t){if(this.data.hasOwnCanvas){"CANVAS"===t.previousSibling?.nodeName&&(t.previousSibling.hidden=!0);t.hidden=!1}}_getKeyModifier(t){const{isWin:e,isMac:i}=s.FeatureTest.platform;return e&&t.ctrlKey||i&&t.metaKey}_setEventListener(t,e,i,s,n){i.includes("mouse")?t.addEventListener(i,(t=>{this.linkService.eventBus?.dispatch("dispatcheventinsandbox",{source:this,detail:{id:this.data.id,name:s,value:n(t),shift:t.shiftKey,modifier:this._getKeyModifier(t)}})})):t.addEventListener(i,(t=>{if("blur"===i){if(!e.focused||!t.relatedTarget)return;e.focused=!1}else if("focus"===i){if(e.focused)return;e.focused=!0}n&&this.linkService.eventBus?.dispatch("dispatcheventinsandbox",{source:this,detail:{id:this.data.id,name:s,value:n(t)}})}))}_setEventListeners(t,e,i,s){for(const[n,a]of i)if("Action"===a||this.data.actions?.[a]){"Focus"!==a&&"Blur"!==a||(e||={focused:!1});this._setEventListener(t,e,n,a,s);"Focus"!==a||this.data.actions?.Blur?"Blur"!==a||this.data.actions?.Focus||this._setEventListener(t,e,"focus","Focus",null):this._setEventListener(t,e,"blur","Blur",null)}}_setBackgroundColor(t){const e=this.data.backgroundColor||null;t.style.backgroundColor=null===e?"transparent":s.Util.makeHexColor(e[0],e[1],e[2])}_setTextStyle(t){const e=["left","center","right"],{fontColor:i}=this.data.defaultAppearanceData,n=this.data.defaultAppearanceData.fontSize||9,a=t.style;let r;const roundToOneDecimal=t=>Math.round(10*t)/10;if(this.data.multiLine){const t=Math.abs(this.data.rect[3]-this.data.rect[1]-2),e=t/(Math.round(t/(s.LINE_FACTOR*n))||1);r=Math.min(n,roundToOneDecimal(e/s.LINE_FACTOR))}else{const t=Math.abs(this.data.rect[3]-this.data.rect[1]-2);r=Math.min(n,roundToOneDecimal(t/s.LINE_FACTOR))}a.fontSize=`calc(${r}px * var(--scale-factor))`;a.color=s.Util.makeHexColor(i[0],i[1],i[2]);null!==this.data.textAlignment&&(a.textAlign=e[this.data.textAlignment])}_setRequired(t,e){e?t.setAttribute("required",!0):t.removeAttribute("required");t.setAttribute("aria-required",e)}}class TextWidgetAnnotationElement extends WidgetAnnotationElement{constructor(t){super(t,{isRenderable:t.renderForms||!t.data.hasAppearance&&!!t.data.fieldValue})}setPropertyOnSiblings(t,e,i,s){const n=this.annotationStorage;for(const a of this._getElementsByName(t.name,t.id)){a.domElement&&(a.domElement[e]=i);n.setValue(a.id,{[s]:i})}}render(){const t=this.annotationStorage,e=this.data.id;this.container.classList.add("textWidgetAnnotation");let i=null;if(this.renderForms){const s=t.getValue(e,{value:this.data.fieldValue});let n=s.value||"";const a=t.getValue(e,{charLimit:this.data.maxLen}).charLimit;a&&n.length>a&&(n=n.slice(0,a));let r=s.formattedValue||this.data.textContent?.join("\n")||null;r&&this.data.comb&&(r=r.replaceAll(/\s+/g,""));const o={userValue:n,formattedValue:r,lastCommittedValue:null,commitKey:1,focused:!1};if(this.data.multiLine){i=document.createElement("textarea");i.textContent=r??n;this.data.doNotScroll&&(i.style.overflowY="hidden")}else{i=document.createElement("input");i.type="text";i.setAttribute("value",r??n);this.data.doNotScroll&&(i.style.overflowX="hidden")}this.data.hasOwnCanvas&&(i.hidden=!0);c.add(i);i.setAttribute("data-element-id",e);i.disabled=this.data.readOnly;i.name=this.data.fieldName;i.tabIndex=h;this._setRequired(i,this.data.required);a&&(i.maxLength=a);i.addEventListener("input",(s=>{t.setValue(e,{value:s.target.value});this.setPropertyOnSiblings(i,"value",s.target.value,"value");o.formattedValue=null}));i.addEventListener("resetform",(t=>{const e=this.data.defaultFieldValue??"";i.value=o.userValue=e;o.formattedValue=null}));let blurListener=t=>{const{formattedValue:e}=o;null!=e&&(t.target.value=e);t.target.scrollLeft=0};if(this.enableScripting&&this.hasJSActions){i.addEventListener("focus",(t=>{if(o.focused)return;const{target:e}=t;o.userValue&&(e.value=o.userValue);o.lastCommittedValue=e.value;o.commitKey=1;o.focused=!0}));i.addEventListener("updatefromsandbox",(i=>{this.showElementAndHideCanvas(i.target);const s={value(i){o.userValue=i.detail.value??"";t.setValue(e,{value:o.userValue.toString()});i.target.value=o.userValue},formattedValue(i){const{formattedValue:s}=i.detail;o.formattedValue=s;null!=s&&i.target!==document.activeElement&&(i.target.value=s);t.setValue(e,{formattedValue:s})},selRange(t){t.target.setSelectionRange(...t.detail.selRange)},charLimit:i=>{const{charLimit:s}=i.detail,{target:n}=i;if(0===s){n.removeAttribute("maxLength");return}n.setAttribute("maxLength",s);let a=o.userValue;if(a&&!(a.length<=s)){a=a.slice(0,s);n.value=o.userValue=a;t.setValue(e,{value:a});this.linkService.eventBus?.dispatch("dispatcheventinsandbox",{source:this,detail:{id:e,name:"Keystroke",value:a,willCommit:!0,commitKey:1,selStart:n.selectionStart,selEnd:n.selectionEnd}})}}};this._dispatchEventFromSandbox(s,i)}));i.addEventListener("keydown",(t=>{o.commitKey=1;let i=-1;"Escape"===t.key?i=0:"Enter"!==t.key||this.data.multiLine?"Tab"===t.key&&(o.commitKey=3):i=2;if(-1===i)return;const{value:s}=t.target;if(o.lastCommittedValue!==s){o.lastCommittedValue=s;o.userValue=s;this.linkService.eventBus?.dispatch("dispatcheventinsandbox",{source:this,detail:{id:e,name:"Keystroke",value:s,willCommit:!0,commitKey:i,selStart:t.target.selectionStart,selEnd:t.target.selectionEnd}})}}));const s=blurListener;blurListener=null;i.addEventListener("blur",(t=>{if(!o.focused||!t.relatedTarget)return;o.focused=!1;const{value:i}=t.target;o.userValue=i;o.lastCommittedValue!==i&&this.linkService.eventBus?.dispatch("dispatcheventinsandbox",{source:this,detail:{id:e,name:"Keystroke",value:i,willCommit:!0,commitKey:o.commitKey,selStart:t.target.selectionStart,selEnd:t.target.selectionEnd}});s(t)}));this.data.actions?.Keystroke&&i.addEventListener("beforeinput",(t=>{o.lastCommittedValue=null;const{data:i,target:s}=t,{value:n,selectionStart:a,selectionEnd:r}=s;let l=a,h=r;switch(t.inputType){case"deleteWordBackward":{const t=n.substring(0,a).match(/\w*[^\w]*$/);t&&(l-=t[0].length);break}case"deleteWordForward":{const t=n.substring(a).match(/^[^\w]*\w*/);t&&(h+=t[0].length);break}case"deleteContentBackward":a===r&&(l-=1);break;case"deleteContentForward":a===r&&(h+=1)}t.preventDefault();this.linkService.eventBus?.dispatch("dispatcheventinsandbox",{source:this,detail:{id:e,name:"Keystroke",value:n,change:i||"",willCommit:!1,selStart:l,selEnd:h}})}));this._setEventListeners(i,o,[["focus","Focus"],["blur","Blur"],["mousedown","Mouse Down"],["mouseenter","Mouse Enter"],["mouseleave","Mouse Exit"],["mouseup","Mouse Up"]],(t=>t.target.value))}blurListener&&i.addEventListener("blur",blurListener);if(this.data.comb){const t=(this.data.rect[2]-this.data.rect[0])/a;i.classList.add("comb");i.style.letterSpacing=`calc(${t}px * var(--scale-factor) - 1ch)`}}else{i=document.createElement("div");i.textContent=this.data.fieldValue;i.style.verticalAlign="middle";i.style.display="table-cell"}this._setTextStyle(i);this._setBackgroundColor(i);this._setDefaultPropertiesFromJS(i);this.container.append(i);return this.container}}class SignatureWidgetAnnotationElement extends WidgetAnnotationElement{constructor(t){super(t,{isRenderable:!!t.data.hasOwnCanvas})}}class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement{constructor(t){super(t,{isRenderable:t.renderForms})}render(){const t=this.annotationStorage,e=this.data,i=e.id;let s=t.getValue(i,{value:e.exportValue===e.fieldValue}).value;if("string"==typeof s){s="Off"!==s;t.setValue(i,{value:s})}this.container.classList.add("buttonWidgetAnnotation","checkBox");const n=document.createElement("input");c.add(n);n.setAttribute("data-element-id",i);n.disabled=e.readOnly;this._setRequired(n,this.data.required);n.type="checkbox";n.name=e.fieldName;s&&n.setAttribute("checked",!0);n.setAttribute("exportValue",e.exportValue);n.tabIndex=h;n.addEventListener("change",(s=>{const{name:n,checked:a}=s.target;for(const s of this._getElementsByName(n,i)){const i=a&&s.exportValue===e.exportValue;s.domElement&&(s.domElement.checked=i);t.setValue(s.id,{value:i})}t.setValue(i,{value:a})}));n.addEventListener("resetform",(t=>{const i=e.defaultFieldValue||"Off";t.target.checked=i===e.exportValue}));if(this.enableScripting&&this.hasJSActions){n.addEventListener("updatefromsandbox",(e=>{const s={value(e){e.target.checked="Off"!==e.detail.value;t.setValue(i,{value:e.target.checked})}};this._dispatchEventFromSandbox(s,e)}));this._setEventListeners(n,null,[["change","Validate"],["change","Action"],["focus","Focus"],["blur","Blur"],["mousedown","Mouse Down"],["mouseenter","Mouse Enter"],["mouseleave","Mouse Exit"],["mouseup","Mouse Up"]],(t=>t.target.checked))}this._setBackgroundColor(n);this._setDefaultPropertiesFromJS(n);this.container.append(n);return this.container}}class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement{constructor(t){super(t,{isRenderable:t.renderForms})}render(){this.container.classList.add("buttonWidgetAnnotation","radioButton");const t=this.annotationStorage,e=this.data,i=e.id;let s=t.getValue(i,{value:e.fieldValue===e.buttonValue}).value;if("string"==typeof s){s=s!==e.buttonValue;t.setValue(i,{value:s})}const n=document.createElement("input");c.add(n);n.setAttribute("data-element-id",i);n.disabled=e.readOnly;this._setRequired(n,this.data.required);n.type="radio";n.name=e.fieldName;s&&n.setAttribute("checked",!0);n.tabIndex=h;n.addEventListener("change",(e=>{const{name:s,checked:n}=e.target;for(const e of this._getElementsByName(s,i))t.setValue(e.id,{value:!1});t.setValue(i,{value:n})}));n.addEventListener("resetform",(t=>{const i=e.defaultFieldValue;t.target.checked=null!=i&&i===e.buttonValue}));if(this.enableScripting&&this.hasJSActions){const s=e.buttonValue;n.addEventListener("updatefromsandbox",(e=>{const n={value:e=>{const n=s===e.detail.value;for(const s of this._getElementsByName(e.target.name)){const e=n&&s.id===i;s.domElement&&(s.domElement.checked=e);t.setValue(s.id,{value:e})}}};this._dispatchEventFromSandbox(n,e)}));this._setEventListeners(n,null,[["change","Validate"],["change","Action"],["focus","Focus"],["blur","Blur"],["mousedown","Mouse Down"],["mouseenter","Mouse Enter"],["mouseleave","Mouse Exit"],["mouseup","Mouse Up"]],(t=>t.target.checked))}this._setBackgroundColor(n);this._setDefaultPropertiesFromJS(n);this.container.append(n);return this.container}}class PushButtonWidgetAnnotationElement extends LinkAnnotationElement{constructor(t){super(t,{ignoreBorder:t.data.hasAppearance})}render(){const t=super.render();t.classList.add("buttonWidgetAnnotation","pushButton");this.data.alternativeText&&(t.title=this.data.alternativeText);const e=t.lastChild;if(this.enableScripting&&this.hasJSActions&&e){this._setDefaultPropertiesFromJS(e);e.addEventListener("updatefromsandbox",(t=>{this._dispatchEventFromSandbox({},t)}))}return t}}class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement{constructor(t){super(t,{isRenderable:t.renderForms})}render(){this.container.classList.add("choiceWidgetAnnotation");const t=this.annotationStorage,e=this.data.id,i=t.getValue(e,{value:this.data.fieldValue}),s=document.createElement("select");c.add(s);s.setAttribute("data-element-id",e);s.disabled=this.data.readOnly;this._setRequired(s,this.data.required);s.name=this.data.fieldName;s.tabIndex=h;let n=this.data.combo&&this.data.options.length>0;if(!this.data.combo){s.size=this.data.options.length;this.data.multiSelect&&(s.multiple=!0)}s.addEventListener("resetform",(t=>{const e=this.data.defaultFieldValue;for(const t of s.options)t.selected=t.value===e}));for(const t of this.data.options){const e=document.createElement("option");e.textContent=t.displayValue;e.value=t.exportValue;if(i.value.includes(t.exportValue)){e.setAttribute("selected",!0);n=!1}s.append(e)}let a=null;if(n){const t=document.createElement("option");t.value=" ";t.setAttribute("hidden",!0);t.setAttribute("selected",!0);s.prepend(t);a=()=>{t.remove();s.removeEventListener("input",a);a=null};s.addEventListener("input",a)}const getValue=t=>{const e=t?"value":"textContent",{options:i,multiple:n}=s;return n?Array.prototype.filter.call(i,(t=>t.selected)).map((t=>t[e])):-1===i.selectedIndex?null:i[i.selectedIndex][e]};let r=getValue(!1);const getItems=t=>{const e=t.target.options;return Array.prototype.map.call(e,(t=>({displayValue:t.textContent,exportValue:t.value})))};if(this.enableScripting&&this.hasJSActions){s.addEventListener("updatefromsandbox",(i=>{const n={value(i){a?.();const n=i.detail.value,o=new Set(Array.isArray(n)?n:[n]);for(const t of s.options)t.selected=o.has(t.value);t.setValue(e,{value:getValue(!0)});r=getValue(!1)},multipleSelection(t){s.multiple=!0},remove(i){const n=s.options,a=i.detail.remove;n[a].selected=!1;s.remove(a);if(n.length>0){-1===Array.prototype.findIndex.call(n,(t=>t.selected))&&(n[0].selected=!0)}t.setValue(e,{value:getValue(!0),items:getItems(i)});r=getValue(!1)},clear(i){for(;0!==s.length;)s.remove(0);t.setValue(e,{value:null,items:[]});r=getValue(!1)},insert(i){const{index:n,displayValue:a,exportValue:o}=i.detail.insert,l=s.children[n],h=document.createElement("option");h.textContent=a;h.value=o;l?l.before(h):s.append(h);t.setValue(e,{value:getValue(!0),items:getItems(i)});r=getValue(!1)},items(i){const{items:n}=i.detail;for(;0!==s.length;)s.remove(0);for(const t of n){const{displayValue:e,exportValue:i}=t,n=document.createElement("option");n.textContent=e;n.value=i;s.append(n)}s.options.length>0&&(s.options[0].selected=!0);t.setValue(e,{value:getValue(!0),items:getItems(i)});r=getValue(!1)},indices(i){const s=new Set(i.detail.indices);for(const t of i.target.options)t.selected=s.has(t.index);t.setValue(e,{value:getValue(!0)});r=getValue(!1)},editable(t){t.target.disabled=!t.detail.editable}};this._dispatchEventFromSandbox(n,i)}));s.addEventListener("input",(i=>{const s=getValue(!0);t.setValue(e,{value:s});i.preventDefault();this.linkService.eventBus?.dispatch("dispatcheventinsandbox",{source:this,detail:{id:e,name:"Keystroke",value:r,changeEx:s,willCommit:!1,commitKey:1,keyDown:!1}})}));this._setEventListeners(s,null,[["focus","Focus"],["blur","Blur"],["mousedown","Mouse Down"],["mouseenter","Mouse Enter"],["mouseleave","Mouse Exit"],["mouseup","Mouse Up"],["input","Action"],["input","Validate"]],(t=>t.target.value))}else s.addEventListener("input",(function(i){t.setValue(e,{value:getValue(!0)})}));this.data.combo&&this._setTextStyle(s);this._setBackgroundColor(s);this._setDefaultPropertiesFromJS(s);this.container.append(s);return this.container}}class PopupAnnotationElement extends AnnotationElement{constructor(t){const{data:e,elements:i}=t;super(t,{isRenderable:AnnotationElement._hasPopupData(e)});this.elements=i}render(){this.container.classList.add("popupAnnotation");const t=new PopupElement({container:this.container,color:this.data.color,titleObj:this.data.titleObj,modificationDate:this.data.modificationDate,contentsObj:this.data.contentsObj,richText:this.data.richText,rect:this.data.rect,parentRect:this.data.parentRect||null,parent:this.parent,elements:this.elements,open:this.data.open}),e=[];for(const i of this.elements){i.popup=t;e.push(i.data.id);i.addHighlightArea()}this.container.setAttribute("aria-controls",e.map((t=>`${s.AnnotationPrefix}${t}`)).join(","));return this.container}}class PopupElement{#ei=null;#ii=this.#si.bind(this);#ni=this.#ai.bind(this);#ri=this.#oi.bind(this);#li=this.#hi.bind(this);#je=null;#Rt=null;#ci=null;#di=null;#ui=null;#pi=null;#gi=!1;#mi=null;#fi=null;#bi=null;#Ai=null;#_i=!1;constructor({container:t,color:e,elements:i,titleObj:s,modificationDate:a,contentsObj:r,richText:o,parent:l,rect:h,parentRect:c,open:d}){this.#Rt=t;this.#Ai=s;this.#ci=r;this.#bi=o;this.#ui=l;this.#je=e;this.#fi=h;this.#pi=c;this.#di=i;const u=n.PDFDateString.toDateObject(a);u&&(this.#ei=l.l10n.get("annotation_date_string",{date:u.toLocaleDateString(),time:u.toLocaleTimeString()}));this.trigger=i.flatMap((t=>t.getElementsToTriggerPopup()));for(const t of this.trigger){t.addEventListener("click",this.#li);t.addEventListener("mouseenter",this.#ri);t.addEventListener("mouseleave",this.#ni);t.classList.add("popupTriggerArea")}for(const t of i)t.container?.addEventListener("keydown",this.#ii);this.#Rt.hidden=!0;d&&this.#hi()}render(){if(this.#mi)return;const{page:{view:t},viewport:{rawDims:{pageWidth:e,pageHeight:i,pageX:n,pageY:a}}}=this.#ui,r=this.#mi=document.createElement("div");r.className="popup";if(this.#je){const t=r.style.outlineColor=s.Util.makeHexColor(...this.#je);if(CSS.supports("background-color","color-mix(in srgb, red 30%, white)"))r.style.backgroundColor=`color-mix(in srgb, ${t} 30%, white)`;else{const t=.7;r.style.backgroundColor=s.Util.makeHexColor(...this.#je.map((e=>Math.floor(t*(255-e)+e))))}}const o=document.createElement("span");o.className="header";const h=document.createElement("h1");o.append(h);({dir:h.dir,str:h.textContent}=this.#Ai);r.append(o);if(this.#ei){const t=document.createElement("span");t.classList.add("popupDate");this.#ei.then((e=>{t.textContent=e}));o.append(t)}const c=this.#ci,d=this.#bi;if(!d?.str||c?.str&&c.str!==d.str){const t=this._formatContents(c);r.append(t)}else{l.XfaLayer.render({xfaHtml:d.html,intent:"richText",div:r});r.lastChild.classList.add("richText","popupContent")}let u=!!this.#pi,p=u?this.#pi:this.#fi;for(const t of this.#di)if(!p||null!==s.Util.intersect(t.data.rect,p)){p=t.data.rect;u=!0;break}const g=s.Util.normalizeRect([p[0],t[3]-p[1]+t[1],p[2],t[3]-p[3]+t[1]]),m=u?p[2]-p[0]+5:0,f=g[0]+m,b=g[1],{style:A}=this.#Rt;A.left=100*(f-n)/e+"%";A.top=100*(b-a)/i+"%";this.#Rt.append(r)}_formatContents({str:t,dir:e}){const i=document.createElement("p");i.classList.add("popupContent");i.dir=e;const s=t.split(/(?:\r\n?|\n)/);for(let t=0,e=s.length;t{"Enter"===t.key&&(n?t.metaKey:t.ctrlKey)&&this.#Ci()}));!e.popupRef&&this.hasPopupData?this._createPopup():i.classList.add("popupTriggerArea");t.append(i);return t}getElementsToTriggerPopup(){return this.#wi}addHighlightArea(){this.container.classList.add("highlightArea")}#Ci(){this.downloadManager?.openOrDownloadData(this.container,this.content,this.filename)}}e.AnnotationLayer=class AnnotationLayer{#Se=null;#Ti=null;#Pi=new Map;constructor({div:t,accessibilityManager:e,annotationCanvasMap:i,l10n:s,page:n,viewport:a}){this.div=t;this.#Se=e;this.#Ti=i;this.l10n=s;this.page=n;this.viewport=a;this.zIndex=0;this.l10n||=o.NullL10n}#Mi(t,e){const i=t.firstChild||t;i.id=`${s.AnnotationPrefix}${e}`;this.div.append(t);this.#Se?.moveElementInDOM(this.div,t,i,!1)}async render(t){const{annotations:e}=t,i=this.div;(0,n.setLayerDimensions)(i,this.viewport);const r=new Map,o={data:null,layer:i,linkService:t.linkService,downloadManager:t.downloadManager,imageResourcesPath:t.imageResourcesPath||"",renderForms:!1!==t.renderForms,svgFactory:new n.DOMSVGFactory,annotationStorage:t.annotationStorage||new a.AnnotationStorage,enableScripting:!0===t.enableScripting,hasJSActions:t.hasJSActions,fieldObjects:t.fieldObjects,parent:this,elements:null};for(const t of e){if(t.noHTML)continue;const e=t.annotationType===s.AnnotationType.POPUP;if(e){const e=r.get(t.id);if(!e)continue;o.elements=e}else{const{width:e,height:i}=getRectDims(t.rect);if(e<=0||i<=0)continue}o.data=t;const i=AnnotationElementFactory.create(o);if(!i.isRenderable)continue;if(!e&&t.popupRef){const e=r.get(t.popupRef);e?e.push(i):r.set(t.popupRef,[i])}i.annotationEditorType>0&&this.#Pi.set(i.data.id,i);const n=i.render();t.hidden&&(n.style.visibility="hidden");this.#Mi(n,t.id)}this.#ki();await this.l10n.translate(i)}update({viewport:t}){const e=this.div;this.viewport=t;(0,n.setLayerDimensions)(e,{rotation:t.rotation});this.#ki();e.hidden=!1}#ki(){if(!this.#Ti)return;const t=this.div;for(const[e,i]of this.#Ti){const s=t.querySelector(`[data-annotation-id="${e}"]`);if(!s)continue;const{firstChild:n}=s;n?"CANVAS"===n.nodeName?n.replaceWith(i):n.before(i):s.append(i)}this.#Ti.clear()}getEditableAnnotations(){return Array.from(this.#Pi.values())}getEditableAnnotation(t){return this.#Pi.get(t)}}},(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});e.ColorConverters=void 0;function makeColorComp(t){return Math.floor(255*Math.max(0,Math.min(1,t))).toString(16).padStart(2,"0")}function scaleAndClamp(t){return Math.max(0,Math.min(255,255*t))}e.ColorConverters=class ColorConverters{static CMYK_G([t,e,i,s]){return["G",1-Math.min(1,.3*t+.59*i+.11*e+s)]}static G_CMYK([t]){return["CMYK",0,0,0,1-t]}static G_RGB([t]){return["RGB",t,t,t]}static G_rgb([t]){return[t=scaleAndClamp(t),t,t]}static G_HTML([t]){const e=makeColorComp(t);return`#${e}${e}${e}`}static RGB_G([t,e,i]){return["G",.3*t+.59*e+.11*i]}static RGB_rgb(t){return t.map(scaleAndClamp)}static RGB_HTML(t){return`#${t.map(makeColorComp).join("")}`}static T_HTML(){return"#00000000"}static T_rgb(){return[null]}static CMYK_RGB([t,e,i,s]){return["RGB",1-Math.min(1,t+s),1-Math.min(1,i+s),1-Math.min(1,e+s)]}static CMYK_rgb([t,e,i,s]){return[scaleAndClamp(1-Math.min(1,t+s)),scaleAndClamp(1-Math.min(1,i+s)),scaleAndClamp(1-Math.min(1,e+s))]}static CMYK_HTML(t){const e=this.CMYK_RGB(t).slice(1);return this.RGB_HTML(e)}static RGB_CMYK([t,e,i]){const s=1-t,n=1-e,a=1-i;return["CMYK",s,n,a,Math.min(s,n,a)]}}},(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0});e.NullL10n=void 0;e.getL10nFallback=getL10nFallback;const i={of_pages:"of {{pagesCount}}",page_of_pages:"({{pageNumber}} of {{pagesCount}})",document_properties_kb:"{{size_kb}} KB ({{size_b}} bytes)",document_properties_mb:"{{size_mb}} MB ({{size_b}} bytes)",document_properties_date_string:"{{date}}, {{time}}",document_properties_page_size_unit_inches:"in",document_properties_page_size_unit_millimeters:"mm",document_properties_page_size_orientation_portrait:"portrait",document_properties_page_size_orientation_landscape:"landscape",document_properties_page_size_name_a3:"A3",document_properties_page_size_name_a4:"A4",document_properties_page_size_name_letter:"Letter",document_properties_page_size_name_legal:"Legal",document_properties_page_size_dimension_string:"{{width}} × {{height}} {{unit}} ({{orientation}})",document_properties_page_size_dimension_name_string:"{{width}} × {{height}} {{unit}} ({{name}}, {{orientation}})",document_properties_linearized_yes:"Yes",document_properties_linearized_no:"No",additional_layers:"Additional Layers",page_landmark:"Page {{page}}",thumb_page_title:"Page {{page}}",thumb_page_canvas:"Thumbnail of Page {{page}}",find_reached_top:"Reached top of document, continued from bottom",find_reached_bottom:"Reached end of document, continued from top","find_match_count[one]":"{{current}} of {{total}} match","find_match_count[other]":"{{current}} of {{total}} matches","find_match_count_limit[one]":"More than {{limit}} match","find_match_count_limit[other]":"More than {{limit}} matches",find_not_found:"Phrase not found",page_scale_width:"Page Width",page_scale_fit:"Page Fit",page_scale_auto:"Automatic Zoom",page_scale_actual:"Actual Size",page_scale_percent:"{{scale}}%",loading_error:"An error occurred while loading the PDF.",invalid_file_error:"Invalid or corrupted PDF file.",missing_file_error:"Missing PDF file.",unexpected_response_error:"Unexpected server response.",rendering_error:"An error occurred while rendering the page.",annotation_date_string:"{{date}}, {{time}}",printing_not_supported:"Warning: Printing is not fully supported by this browser.",printing_not_ready:"Warning: The PDF is not fully loaded for printing.",web_fonts_disabled:"Web fonts are disabled: unable to use embedded PDF fonts.",free_text2_default_content:"Start typing…",editor_free_text2_aria_label:"Text Editor",editor_ink2_aria_label:"Draw Editor",editor_ink_canvas_aria_label:"User-created image",editor_alt_text_button_label:"Alt text",editor_alt_text_edit_button_label:"Edit alt text",editor_alt_text_decorative_tooltip:"Marked as decorative",print_progress_percent:"{{progress}}%"};function getL10nFallback(t,e){switch(t){case"find_match_count":t=`find_match_count[${1===e.total?"one":"other"}]`;break;case"find_match_count_limit":t=`find_match_count_limit[${1===e.limit?"one":"other"}]`}return i[t]||""}const s={getLanguage:async()=>"en-us",getDirection:async()=>"ltr",get:async(t,e=null,i=getL10nFallback(t,e))=>function formatL10nValue(t,e){return e?t.replaceAll(/\{\{\s*(\w+)\s*\}\}/g,((t,i)=>i in e?e[i]:"{{"+i+"}}")):t}(i,e),async translate(t){}};e.NullL10n=s},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.XfaLayer=void 0;var s=i(25);e.XfaLayer=class XfaLayer{static setupStorage(t,e,i,s,n){const a=s.getValue(e,{value:null});switch(i.name){case"textarea":null!==a.value&&(t.textContent=a.value);if("print"===n)break;t.addEventListener("input",(t=>{s.setValue(e,{value:t.target.value})}));break;case"input":if("radio"===i.attributes.type||"checkbox"===i.attributes.type){a.value===i.attributes.xfaOn?t.setAttribute("checked",!0):a.value===i.attributes.xfaOff&&t.removeAttribute("checked");if("print"===n)break;t.addEventListener("change",(t=>{s.setValue(e,{value:t.target.checked?t.target.getAttribute("xfaOn"):t.target.getAttribute("xfaOff")})}))}else{null!==a.value&&t.setAttribute("value",a.value);if("print"===n)break;t.addEventListener("input",(t=>{s.setValue(e,{value:t.target.value})}))}break;case"select":if(null!==a.value){t.setAttribute("value",a.value);for(const t of i.children)t.attributes.value===a.value?t.attributes.selected=!0:t.attributes.hasOwnProperty("selected")&&delete t.attributes.selected}t.addEventListener("input",(t=>{const i=t.target.options,n=-1===i.selectedIndex?"":i[i.selectedIndex].value;s.setValue(e,{value:n})}))}}static setAttributes({html:t,element:e,storage:i=null,intent:s,linkService:n}){const{attributes:a}=e,r=t instanceof HTMLAnchorElement;"radio"===a.type&&(a.name=`${a.name}-${s}`);for(const[e,i]of Object.entries(a))if(null!=i)switch(e){case"class":i.length&&t.setAttribute(e,i.join(" "));break;case"dataId":break;case"id":t.setAttribute("data-element-id",i);break;case"style":Object.assign(t.style,i);break;case"textContent":t.textContent=i;break;default:(!r||"href"!==e&&"newWindow"!==e)&&t.setAttribute(e,i)}r&&n.addLinkAttributes(t,a.href,a.newWindow);i&&a.dataId&&this.setupStorage(t,a.dataId,e,i)}static render(t){const e=t.annotationStorage,i=t.linkService,n=t.xfaHtml,a=t.intent||"display",r=document.createElement(n.name);n.attributes&&this.setAttributes({html:r,element:n,intent:a,linkService:i});const o=[[n,-1,r]],l=t.div;l.append(r);if(t.viewport){const e=`matrix(${t.viewport.transform.join(",")})`;l.style.transform=e}"richText"!==a&&l.setAttribute("class","xfaLayer xfaFont");const h=[];for(;o.length>0;){const[t,n,r]=o.at(-1);if(n+1===t.children.length){o.pop();continue}const l=t.children[++o.at(-1)[1]];if(null===l)continue;const{name:c}=l;if("#text"===c){const t=document.createTextNode(l.value);h.push(t);r.append(t);continue}const d=l?.attributes?.xmlns?document.createElementNS(l.attributes.xmlns,c):document.createElement(c);r.append(d);l.attributes&&this.setAttributes({html:d,element:l,storage:e,intent:a,linkService:i});if(l.children&&l.children.length>0)o.push([l,-1,d]);else if(l.value){const t=document.createTextNode(l.value);s.XfaText.shouldBuildText(c)&&h.push(t);d.append(t)}}for(const t of l.querySelectorAll(".xfaNonInteractive input, .xfaNonInteractive textarea"))t.setAttribute("readOnly",!0);return{textDivs:h}}static update(t){const e=`matrix(${t.viewport.transform.join(",")})`;t.div.style.transform=e;t.div.hidden=!1}}},(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0});e.InkEditor=void 0;var s=i(1),n=i(4),a=i(29),r=i(6),o=i(5);class InkEditor extends n.AnnotationEditor{#Fi=0;#Ri=0;#Di=this.canvasPointermove.bind(this);#Ii=this.canvasPointerleave.bind(this);#Li=this.canvasPointerup.bind(this);#Oi=this.canvasPointerdown.bind(this);#Ni=new Path2D;#Bi=!1;#Ui=!1;#ji=!1;#zi=null;#Hi=0;#Wi=0;#Gi=null;static _defaultColor=null;static _defaultOpacity=1;static _defaultThickness=1;static _type="ink";constructor(t){super({...t,name:"inkEditor"});this.color=t.color||null;this.thickness=t.thickness||null;this.opacity=t.opacity||null;this.paths=[];this.bezierPath2D=[];this.allRawPaths=[];this.currentPath=[];this.scaleFactor=1;this.translationX=this.translationY=0;this.x=0;this.y=0;this._willKeepAspectRatio=!0}static initialize(t){n.AnnotationEditor.initialize(t,{strings:["editor_ink_canvas_aria_label","editor_ink2_aria_label"]})}static updateDefaultParams(t,e){switch(t){case s.AnnotationEditorParamsType.INK_THICKNESS:InkEditor._defaultThickness=e;break;case s.AnnotationEditorParamsType.INK_COLOR:InkEditor._defaultColor=e;break;case s.AnnotationEditorParamsType.INK_OPACITY:InkEditor._defaultOpacity=e/100}}updateParams(t,e){switch(t){case s.AnnotationEditorParamsType.INK_THICKNESS:this.#qi(e);break;case s.AnnotationEditorParamsType.INK_COLOR:this.#Ve(e);break;case s.AnnotationEditorParamsType.INK_OPACITY:this.#Vi(e)}}static get defaultPropertiesToUpdate(){return[[s.AnnotationEditorParamsType.INK_THICKNESS,InkEditor._defaultThickness],[s.AnnotationEditorParamsType.INK_COLOR,InkEditor._defaultColor||n.AnnotationEditor._defaultLineColor],[s.AnnotationEditorParamsType.INK_OPACITY,Math.round(100*InkEditor._defaultOpacity)]]}get propertiesToUpdate(){return[[s.AnnotationEditorParamsType.INK_THICKNESS,this.thickness||InkEditor._defaultThickness],[s.AnnotationEditorParamsType.INK_COLOR,this.color||InkEditor._defaultColor||n.AnnotationEditor._defaultLineColor],[s.AnnotationEditorParamsType.INK_OPACITY,Math.round(100*(this.opacity??InkEditor._defaultOpacity))]]}#qi(t){const e=this.thickness;this.addCommands({cmd:()=>{this.thickness=t;this.#$i()},undo:()=>{this.thickness=e;this.#$i()},mustExec:!0,type:s.AnnotationEditorParamsType.INK_THICKNESS,overwriteIfSameType:!0,keepUndo:!0})}#Ve(t){const e=this.color;this.addCommands({cmd:()=>{this.color=t;this.#Xi()},undo:()=>{this.color=e;this.#Xi()},mustExec:!0,type:s.AnnotationEditorParamsType.INK_COLOR,overwriteIfSameType:!0,keepUndo:!0})}#Vi(t){t/=100;const e=this.opacity;this.addCommands({cmd:()=>{this.opacity=t;this.#Xi()},undo:()=>{this.opacity=e;this.#Xi()},mustExec:!0,type:s.AnnotationEditorParamsType.INK_OPACITY,overwriteIfSameType:!0,keepUndo:!0})}rebuild(){if(this.parent){super.rebuild();if(null!==this.div){if(!this.canvas){this.#Ki();this.#Yi()}if(!this.isAttachedToDOM){this.parent.add(this);this.#Ji()}this.#$i()}}}remove(){if(null!==this.canvas){this.isEmpty()||this.commit();this.canvas.width=this.canvas.height=0;this.canvas.remove();this.canvas=null;this.#zi.disconnect();this.#zi=null;super.remove()}}setParent(t){!this.parent&&t?this._uiManager.removeShouldRescale(this):this.parent&&null===t&&this._uiManager.addShouldRescale(this);super.setParent(t)}onScaleChanging(){const[t,e]=this.parentDimensions,i=this.width*t,s=this.height*e;this.setDimensions(i,s)}enableEditMode(){if(!this.#Bi&&null!==this.canvas){super.enableEditMode();this._isDraggable=!1;this.canvas.addEventListener("pointerdown",this.#Oi)}}disableEditMode(){if(this.isInEditMode()&&null!==this.canvas){super.disableEditMode();this._isDraggable=!this.isEmpty();this.div.classList.remove("editing");this.canvas.removeEventListener("pointerdown",this.#Oi)}}onceAdded(){this._isDraggable=!this.isEmpty()}isEmpty(){return 0===this.paths.length||1===this.paths.length&&0===this.paths[0].length}#Qi(){const{parentRotation:t,parentDimensions:[e,i]}=this;switch(t){case 90:return[0,i,i,e];case 180:return[e,i,e,i];case 270:return[e,0,i,e];default:return[0,0,e,i]}}#Zi(){const{ctx:t,color:e,opacity:i,thickness:s,parentScale:n,scaleFactor:a}=this;t.lineWidth=s*n/a;t.lineCap="round";t.lineJoin="round";t.miterLimit=10;t.strokeStyle=`${e}${(0,o.opacityToHex)(i)}`}#ts(t,e){this.canvas.addEventListener("contextmenu",r.noContextMenu);this.canvas.addEventListener("pointerleave",this.#Ii);this.canvas.addEventListener("pointermove",this.#Di);this.canvas.addEventListener("pointerup",this.#Li);this.canvas.removeEventListener("pointerdown",this.#Oi);this.isEditing=!0;if(!this.#ji){this.#ji=!0;this.#Ji();this.thickness||=InkEditor._defaultThickness;this.color||=InkEditor._defaultColor||n.AnnotationEditor._defaultLineColor;this.opacity??=InkEditor._defaultOpacity}this.currentPath.push([t,e]);this.#Ui=!1;this.#Zi();this.#Gi=()=>{this.#es();this.#Gi&&window.requestAnimationFrame(this.#Gi)};window.requestAnimationFrame(this.#Gi)}#is(t,e){const[i,s]=this.currentPath.at(-1);if(this.currentPath.length>1&&t===i&&e===s)return;const n=this.currentPath;let a=this.#Ni;n.push([t,e]);this.#Ui=!0;if(n.length<=2){a.moveTo(...n[0]);a.lineTo(t,e)}else{if(3===n.length){this.#Ni=a=new Path2D;a.moveTo(...n[0])}this.#ss(a,...n.at(-3),...n.at(-2),t,e)}}#ns(){if(0===this.currentPath.length)return;const t=this.currentPath.at(-1);this.#Ni.lineTo(...t)}#as(t,e){this.#Gi=null;t=Math.min(Math.max(t,0),this.canvas.width);e=Math.min(Math.max(e,0),this.canvas.height);this.#is(t,e);this.#ns();let i;if(1!==this.currentPath.length)i=this.#rs();else{const s=[t,e];i=[[s,s.slice(),s.slice(),s]]}const s=this.#Ni,n=this.currentPath;this.currentPath=[];this.#Ni=new Path2D;this.addCommands({cmd:()=>{this.allRawPaths.push(n);this.paths.push(i);this.bezierPath2D.push(s);this.rebuild()},undo:()=>{this.allRawPaths.pop();this.paths.pop();this.bezierPath2D.pop();if(0===this.paths.length)this.remove();else{if(!this.canvas){this.#Ki();this.#Yi()}this.#$i()}},mustExec:!0})}#es(){if(!this.#Ui)return;this.#Ui=!1;const t=Math.ceil(this.thickness*this.parentScale),e=this.currentPath.slice(-3),i=e.map((t=>t[0])),s=e.map((t=>t[1])),{ctx:n}=(Math.min(...i),Math.max(...i),Math.min(...s),Math.max(...s),this);n.save();n.clearRect(0,0,this.canvas.width,this.canvas.height);for(const t of this.bezierPath2D)n.stroke(t);n.stroke(this.#Ni);n.restore()}#ss(t,e,i,s,n,a,r){const o=(e+s)/2,l=(i+n)/2,h=(s+a)/2,c=(n+r)/2;t.bezierCurveTo(o+2*(s-o)/3,l+2*(n-l)/3,h+2*(s-h)/3,c+2*(n-c)/3,h,c)}#rs(){const t=this.currentPath;if(t.length<=2)return[[t[0],t[0],t.at(-1),t.at(-1)]];const e=[];let i,[s,n]=t[0];for(i=1;i{this.canvas.removeEventListener("contextmenu",r.noContextMenu)}),10);this.#as(t.offsetX,t.offsetY);this.addToAnnotationStorage();this.setInBackground()}#Ki(){this.canvas=document.createElement("canvas");this.canvas.width=this.canvas.height=0;this.canvas.className="inkEditorCanvas";n.AnnotationEditor._l10nPromise.get("editor_ink_canvas_aria_label").then((t=>this.canvas?.setAttribute("aria-label",t)));this.div.append(this.canvas);this.ctx=this.canvas.getContext("2d")}#Yi(){this.#zi=new ResizeObserver((t=>{const e=t[0].contentRect;e.width&&e.height&&this.setDimensions(e.width,e.height)}));this.#zi.observe(this.div)}get isResizable(){return!this.isEmpty()&&this.#Bi}render(){if(this.div)return this.div;let t,e;if(this.width){t=this.x;e=this.y}super.render();n.AnnotationEditor._l10nPromise.get("editor_ink2_aria_label").then((t=>this.div?.setAttribute("aria-label",t)));const[i,s,a,r]=this.#Qi();this.setAt(i,s,0,0);this.setDims(a,r);this.#Ki();if(this.width){const[i,s]=this.parentDimensions;this.setAspectRatio(this.width*i,this.height*s);this.setAt(t*i,e*s,this.width*i,this.height*s);this.#ji=!0;this.#Ji();this.setDims(this.width*i,this.height*s);this.#Xi();this.div.classList.add("disabled")}else{this.div.classList.add("editing");this.enableEditMode()}this.#Yi();return this.div}#Ji(){if(!this.#ji)return;const[t,e]=this.parentDimensions;this.canvas.width=Math.ceil(this.width*t);this.canvas.height=Math.ceil(this.height*e);this.#os()}setDimensions(t,e){const i=Math.round(t),s=Math.round(e);if(this.#Hi===i&&this.#Wi===s)return;this.#Hi=i;this.#Wi=s;this.canvas.style.visibility="hidden";const[n,a]=this.parentDimensions;this.width=t/n;this.height=e/a;this.fixAndSetPosition();this.#Bi&&this.#hs(t,e);this.#Ji();this.#Xi();this.canvas.style.visibility="visible";this.fixDims()}#hs(t,e){const i=this.#cs(),s=(t-i)/this.#Ri,n=(e-i)/this.#Fi;this.scaleFactor=Math.min(s,n)}#os(){const t=this.#cs()/2;this.ctx.setTransform(this.scaleFactor,0,0,this.scaleFactor,this.translationX*this.scaleFactor+t,this.translationY*this.scaleFactor+t)}static#ds(t){const e=new Path2D;for(let i=0,s=t.length;i{Object.defineProperty(e,"__esModule",{value:!0});e.StampEditor=void 0;var s=i(1),n=i(4),a=i(6),r=i(29);class StampEditor extends n.AnnotationEditor{#fs=null;#bs=null;#As=null;#_s=null;#vs=null;#ys=null;#zi=null;#Ss=null;#Es=!1;#xs=!1;static _type="stamp";constructor(t){super({...t,name:"stampEditor"});this.#_s=t.bitmapUrl;this.#vs=t.bitmapFile}static initialize(t){n.AnnotationEditor.initialize(t)}static get supportedTypes(){return(0,s.shadow)(this,"supportedTypes",["apng","avif","bmp","gif","jpeg","png","svg+xml","webp","x-icon"].map((t=>`image/${t}`)))}static get supportedTypesStr(){return(0,s.shadow)(this,"supportedTypesStr",this.supportedTypes.join(","))}static isHandlingMimeForPasting(t){return this.supportedTypes.includes(t)}static paste(t,e){e.pasteEditor(s.AnnotationEditorType.STAMP,{bitmapFile:t.getAsFile()})}#ws(t,e=!1){if(t){this.#fs=t.bitmap;if(!e){this.#bs=t.id;this.#Es=t.isSvg}this.#Ki()}else this.remove()}#Cs(){this.#As=null;this._uiManager.enableWaiting(!1);this.#ys&&this.div.focus()}#Ts(){if(this.#bs){this._uiManager.enableWaiting(!0);this._uiManager.imageManager.getFromId(this.#bs).then((t=>this.#ws(t,!0))).finally((()=>this.#Cs()));return}if(this.#_s){const t=this.#_s;this.#_s=null;this._uiManager.enableWaiting(!0);this.#As=this._uiManager.imageManager.getFromUrl(t).then((t=>this.#ws(t))).finally((()=>this.#Cs()));return}if(this.#vs){const t=this.#vs;this.#vs=null;this._uiManager.enableWaiting(!0);this.#As=this._uiManager.imageManager.getFromFile(t).then((t=>this.#ws(t))).finally((()=>this.#Cs()));return}const t=document.createElement("input");t.type="file";t.accept=StampEditor.supportedTypesStr;this.#As=new Promise((e=>{t.addEventListener("change",(async()=>{if(t.files&&0!==t.files.length){this._uiManager.enableWaiting(!0);const e=await this._uiManager.imageManager.getFromFile(t.files[0]);this.#ws(e)}else this.remove();e()}));t.addEventListener("cancel",(()=>{this.remove();e()}))})).finally((()=>this.#Cs()));t.click()}remove(){if(this.#bs){this.#fs=null;this._uiManager.imageManager.deleteId(this.#bs);this.#ys?.remove();this.#ys=null;this.#zi?.disconnect();this.#zi=null}super.remove()}rebuild(){if(this.parent){super.rebuild();if(null!==this.div){this.#bs&&this.#Ts();this.isAttachedToDOM||this.parent.add(this)}}else this.#bs&&this.#Ts()}onceAdded(){this._isDraggable=!0;this.div.focus()}isEmpty(){return!(this.#As||this.#fs||this.#_s||this.#vs)}get isResizable(){return!0}render(){if(this.div)return this.div;let t,e;if(this.width){t=this.x;e=this.y}super.render();this.div.hidden=!0;this.#fs?this.#Ki():this.#Ts();if(this.width){const[i,s]=this.parentDimensions;this.setAt(t*i,e*s,this.width*i,this.height*s)}return this.div}#Ki(){const{div:t}=this;let{width:e,height:i}=this.#fs;const[s,n]=this.pageDimensions,a=.75;if(this.width){e=this.width*s;i=this.height*n}else if(e>a*s||i>a*n){const t=Math.min(a*s/e,a*n/i);e*=t;i*=t}const[r,o]=this.parentDimensions;this.setDims(e*r/s,i*o/n);this._uiManager.enableWaiting(!1);const l=this.#ys=document.createElement("canvas");t.append(l);t.hidden=!1;this.#Ps(e,i);this.#Yi();if(!this.#xs){this.parent.addUndoableEditor(this);this.#xs=!0}this._uiManager._eventBus.dispatch("reporttelemetry",{source:this,details:{type:"editing",subtype:this.editorType,data:{action:"inserted_image"}}});this.addAltTextButton()}#Ms(t,e){const[i,s]=this.parentDimensions;this.width=t/i;this.height=e/s;this.setDims(t,e);this._initialOptions?.isCentered?this.center():this.fixAndSetPosition();this._initialOptions=null;null!==this.#Ss&&clearTimeout(this.#Ss);this.#Ss=setTimeout((()=>{this.#Ss=null;this.#Ps(t,e)}),200)}#ks(t,e){const{width:i,height:s}=this.#fs;let n=i,a=s,r=this.#fs;for(;n>2*t||a>2*e;){const i=n,s=a;n>2*t&&(n=n>=16384?Math.floor(n/2)-1:Math.ceil(n/2));a>2*e&&(a=a>=16384?Math.floor(a/2)-1:Math.ceil(a/2));const o=new OffscreenCanvas(n,a);o.getContext("2d").drawImage(r,0,0,i,s,0,0,n,a);r=o.transferToImageBitmap()}return r}#Ps(t,e){t=Math.ceil(t);e=Math.ceil(e);const i=this.#ys;if(!i||i.width===t&&i.height===e)return;i.width=t;i.height=e;const s=this.#Es?this.#fs:this.#ks(t,e),n=i.getContext("2d");n.filter=this._uiManager.hcmFilter;n.drawImage(s,0,0,s.width,s.height,0,0,t,e)}#Fs(t){if(t){if(this.#Es){const t=this._uiManager.imageManager.getSvgUrl(this.#bs);if(t)return t}const t=document.createElement("canvas");({width:t.width,height:t.height}=this.#fs);t.getContext("2d").drawImage(this.#fs,0,0);return t.toDataURL()}if(this.#Es){const[t,e]=this.pageDimensions,i=Math.round(this.width*t*a.PixelsPerInch.PDF_TO_CSS_UNITS),s=Math.round(this.height*e*a.PixelsPerInch.PDF_TO_CSS_UNITS),n=new OffscreenCanvas(i,s);n.getContext("2d").drawImage(this.#fs,0,0,this.#fs.width,this.#fs.height,0,0,i,s);return n.transferToImageBitmap()}return structuredClone(this.#fs)}#Yi(){this.#zi=new ResizeObserver((t=>{const e=t[0].contentRect;e.width&&e.height&&this.#Ms(e.width,e.height)}));this.#zi.observe(this.div)}static deserialize(t,e,i){if(t instanceof r.StampAnnotationElement)return null;const s=super.deserialize(t,e,i),{rect:n,bitmapUrl:a,bitmapId:o,isSvg:l,accessibilityData:h}=t;o&&i.imageManager.isValidId(o)?s.#bs=o:s.#_s=a;s.#Es=l;const[c,d]=s.pageDimensions;s.width=(n[2]-n[0])/c;s.height=(n[3]-n[1])/d;h&&(s.altTextData=h);return s}serialize(t=!1,e=null){if(this.isEmpty())return null;const i={annotationType:s.AnnotationEditorType.STAMP,bitmapId:this.#bs,pageIndex:this.pageIndex,rect:this.getRect(0,0),rotation:this.rotation,isSvg:this.#Es,structTreeParentId:this._structTreeParentId};if(t){i.bitmapUrl=this.#Fs(!0);i.accessibilityData=this.altTextData;return i}const{decorative:n,altText:a}=this.altTextData;!n&&a&&(i.accessibilityData={type:"Figure",alt:a});if(null===e)return i;e.stamps||=new Map;const r=this.#Es?(i.rect[2]-i.rect[0])*(i.rect[3]-i.rect[1]):null;if(e.stamps.has(this.#bs)){if(this.#Es){const t=e.stamps.get(this.#bs);if(r>t.area){t.area=r;t.serialized.bitmap.close();t.serialized.bitmap=this.#Fs(!1)}}}else{e.stamps.set(this.#bs,{area:r,serialized:i});i.bitmap=this.#Fs(!1)}return i}}e.StampEditor=StampEditor}],__webpack_module_cache__={};function __w_pdfjs_require__(t){var e=__webpack_module_cache__[t];if(void 0!==e)return e.exports;var i=__webpack_module_cache__[t]={exports:{}};__webpack_modules__[t](i,i.exports,__w_pdfjs_require__);return i.exports}var __webpack_exports__={};(()=>{var t=__webpack_exports__;Object.defineProperty(t,"__esModule",{value:!0});Object.defineProperty(t,"AbortException",{enumerable:!0,get:function(){return e.AbortException}});Object.defineProperty(t,"AnnotationEditorLayer",{enumerable:!0,get:function(){return a.AnnotationEditorLayer}});Object.defineProperty(t,"AnnotationEditorParamsType",{enumerable:!0,get:function(){return e.AnnotationEditorParamsType}});Object.defineProperty(t,"AnnotationEditorType",{enumerable:!0,get:function(){return e.AnnotationEditorType}});Object.defineProperty(t,"AnnotationEditorUIManager",{enumerable:!0,get:function(){return r.AnnotationEditorUIManager}});Object.defineProperty(t,"AnnotationLayer",{enumerable:!0,get:function(){return o.AnnotationLayer}});Object.defineProperty(t,"AnnotationMode",{enumerable:!0,get:function(){return e.AnnotationMode}});Object.defineProperty(t,"CMapCompressionType",{enumerable:!0,get:function(){return e.CMapCompressionType}});Object.defineProperty(t,"DOMSVGFactory",{enumerable:!0,get:function(){return s.DOMSVGFactory}});Object.defineProperty(t,"FeatureTest",{enumerable:!0,get:function(){return e.FeatureTest}});Object.defineProperty(t,"GlobalWorkerOptions",{enumerable:!0,get:function(){return l.GlobalWorkerOptions}});Object.defineProperty(t,"ImageKind",{enumerable:!0,get:function(){return e.ImageKind}});Object.defineProperty(t,"InvalidPDFException",{enumerable:!0,get:function(){return e.InvalidPDFException}});Object.defineProperty(t,"MissingPDFException",{enumerable:!0,get:function(){return e.MissingPDFException}});Object.defineProperty(t,"OPS",{enumerable:!0,get:function(){return e.OPS}});Object.defineProperty(t,"PDFDataRangeTransport",{enumerable:!0,get:function(){return i.PDFDataRangeTransport}});Object.defineProperty(t,"PDFDateString",{enumerable:!0,get:function(){return s.PDFDateString}});Object.defineProperty(t,"PDFWorker",{enumerable:!0,get:function(){return i.PDFWorker}});Object.defineProperty(t,"PasswordResponses",{enumerable:!0,get:function(){return e.PasswordResponses}});Object.defineProperty(t,"PermissionFlag",{enumerable:!0,get:function(){return e.PermissionFlag}});Object.defineProperty(t,"PixelsPerInch",{enumerable:!0,get:function(){return s.PixelsPerInch}});Object.defineProperty(t,"PromiseCapability",{enumerable:!0,get:function(){return e.PromiseCapability}});Object.defineProperty(t,"RenderingCancelledException",{enumerable:!0,get:function(){return s.RenderingCancelledException}});Object.defineProperty(t,"SVGGraphics",{enumerable:!0,get:function(){return i.SVGGraphics}});Object.defineProperty(t,"UnexpectedResponseException",{enumerable:!0,get:function(){return e.UnexpectedResponseException}});Object.defineProperty(t,"Util",{enumerable:!0,get:function(){return e.Util}});Object.defineProperty(t,"VerbosityLevel",{enumerable:!0,get:function(){return e.VerbosityLevel}});Object.defineProperty(t,"XfaLayer",{enumerable:!0,get:function(){return h.XfaLayer}});Object.defineProperty(t,"build",{enumerable:!0,get:function(){return i.build}});Object.defineProperty(t,"createValidAbsoluteUrl",{enumerable:!0,get:function(){return e.createValidAbsoluteUrl}});Object.defineProperty(t,"getDocument",{enumerable:!0,get:function(){return i.getDocument}});Object.defineProperty(t,"getFilenameFromUrl",{enumerable:!0,get:function(){return s.getFilenameFromUrl}});Object.defineProperty(t,"getPdfFilenameFromUrl",{enumerable:!0,get:function(){return s.getPdfFilenameFromUrl}});Object.defineProperty(t,"getXfaPageViewport",{enumerable:!0,get:function(){return s.getXfaPageViewport}});Object.defineProperty(t,"isDataScheme",{enumerable:!0,get:function(){return s.isDataScheme}});Object.defineProperty(t,"isPdfFile",{enumerable:!0,get:function(){return s.isPdfFile}});Object.defineProperty(t,"loadScript",{enumerable:!0,get:function(){return s.loadScript}});Object.defineProperty(t,"noContextMenu",{enumerable:!0,get:function(){return s.noContextMenu}});Object.defineProperty(t,"normalizeUnicode",{enumerable:!0,get:function(){return e.normalizeUnicode}});Object.defineProperty(t,"renderTextLayer",{enumerable:!0,get:function(){return n.renderTextLayer}});Object.defineProperty(t,"setLayerDimensions",{enumerable:!0,get:function(){return s.setLayerDimensions}});Object.defineProperty(t,"shadow",{enumerable:!0,get:function(){return e.shadow}});Object.defineProperty(t,"updateTextLayer",{enumerable:!0,get:function(){return n.updateTextLayer}});Object.defineProperty(t,"version",{enumerable:!0,get:function(){return i.version}});var e=__w_pdfjs_require__(1),i=__w_pdfjs_require__(2),s=__w_pdfjs_require__(6),n=__w_pdfjs_require__(26),a=__w_pdfjs_require__(27),r=__w_pdfjs_require__(5),o=__w_pdfjs_require__(29),l=__w_pdfjs_require__(14),h=__w_pdfjs_require__(32)})();return __webpack_exports__})())); \ No newline at end of file diff --git a/frontend/public/thumbnailWorker.js b/frontend/public/thumbnailWorker.js new file mode 100644 index 000000000..2654ce6a4 --- /dev/null +++ b/frontend/public/thumbnailWorker.js @@ -0,0 +1,157 @@ +// Web Worker for parallel thumbnail generation +console.log('🔧 Thumbnail worker starting up...'); + +let pdfJsLoaded = false; + +// Import PDF.js properly for worker context +try { + console.log('📦 Loading PDF.js locally...'); + importScripts('/pdf.js'); + + // PDF.js exports to globalThis, check both self and globalThis + const pdfjsLib = self.pdfjsLib || globalThis.pdfjsLib; + + if (pdfjsLib) { + // Make it available on self for consistency + self.pdfjsLib = pdfjsLib; + + // Set up PDF.js worker + self.pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdf.worker.js'; + pdfJsLoaded = true; + console.log('✓ PDF.js loaded successfully from local files'); + console.log('✓ PDF.js version:', self.pdfjsLib.version || 'unknown'); + } else { + throw new Error('pdfjsLib not available after import - neither self.pdfjsLib nor globalThis.pdfjsLib found'); + } +} catch (error) { + console.error('✗ Failed to load local PDF.js:', error.message || error); + console.error('✗ Available globals:', Object.keys(self).filter(key => key.includes('pdf'))); + pdfJsLoaded = false; +} + +// Log the final status +if (pdfJsLoaded) { + console.log('✅ Thumbnail worker ready for PDF processing'); +} else { + console.log('❌ Thumbnail worker failed to initialize - PDF.js not available'); +} + +self.onmessage = async function(e) { + const { type, data, jobId } = e.data; + + try { + // Handle PING for worker health check + if (type === 'PING') { + console.log('🏓 Worker PING received, checking PDF.js status...'); + + // Check if PDF.js is loaded before responding + if (pdfJsLoaded && self.pdfjsLib) { + console.log('✓ Worker PONG - PDF.js ready'); + self.postMessage({ type: 'PONG', jobId }); + } else { + console.error('✗ PDF.js not loaded - worker not ready'); + console.error('✗ pdfJsLoaded:', pdfJsLoaded); + console.error('✗ self.pdfjsLib:', !!self.pdfjsLib); + self.postMessage({ + type: 'ERROR', + jobId, + data: { error: 'PDF.js not loaded in worker' } + }); + } + return; + } + + if (type === 'GENERATE_THUMBNAILS') { + console.log('🖼️ Starting thumbnail generation for', data.pageNumbers.length, 'pages'); + + if (!pdfJsLoaded || !self.pdfjsLib) { + const error = 'PDF.js not available in worker'; + console.error('✗', error); + throw new Error(error); + } + const { pdfArrayBuffer, pageNumbers, scale = 0.2, quality = 0.8 } = data; + + console.log('📄 Loading PDF document, size:', pdfArrayBuffer.byteLength, 'bytes'); + // Load PDF in worker using imported PDF.js + const pdf = await self.pdfjsLib.getDocument({ data: pdfArrayBuffer }).promise; + console.log('✓ PDF loaded, total pages:', pdf.numPages); + + const thumbnails = []; + + // Process pages in smaller batches for smoother UI + const batchSize = 3; // Process 3 pages at once for smoother UI + for (let i = 0; i < pageNumbers.length; i += batchSize) { + const batch = pageNumbers.slice(i, i + batchSize); + + const batchPromises = batch.map(async (pageNumber) => { + try { + console.log(`🎯 Processing page ${pageNumber}...`); + const page = await pdf.getPage(pageNumber); + const viewport = page.getViewport({ scale }); + console.log(`📐 Page ${pageNumber} viewport:`, viewport.width, 'x', viewport.height); + + // Create OffscreenCanvas for better performance + const canvas = new OffscreenCanvas(viewport.width, viewport.height); + const context = canvas.getContext('2d'); + + if (!context) { + throw new Error('Failed to get 2D context from OffscreenCanvas'); + } + + await page.render({ canvasContext: context, viewport }).promise; + console.log(`✓ Page ${pageNumber} rendered`); + + // Convert to blob then to base64 (more efficient than toDataURL) + const blob = await canvas.convertToBlob({ type: 'image/jpeg', quality }); + const arrayBuffer = await blob.arrayBuffer(); + const base64 = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer))); + const thumbnail = `data:image/jpeg;base64,${base64}`; + console.log(`✓ Page ${pageNumber} thumbnail generated (${base64.length} chars)`); + + return { pageNumber, thumbnail, success: true }; + } catch (error) { + console.error(`✗ Failed to generate thumbnail for page ${pageNumber}:`, error.message || error); + return { pageNumber, error: error.message || String(error), success: false }; + } + }); + + const batchResults = await Promise.all(batchPromises); + thumbnails.push(...batchResults); + + // Send progress update + console.log(`📊 Worker: Sending progress update - ${thumbnails.length}/${pageNumbers.length} completed, ${batchResults.filter(r => r.success).length} new thumbnails`); + self.postMessage({ + type: 'PROGRESS', + jobId, + data: { + completed: thumbnails.length, + total: pageNumbers.length, + thumbnails: batchResults.filter(r => r.success) + } + }); + + // Small delay between batches to keep UI smooth + if (i + batchSize < pageNumbers.length) { + console.log(`⏸️ Worker: Pausing 100ms before next batch (${i + batchSize}/${pageNumbers.length})`); + await new Promise(resolve => setTimeout(resolve, 100)); // Increased to 100ms pause between batches for smoother scrolling + } + } + + // Clean up + pdf.destroy(); + + self.postMessage({ + type: 'COMPLETE', + jobId, + data: { thumbnails: thumbnails.filter(r => r.success) } + }); + + } + } catch (error) { + self.postMessage({ + type: 'ERROR', + jobId, + data: { error: error.message } + }); + } +}; diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 8083f37fd..de5001850 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,5 +1,6 @@ import React from 'react'; import { RainbowThemeProvider } from './components/shared/RainbowThemeProvider'; +import { FileContextProvider } from './contexts/FileContext'; import HomePage from './pages/HomePage'; // Import global styles @@ -9,7 +10,9 @@ import './index.css'; export default function App() { return ( - + + + ); } diff --git a/frontend/src/commands/pageCommands.ts b/frontend/src/commands/pageCommands.ts index d0ecd699b..4e5572234 100644 --- a/frontend/src/commands/pageCommands.ts +++ b/frontend/src/commands/pageCommands.ts @@ -97,27 +97,8 @@ export class DeletePagesCommand extends PageCommand { } undo(): void { - let restoredPages = [...this.pdfDocument.pages]; - - // Insert deleted pages back at their original positions - this.deletedPages - .sort((a, b) => (this.deletedPositions.get(a.id) || 0) - (this.deletedPositions.get(b.id) || 0)) - .forEach(page => { - const originalIndex = this.deletedPositions.get(page.id) || 0; - restoredPages.splice(originalIndex, 0, page); - }); - - // Update page numbers - restoredPages = restoredPages.map((page, index) => ({ - ...page, - pageNumber: index + 1 - })); - - this.setPdfDocument({ - ...this.pdfDocument, - pages: restoredPages, - totalPages: restoredPages.length - }); + // Simply restore to the previous state (before deletion) + this.setPdfDocument(this.previousState); } get description(): string { diff --git a/frontend/src/components/fileEditor/FileEditor.tsx b/frontend/src/components/fileEditor/FileEditor.tsx new file mode 100644 index 000000000..c0badafd8 --- /dev/null +++ b/frontend/src/components/fileEditor/FileEditor.tsx @@ -0,0 +1,858 @@ +import React, { useState, useCallback, useRef, useEffect } from 'react'; +import { + Button, Text, Center, Box, Notification, TextInput, LoadingOverlay, Modal, Alert, Container, + Stack, Group +} from '@mantine/core'; +import { Dropzone } from '@mantine/dropzone'; +import { useTranslation } from 'react-i18next'; +import UploadFileIcon from '@mui/icons-material/UploadFile'; +import { useFileContext } from '../../contexts/FileContext'; +import { FileOperation } from '../../types/fileContext'; +import { fileStorage } from '../../services/fileStorage'; +import { generateThumbnailForFile } from '../../utils/thumbnailUtils'; +import { zipFileService } from '../../services/zipFileService'; +import styles from '../pageEditor/PageEditor.module.css'; +import FileThumbnail from '../pageEditor/FileThumbnail'; +import DragDropGrid from '../pageEditor/DragDropGrid'; +import FilePickerModal from '../shared/FilePickerModal'; +import SkeletonLoader from '../shared/SkeletonLoader'; + +interface FileItem { + id: string; + name: string; + pageCount: number; + thumbnail: string; + size: number; + file: File; + splitBefore?: boolean; +} + +interface FileEditorProps { + onOpenPageEditor?: (file: File) => void; + onMergeFiles?: (files: File[]) => void; + toolMode?: boolean; + multiSelect?: boolean; + showUpload?: boolean; + showBulkActions?: boolean; + onFileSelect?: (files: File[]) => void; +} + +const FileEditor = ({ + onOpenPageEditor, + onMergeFiles, + toolMode = false, + multiSelect = true, + showUpload = true, + showBulkActions = true, + onFileSelect +}: FileEditorProps) => { + const { t } = useTranslation(); + + // Get file context + const fileContext = useFileContext(); + const { + activeFiles, + processedFiles, + selectedFileIds, + setSelectedFiles: setContextSelectedFiles, + isProcessing, + addFiles, + removeFiles, + setCurrentView, + recordOperation, + markOperationApplied + } = fileContext; + + const [files, setFiles] = useState([]); + const [status, setStatus] = useState(null); + const [error, setError] = useState(null); + const [localLoading, setLocalLoading] = useState(false); + const [selectionMode, setSelectionMode] = useState(toolMode); + + // Enable selection mode automatically in tool mode + React.useEffect(() => { + if (toolMode) { + setSelectionMode(true); + } + }, [toolMode]); + const [draggedFile, setDraggedFile] = useState(null); + const [dropTarget, setDropTarget] = useState(null); + const [multiFileDrag, setMultiFileDrag] = useState<{fileIds: string[], count: number} | null>(null); + const [dragPosition, setDragPosition] = useState<{x: number, y: number} | null>(null); + const [isAnimating, setIsAnimating] = useState(false); + const [showFilePickerModal, setShowFilePickerModal] = useState(false); + const [conversionProgress, setConversionProgress] = useState(0); + const [zipExtractionProgress, setZipExtractionProgress] = useState<{ + isExtracting: boolean; + currentFile: string; + progress: number; + extractedCount: number; + totalFiles: number; + }>({ + isExtracting: false, + currentFile: '', + progress: 0, + extractedCount: 0, + totalFiles: 0 + }); + const fileRefs = useRef>(new Map()); + const lastActiveFilesRef = useRef([]); + const lastProcessedFilesRef = useRef(0); + + // Map context selected file names to local file IDs + // Defensive programming: ensure selectedFileIds is always an array + const safeSelectedFileIds = Array.isArray(selectedFileIds) ? selectedFileIds : []; + + const localSelectedFiles = files + .filter(file => { + const fileId = (file.file as any).id || file.name; + return safeSelectedFileIds.includes(fileId); + }) + .map(file => file.id); + + // Convert shared files to FileEditor format + const convertToFileItem = useCallback(async (sharedFile: any): Promise => { + // Generate thumbnail if not already available + const thumbnail = sharedFile.thumbnail || await generateThumbnailForFile(sharedFile.file || sharedFile); + + return { + id: sharedFile.id || `file-${Date.now()}-${Math.random()}`, + name: (sharedFile.file?.name || sharedFile.name || 'unknown'), + pageCount: sharedFile.pageCount || Math.floor(Math.random() * 20) + 1, // Mock for now + thumbnail, + size: sharedFile.file?.size || sharedFile.size || 0, + file: sharedFile.file || sharedFile, + }; + }, []); + + // Convert activeFiles to FileItem format using context (async to avoid blocking) + useEffect(() => { + // Check if the actual content has changed, not just references + const currentActiveFileNames = activeFiles.map(f => f.name); + const currentProcessedFilesSize = processedFiles.size; + + const activeFilesChanged = JSON.stringify(currentActiveFileNames) !== JSON.stringify(lastActiveFilesRef.current); + const processedFilesChanged = currentProcessedFilesSize !== lastProcessedFilesRef.current; + + if (!activeFilesChanged && !processedFilesChanged) { + return; + } + + // Update refs + lastActiveFilesRef.current = currentActiveFileNames; + lastProcessedFilesRef.current = currentProcessedFilesSize; + + const convertActiveFiles = async () => { + + if (activeFiles.length > 0) { + setLocalLoading(true); + try { + // Process files in chunks to avoid blocking UI + const convertedFiles: FileItem[] = []; + + for (let i = 0; i < activeFiles.length; i++) { + const file = activeFiles[i]; + + // Try to get thumbnail from processed file first + const processedFile = processedFiles.get(file); + let thumbnail = processedFile?.pages?.[0]?.thumbnail; + + // If no thumbnail from processed file, try to generate one + if (!thumbnail) { + try { + thumbnail = await generateThumbnailForFile(file); + } catch (error) { + console.warn(`Failed to generate thumbnail for ${file.name}:`, error); + thumbnail = undefined; // Use placeholder + } + } + + const convertedFile = { + id: `file-${Date.now()}-${Math.random()}`, + name: file.name, + pageCount: processedFile?.totalPages || Math.floor(Math.random() * 20) + 1, + thumbnail, + size: file.size, + file, + }; + + convertedFiles.push(convertedFile); + + // Update progress + setConversionProgress(((i + 1) / activeFiles.length) * 100); + + // Yield to main thread between files + if (i < activeFiles.length - 1) { + await new Promise(resolve => requestAnimationFrame(resolve)); + } + } + + + setFiles(convertedFiles); + } catch (err) { + console.error('Error converting active files:', err); + } finally { + setLocalLoading(false); + setConversionProgress(0); + } + } else { + setFiles([]); + setLocalLoading(false); + setConversionProgress(0); + } + }; + + convertActiveFiles(); + }, [activeFiles, processedFiles]); + + + // Process uploaded files using context + const handleFileUpload = useCallback(async (uploadedFiles: File[]) => { + setError(null); + + try { + const allExtractedFiles: File[] = []; + const errors: string[] = []; + + for (const file of uploadedFiles) { + if (file.type === 'application/pdf') { + // Handle PDF files normally + allExtractedFiles.push(file); + } else if (file.type === 'application/zip' || file.type === 'application/x-zip-compressed' || file.name.toLowerCase().endsWith('.zip')) { + // Handle ZIP files + try { + // Validate ZIP file first + const validation = await zipFileService.validateZipFile(file); + if (!validation.isValid) { + errors.push(`ZIP file "${file.name}": ${validation.errors.join(', ')}`); + continue; + } + + // Extract PDF files from ZIP + setZipExtractionProgress({ + isExtracting: true, + currentFile: file.name, + progress: 0, + extractedCount: 0, + totalFiles: validation.fileCount + }); + + const extractionResult = await zipFileService.extractPdfFiles(file, (progress) => { + setZipExtractionProgress({ + isExtracting: true, + currentFile: progress.currentFile, + progress: progress.progress, + extractedCount: progress.extractedCount, + totalFiles: progress.totalFiles + }); + }); + + // Reset extraction progress + setZipExtractionProgress({ + isExtracting: false, + currentFile: '', + progress: 0, + extractedCount: 0, + totalFiles: 0 + }); + + if (extractionResult.success) { + allExtractedFiles.push(...extractionResult.extractedFiles); + + // Record ZIP extraction operation + const operationId = `zip-extract-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; + const operation: FileOperation = { + id: operationId, + type: 'convert', + timestamp: Date.now(), + fileIds: extractionResult.extractedFiles.map(f => f.name), + status: 'pending', + metadata: { + originalFileName: file.name, + outputFileNames: extractionResult.extractedFiles.map(f => f.name), + fileSize: file.size, + parameters: { + extractionType: 'zip', + extractedCount: extractionResult.extractedCount, + totalFiles: extractionResult.totalFiles + } + } + }; + + recordOperation(file.name, operation); + markOperationApplied(file.name, operationId); + + if (extractionResult.errors.length > 0) { + errors.push(...extractionResult.errors); + } + } else { + errors.push(`Failed to extract ZIP file "${file.name}": ${extractionResult.errors.join(', ')}`); + } + } catch (zipError) { + errors.push(`Failed to process ZIP file "${file.name}": ${zipError instanceof Error ? zipError.message : 'Unknown error'}`); + setZipExtractionProgress({ + isExtracting: false, + currentFile: '', + progress: 0, + extractedCount: 0, + totalFiles: 0 + }); + } + } else { + errors.push(`Unsupported file type: ${file.name} (${file.type})`); + } + } + + // Show any errors + if (errors.length > 0) { + setError(errors.join('\n')); + } + + // Process all extracted files + if (allExtractedFiles.length > 0) { + // Record upload operations for PDF files + for (const file of allExtractedFiles) { + const operationId = `upload-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; + const operation: FileOperation = { + id: operationId, + type: 'upload', + timestamp: Date.now(), + fileIds: [file.name], + status: 'pending', + metadata: { + originalFileName: file.name, + fileSize: file.size, + parameters: { + uploadMethod: 'drag-drop' + } + } + }; + + recordOperation(file.name, operation); + markOperationApplied(file.name, operationId); + } + + // Add files to context (they will be processed automatically) + await addFiles(allExtractedFiles); + setStatus(`Added ${allExtractedFiles.length} files`); + } + } catch (err) { + const errorMessage = err instanceof Error ? err.message : 'Failed to process files'; + setError(errorMessage); + console.error('File processing error:', err); + + // Reset extraction progress on error + setZipExtractionProgress({ + isExtracting: false, + currentFile: '', + progress: 0, + extractedCount: 0, + totalFiles: 0 + }); + } + }, [addFiles, recordOperation, markOperationApplied]); + + const selectAll = useCallback(() => { + setContextSelectedFiles(files.map(f => (f.file as any).id || f.name)); + }, [files, setContextSelectedFiles]); + + const deselectAll = useCallback(() => setContextSelectedFiles([]), [setContextSelectedFiles]); + + const closeAllFiles = useCallback(() => { + if (activeFiles.length === 0) return; + + // Record close all operation for each file + activeFiles.forEach(file => { + const operationId = `close-all-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; + const operation: FileOperation = { + id: operationId, + type: 'remove', + timestamp: Date.now(), + fileIds: [file.name], + status: 'pending', + metadata: { + originalFileName: file.name, + fileSize: file.size, + parameters: { + action: 'close_all', + reason: 'user_request' + } + } + }; + + recordOperation(file.name, operation); + markOperationApplied(file.name, operationId); + }); + + // Remove all files from context but keep in storage + removeFiles(activeFiles.map(f => (f as any).id || f.name), false); + + // Clear selections + setContextSelectedFiles([]); + }, [activeFiles, removeFiles, setContextSelectedFiles, recordOperation, markOperationApplied]); + + const toggleFile = useCallback((fileId: string) => { + const targetFile = files.find(f => f.id === fileId); + if (!targetFile) return; + + const contextFileId = (targetFile.file as any).id || targetFile.name; + + if (!multiSelect) { + // Single select mode for tools - toggle on/off + const isCurrentlySelected = safeSelectedFileIds.includes(contextFileId); + if (isCurrentlySelected) { + // Deselect the file + setContextSelectedFiles([]); + if (onFileSelect) { + onFileSelect([]); + } + } else { + // Select the file + setContextSelectedFiles([contextFileId]); + if (onFileSelect) { + onFileSelect([targetFile.file]); + } + } + } else { + // Multi select mode (default) + setContextSelectedFiles(prev => { + const safePrev = Array.isArray(prev) ? prev : []; + return safePrev.includes(contextFileId) + ? safePrev.filter(id => id !== contextFileId) + : [...safePrev, contextFileId]; + }); + + // Notify parent with selected files + if (onFileSelect) { + const selectedFiles = files + .filter(f => { + const fId = (f.file as any).id || f.name; + return safeSelectedFileIds.includes(fId) || fId === contextFileId; + }) + .map(f => f.file); + onFileSelect(selectedFiles); + } + } + }, [files, setContextSelectedFiles, multiSelect, onFileSelect, safeSelectedFileIds]); + + const toggleSelectionMode = useCallback(() => { + setSelectionMode(prev => { + const newMode = !prev; + if (!newMode) { + setContextSelectedFiles([]); + } + return newMode; + }); + }, [setContextSelectedFiles]); + + + // Drag and drop handlers + const handleDragStart = useCallback((fileId: string) => { + setDraggedFile(fileId); + + if (selectionMode && localSelectedFiles.includes(fileId) && localSelectedFiles.length > 1) { + setMultiFileDrag({ + fileIds: localSelectedFiles, + count: localSelectedFiles.length + }); + } else { + setMultiFileDrag(null); + } + }, [selectionMode, localSelectedFiles]); + + const handleDragEnd = useCallback(() => { + setDraggedFile(null); + setDropTarget(null); + setMultiFileDrag(null); + setDragPosition(null); + }, []); + + const handleDragOver = useCallback((e: React.DragEvent) => { + e.preventDefault(); + + if (!draggedFile) return; + + if (multiFileDrag) { + setDragPosition({ x: e.clientX, y: e.clientY }); + } + + const elementUnderCursor = document.elementFromPoint(e.clientX, e.clientY); + if (!elementUnderCursor) return; + + const fileContainer = elementUnderCursor.closest('[data-file-id]'); + if (fileContainer) { + const fileId = fileContainer.getAttribute('data-file-id'); + if (fileId && fileId !== draggedFile) { + setDropTarget(fileId); + return; + } + } + + const endZone = elementUnderCursor.closest('[data-drop-zone="end"]'); + if (endZone) { + setDropTarget('end'); + return; + } + + setDropTarget(null); + }, [draggedFile, multiFileDrag]); + + const handleDragEnter = useCallback((fileId: string) => { + if (draggedFile && fileId !== draggedFile) { + setDropTarget(fileId); + } + }, [draggedFile]); + + const handleDragLeave = useCallback(() => { + // Let dragover handle this + }, []); + + const handleDrop = useCallback((e: React.DragEvent, targetFileId: string | 'end') => { + e.preventDefault(); + if (!draggedFile || draggedFile === targetFileId) return; + + let targetIndex: number; + if (targetFileId === 'end') { + targetIndex = files.length; + } else { + targetIndex = files.findIndex(f => f.id === targetFileId); + if (targetIndex === -1) return; + } + + const filesToMove = selectionMode && localSelectedFiles.includes(draggedFile) + ? localSelectedFiles + : [draggedFile]; + + // Update the local files state and sync with activeFiles + setFiles(prev => { + const newFiles = [...prev]; + const movedFiles = filesToMove.map(id => newFiles.find(f => f.id === id)!).filter(Boolean); + + // Remove moved files + filesToMove.forEach(id => { + const index = newFiles.findIndex(f => f.id === id); + if (index !== -1) newFiles.splice(index, 1); + }); + + // Insert at target position + newFiles.splice(targetIndex, 0, ...movedFiles); + + // TODO: Update context with reordered files (need to implement file reordering in context) + // For now, just return the reordered local state + return newFiles; + }); + + const moveCount = multiFileDrag ? multiFileDrag.count : 1; + setStatus(`${moveCount > 1 ? `${moveCount} files` : 'File'} reordered`); + + }, [draggedFile, files, selectionMode, localSelectedFiles, multiFileDrag]); + + const handleEndZoneDragEnter = useCallback(() => { + if (draggedFile) { + setDropTarget('end'); + } + }, [draggedFile]); + + // File operations using context + const handleDeleteFile = useCallback((fileId: string) => { + console.log('handleDeleteFile called with fileId:', fileId); + const file = files.find(f => f.id === fileId); + console.log('Found file:', file); + + if (file) { + console.log('Attempting to remove file:', file.name); + console.log('Actual file object:', file.file); + console.log('Actual file.file.name:', file.file.name); + + // Record close operation + const fileName = file.file.name; + const fileId = (file.file as any).id || fileName; + const operationId = `close-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; + const operation: FileOperation = { + id: operationId, + type: 'remove', + timestamp: Date.now(), + fileIds: [fileName], + status: 'pending', + metadata: { + originalFileName: fileName, + fileSize: file.size, + parameters: { + action: 'close', + reason: 'user_request' + } + } + }; + + recordOperation(fileName, operation); + + // Remove file from context but keep in storage (close, don't delete) + console.log('Calling removeFiles with:', [fileId]); + removeFiles([fileId], false); + + // Remove from context selections + setContextSelectedFiles(prev => { + const safePrev = Array.isArray(prev) ? prev : []; + return safePrev.filter(id => id !== fileId); + }); + + // Mark operation as applied + markOperationApplied(fileName, operationId); + } else { + console.log('File not found for fileId:', fileId); + } + }, [files, removeFiles, setContextSelectedFiles, recordOperation, markOperationApplied]); + + const handleViewFile = useCallback((fileId: string) => { + const file = files.find(f => f.id === fileId); + if (file) { + // Set the file as selected in context and switch to page editor view + const contextFileId = (file.file as any).id || file.name; + setContextSelectedFiles([contextFileId]); + setCurrentView('pageEditor'); + onOpenPageEditor?.(file.file); + } + }, [files, setContextSelectedFiles, setCurrentView, onOpenPageEditor]); + + const handleMergeFromHere = useCallback((fileId: string) => { + const startIndex = files.findIndex(f => f.id === fileId); + if (startIndex === -1) return; + + const filesToMerge = files.slice(startIndex).map(f => f.file); + if (onMergeFiles) { + onMergeFiles(filesToMerge); + } + }, [files, onMergeFiles]); + + const handleSplitFile = useCallback((fileId: string) => { + const file = files.find(f => f.id === fileId); + if (file && onOpenPageEditor) { + onOpenPageEditor(file.file); + } + }, [files, onOpenPageEditor]); + + const handleLoadFromStorage = useCallback(async (selectedFiles: any[]) => { + if (selectedFiles.length === 0) return; + + setLocalLoading(true); + try { + const convertedFiles = await Promise.all( + selectedFiles.map(convertToFileItem) + ); + setFiles(prev => [...prev, ...convertedFiles]); + setStatus(`Loaded ${selectedFiles.length} files from storage`); + } catch (err) { + console.error('Error loading files from storage:', err); + setError('Failed to load some files from storage'); + } finally { + setLocalLoading(false); + } + }, [convertToFileItem]); + + + return ( + + + + + + {showBulkActions && !toolMode && ( + <> + + + + + )} + + {/* Load from storage and upload buttons */} + {showUpload && ( + <> + + + + + + + )} + + + + {files.length === 0 && !localLoading && !zipExtractionProgress.isExtracting ? ( +
+ + 📁 + No files loaded + Upload PDF files, ZIP archives, or load from storage to get started + +
+ ) : files.length === 0 && (localLoading || zipExtractionProgress.isExtracting) ? ( + + + + {/* ZIP Extraction Progress */} + {zipExtractionProgress.isExtracting && ( + + + Extracting ZIP archive... + {Math.round(zipExtractionProgress.progress)}% + + + {zipExtractionProgress.currentFile || 'Processing files...'} + + + {zipExtractionProgress.extractedCount} of {zipExtractionProgress.totalFiles} files extracted + +
+
+
+ + )} + + {/* Processing indicator */} + {localLoading && ( + + + Loading files... + {Math.round(conversionProgress)}% + +
+
+
+ + )} + + + + ) : ( + ( + + )} + renderSplitMarker={(file, index) => ( +
+ )} + /> + )} + + + {/* File Picker Modal */} + setShowFilePickerModal(false)} + storedFiles={[]} // FileEditor doesn't have access to stored files, needs to be passed from parent + onSelectFiles={handleLoadFromStorage} + allowMultiple={true} + /> + + {status && ( + setStatus(null)} + style={{ position: 'fixed', bottom: 20, right: 20, zIndex: 1000 }} + > + {status} + + )} + + {error && ( + setError(null)} + style={{ position: 'fixed', bottom: 80, right: 20, zIndex: 1000 }} + > + {error} + + )} + + ); +}; + +export default FileEditor; diff --git a/frontend/src/components/fileManagement/FileCard.tsx b/frontend/src/components/fileManagement/FileCard.tsx index 6b275e556..f0972356b 100644 --- a/frontend/src/components/fileManagement/FileCard.tsx +++ b/frontend/src/components/fileManagement/FileCard.tsx @@ -6,9 +6,9 @@ import StorageIcon from "@mui/icons-material/Storage"; import VisibilityIcon from "@mui/icons-material/Visibility"; import EditIcon from "@mui/icons-material/Edit"; -import { FileWithUrl } from "../../types/file"; import { getFileSize, getFileDate } from "../../utils/fileUtils"; import { useIndexedDBThumbnail } from "../../hooks/useIndexedDBThumbnail"; +import { fileStorage } from "../../services/fileStorage"; interface FileCardProps { file: FileWithUrl; diff --git a/frontend/src/components/fileManagement/FileManager.tsx b/frontend/src/components/fileManagement/FileManager.tsx deleted file mode 100644 index 45bf95b5b..000000000 --- a/frontend/src/components/fileManagement/FileManager.tsx +++ /dev/null @@ -1,440 +0,0 @@ -import React, { useState, useEffect } from "react"; -import { Box, Flex, Text, Notification, Button, Group } from "@mantine/core"; -import { Dropzone, MIME_TYPES } from "@mantine/dropzone"; -import { useTranslation } from "react-i18next"; - -import { GlobalWorkerOptions } from "pdfjs-dist"; -import { StorageStats } from "../../services/fileStorage"; -import { FileWithUrl, defaultStorageConfig, initializeStorageConfig, StorageConfig } from "../../types/file"; - -// Refactored imports -import { fileOperationsService } from "../../services/fileOperationsService"; -import { checkStorageWarnings } from "../../utils/storageUtils"; -import StorageStatsCard from "./StorageStatsCard"; -import FileCard from "./FileCard"; -import FileUploadSelector from "../shared/FileUploadSelector"; - -GlobalWorkerOptions.workerSrc = "/pdf.worker.js"; - -interface FileManagerProps { - files: FileWithUrl[]; - setFiles: React.Dispatch>; - allowMultiple?: boolean; - setCurrentView?: (view: string) => void; - onOpenFileEditor?: (selectedFiles?: FileWithUrl[]) => void; - onOpenPageEditor?: (selectedFiles?: FileWithUrl[]) => void; - onLoadFileToActive?: (file: File) => void; -} - -const FileManager = ({ - files = [], - setFiles, - allowMultiple = true, - setCurrentView, - onOpenFileEditor, - onOpenPageEditor, - onLoadFileToActive, -}: FileManagerProps) => { - const { t } = useTranslation(); - const [loading, setLoading] = useState(false); - const [storageStats, setStorageStats] = useState(null); - const [notification, setNotification] = useState(null); - const [filesLoaded, setFilesLoaded] = useState(false); - const [selectedFiles, setSelectedFiles] = useState([]); - const [storageConfig, setStorageConfig] = useState(defaultStorageConfig); - - // Extract operations from service for cleaner code - const { - loadStorageStats, - forceReloadFiles, - loadExistingFiles, - uploadFiles, - removeFile, - clearAllFiles, - createBlobUrlForFile, - checkForPurge, - updateStorageStatsIncremental - } = fileOperationsService; - - // Add CSS for spinner animation - useEffect(() => { - if (!document.querySelector('#spinner-animation')) { - const style = document.createElement('style'); - style.id = 'spinner-animation'; - style.textContent = ` - @keyframes spin { - 0% { transform: rotate(0deg); } - 100% { transform: rotate(360deg); } - } - `; - document.head.appendChild(style); - } - }, []); - - // Load existing files from IndexedDB on mount - useEffect(() => { - if (!filesLoaded) { - handleLoadExistingFiles(); - } - }, [filesLoaded]); - - // Initialize storage configuration on mount - useEffect(() => { - const initStorage = async () => { - try { - const config = await initializeStorageConfig(); - setStorageConfig(config); - console.log('Initialized storage config:', config); - } catch (error) { - console.warn('Failed to initialize storage config, using defaults:', error); - } - }; - - initStorage(); - }, []); - - // Load storage stats and set up periodic updates - useEffect(() => { - handleLoadStorageStats(); - - const interval = setInterval(async () => { - await handleLoadStorageStats(); - await handleCheckForPurge(); - }, 10000); // Update every 10 seconds - - return () => clearInterval(interval); - }, []); - - // Sync UI with IndexedDB whenever storage stats change - useEffect(() => { - const syncWithStorage = async () => { - if (storageStats && filesLoaded) { - // If file counts don't match, force reload - if (storageStats.fileCount !== files.length) { - console.warn('File count mismatch: storage has', storageStats.fileCount, 'but UI shows', files.length, '- forcing reload'); - const reloadedFiles = await forceReloadFiles(); - setFiles(reloadedFiles); - } - } - }; - - syncWithStorage(); - }, [storageStats, filesLoaded, files.length]); - - // Handlers using extracted operations - const handleLoadStorageStats = async () => { - const stats = await loadStorageStats(); - if (stats) { - setStorageStats(stats); - - // Check for storage warnings - const warning = checkStorageWarnings(stats); - if (warning) { - setNotification(warning); - } - } - }; - - const handleLoadExistingFiles = async () => { - try { - const loadedFiles = await loadExistingFiles(filesLoaded, files); - setFiles(loadedFiles); - setFilesLoaded(true); - } catch (error) { - console.error('Failed to load existing files:', error); - setFilesLoaded(true); - } - }; - - const handleCheckForPurge = async () => { - try { - const isPurged = await checkForPurge(files); - if (isPurged) { - console.warn('IndexedDB purge detected - forcing UI reload'); - setNotification(t("fileManager.storageCleared", "Browser cleared storage. Files have been removed. Please re-upload.")); - const reloadedFiles = await forceReloadFiles(); - setFiles(reloadedFiles); - setFilesLoaded(true); - } - } catch (error) { - console.error('Error checking for purge:', error); - } - }; - - const validateStorageLimits = (filesToUpload: File[]): { valid: boolean; error?: string } => { - // Check individual file sizes - for (const file of filesToUpload) { - if (file.size > storageConfig.maxFileSize) { - const maxSizeMB = Math.round(storageConfig.maxFileSize / (1024 * 1024)); - return { - valid: false, - error: `${t("storage.fileTooLarge", "File too large. Maximum size per file is")} ${maxSizeMB}MB` - }; - } - } - - // Check total storage capacity - if (storageStats) { - const totalNewSize = filesToUpload.reduce((sum, file) => sum + file.size, 0); - const projectedUsage = storageStats.totalSize + totalNewSize; - - if (projectedUsage > storageConfig.maxTotalStorage) { - return { - valid: false, - error: t("storage.storageQuotaExceeded", "Storage quota exceeded. Please remove some files before uploading more.") - }; - } - } - - return { valid: true }; - }; - - const handleDrop = async (uploadedFiles: File[]) => { - setLoading(true); - - try { - // Validate storage limits before uploading - const validation = validateStorageLimits(uploadedFiles); - if (!validation.valid) { - setNotification(validation.error); - setLoading(false); - return; - } - - const newFiles = await uploadFiles(uploadedFiles, storageConfig.useIndexedDB); - - // Update files state - setFiles((prevFiles) => (allowMultiple ? [...prevFiles, ...newFiles] : newFiles)); - - // Update storage stats incrementally - if (storageStats) { - const updatedStats = updateStorageStatsIncremental(storageStats, 'add', newFiles); - setStorageStats(updatedStats); - - // Check for storage warnings - const warning = checkStorageWarnings(updatedStats); - if (warning) { - setNotification(warning); - } - } - } catch (error) { - console.error('Error handling file drop:', error); - setNotification(t("fileManager.uploadError", "Failed to upload some files.")); - } finally { - setLoading(false); - } - }; - - const handleRemoveFile = async (index: number) => { - const file = files[index]; - - try { - await removeFile(file); - - // Update storage stats incrementally - if (storageStats) { - const updatedStats = updateStorageStatsIncremental(storageStats, 'remove', [file]); - setStorageStats(updatedStats); - } - - setFiles((prevFiles) => prevFiles.filter((_, i) => i !== index)); - } catch (error) { - console.error('Failed to remove file:', error); - } - }; - - const handleClearAll = async () => { - try { - await clearAllFiles(files); - - // Reset storage stats - if (storageStats) { - const clearedStats = updateStorageStatsIncremental(storageStats, 'clear'); - setStorageStats(clearedStats); - } - - setFiles([]); - } catch (error) { - console.error('Failed to clear all files:', error); - } - }; - - const handleReloadFiles = () => { - setFilesLoaded(false); - setFiles([]); - }; - - const handleFileDoubleClick = async (file: FileWithUrl) => { - try { - // Reconstruct File object from storage and add to active files - if (onLoadFileToActive) { - const reconstructedFile = await reconstructFileFromStorage(file); - onLoadFileToActive(reconstructedFile); - setCurrentView && setCurrentView("viewer"); - } - } catch (error) { - console.error('Failed to load file to active set:', error); - setNotification(t("fileManager.failedToOpen", "Failed to open file. It may have been removed from storage.")); - } - }; - - const handleFileView = async (file: FileWithUrl) => { - try { - // Reconstruct File object from storage and add to active files - if (onLoadFileToActive) { - const reconstructedFile = await reconstructFileFromStorage(file); - onLoadFileToActive(reconstructedFile); - setCurrentView && setCurrentView("viewer"); - } - } catch (error) { - console.error('Failed to load file to active set:', error); - setNotification(t("fileManager.failedToOpen", "Failed to open file. It may have been removed from storage.")); - } - }; - - const reconstructFileFromStorage = async (fileWithUrl: FileWithUrl): Promise => { - // If it's already a regular file, return it - if (fileWithUrl instanceof File) { - return fileWithUrl; - } - - // Reconstruct from IndexedDB - const arrayBuffer = await createBlobUrlForFile(fileWithUrl); - if (typeof arrayBuffer === 'string') { - // createBlobUrlForFile returned a blob URL, we need the actual data - const response = await fetch(arrayBuffer); - const data = await response.arrayBuffer(); - return new File([data], fileWithUrl.name, { - type: fileWithUrl.type || 'application/pdf', - lastModified: fileWithUrl.lastModified || Date.now() - }); - } else { - return new File([arrayBuffer], fileWithUrl.name, { - type: fileWithUrl.type || 'application/pdf', - lastModified: fileWithUrl.lastModified || Date.now() - }); - } - }; - - const handleFileEdit = (file: FileWithUrl) => { - if (onOpenFileEditor) { - onOpenFileEditor([file]); - } - }; - - const toggleFileSelection = (fileId: string) => { - setSelectedFiles(prev => - prev.includes(fileId) - ? prev.filter(id => id !== fileId) - : [...prev, fileId] - ); - }; - - const handleOpenSelectedInEditor = () => { - if (onOpenFileEditor && selectedFiles.length > 0) { - const selected = files.filter(f => selectedFiles.includes(f.id || f.name)); - onOpenFileEditor(selected); - } - }; - - const handleOpenSelectedInPageEditor = () => { - if (onOpenPageEditor && selectedFiles.length > 0) { - const selected = files.filter(f => selectedFiles.includes(f.id || f.name)); - onOpenPageEditor(selected); - } - }; - - return ( -
- - {/* File upload is now handled by FileUploadSelector when no files exist */} - - {/* Storage Stats Card */} - - - {/* Multi-selection controls */} - {selectedFiles.length > 0 && ( - - - - {selectedFiles.length} {t("fileManager.filesSelected", "files selected")} - - - - - - - - - )} - - - - {files.map((file, idx) => ( - handleRemoveFile(idx)} - onDoubleClick={() => handleFileDoubleClick(file)} - onView={() => handleFileView(file)} - onEdit={() => handleFileEdit(file)} - isSelected={selectedFiles.includes(file.id || file.name)} - onSelect={() => toggleFileSelection(file.id || file.name)} - /> - ))} - - - - {/* Notifications */} - {notification && ( - setNotification(null)} - style={{ position: "fixed", bottom: 20, right: 20, zIndex: 1000 }} - > - {notification} - - )} -
- ); -}; - -export default FileManager; diff --git a/frontend/src/components/history/FileOperationHistory.tsx b/frontend/src/components/history/FileOperationHistory.tsx new file mode 100644 index 000000000..365b5a8f8 --- /dev/null +++ b/frontend/src/components/history/FileOperationHistory.tsx @@ -0,0 +1,177 @@ +import React from 'react'; +import { + Stack, + Paper, + Text, + Badge, + Group, + Collapse, + Box, + ScrollArea, + Code, + Divider +} from '@mantine/core'; +import { useFileContext } from '../../contexts/FileContext'; +import { FileOperation, FileOperationHistory as FileOperationHistoryType } from '../../types/fileContext'; +import { PageOperation } from '../../types/pageEditor'; + +interface FileOperationHistoryProps { + fileId: string; + showOnlyApplied?: boolean; + maxHeight?: number; +} + +const FileOperationHistory: React.FC = ({ + fileId, + showOnlyApplied = false, + maxHeight = 400 +}) => { + const { getFileHistory, getAppliedOperations } = useFileContext(); + + const history = getFileHistory(fileId); + const operations = showOnlyApplied ? getAppliedOperations(fileId) : history?.operations || []; + + const formatTimestamp = (timestamp: number) => { + return new Date(timestamp).toLocaleString(); + }; + + const getOperationIcon = (type: string) => { + switch (type) { + case 'split': return '✂️'; + case 'merge': return '🔗'; + case 'compress': return '🗜️'; + case 'rotate': return '🔄'; + case 'delete': return '🗑️'; + case 'move': return '↕️'; + case 'insert': return '📄'; + case 'upload': return '⬆️'; + case 'add': return '➕'; + case 'remove': return '➖'; + case 'replace': return '🔄'; + case 'convert': return '🔄'; + default: return '⚙️'; + } + }; + + const getStatusColor = (status: string) => { + switch (status) { + case 'applied': return 'green'; + case 'failed': return 'red'; + case 'pending': return 'yellow'; + default: return 'gray'; + } + }; + + const renderOperationDetails = (operation: FileOperation | PageOperation) => { + if ('metadata' in operation && operation.metadata) { + const { metadata } = operation; + return ( + + {metadata.parameters && ( + + Parameters: {JSON.stringify(metadata.parameters, null, 2)} + + )} + {metadata.originalFileName && ( + + Original file: {metadata.originalFileName} + + )} + {metadata.outputFileNames && ( + + Output files: {metadata.outputFileNames.join(', ')} + + )} + {metadata.fileSize && ( + + File size: {(metadata.fileSize / 1024 / 1024).toFixed(2)} MB + + )} + {metadata.pageCount && ( + + Pages: {metadata.pageCount} + + )} + {metadata.error && ( + + Error: {metadata.error} + + )} + + ); + } + return null; + }; + + if (!history || operations.length === 0) { + return ( + + + {showOnlyApplied ? 'No applied operations found' : 'No operation history available'} + + + ); + } + + return ( + + + + {showOnlyApplied ? 'Applied Operations' : 'Operation History'} + + + {operations.length} operations + + + + + + {operations.map((operation, index) => ( + + + + + {getOperationIcon(operation.type)} + + + + {operation.type.charAt(0).toUpperCase() + operation.type.slice(1)} + + + {formatTimestamp(operation.timestamp)} + + + + + + {operation.status} + + + + {renderOperationDetails(operation)} + + {index < operations.length - 1 && } + + ))} + + + + {history && ( + + + Created: {formatTimestamp(history.createdAt)} + + + Last modified: {formatTimestamp(history.lastModified)} + + + )} + + ); +}; + +export default FileOperationHistory; \ No newline at end of file diff --git a/frontend/src/components/pageEditor/BulkSelectionPanel.tsx b/frontend/src/components/pageEditor/BulkSelectionPanel.tsx index e28d0c41f..5a6b4504f 100644 --- a/frontend/src/components/pageEditor/BulkSelectionPanel.tsx +++ b/frontend/src/components/pageEditor/BulkSelectionPanel.tsx @@ -4,7 +4,7 @@ import { Paper, Group, TextInput, Button, Text } from '@mantine/core'; interface BulkSelectionPanelProps { csvInput: string; setCsvInput: (value: string) => void; - selectedPages: string[]; + selectedPages: number[]; onUpdatePagesFromCSV: () => void; } diff --git a/frontend/src/components/pageEditor/DragDropGrid.tsx b/frontend/src/components/pageEditor/DragDropGrid.tsx index 18ccda8f9..39dbb396f 100644 --- a/frontend/src/components/pageEditor/DragDropGrid.tsx +++ b/frontend/src/components/pageEditor/DragDropGrid.tsx @@ -9,21 +9,21 @@ interface DragDropItem { interface DragDropGridProps { items: T[]; - selectedItems: string[]; + selectedItems: number[]; selectionMode: boolean; isAnimating: boolean; - onDragStart: (itemId: string) => void; + onDragStart: (pageNumber: number) => void; onDragEnd: () => void; onDragOver: (e: React.DragEvent) => void; - onDragEnter: (itemId: string) => void; + onDragEnter: (pageNumber: number) => void; onDragLeave: () => void; - onDrop: (e: React.DragEvent, targetId: string | 'end') => void; + onDrop: (e: React.DragEvent, targetPageNumber: number | 'end') => void; onEndZoneDragEnter: () => void; renderItem: (item: T, index: number, refs: React.MutableRefObject>) => React.ReactNode; renderSplitMarker?: (item: T, index: number) => React.ReactNode; - draggedItem: string | null; - dropTarget: string | null; - multiItemDrag: {itemIds: string[], count: number} | null; + draggedItem: number | null; + dropTarget: number | null; + multiItemDrag: {pageNumbers: number[], count: number} | null; dragPosition: {x: number, y: number} | null; } @@ -77,7 +77,13 @@ const DragDropGrid = ({ flexWrap: 'wrap', gap: '1.5rem', justifyContent: 'flex-start', - paddingBottom: '100px' + paddingBottom: '100px', + // Performance optimizations for smooth scrolling + willChange: 'scroll-position', + transform: 'translateZ(0)', // Force hardware acceleration + backfaceVisibility: 'hidden', + // Use containment for better rendering performance + contain: 'layout style paint', }} > {items.map((item, index) => ( diff --git a/frontend/src/components/pageEditor/FileEditor.tsx b/frontend/src/components/pageEditor/FileEditor.tsx deleted file mode 100644 index 7224f6453..000000000 --- a/frontend/src/components/pageEditor/FileEditor.tsx +++ /dev/null @@ -1,561 +0,0 @@ -import React, { useState, useCallback, useRef, useEffect } from 'react'; -import { - Button, Text, Center, Box, Notification, TextInput, LoadingOverlay, Modal, Alert, Container, - Stack, Group -} from '@mantine/core'; -import { Dropzone } from '@mantine/dropzone'; -import { useTranslation } from 'react-i18next'; -import UploadFileIcon from '@mui/icons-material/UploadFile'; -import { fileStorage } from '../../services/fileStorage'; -import { generateThumbnailForFile } from '../../utils/thumbnailUtils'; -import styles from './PageEditor.module.css'; -import FileThumbnail from './FileThumbnail'; -import BulkSelectionPanel from './BulkSelectionPanel'; -import DragDropGrid from './DragDropGrid'; -import FilePickerModal from '../shared/FilePickerModal'; - -interface FileItem { - id: string; - name: string; - pageCount: number; - thumbnail: string; - size: number; - file: File; - splitBefore?: boolean; -} - -interface FileEditorProps { - onOpenPageEditor?: (file: File) => void; - onMergeFiles?: (files: File[]) => void; - activeFiles?: File[]; - setActiveFiles?: (files: File[]) => void; - preSelectedFiles?: { file: File; url: string }[]; - onClearPreSelection?: () => void; -} - -const FileEditor = ({ - onOpenPageEditor, - onMergeFiles, - activeFiles = [], - setActiveFiles, - preSelectedFiles = [], - onClearPreSelection -}: FileEditorProps) => { - const { t } = useTranslation(); - - const [files, setFiles] = useState([]); - const [selectedFiles, setSelectedFiles] = useState([]); - const [status, setStatus] = useState(null); - const [loading, setLoading] = useState(false); - const [error, setError] = useState(null); - const [csvInput, setCsvInput] = useState(''); - const [selectionMode, setSelectionMode] = useState(false); - const [draggedFile, setDraggedFile] = useState(null); - const [dropTarget, setDropTarget] = useState(null); - const [multiFileDrag, setMultiFileDrag] = useState<{fileIds: string[], count: number} | null>(null); - const [dragPosition, setDragPosition] = useState<{x: number, y: number} | null>(null); - const [isAnimating, setIsAnimating] = useState(false); - const [showFilePickerModal, setShowFilePickerModal] = useState(false); - const fileRefs = useRef>(new Map()); - - // Convert shared files to FileEditor format - const convertToFileItem = useCallback(async (sharedFile: any): Promise => { - // Generate thumbnail if not already available - const thumbnail = sharedFile.thumbnail || await generateThumbnailForFile(sharedFile.file || sharedFile); - - return { - id: sharedFile.id || `file-${Date.now()}-${Math.random()}`, - name: (sharedFile.file?.name || sharedFile.name || 'unknown').replace(/\.pdf$/i, ''), - pageCount: sharedFile.pageCount || Math.floor(Math.random() * 20) + 1, // Mock for now - thumbnail, - size: sharedFile.file?.size || sharedFile.size || 0, - file: sharedFile.file || sharedFile, - }; - }, []); - - // Convert activeFiles to FileItem format - useEffect(() => { - const convertActiveFiles = async () => { - if (activeFiles.length > 0) { - setLoading(true); - try { - const convertedFiles = await Promise.all( - activeFiles.map(async (file) => { - const thumbnail = await generateThumbnailForFile(file); - return { - id: `file-${Date.now()}-${Math.random()}`, - name: file.name.replace(/\.pdf$/i, ''), - pageCount: Math.floor(Math.random() * 20) + 1, // Mock for now - thumbnail, - size: file.size, - file, - }; - }) - ); - setFiles(convertedFiles); - } catch (err) { - console.error('Error converting active files:', err); - } finally { - setLoading(false); - } - } else { - setFiles([]); - } - }; - - convertActiveFiles(); - }, [activeFiles]); - - // Only load shared files when explicitly passed (not on mount) - useEffect(() => { - const loadSharedFiles = async () => { - // Only load if we have pre-selected files (coming from FileManager) - if (preSelectedFiles.length > 0) { - setLoading(true); - try { - const convertedFiles = await Promise.all( - preSelectedFiles.map(convertToFileItem) - ); - if (setActiveFiles) { - const updatedActiveFiles = convertedFiles.map(fileItem => fileItem.file); - setActiveFiles(updatedActiveFiles); - } - } catch (err) { - console.error('Error converting pre-selected files:', err); - } finally { - setLoading(false); - } - } - }; - - loadSharedFiles(); - }, [preSelectedFiles, convertToFileItem]); - - // Handle pre-selected files - useEffect(() => { - if (preSelectedFiles.length > 0) { - const preSelectedIds = preSelectedFiles.map(f => f.id || f.name); - setSelectedFiles(preSelectedIds); - onClearPreSelection?.(); - } - }, [preSelectedFiles, onClearPreSelection]); - - // Process uploaded files - const handleFileUpload = useCallback(async (uploadedFiles: File[]) => { - setLoading(true); - setError(null); - - try { - const newFiles: FileItem[] = []; - - for (const file of uploadedFiles) { - if (file.type !== 'application/pdf') { - setError('Please upload only PDF files'); - continue; - } - - // Generate thumbnail and get page count - const thumbnail = await generateThumbnailForFile(file); - - const fileItem: FileItem = { - id: `file-${Date.now()}-${Math.random()}`, - name: file.name.replace(/\.pdf$/i, ''), - pageCount: Math.floor(Math.random() * 20) + 1, // Mock page count - thumbnail, - size: file.size, - file, - }; - - newFiles.push(fileItem); - - // Store in IndexedDB - await fileStorage.storeFile(file, thumbnail); - } - - if (setActiveFiles) { - setActiveFiles(prev => [...prev, ...newFiles.map(f => f.file)]); - } - - setStatus(`Added ${newFiles.length} files`); - } catch (err) { - const errorMessage = err instanceof Error ? err.message : 'Failed to process files'; - setError(errorMessage); - console.error('File processing error:', err); - } finally { - setLoading(false); - } - }, [setActiveFiles]); - - const selectAll = useCallback(() => { - setSelectedFiles(files.map(f => f.id)); - }, [files]); - - const deselectAll = useCallback(() => setSelectedFiles([]), []); - - const toggleFile = useCallback((fileId: string) => { - setSelectedFiles(prev => - prev.includes(fileId) - ? prev.filter(id => id !== fileId) - : [...prev, fileId] - ); - }, []); - - const toggleSelectionMode = useCallback(() => { - setSelectionMode(prev => { - const newMode = !prev; - if (!newMode) { - setSelectedFiles([]); - setCsvInput(''); - } - return newMode; - }); - }, []); - - const parseCSVInput = useCallback((csv: string) => { - const fileIds: string[] = []; - const ranges = csv.split(',').map(s => s.trim()).filter(Boolean); - - ranges.forEach(range => { - if (range.includes('-')) { - const [start, end] = range.split('-').map(n => parseInt(n.trim())); - for (let i = start; i <= end && i <= files.length; i++) { - if (i > 0) { - const file = files[i - 1]; - if (file) fileIds.push(file.id); - } - } - } else { - const fileIndex = parseInt(range); - if (fileIndex > 0 && fileIndex <= files.length) { - const file = files[fileIndex - 1]; - if (file) fileIds.push(file.id); - } - } - }); - - return fileIds; - }, [files]); - - const updateFilesFromCSV = useCallback(() => { - const fileIds = parseCSVInput(csvInput); - setSelectedFiles(fileIds); - }, [csvInput, parseCSVInput]); - - // Drag and drop handlers - const handleDragStart = useCallback((fileId: string) => { - setDraggedFile(fileId); - - if (selectionMode && selectedFiles.includes(fileId) && selectedFiles.length > 1) { - setMultiFileDrag({ - fileIds: selectedFiles, - count: selectedFiles.length - }); - } else { - setMultiFileDrag(null); - } - }, [selectionMode, selectedFiles]); - - const handleDragEnd = useCallback(() => { - setDraggedFile(null); - setDropTarget(null); - setMultiFileDrag(null); - setDragPosition(null); - }, []); - - const handleDragOver = useCallback((e: React.DragEvent) => { - e.preventDefault(); - - if (!draggedFile) return; - - if (multiFileDrag) { - setDragPosition({ x: e.clientX, y: e.clientY }); - } - - const elementUnderCursor = document.elementFromPoint(e.clientX, e.clientY); - if (!elementUnderCursor) return; - - const fileContainer = elementUnderCursor.closest('[data-file-id]'); - if (fileContainer) { - const fileId = fileContainer.getAttribute('data-file-id'); - if (fileId && fileId !== draggedFile) { - setDropTarget(fileId); - return; - } - } - - const endZone = elementUnderCursor.closest('[data-drop-zone="end"]'); - if (endZone) { - setDropTarget('end'); - return; - } - - setDropTarget(null); - }, [draggedFile, multiFileDrag]); - - const handleDragEnter = useCallback((fileId: string) => { - if (draggedFile && fileId !== draggedFile) { - setDropTarget(fileId); - } - }, [draggedFile]); - - const handleDragLeave = useCallback(() => { - // Let dragover handle this - }, []); - - const handleDrop = useCallback((e: React.DragEvent, targetFileId: string | 'end') => { - e.preventDefault(); - if (!draggedFile || draggedFile === targetFileId) return; - - let targetIndex: number; - if (targetFileId === 'end') { - targetIndex = files.length; - } else { - targetIndex = files.findIndex(f => f.id === targetFileId); - if (targetIndex === -1) return; - } - - const filesToMove = selectionMode && selectedFiles.includes(draggedFile) - ? selectedFiles - : [draggedFile]; - - if (setActiveFiles) { - // Update the local files state and sync with activeFiles - setFiles(prev => { - const newFiles = [...prev]; - const movedFiles = filesToMove.map(id => newFiles.find(f => f.id === id)!).filter(Boolean); - - // Remove moved files - filesToMove.forEach(id => { - const index = newFiles.findIndex(f => f.id === id); - if (index !== -1) newFiles.splice(index, 1); - }); - - // Insert at target position - newFiles.splice(targetIndex, 0, ...movedFiles); - - // Update activeFiles with the reordered File objects - setActiveFiles(newFiles.map(f => f.file)); - - return newFiles; - }); - } - - const moveCount = multiFileDrag ? multiFileDrag.count : 1; - setStatus(`${moveCount > 1 ? `${moveCount} files` : 'File'} reordered`); - - handleDragEnd(); - }, [draggedFile, files, selectionMode, selectedFiles, multiFileDrag, handleDragEnd, setActiveFiles]); - - const handleEndZoneDragEnter = useCallback(() => { - if (draggedFile) { - setDropTarget('end'); - } - }, [draggedFile]); - - // File operations - const handleDeleteFile = useCallback((fileId: string) => { - if (setActiveFiles) { - // Remove from local files and sync with activeFiles - setFiles(prev => { - const newFiles = prev.filter(f => f.id !== fileId); - setActiveFiles(newFiles.map(f => f.file)); - return newFiles; - }); - } - setSelectedFiles(prev => prev.filter(id => id !== fileId)); - }, [setActiveFiles]); - - const handleViewFile = useCallback((fileId: string) => { - const file = files.find(f => f.id === fileId); - if (file && onOpenPageEditor) { - onOpenPageEditor(file.file); - } - }, [files, onOpenPageEditor]); - - const handleMergeFromHere = useCallback((fileId: string) => { - const startIndex = files.findIndex(f => f.id === fileId); - if (startIndex === -1) return; - - const filesToMerge = files.slice(startIndex).map(f => f.file); - if (onMergeFiles) { - onMergeFiles(filesToMerge); - } - }, [files, onMergeFiles]); - - const handleSplitFile = useCallback((fileId: string) => { - const file = files.find(f => f.id === fileId); - if (file && onOpenPageEditor) { - onOpenPageEditor(file.file); - } - }, [files, onOpenPageEditor]); - - const handleLoadFromStorage = useCallback(async (selectedFiles: any[]) => { - if (selectedFiles.length === 0) return; - - setLoading(true); - try { - const convertedFiles = await Promise.all( - selectedFiles.map(convertToFileItem) - ); - setFiles(prev => [...prev, ...convertedFiles]); - setStatus(`Loaded ${selectedFiles.length} files from storage`); - } catch (err) { - console.error('Error loading files from storage:', err); - setError('Failed to load some files from storage'); - } finally { - setLoading(false); - } - }, [convertToFileItem]); - - - return ( - - - - - - - {selectionMode && ( - <> - - - - )} - - {/* Load from storage and upload buttons */} - - - - - - - - {selectionMode && ( - - )} - - ( - - )} - renderSplitMarker={(file, index) => ( -
- )} - /> - - - {/* File Picker Modal */} - setShowFilePickerModal(false)} - storedFiles={[]} // FileEditor doesn't have access to stored files, needs to be passed from parent - onSelectFiles={handleLoadFromStorage} - allowMultiple={true} - /> - - {status && ( - setStatus(null)} - style={{ position: 'fixed', bottom: 20, right: 20, zIndex: 1000 }} - > - {status} - - )} - - {error && ( - setError(null)} - style={{ position: 'fixed', bottom: 80, right: 20, zIndex: 1000 }} - > - {error} - - )} - - ); -}; - -export default FileEditor; diff --git a/frontend/src/components/pageEditor/FileThumbnail.tsx b/frontend/src/components/pageEditor/FileThumbnail.tsx index 46448a34c..759194dea 100644 --- a/frontend/src/components/pageEditor/FileThumbnail.tsx +++ b/frontend/src/components/pageEditor/FileThumbnail.tsx @@ -1,11 +1,11 @@ -import React from 'react'; -import { Text, Checkbox, Tooltip, ActionIcon, Badge } from '@mantine/core'; -import DeleteIcon from '@mui/icons-material/Delete'; +import React, { useState } from 'react'; +import { Text, Checkbox, Tooltip, ActionIcon, Badge, Modal } from '@mantine/core'; +import CloseIcon from '@mui/icons-material/Close'; import VisibilityIcon from '@mui/icons-material/Visibility'; -import MergeIcon from '@mui/icons-material/Merge'; -import SplitscreenIcon from '@mui/icons-material/Splitscreen'; +import HistoryIcon from '@mui/icons-material/History'; import DragIndicatorIcon from '@mui/icons-material/DragIndicator'; import styles from './PageEditor.module.css'; +import FileOperationHistory from '../history/FileOperationHistory'; interface FileItem { id: string; @@ -35,9 +35,8 @@ interface FileThumbnailProps { onToggleFile: (fileId: string) => void; onDeleteFile: (fileId: string) => void; onViewFile: (fileId: string) => void; - onMergeFromHere: (fileId: string) => void; - onSplitFile: (fileId: string) => void; onSetStatus: (status: string) => void; + toolMode?: boolean; } const FileThumbnail = ({ @@ -59,10 +58,11 @@ const FileThumbnail = ({ onToggleFile, onDeleteFile, onViewFile, - onMergeFromHere, - onSplitFile, onSetStatus, + toolMode = false, }: FileThumbnailProps) => { + const [showHistory, setShowHistory] = useState(false); + const formatFileSize = (bytes: number) => { if (bytes === 0) return '0 B'; const k = 1024; @@ -238,63 +238,53 @@ const FileThumbnail = ({ whiteSpace: 'nowrap' }} > - + {!toolMode && ( + <> + + { + e.stopPropagation(); + onViewFile(file.id); + onSetStatus(`Opened ${file.name}`); + }} + > + + + + + + )} + + { e.stopPropagation(); - onViewFile(file.id); - onSetStatus(`Opened ${file.name}`); + setShowHistory(true); + onSetStatus(`Viewing history for ${file.name}`); }} > - + - + { - e.stopPropagation(); - onMergeFromHere(file.id); - onSetStatus(`Starting merge from ${file.name}`); - }} - > - - - - - - { - e.stopPropagation(); - onSplitFile(file.id); - onSetStatus(`Opening ${file.name} in page editor`); - }} - > - - - - - - { e.stopPropagation(); onDeleteFile(file.id); - onSetStatus(`Deleted ${file.name}`); + onSetStatus(`Closed ${file.name}`); }} > - +
@@ -320,6 +310,21 @@ const FileThumbnail = ({ {formatFileSize(file.size)}
+ + {/* History Modal */} + setShowHistory(false)} + title={`Operation History - ${file.name}`} + size="lg" + scrollAreaComponent="div" + > + +
); }; diff --git a/frontend/src/components/pageEditor/PageEditor.module.css b/frontend/src/components/pageEditor/PageEditor.module.css index 5901e80e6..8b1c84638 100644 --- a/frontend/src/components/pageEditor/PageEditor.module.css +++ b/frontend/src/components/pageEditor/PageEditor.module.css @@ -1,10 +1,14 @@ -/* Page container hover effects */ +/* Page container hover effects - optimized for smooth scrolling */ .pageContainer { transition: transform 0.2s ease-in-out; + /* Enable hardware acceleration for smoother scrolling */ + will-change: transform; + transform: translateZ(0); + backface-visibility: hidden; } .pageContainer:hover { - transform: scale(1.02); + transform: scale(1.02) translateZ(0); } .pageContainer:hover .pageNumber { diff --git a/frontend/src/components/pageEditor/PageEditor.tsx b/frontend/src/components/pageEditor/PageEditor.tsx index fc4f93e3d..f1a5dfd3c 100644 --- a/frontend/src/components/pageEditor/PageEditor.tsx +++ b/frontend/src/components/pageEditor/PageEditor.tsx @@ -1,15 +1,14 @@ -import React, { useState, useCallback, useRef, useEffect } from "react"; +import React, { useState, useCallback, useRef, useEffect, useMemo } from "react"; import { Button, Text, Center, Checkbox, Box, Tooltip, ActionIcon, - Notification, TextInput, FileInput, LoadingOverlay, Modal, Alert, Container, - Stack, Group, Paper, SimpleGrid + Notification, TextInput, LoadingOverlay, Modal, Alert, + Stack, Group } from "@mantine/core"; import { useTranslation } from "react-i18next"; -import UploadFileIcon from "@mui/icons-material/UploadFile"; -import { usePDFProcessor } from "../../hooks/usePDFProcessor"; +import { useFileContext, useCurrentFile } from "../../contexts/FileContext"; +import { ViewType, ToolType } from "../../types/fileContext"; import { PDFDocument, PDFPage } from "../../types/pageEditor"; -import { fileStorage } from "../../services/fileStorage"; -import { generateThumbnailForFile } from "../../utils/thumbnailUtils"; +import { ProcessedFile as EnhancedProcessedFile } from "../../types/processing"; import { useUndoRedo } from "../../hooks/useUndoRedo"; import { RotatePagesCommand, @@ -19,20 +18,17 @@ import { ToggleSplitCommand } from "../../commands/pageCommands"; import { pdfExportService } from "../../services/pdfExportService"; -import styles from './pageEditor.module.css'; +import { useThumbnailGeneration } from "../../hooks/useThumbnailGeneration"; +import { calculateScaleFromFileSize } from "../../utils/thumbnailUtils"; +import { fileStorage } from "../../services/fileStorage"; +import './pageEditor.module.css'; import PageThumbnail from './PageThumbnail'; import BulkSelectionPanel from './BulkSelectionPanel'; import DragDropGrid from './DragDropGrid'; -import FilePickerModal from '../shared/FilePickerModal'; -import FileUploadSelector from '../shared/FileUploadSelector'; +import SkeletonLoader from '../shared/SkeletonLoader'; +import NavigationWarningModal from '../shared/NavigationWarningModal'; export interface PageEditorProps { - activeFiles: File[]; - setActiveFiles: (files: File[]) => void; - downloadUrl?: string | null; - setDownloadUrl?: (url: string | null) => void; - sharedFiles?: any[]; // For FileUploadSelector when no files loaded - // Optional callbacks to expose internal functions for PageEditorControls onFunctionsReady?: (functions: { handleUndo: () => void; @@ -53,33 +49,108 @@ export interface PageEditorProps { } const PageEditor = ({ - activeFiles, - setActiveFiles, - downloadUrl, - setDownloadUrl, - sharedFiles = [], onFunctionsReady, }: PageEditorProps) => { const { t } = useTranslation(); - const { processPDFFile, loading: pdfLoading } = usePDFProcessor(); - // Single merged document state - const [mergedPdfDocument, setMergedPdfDocument] = useState(null); - const [processedFiles, setProcessedFiles] = useState>(new Map()); + // Get file context + const fileContext = useFileContext(); + const { file: currentFile, processedFile: currentProcessedFile } = useCurrentFile(); + + // Use file context state + const { + activeFiles, + processedFiles, + selectedPageNumbers, + setSelectedPages, + updateProcessedFile, + setHasUnsavedChanges, + hasUnsavedChanges, + isProcessing: globalProcessing, + processingProgress, + clearAllFiles + } = fileContext; + + // Edit state management + const [editedDocument, setEditedDocument] = useState(null); + const [hasUnsavedDraft, setHasUnsavedDraft] = useState(false); + const [showResumeModal, setShowResumeModal] = useState(false); + const [foundDraft, setFoundDraft] = useState(null); + const autoSaveTimer = useRef(null); + + // Simple computed document from processed files (no caching needed) + const mergedPdfDocument = useMemo(() => { + if (activeFiles.length === 0) return null; + + if (activeFiles.length === 1) { + // Single file + const processedFile = processedFiles.get(activeFiles[0]); + if (!processedFile) return null; + + return { + id: processedFile.id, + name: activeFiles[0].name, + file: activeFiles[0], + pages: processedFile.pages.map(page => ({ + ...page, + rotation: page.rotation || 0, + splitBefore: page.splitBefore || false + })), + totalPages: processedFile.totalPages + }; + } else { + // Multiple files - merge them + const allPages: PDFPage[] = []; + let totalPages = 0; + const filenames: string[] = []; + + activeFiles.forEach((file, i) => { + const processedFile = processedFiles.get(file); + if (processedFile) { + filenames.push(file.name.replace(/\.pdf$/i, '')); + + processedFile.pages.forEach((page, pageIndex) => { + const newPage: PDFPage = { + ...page, + id: `${i}-${page.id}`, // Unique ID across all files + pageNumber: totalPages + pageIndex + 1, + rotation: page.rotation || 0, + splitBefore: page.splitBefore || false + }; + allPages.push(newPage); + }); + + totalPages += processedFile.pages.length; + } + }); + + if (allPages.length === 0) return null; + + return { + id: `merged-${Date.now()}`, + name: filenames.join(' + '), + file: activeFiles[0], // Use first file as reference + pages: allPages, + totalPages: totalPages + }; + } + }, [activeFiles, processedFiles]); + + // Display document: Use edited version if exists, otherwise original + const displayDocument = editedDocument || mergedPdfDocument; + const [filename, setFilename] = useState(""); + - // Page editor state - const [selectedPages, setSelectedPages] = useState([]); + // Page editor state (use context for selectedPages) const [status, setStatus] = useState(null); - const [loading, setLoading] = useState(false); - const [error, setError] = useState(null); const [csvInput, setCsvInput] = useState(""); const [selectionMode, setSelectionMode] = useState(false); // Drag and drop state - const [draggedPage, setDraggedPage] = useState(null); - const [dropTarget, setDropTarget] = useState(null); - const [multiPageDrag, setMultiPageDrag] = useState<{pageIds: string[], count: number} | null>(null); + const [draggedPage, setDraggedPage] = useState(null); + const [dropTarget, setDropTarget] = useState(null); + const [multiPageDrag, setMultiPageDrag] = useState<{pageNumbers: number[], count: number} | null>(null); const [dragPosition, setDragPosition] = useState<{x: number, y: number} | null>(null); // Export state @@ -88,7 +159,7 @@ const PageEditor = ({ const [exportPreview, setExportPreview] = useState<{pageCount: number; splitCount: number; estimatedSize: string} | null>(null); // Animation state - const [movingPage, setMovingPage] = useState(null); + const [movingPage, setMovingPage] = useState(null); const [pagePositions, setPagePositions] = useState>(new Map()); const [isAnimating, setIsAnimating] = useState(false); const pageRefs = useRef>(new Map()); @@ -97,185 +168,197 @@ const PageEditor = ({ // Undo/Redo system const { executeCommand, undo, redo, canUndo, canRedo } = useUndoRedo(); - // Process uploaded file - const handleFileUpload = useCallback(async (uploadedFile: File | any) => { - if (!uploadedFile) { - setError('No file provided'); - return; - } - - let fileToProcess: File; - - // Handle FileWithUrl objects from storage - if (uploadedFile.storedInIndexedDB && uploadedFile.arrayBuffer) { - try { - console.log('Converting FileWithUrl to File:', uploadedFile.name); - const arrayBuffer = await uploadedFile.arrayBuffer(); - const blob = new Blob([arrayBuffer], { type: uploadedFile.type || 'application/pdf' }); - fileToProcess = new File([blob], uploadedFile.name, { - type: uploadedFile.type || 'application/pdf', - lastModified: uploadedFile.lastModified || Date.now() - }); - } catch (error) { - console.error('Error converting FileWithUrl:', error); - setError('Unable to load file from storage'); - return; - } - } else if (uploadedFile instanceof File) { - fileToProcess = uploadedFile; - } else { - setError('Invalid file object'); - console.error('handleFileUpload received unsupported object:', uploadedFile); - return; - } - - if (fileToProcess.type !== 'application/pdf') { - setError('Please upload a valid PDF file'); - return; - } - - const fileKey = `${fileToProcess.name}-${fileToProcess.size}`; - - // Skip processing if already processed - if (processedFiles.has(fileKey)) return; - - setLoading(true); - setError(null); - - try { - const document = await processPDFFile(fileToProcess); - - // Store processed document - setProcessedFiles(prev => new Map(prev).set(fileKey, document)); - setFilename(fileToProcess.name.replace(/\.pdf$/i, '')); - setSelectedPages([]); - - - if (document.pages.length > 0) { - // Only store if it's a new file (not from storage) - if (!uploadedFile.storedInIndexedDB) { - const thumbnail = await generateThumbnailForFile(fileToProcess); - await fileStorage.storeFile(fileToProcess, thumbnail); - } - } - - setStatus(`PDF loaded successfully with ${document.totalPages} pages`); - } catch (err) { - const errorMessage = err instanceof Error ? err.message : 'Failed to process PDF'; - setError(errorMessage); - console.error('PDF processing error:', err); - } finally { - setLoading(false); - } - }, [processPDFFile, activeFiles, setActiveFiles, processedFiles]); - - // Process multiple uploaded files - just add them to activeFiles like FileManager does - const handleMultipleFileUpload = useCallback((uploadedFiles: File[]) => { - if (!uploadedFiles || uploadedFiles.length === 0) { - setError('No files provided'); - return; - } - - // Simply set the activeFiles to the selected files (same as FileManager approach) - setActiveFiles(uploadedFiles); - }, []); - - // Merge multiple PDF documents into one - const mergeAllPDFs = useCallback(() => { - if (activeFiles.length === 0) { - setMergedPdfDocument(null); - return; - } - - if (activeFiles.length === 1) { - // Single file - use it directly - const fileKey = `${activeFiles[0].name}-${activeFiles[0].size}`; - const pdfDoc = processedFiles.get(fileKey); - if (pdfDoc) { - setMergedPdfDocument(pdfDoc); + // Set initial filename when document changes + useEffect(() => { + if (mergedPdfDocument) { + if (activeFiles.length === 1) { setFilename(activeFiles[0].name.replace(/\.pdf$/i, '')); + } else { + const filenames = activeFiles.map(f => f.name.replace(/\.pdf$/i, '')); + setFilename(filenames.join('_')); } - } else { - // Multiple files - merge them - const allPages: PDFPage[] = []; - let totalPages = 0; - const filenames: string[] = []; - - activeFiles.forEach((file, fileIndex) => { - const fileKey = `${file.name}-${file.size}`; - const pdfDoc = processedFiles.get(fileKey); - if (pdfDoc) { - filenames.push(file.name.replace(/\.pdf$/i, '')); - pdfDoc.pages.forEach((page, pageIndex) => { - // Create new page with updated IDs and page numbers for merged document - const newPage: PDFPage = { - ...page, - id: `${fileIndex}-${page.id}`, // Unique ID across all files - pageNumber: totalPages + pageIndex + 1, - sourceFile: file.name // Track which file this page came from - }; - allPages.push(newPage); - }); - totalPages += pdfDoc.pages.length; - } - }); - - const mergedDocument: PDFDocument = { - pages: allPages, - totalPages: totalPages, - title: filenames.join(' + '), - metadata: { - title: filenames.join(' + '), - createdAt: new Date().toISOString(), - modifiedAt: new Date().toISOString(), - } - }; - - setMergedPdfDocument(mergedDocument); - setFilename(filenames.join('_')); } - }, [activeFiles, processedFiles]); + }, [mergedPdfDocument, activeFiles]); - // Auto-process files from activeFiles - useEffect(() => { - console.log('Auto-processing effect triggered:', { - activeFilesCount: activeFiles.length, - processedFilesCount: processedFiles.size, - activeFileNames: activeFiles.map(f => f.name) - }); + // Handle file upload from FileUploadSelector (now using context) + const handleMultipleFileUpload = useCallback(async (uploadedFiles: File[]) => { + if (!uploadedFiles || uploadedFiles.length === 0) { + setStatus('No files provided'); + return; + } + + // Add files to context + await fileContext.addFiles(uploadedFiles); + setStatus(`Added ${uploadedFiles.length} file(s) for processing`); + }, [fileContext]); + + + // PageEditor no longer handles cleanup - it's centralized in FileContext + + // Shared PDF instance for thumbnail generation + const [sharedPdfInstance, setSharedPdfInstance] = useState(null); + const [thumbnailGenerationStarted, setThumbnailGenerationStarted] = useState(false); + + // Thumbnail generation (opt-in for visual tools) + const { + generateThumbnails, + addThumbnailToCache, + getThumbnailFromCache, + stopGeneration, + destroyThumbnails + } = useThumbnailGeneration(); + + // Start thumbnail generation process (separate from document loading) + const startThumbnailGeneration = useCallback(() => { + console.log('🎬 PageEditor: startThumbnailGeneration called'); + console.log('🎬 Conditions - mergedPdfDocument:', !!mergedPdfDocument, 'activeFiles:', activeFiles.length, 'started:', thumbnailGenerationStarted); - activeFiles.forEach(file => { - const fileKey = `${file.name}-${file.size}`; - console.log(`Checking file ${file.name}: processed =`, processedFiles.has(fileKey)); - if (!processedFiles.has(fileKey)) { - console.log('Processing file:', file.name); - handleFileUpload(file); - } - }); - }, [activeFiles, processedFiles, handleFileUpload]); - - // Merge multiple PDF documents into one when all files are processed - useEffect(() => { - if (activeFiles.length > 0) { - const allProcessed = activeFiles.every(file => { - const fileKey = `${file.name}-${file.size}`; - return processedFiles.has(fileKey); - }); - - if (allProcessed && activeFiles.length > 0) { - mergeAllPDFs(); - } + if (!mergedPdfDocument || activeFiles.length !== 1 || thumbnailGenerationStarted) { + console.log('🎬 PageEditor: Skipping thumbnail generation due to conditions'); + return; } - }, [activeFiles, processedFiles, mergeAllPDFs]); + + const file = activeFiles[0]; + const totalPages = mergedPdfDocument.totalPages; + + console.log('🎬 PageEditor: Starting thumbnail generation for', totalPages, 'pages'); + setThumbnailGenerationStarted(true); + + // Run everything asynchronously to avoid blocking the main thread + setTimeout(async () => { + try { + // Load PDF array buffer for Web Workers + const arrayBuffer = await file.arrayBuffer(); + + // Generate page numbers for pages that don't have thumbnails yet + const pageNumbers = Array.from({ length: totalPages }, (_, i) => i + 1) + .filter(pageNum => { + const page = mergedPdfDocument.pages.find(p => p.pageNumber === pageNum); + return !page?.thumbnail; // Only generate for pages without thumbnails + }); + + console.log(`🎬 PageEditor: Generating thumbnails for ${pageNumbers.length} pages (out of ${totalPages} total):`, pageNumbers.slice(0, 10), pageNumbers.length > 10 ? '...' : ''); + + // If no pages need thumbnails, we're done + if (pageNumbers.length === 0) { + console.log('🎬 PageEditor: All pages already have thumbnails, no generation needed'); + return; + } + + // Calculate quality scale based on file size + const scale = activeFiles.length === 1 ? calculateScaleFromFileSize(activeFiles[0].size) : 0.2; + + // Start parallel thumbnail generation WITHOUT blocking the main thread + const generationPromise = generateThumbnails( + arrayBuffer, + pageNumbers, + { + scale, // Dynamic quality based on file size + quality: 0.8, + batchSize: 15, // Smaller batches per worker for smoother UI + parallelBatches: 3 // Use 3 Web Workers in parallel + }, + // Progress callback (throttled for better performance) + (progress) => { + console.log(`🎬 PageEditor: Progress - ${progress.completed}/${progress.total} pages, ${progress.thumbnails.length} new thumbnails`); + // Batch process thumbnails to reduce main thread work + requestAnimationFrame(() => { + progress.thumbnails.forEach(({ pageNumber, thumbnail }) => { + // Check cache first, then send thumbnail + const pageId = `${file.name}-page-${pageNumber}`; + const cached = getThumbnailFromCache(pageId); + + if (!cached) { + // Cache and send to component + addThumbnailToCache(pageId, thumbnail); + + window.dispatchEvent(new CustomEvent('thumbnailReady', { + detail: { pageNumber, thumbnail, pageId } + })); + console.log(`✓ PageEditor: Dispatched thumbnail for page ${pageNumber}`); + } + }); + }); + } + ); + + // Handle completion properly + generationPromise + .then((allThumbnails) => { + console.log(`✅ PageEditor: Thumbnail generation completed! Generated ${allThumbnails.length} thumbnails`); + // Don't reset thumbnailGenerationStarted here - let it stay true to prevent restarts + }) + .catch(error => { + console.error('✗ PageEditor: Web Worker thumbnail generation failed:', error); + setThumbnailGenerationStarted(false); + }); + + } catch (error) { + console.error('Failed to start Web Worker thumbnail generation:', error); + setThumbnailGenerationStarted(false); + } + }, 0); // setTimeout with 0ms to defer to next tick + }, [mergedPdfDocument, activeFiles, thumbnailGenerationStarted, getThumbnailFromCache, addThumbnailToCache]); + + // Start thumbnail generation after document loads + useEffect(() => { + console.log('🎬 PageEditor: Thumbnail generation effect triggered'); + console.log('🎬 Conditions - mergedPdfDocument:', !!mergedPdfDocument, 'started:', thumbnailGenerationStarted); + + if (mergedPdfDocument && !thumbnailGenerationStarted) { + // Check if ALL pages already have thumbnails from processed files + const totalPages = mergedPdfDocument.pages.length; + const pagesWithThumbnails = mergedPdfDocument.pages.filter(page => page.thumbnail).length; + const hasAllThumbnails = pagesWithThumbnails === totalPages; + + console.log('🎬 PageEditor: Thumbnail status:', { + totalPages, + pagesWithThumbnails, + hasAllThumbnails, + missingThumbnails: totalPages - pagesWithThumbnails + }); + + if (hasAllThumbnails) { + console.log('🎬 PageEditor: Skipping generation - all thumbnails already exist'); + return; // Skip generation if ALL thumbnails already exist + } + + console.log('🎬 PageEditor: Some thumbnails missing, proceeding with generation'); + // Small delay to let document render, then start thumbnail generation + console.log('🎬 PageEditor: Scheduling thumbnail generation in 500ms'); + const timer = setTimeout(startThumbnailGeneration, 500); + return () => clearTimeout(timer); + } + }, [mergedPdfDocument, startThumbnailGeneration, thumbnailGenerationStarted]); + + // Cleanup shared PDF instance when component unmounts (but preserve cache) + useEffect(() => { + return () => { + if (sharedPdfInstance) { + sharedPdfInstance.destroy(); + setSharedPdfInstance(null); + } + setThumbnailGenerationStarted(false); + // DON'T stop generation on file changes - preserve cache for view switching + // stopGeneration(); + }; + }, [sharedPdfInstance]); // Only depend on PDF instance, not activeFiles // Clear selections when files change useEffect(() => { setSelectedPages([]); setCsvInput(""); setSelectionMode(false); - }, [activeFiles]); + }, [activeFiles, setSelectedPages]); + + // Sync csvInput with selectedPageNumbers changes + useEffect(() => { + // Simply sort the page numbers and join them + const sortedPageNumbers = [...selectedPageNumbers].sort((a, b) => a - b); + const newCsvInput = sortedPageNumbers.join(', '); + setCsvInput(newCsvInput); + }, [selectedPageNumbers]); - // Global drag cleanup to handle drops outside valid areas useEffect(() => { const handleGlobalDragEnd = () => { // Clean up drag state when drag operation ends anywhere @@ -286,7 +369,7 @@ const PageEditor = ({ }; const handleGlobalDrop = (e: DragEvent) => { - // Prevent default to avoid browser navigation on invalid drops + // Prevent default to handle invalid drops e.preventDefault(); }; @@ -303,19 +386,30 @@ const PageEditor = ({ const selectAll = useCallback(() => { if (mergedPdfDocument) { - setSelectedPages(mergedPdfDocument.pages.map(p => p.id)); + setSelectedPages(mergedPdfDocument.pages.map(p => p.pageNumber)); } - }, [mergedPdfDocument]); + }, [mergedPdfDocument, setSelectedPages]); - const deselectAll = useCallback(() => setSelectedPages([]), []); + const deselectAll = useCallback(() => setSelectedPages([]), [setSelectedPages]); - const togglePage = useCallback((pageId: string) => { - setSelectedPages(prev => - prev.includes(pageId) - ? prev.filter(id => id !== pageId) - : [...prev, pageId] - ); - }, []); + const togglePage = useCallback((pageNumber: number) => { + console.log('🔄 Toggling page', pageNumber); + + // Check if currently selected and update accordingly + const isCurrentlySelected = selectedPageNumbers.includes(pageNumber); + + if (isCurrentlySelected) { + // Remove from selection + console.log('🔄 Removing page', pageNumber); + const newSelectedPageNumbers = selectedPageNumbers.filter(num => num !== pageNumber); + setSelectedPages(newSelectedPageNumbers); + } else { + // Add to selection + console.log('🔄 Adding page', pageNumber); + const newSelectedPageNumbers = [...selectedPageNumbers, pageNumber]; + setSelectedPages(newSelectedPageNumbers); + } + }, [selectedPageNumbers, setSelectedPages]); const toggleSelectionMode = useCallback(() => { setSelectionMode(prev => { @@ -332,7 +426,7 @@ const PageEditor = ({ const parseCSVInput = useCallback((csv: string) => { if (!mergedPdfDocument) return []; - const pageIds: string[] = []; + const pageNumbers: number[] = []; const ranges = csv.split(',').map(s => s.trim()).filter(Boolean); ranges.forEach(range => { @@ -340,40 +434,38 @@ const PageEditor = ({ const [start, end] = range.split('-').map(n => parseInt(n.trim())); for (let i = start; i <= end && i <= mergedPdfDocument.totalPages; i++) { if (i > 0) { - const page = mergedPdfDocument.pages.find(p => p.pageNumber === i); - if (page) pageIds.push(page.id); + pageNumbers.push(i); } } } else { const pageNum = parseInt(range); if (pageNum > 0 && pageNum <= mergedPdfDocument.totalPages) { - const page = mergedPdfDocument.pages.find(p => p.pageNumber === pageNum); - if (page) pageIds.push(page.id); + pageNumbers.push(pageNum); } } }); - return pageIds; + return pageNumbers; }, [mergedPdfDocument]); const updatePagesFromCSV = useCallback(() => { - const pageIds = parseCSVInput(csvInput); - setSelectedPages(pageIds); - }, [csvInput, parseCSVInput]); + const pageNumbers = parseCSVInput(csvInput); + setSelectedPages(pageNumbers); + }, [csvInput, parseCSVInput, setSelectedPages]); - const handleDragStart = useCallback((pageId: string) => { - setDraggedPage(pageId); + const handleDragStart = useCallback((pageNumber: number) => { + setDraggedPage(pageNumber); // Check if this is a multi-page drag in selection mode - if (selectionMode && selectedPages.includes(pageId) && selectedPages.length > 1) { + if (selectionMode && selectedPageNumbers.includes(pageNumber) && selectedPageNumbers.length > 1) { setMultiPageDrag({ - pageIds: selectedPages, - count: selectedPages.length + pageNumbers: selectedPageNumbers, + count: selectedPageNumbers.length }); } else { setMultiPageDrag(null); } - }, [selectionMode, selectedPages]); + }, [selectionMode, selectedPageNumbers]); const handleDragEnd = useCallback(() => { // Clean up drag state regardless of where the drop happened @@ -398,11 +490,12 @@ const PageEditor = ({ if (!elementUnderCursor) return; // Find the closest page container - const pageContainer = elementUnderCursor.closest('[data-page-id]'); + const pageContainer = elementUnderCursor.closest('[data-page-number]'); if (pageContainer) { - const pageId = pageContainer.getAttribute('data-page-id'); - if (pageId && pageId !== draggedPage) { - setDropTarget(pageId); + const pageNumberStr = pageContainer.getAttribute('data-page-number'); + const pageNumber = pageNumberStr ? parseInt(pageNumberStr) : null; + if (pageNumber && pageNumber !== draggedPage) { + setDropTarget(pageNumber); return; } } @@ -418,9 +511,9 @@ const PageEditor = ({ setDropTarget(null); }, [draggedPage, multiPageDrag]); - const handleDragEnter = useCallback((pageId: string) => { - if (draggedPage && pageId !== draggedPage) { - setDropTarget(pageId); + const handleDragEnter = useCallback((pageNumber: number) => { + if (draggedPage && pageNumber !== draggedPage) { + setDropTarget(pageNumber); } }, [draggedPage]); @@ -428,127 +521,250 @@ const PageEditor = ({ // Don't clear drop target on drag leave - let dragover handle it }, []); - // Create setPdfDocument wrapper for merged document + // Update PDF document state with edit tracking const setPdfDocument = useCallback((updatedDoc: PDFDocument) => { - setMergedPdfDocument(updatedDoc); - // Return the updated document for immediate use in animations + console.log('setPdfDocument called - setting edited state'); + + // Update local edit state for immediate visual feedback + setEditedDocument(updatedDoc); + setHasUnsavedChanges(true); // Use global state + setHasUnsavedDraft(true); // Mark that we have unsaved draft changes + + // Auto-save to drafts (debounced) - only if we have new changes + if (autoSaveTimer.current) { + clearTimeout(autoSaveTimer.current); + } + + autoSaveTimer.current = setTimeout(() => { + if (hasUnsavedDraft) { + saveDraftToIndexedDB(updatedDoc); + setHasUnsavedDraft(false); // Mark draft as saved + } + }, 30000); // Auto-save after 30 seconds of inactivity + return updatedDoc; - }, []); + }, [setHasUnsavedChanges, hasUnsavedDraft]); - const animateReorder = useCallback((pageId: string, targetIndex: number) => { - if (!mergedPdfDocument || isAnimating) return; + // Save draft to separate IndexedDB location + const saveDraftToIndexedDB = useCallback(async (doc: PDFDocument) => { + try { + const draftKey = `draft-${doc.id || 'merged'}`; + const draftData = { + document: doc, + timestamp: Date.now(), + originalFiles: activeFiles.map(f => f.name) + }; + + // Save to 'pdf-drafts' store in IndexedDB + const request = indexedDB.open('stirling-pdf-drafts', 1); + request.onupgradeneeded = () => { + const db = request.result; + if (!db.objectStoreNames.contains('drafts')) { + db.createObjectStore('drafts'); + } + }; + + request.onsuccess = () => { + const db = request.result; + const transaction = db.transaction('drafts', 'readwrite'); + const store = transaction.objectStore('drafts'); + store.put(draftData, draftKey); + console.log('Draft auto-saved to IndexedDB'); + }; + } catch (error) { + console.warn('Failed to auto-save draft:', error); + } + }, [activeFiles]); + // Clean up draft from IndexedDB + const cleanupDraft = useCallback(async () => { + try { + const draftKey = `draft-${mergedPdfDocument?.id || 'merged'}`; + const request = indexedDB.open('stirling-pdf-drafts', 1); + + request.onsuccess = () => { + const db = request.result; + const transaction = db.transaction('drafts', 'readwrite'); + const store = transaction.objectStore('drafts'); + store.delete(draftKey); + }; + } catch (error) { + console.warn('Failed to cleanup draft:', error); + } + }, [mergedPdfDocument]); + + // Apply changes to create new processed file + const applyChanges = useCallback(async () => { + if (!editedDocument || !mergedPdfDocument) return; + + try { + if (activeFiles.length === 1) { + const file = activeFiles[0]; + const currentProcessedFile = processedFiles.get(file); + + if (currentProcessedFile) { + const updatedProcessedFile = { + ...currentProcessedFile, + id: `${currentProcessedFile.id}-edited-${Date.now()}`, + pages: editedDocument.pages.map(page => ({ + ...page, + rotation: page.rotation || 0, + splitBefore: page.splitBefore || false + })), + totalPages: editedDocument.pages.length, + lastModified: Date.now() + }; + + updateProcessedFile(file, updatedProcessedFile); + } + } else if (activeFiles.length > 1) { + setStatus('Apply changes for multiple files not yet supported'); + return; + } + + // Wait for the processed file update to complete before clearing edit state + setTimeout(() => { + setEditedDocument(null); + setHasUnsavedChanges(false); + setHasUnsavedDraft(false); + cleanupDraft(); + setStatus('Changes applied successfully'); + }, 100); + + } catch (error) { + console.error('Failed to apply changes:', error); + setStatus('Failed to apply changes'); + } + }, [editedDocument, mergedPdfDocument, processedFiles, activeFiles, updateProcessedFile, setHasUnsavedChanges, setStatus, cleanupDraft]); + + const animateReorder = useCallback((pageNumber: number, targetIndex: number) => { + if (!displayDocument || isAnimating) return; // In selection mode, if the dragged page is selected, move all selected pages - const pagesToMove = selectionMode && selectedPages.includes(pageId) - ? selectedPages - : [pageId]; + const pagesToMove = selectionMode && selectedPageNumbers.includes(pageNumber) + ? selectedPageNumbers.map(num => { + const page = displayDocument.pages.find(p => p.pageNumber === num); + return page?.id || ''; + }).filter(id => id) + : [displayDocument.pages.find(p => p.pageNumber === pageNumber)?.id || ''].filter(id => id); - const originalIndex = mergedPdfDocument.pages.findIndex(p => p.id === pageId); + const originalIndex = displayDocument.pages.findIndex(p => p.pageNumber === pageNumber); if (originalIndex === -1 || originalIndex === targetIndex) return; + // Skip animation for large documents (500+ pages) to improve performance + const isLargeDocument = displayDocument.pages.length > 500; + + if (isLargeDocument) { + // For large documents, just execute the command without animation + if (pagesToMove.length > 1) { + const command = new MovePagesCommand(displayDocument, setPdfDocument, pagesToMove, targetIndex); + executeCommand(command); + } else { + const pageId = pagesToMove[0]; + const command = new ReorderPageCommand(displayDocument, setPdfDocument, pageId, targetIndex); + executeCommand(command); + } + return; + } + setIsAnimating(true); - // Get current positions of all pages by querying DOM directly + // For smaller documents, determine which pages might be affected by the move + const startIndex = Math.min(originalIndex, targetIndex); + const endIndex = Math.max(originalIndex, targetIndex); + const affectedPageIds = displayDocument.pages + .slice(Math.max(0, startIndex - 5), Math.min(displayDocument.pages.length, endIndex + 5)) + .map(p => p.id); + + // Only capture positions for potentially affected pages const currentPositions = new Map(); - const allCurrentElements = Array.from(document.querySelectorAll('[data-page-id]')); - - - // Capture positions from actual DOM elements - allCurrentElements.forEach((element) => { - const pageId = element.getAttribute('data-page-id'); - if (pageId) { + + affectedPageIds.forEach(pageId => { + const element = document.querySelector(`[data-page-number="${pageId}"]`); + if (element) { const rect = element.getBoundingClientRect(); currentPositions.set(pageId, { x: rect.left, y: rect.top }); } }); - - // Execute the reorder - for multi-page, we use a different command + // Execute the reorder command if (pagesToMove.length > 1) { - // Multi-page move - use MovePagesCommand - const command = new MovePagesCommand(mergedPdfDocument, setPdfDocument, pagesToMove, targetIndex); + const command = new MovePagesCommand(displayDocument, setPdfDocument, pagesToMove, targetIndex); executeCommand(command); } else { - // Single page move - const command = new ReorderPageCommand(mergedPdfDocument, setPdfDocument, pageId, targetIndex); + const pageId = pagesToMove[0]; + const command = new ReorderPageCommand(displayDocument, setPdfDocument, pageId, targetIndex); executeCommand(command); } - // Wait for state update and DOM to update, then get new positions and animate + // Animate only the affected pages setTimeout(() => { requestAnimationFrame(() => { requestAnimationFrame(() => { - const newPositions = new Map(); + const newPositions = new Map(); - // Re-get all page elements after state update - const allPageElements = Array.from(document.querySelectorAll('[data-page-id]')); - - allPageElements.forEach((element) => { - const pageId = element.getAttribute('data-page-id'); - if (pageId) { + // Get new positions only for affected pages + affectedPageIds.forEach(pageId => { + const element = document.querySelector(`[data-page-number="${pageId}"]`); + if (element) { const rect = element.getBoundingClientRect(); newPositions.set(pageId, { x: rect.left, y: rect.top }); } }); - let animationCount = 0; + const elementsToAnimate: HTMLElement[] = []; - // Calculate and apply animations using DOM elements directly - allPageElements.forEach((element) => { - const pageId = element.getAttribute('data-page-id'); - if (!pageId) return; + // Apply animations only to pages that actually moved + affectedPageIds.forEach(pageId => { + const element = document.querySelector(`[data-page-number="${pageId}"]`) as HTMLElement; + if (!element) return; const currentPos = currentPositions.get(pageId); const newPos = newPositions.get(pageId); - if (element && currentPos && newPos) { + if (currentPos && newPos) { const deltaX = currentPos.x - newPos.x; const deltaY = currentPos.y - newPos.y; - if (Math.abs(deltaX) > 1 || Math.abs(deltaY) > 1) { - animationCount++; - const htmlElement = element as HTMLElement; - // Apply initial transform (from new position back to old position) - htmlElement.style.transform = `translate(${deltaX}px, ${deltaY}px)`; - htmlElement.style.transition = 'none'; - + elementsToAnimate.push(element); + + // Apply initial transform + element.style.transform = `translate(${deltaX}px, ${deltaY}px)`; + element.style.transition = 'none'; + // Force reflow - htmlElement.offsetHeight; - + element.offsetHeight; + // Animate to final position - htmlElement.style.transition = 'transform 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94)'; - htmlElement.style.transform = 'translate(0px, 0px)'; + element.style.transition = 'transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94)'; + element.style.transform = 'translate(0px, 0px)'; } } }); - - // Clean up after animation + // Clean up after animation (only for animated elements) setTimeout(() => { - const elementsToCleanup = Array.from(document.querySelectorAll('[data-page-id]')); - elementsToCleanup.forEach((element) => { - const htmlElement = element as HTMLElement; - htmlElement.style.transform = ''; - htmlElement.style.transition = ''; + elementsToAnimate.forEach((element) => { + element.style.transform = ''; + element.style.transition = ''; }); setIsAnimating(false); - }, 400); + }, 300); }); }); }, 10); // Small delay to allow state update - }, [mergedPdfDocument, isAnimating, executeCommand, selectionMode, selectedPages, setPdfDocument]); + }, [displayDocument, isAnimating, executeCommand, selectionMode, selectedPageNumbers, setPdfDocument]); - const handleDrop = useCallback((e: React.DragEvent, targetPageId: string | 'end') => { + const handleDrop = useCallback((e: React.DragEvent, targetPageNumber: number | 'end') => { e.preventDefault(); - if (!draggedPage || !mergedPdfDocument || draggedPage === targetPageId) return; + if (!draggedPage || !displayDocument || draggedPage === targetPageNumber) return; let targetIndex: number; - if (targetPageId === 'end') { - targetIndex = mergedPdfDocument.pages.length; + if (targetPageNumber === 'end') { + targetIndex = displayDocument.pages.length; } else { - targetIndex = mergedPdfDocument.pages.findIndex(p => p.id === targetPageId); + targetIndex = displayDocument.pages.findIndex(p => p.pageNumber === targetPageNumber); if (targetIndex === -1) return; } @@ -561,7 +777,7 @@ const PageEditor = ({ const moveCount = multiPageDrag ? multiPageDrag.count : 1; setStatus(`${moveCount > 1 ? `${moveCount} pages` : 'Page'} reordered`); - }, [draggedPage, mergedPdfDocument, animateReorder, multiPageDrag]); + }, [draggedPage, displayDocument, animateReorder, multiPageDrag]); const handleEndZoneDragEnter = useCallback(() => { if (draggedPage) { @@ -570,38 +786,44 @@ const PageEditor = ({ }, [draggedPage]); const handleRotate = useCallback((direction: 'left' | 'right') => { - if (!mergedPdfDocument) return; + if (!displayDocument) return; const rotation = direction === 'left' ? -90 : 90; const pagesToRotate = selectionMode - ? selectedPages - : mergedPdfDocument.pages.map(p => p.id); + ? selectedPageNumbers.map(pageNum => { + const page = displayDocument.pages.find(p => p.pageNumber === pageNum); + return page?.id || ''; + }).filter(id => id) + : displayDocument.pages.map(p => p.id); - if (selectionMode && selectedPages.length === 0) return; + if (selectionMode && selectedPageNumbers.length === 0) return; const command = new RotatePagesCommand( - mergedPdfDocument, + displayDocument, setPdfDocument, pagesToRotate, rotation ); executeCommand(command); - const pageCount = selectionMode ? selectedPages.length : mergedPdfDocument.pages.length; + const pageCount = selectionMode ? selectedPageNumbers.length : displayDocument.pages.length; setStatus(`Rotated ${pageCount} pages ${direction}`); - }, [mergedPdfDocument, selectedPages, selectionMode, executeCommand, setPdfDocument]); + }, [displayDocument, selectedPageNumbers, selectionMode, executeCommand, setPdfDocument]); const handleDelete = useCallback(() => { - if (!mergedPdfDocument) return; + if (!displayDocument) return; const pagesToDelete = selectionMode - ? selectedPages - : mergedPdfDocument.pages.map(p => p.id); + ? selectedPageNumbers.map(pageNum => { + const page = displayDocument.pages.find(p => p.pageNumber === pageNum); + return page?.id || ''; + }).filter(id => id) + : displayDocument.pages.map(p => p.id); - if (selectionMode && selectedPages.length === 0) return; + if (selectionMode && selectedPageNumbers.length === 0) return; const command = new DeletePagesCommand( - mergedPdfDocument, + displayDocument, setPdfDocument, pagesToDelete ); @@ -610,45 +832,62 @@ const PageEditor = ({ if (selectionMode) { setSelectedPages([]); } - const pageCount = selectionMode ? selectedPages.length : mergedPdfDocument.pages.length; + const pageCount = selectionMode ? selectedPageNumbers.length : displayDocument.pages.length; setStatus(`Deleted ${pageCount} pages`); - }, [mergedPdfDocument, selectedPages, selectionMode, executeCommand, setPdfDocument]); + }, [displayDocument, selectedPageNumbers, selectionMode, executeCommand, setPdfDocument, setSelectedPages]); const handleSplit = useCallback(() => { - if (!mergedPdfDocument) return; + if (!displayDocument) return; const pagesToSplit = selectionMode - ? selectedPages - : mergedPdfDocument.pages.map(p => p.id); + ? selectedPageNumbers.map(pageNum => { + const page = displayDocument.pages.find(p => p.pageNumber === pageNum); + return page?.id || ''; + }).filter(id => id) + : displayDocument.pages.map(p => p.id); - if (selectionMode && selectedPages.length === 0) return; + if (selectionMode && selectedPageNumbers.length === 0) return; const command = new ToggleSplitCommand( - mergedPdfDocument, + displayDocument, setPdfDocument, pagesToSplit ); executeCommand(command); - const pageCount = selectionMode ? selectedPages.length : mergedPdfDocument.pages.length; + const pageCount = selectionMode ? selectedPageNumbers.length : displayDocument.pages.length; setStatus(`Split markers toggled for ${pageCount} pages`); - }, [mergedPdfDocument, selectedPages, selectionMode, executeCommand, setPdfDocument]); + }, [displayDocument, selectedPageNumbers, selectionMode, executeCommand, setPdfDocument]); const showExportPreview = useCallback((selectedOnly: boolean = false) => { if (!mergedPdfDocument) return; - const exportPageIds = selectedOnly ? selectedPages : []; + // Convert page numbers to page IDs for export service + const exportPageIds = selectedOnly + ? selectedPageNumbers.map(pageNum => { + const page = mergedPdfDocument.pages.find(p => p.pageNumber === pageNum); + return page?.id || ''; + }).filter(id => id) + : []; + const preview = pdfExportService.getExportInfo(mergedPdfDocument, exportPageIds, selectedOnly); setExportPreview(preview); setShowExportModal(true); - }, [mergedPdfDocument, selectedPages]); + }, [mergedPdfDocument, selectedPageNumbers]); const handleExport = useCallback(async (selectedOnly: boolean = false) => { if (!mergedPdfDocument) return; setExportLoading(true); try { - const exportPageIds = selectedOnly ? selectedPages : []; + // Convert page numbers to page IDs for export service + const exportPageIds = selectedOnly + ? selectedPageNumbers.map(pageNum => { + const page = mergedPdfDocument.pages.find(p => p.pageNumber === pageNum); + return page?.id || ''; + }).filter(id => id) + : []; + const errors = pdfExportService.validateExport(mergedPdfDocument, exportPageIds, selectedOnly); if (errors.length > 0) { setError(errors.join(', ')); @@ -686,7 +925,7 @@ const PageEditor = ({ } finally { setExportLoading(false); } - }, [mergedPdfDocument, selectedPages, filename]); + }, [mergedPdfDocument, selectedPageNumbers, filename]); const handleUndo = useCallback(() => { if (undo()) { @@ -701,11 +940,12 @@ const PageEditor = ({ }, [redo]); const closePdf = useCallback(() => { - setActiveFiles([]); - setProcessedFiles(new Map()); - setMergedPdfDocument(null); - setSelectedPages([]); - }, [setActiveFiles]); + // Use global navigation guard system + fileContext.requestNavigation(() => { + clearAllFiles(); // This now handles all cleanup centrally (including merged docs) + setSelectedPages([]); + }); + }, [fileContext, clearAllFiles, setSelectedPages]); // PageEditorControls needs onExportSelected and onExportAll const onExportSelected = useCallback(() => showExportPreview(true), [showExportPreview]); @@ -727,7 +967,7 @@ const PageEditor = ({ onExportAll, exportLoading, selectionMode, - selectedPages, + selectedPages: selectedPageNumbers, closePdf, }); } @@ -745,35 +985,203 @@ const PageEditor = ({ onExportAll, exportLoading, selectionMode, - selectedPages, + selectedPageNumbers, closePdf ]); - if (!mergedPdfDocument) { - return ( - - + // Show loading or empty state instead of blocking + const showLoading = !mergedPdfDocument && (globalProcessing || activeFiles.length > 0); + const showEmpty = !mergedPdfDocument && !globalProcessing && activeFiles.length === 0; + // Functions for global NavigationWarningModal + const handleApplyAndContinue = useCallback(async () => { + if (editedDocument) { + await applyChanges(); + } + }, [editedDocument, applyChanges]); - - - - - ); - } + const handleExportAndContinue = useCallback(async () => { + if (editedDocument) { + await applyChanges(); + await handleExport(false); + } + }, [editedDocument, applyChanges, handleExport]); + + // Check for existing drafts + const checkForDrafts = useCallback(async () => { + if (!mergedPdfDocument) return; + + try { + const draftKey = `draft-${mergedPdfDocument.id || 'merged'}`; + const request = indexedDB.open('stirling-pdf-drafts', 1); + + request.onsuccess = () => { + const db = request.result; + if (!db.objectStoreNames.contains('drafts')) return; + + const transaction = db.transaction('drafts', 'readonly'); + const store = transaction.objectStore('drafts'); + const getRequest = store.get(draftKey); + + getRequest.onsuccess = () => { + const draft = getRequest.result; + if (draft && draft.timestamp) { + // Check if draft is recent (within last 24 hours) + const draftAge = Date.now() - draft.timestamp; + const twentyFourHours = 24 * 60 * 60 * 1000; + + if (draftAge < twentyFourHours) { + setFoundDraft(draft); + setShowResumeModal(true); + } + } + }; + }; + } catch (error) { + console.warn('Failed to check for drafts:', error); + } + }, [mergedPdfDocument]); + + // Resume work from draft + const resumeWork = useCallback(() => { + if (foundDraft && foundDraft.document) { + setEditedDocument(foundDraft.document); + setHasUnsavedChanges(true); + setFoundDraft(null); + setShowResumeModal(false); + setStatus('Resumed previous work'); + } + }, [foundDraft]); + + // Start fresh (ignore draft) + const startFresh = useCallback(() => { + if (foundDraft) { + // Clean up the draft + cleanupDraft(); + } + setFoundDraft(null); + setShowResumeModal(false); + }, [foundDraft, cleanupDraft]); + + // Cleanup on unmount + useEffect(() => { + return () => { + console.log('PageEditor unmounting - cleaning up resources'); + + // Clear auto-save timer + if (autoSaveTimer.current) { + clearTimeout(autoSaveTimer.current); + } + + // Clean up draft if component unmounts with unsaved changes + if (hasUnsavedChanges) { + cleanupDraft(); + } + }; + }, [hasUnsavedChanges, cleanupDraft]); + + // Check for drafts when document loads + useEffect(() => { + if (mergedPdfDocument && !editedDocument && !hasUnsavedChanges) { + // Small delay to let the component settle + setTimeout(checkForDrafts, 1000); + } + }, [mergedPdfDocument, editedDocument, hasUnsavedChanges, checkForDrafts]); + + // Global navigation intercept - listen for navigation events + useEffect(() => { + if (!hasUnsavedChanges) return; + + const handleBeforeUnload = (e: BeforeUnloadEvent) => { + e.preventDefault(); + e.returnValue = 'You have unsaved changes. Are you sure you want to leave?'; + return 'You have unsaved changes. Are you sure you want to leave?'; + }; + + // Intercept browser navigation + window.addEventListener('beforeunload', handleBeforeUnload); + + return () => { + window.removeEventListener('beforeunload', handleBeforeUnload); + }; + }, [hasUnsavedChanges]); + + // Display all pages - use edited or original document + const displayedPages = displayDocument?.pages || []; return ( - + + {showEmpty && ( +
+ + 📄 + No PDF files loaded + Add files to start editing pages + +
+ )} + {showLoading && ( + + + {/* Progress indicator */} + + + + Processing PDF files... + + + {Math.round(processingProgress || 0)}% + + +
+
+
+ + + + + )} + + {displayDocument && ( + + {/* Enhanced Processing Status */} + {globalProcessing && processingProgress < 100 && ( + + + Processing thumbnails... + {Math.round(processingProgress || 0)}% + +
+
+
+ + )} + Deselect All )} + + {/* Apply Changes Button */} + {hasUnsavedChanges && ( + + )} {selectionMode && ( )} + )} @@ -874,10 +1296,11 @@ const PageEditor = ({ )} /> - + )} - setShowExportModal(false)} title="Export Preview" @@ -930,36 +1353,62 @@ const PageEditor = ({ )} - file && handleFileUpload(file)} - style={{ display: 'none' }} + {/* Global Navigation Warning Modal */} + - {status && ( - setStatus(null)} - style={{ position: 'fixed', bottom: 20, right: 20, zIndex: 1000 }} - > - {status} - - )} + {/* Resume Work Modal */} + + + + We found unsaved changes from a previous session. Would you like to resume where you left off? + + + {foundDraft && ( + + Last saved: {new Date(foundDraft.timestamp).toLocaleString()} + + )} + + + + + + + + - {error && ( - setError(null)} - style={{ position: 'fixed', bottom: 70, right: 20, zIndex: 1000 }} - > - {error} - - )} - - + {status && ( + setStatus(null)} + style={{ position: 'fixed', bottom: 20, right: 20, zIndex: 1000 }} + > + {status} + + )} + ); }; diff --git a/frontend/src/components/pageEditor/PageEditorControls.tsx b/frontend/src/components/pageEditor/PageEditorControls.tsx index 175a83eec..9ab23ae2d 100644 --- a/frontend/src/components/pageEditor/PageEditorControls.tsx +++ b/frontend/src/components/pageEditor/PageEditorControls.tsx @@ -56,7 +56,7 @@ const PageEditorControls = ({ return (
>; - onDragStart: (pageId: string) => void; + onDragStart: (pageNumber: number) => void; onDragEnd: () => void; onDragOver: (e: React.DragEvent) => void; - onDragEnter: (pageId: string) => void; + onDragEnter: (pageNumber: number) => void; onDragLeave: () => void; - onDrop: (e: React.DragEvent, pageId: string) => void; - onTogglePage: (pageId: string) => void; - onAnimateReorder: (pageId: string, targetIndex: number) => void; - onExecuteCommand: (command: any) => void; + onDrop: (e: React.DragEvent, pageNumber: number) => void; + onTogglePage: (pageNumber: number) => void; + onAnimateReorder: (pageNumber: number, targetIndex: number) => void; + onExecuteCommand: (command: Command) => void; onSetStatus: (status: string) => void; - onSetMovingPage: (pageId: string | null) => void; - RotatePagesCommand: any; - DeletePagesCommand: any; - ToggleSplitCommand: any; - pdfDocument: any; - setPdfDocument: any; + onSetMovingPage: (pageNumber: number | null) => void; + RotatePagesCommand: typeof RotatePagesCommand; + DeletePagesCommand: typeof DeletePagesCommand; + ToggleSplitCommand: typeof ToggleSplitCommand; + pdfDocument: PDFDocument; + setPdfDocument: (doc: PDFDocument) => void; } -const PageThumbnail = ({ +const PageThumbnail = React.memo(({ page, index, totalPages, + originalFile, selectedPages, selectionMode, draggedPage, @@ -67,6 +80,44 @@ const PageThumbnail = ({ pdfDocument, setPdfDocument, }: PageThumbnailProps) => { + const [thumbnailUrl, setThumbnailUrl] = useState(page.thumbnail); + const [isLoadingThumbnail, setIsLoadingThumbnail] = useState(false); + + // Update thumbnail URL when page prop changes + useEffect(() => { + if (page.thumbnail && page.thumbnail !== thumbnailUrl) { + console.log(`📸 PageThumbnail: Updating thumbnail URL for page ${page.pageNumber}`, page.thumbnail.substring(0, 50) + '...'); + setThumbnailUrl(page.thumbnail); + } + }, [page.thumbnail, page.pageNumber, page.id, thumbnailUrl]); + + // Listen for ready thumbnails from Web Workers (only if no existing thumbnail) + useEffect(() => { + if (thumbnailUrl) { + console.log(`📸 PageThumbnail: Page ${page.pageNumber} already has thumbnail, skipping worker listener`); + return; // Skip if we already have a thumbnail + } + + console.log(`📸 PageThumbnail: Setting up worker listener for page ${page.pageNumber} (${page.id})`); + + const handleThumbnailReady = (event: CustomEvent) => { + const { pageNumber, thumbnail, pageId } = event.detail; + console.log(`📸 PageThumbnail: Received worker thumbnail for page ${pageNumber}, looking for page ${page.pageNumber} (${page.id})`); + + if (pageNumber === page.pageNumber && pageId === page.id) { + console.log(`✓ PageThumbnail: Thumbnail matched for page ${page.pageNumber}, setting URL`); + setThumbnailUrl(thumbnail); + } + }; + + window.addEventListener('thumbnailReady', handleThumbnailReady as EventListener); + return () => { + console.log(`📸 PageThumbnail: Cleaning up worker listener for page ${page.pageNumber}`); + window.removeEventListener('thumbnailReady', handleThumbnailReady as EventListener); + }; + }, [page.pageNumber, page.id, thumbnailUrl]); + + // Register this component with pageRefs for animations const pageElementRef = useCallback((element: HTMLDivElement | null) => { if (element) { @@ -79,7 +130,7 @@ const PageThumbnail = ({ return (
{ - if (!isAnimating && draggedPage && page.id !== draggedPage && dropTarget === page.id) { + if (!isAnimating && draggedPage && page.pageNumber !== draggedPage && dropTarget === page.pageNumber) { return 'translateX(20px)'; } return 'translateX(0)'; @@ -109,12 +160,12 @@ const PageThumbnail = ({ transition: isAnimating ? 'none' : 'transform 0.2s ease-in-out' }} draggable - onDragStart={() => onDragStart(page.id)} + onDragStart={() => onDragStart(page.pageNumber)} onDragEnd={onDragEnd} onDragOver={onDragOver} - onDragEnter={() => onDragEnter(page.id)} + onDragEnter={() => onDragEnter(page.pageNumber)} onDragLeave={onDragLeave} - onDrop={(e) => onDrop(e, page.id)} + onDrop={(e) => onDrop(e, page.pageNumber)} > {selectionMode && (
e.stopPropagation()} onDragStart={(e) => { e.preventDefault(); e.stopPropagation(); }} + onClick={(e) => { + console.log('📸 Checkbox clicked for page', page.pageNumber); + e.stopPropagation(); + onTogglePage(page.pageNumber); + }} > { - event.stopPropagation(); - onTogglePage(page.id); + checked={Array.isArray(selectedPages) ? selectedPages.includes(page.pageNumber) : false} + onChange={() => { + // onChange is handled by the parent div click }} - onClick={(e) => e.stopPropagation()} size="sm" />
@@ -162,18 +218,30 @@ const PageThumbnail = ({ justifyContent: 'center' }} > - {`Page + {thumbnailUrl ? ( + {`Page + ) : isLoadingThumbnail ? ( +
+ + Loading... +
+ ) : ( +
+ 📄 + Page {page.pageNumber} +
+ )}
{ e.stopPropagation(); if (index > 0 && !movingPage && !isAnimating) { - onSetMovingPage(page.id); - onAnimateReorder(page.id, index - 1); + onSetMovingPage(page.pageNumber); + onAnimateReorder(page.pageNumber, index - 1); setTimeout(() => onSetMovingPage(null), 500); onSetStatus(`Moved page ${page.pageNumber} left`); } @@ -244,8 +312,8 @@ const PageThumbnail = ({ onClick={(e) => { e.stopPropagation(); if (index < totalPages - 1 && !movingPage && !isAnimating) { - onSetMovingPage(page.id); - onAnimateReorder(page.id, index + 1); + onSetMovingPage(page.pageNumber); + onAnimateReorder(page.pageNumber, index + 1); setTimeout(() => onSetMovingPage(null), 500); onSetStatus(`Moved page ${page.pageNumber} right`); } @@ -353,6 +421,20 @@ const PageThumbnail = ({
); -}; +}, (prevProps, nextProps) => { + // Only re-render if essential props change + return ( + prevProps.page.id === nextProps.page.id && + prevProps.page.pageNumber === nextProps.page.pageNumber && + prevProps.page.rotation === nextProps.page.rotation && + prevProps.page.thumbnail === nextProps.page.thumbnail && + prevProps.selectedPages === nextProps.selectedPages && // Compare array reference - will re-render when selection changes + prevProps.selectionMode === nextProps.selectionMode && + prevProps.draggedPage === nextProps.draggedPage && + prevProps.dropTarget === nextProps.dropTarget && + prevProps.movingPage === nextProps.movingPage && + prevProps.isAnimating === nextProps.isAnimating + ); +}); export default PageThumbnail; diff --git a/frontend/src/components/shared/FileGrid.tsx b/frontend/src/components/shared/FileGrid.tsx new file mode 100644 index 000000000..92b47ee30 --- /dev/null +++ b/frontend/src/components/shared/FileGrid.tsx @@ -0,0 +1,168 @@ +import React, { useState } from "react"; +import { Box, Flex, Group, Text, Button, TextInput, Select, Badge } from "@mantine/core"; +import { useTranslation } from "react-i18next"; +import SearchIcon from "@mui/icons-material/Search"; +import SortIcon from "@mui/icons-material/Sort"; +import FileCard from "../fileManagement/FileCard"; +import { FileWithUrl } from "../../types/file"; + +interface FileGridProps { + files: FileWithUrl[]; + onRemove?: (index: number) => void; + onDoubleClick?: (file: FileWithUrl) => void; + onView?: (file: FileWithUrl) => void; + onEdit?: (file: FileWithUrl) => void; + onSelect?: (fileId: string) => void; + selectedFiles?: string[]; + showSearch?: boolean; + showSort?: boolean; + maxDisplay?: number; // If set, shows only this many files with "Show All" option + onShowAll?: () => void; + showingAll?: boolean; + onDeleteAll?: () => void; +} + +type SortOption = 'date' | 'name' | 'size'; + +const FileGrid = ({ + files, + onRemove, + onDoubleClick, + onView, + onEdit, + onSelect, + selectedFiles = [], + showSearch = false, + showSort = false, + maxDisplay, + onShowAll, + showingAll = false, + onDeleteAll +}: FileGridProps) => { + const { t } = useTranslation(); + const [searchTerm, setSearchTerm] = useState(""); + const [sortBy, setSortBy] = useState('date'); + + // Filter files based on search term + const filteredFiles = files.filter(file => + file.name.toLowerCase().includes(searchTerm.toLowerCase()) + ); + + // Sort files + const sortedFiles = [...filteredFiles].sort((a, b) => { + switch (sortBy) { + case 'date': + return (b.lastModified || 0) - (a.lastModified || 0); + case 'name': + return a.name.localeCompare(b.name); + case 'size': + return (b.size || 0) - (a.size || 0); + default: + return 0; + } + }); + + // Apply max display limit if specified + const displayFiles = maxDisplay && !showingAll + ? sortedFiles.slice(0, maxDisplay) + : sortedFiles; + + const hasMoreFiles = maxDisplay && !showingAll && sortedFiles.length > maxDisplay; + + return ( + + {/* Search and Sort Controls */} + {(showSearch || showSort || onDeleteAll) && ( + + + {showSearch && ( + } + value={searchTerm} + onChange={(e) => setSearchTerm(e.currentTarget.value)} + style={{ flexGrow: 1, maxWidth: 300, minWidth: 200 }} + /> + )} + + {showSort && ( + )} - - {/* File Picker Modal */} - setShowFilePickerModal(false)} - storedFiles={sharedFiles} - onSelectFiles={handleStorageSelection} - /> + {/* Recent Files Section */} + {showRecentFiles && recentFiles.length > 0 && ( + + + + {t("fileUpload.recentFiles", "Recent Files")} + + { + await Promise.all(recentFiles.map(async (file) => { + await fileStorage.deleteFile(file.id || file.name); + })); + setRecentFiles([]); + setSelectedFiles([]); + }} + /> + + { + await Promise.all(recentFiles.map(async (file) => { + await fileStorage.deleteFile(file.id || file.name); + })); + setRecentFiles([]); + setSelectedFiles([]); + }} + /> + + )} + ); }; diff --git a/frontend/src/components/shared/MultiSelectControls.tsx b/frontend/src/components/shared/MultiSelectControls.tsx new file mode 100644 index 000000000..11790abf8 --- /dev/null +++ b/frontend/src/components/shared/MultiSelectControls.tsx @@ -0,0 +1,88 @@ +import React from "react"; +import { Box, Group, Text, Button } from "@mantine/core"; +import { useTranslation } from "react-i18next"; + +interface MultiSelectControlsProps { + selectedCount: number; + onClearSelection: () => void; + onOpenInFileEditor?: () => void; + onOpenInPageEditor?: () => void; + onAddToUpload?: () => void; + onDeleteAll?: () => void; +} + +const MultiSelectControls = ({ + selectedCount, + onClearSelection, + onOpenInFileEditor, + onOpenInPageEditor, + onAddToUpload, + onDeleteAll +}: MultiSelectControlsProps) => { + const { t } = useTranslation(); + + if (selectedCount === 0) return null; + + return ( + + + + {selectedCount} {t("fileManager.filesSelected", "files selected")} + + + + + {onAddToUpload && ( + + )} + + {onOpenInFileEditor && ( + + )} + + {onOpenInPageEditor && ( + + )} + + {onDeleteAll && ( + + )} + + + + ); +}; + +export default MultiSelectControls; \ No newline at end of file diff --git a/frontend/src/components/shared/NavigationWarningModal.tsx b/frontend/src/components/shared/NavigationWarningModal.tsx new file mode 100644 index 000000000..a3d3983d2 --- /dev/null +++ b/frontend/src/components/shared/NavigationWarningModal.tsx @@ -0,0 +1,106 @@ +import React from 'react'; +import { Modal, Text, Button, Group, Stack } from '@mantine/core'; +import { useFileContext } from '../../contexts/FileContext'; + +interface NavigationWarningModalProps { + onApplyAndContinue?: () => Promise; + onExportAndContinue?: () => Promise; +} + +const NavigationWarningModal = ({ + onApplyAndContinue, + onExportAndContinue +}: NavigationWarningModalProps) => { + const { + showNavigationWarning, + hasUnsavedChanges, + confirmNavigation, + cancelNavigation, + setHasUnsavedChanges + } = useFileContext(); + + const handleKeepWorking = () => { + cancelNavigation(); + }; + + const handleDiscardChanges = () => { + setHasUnsavedChanges(false); + confirmNavigation(); + }; + + const handleApplyAndContinue = async () => { + if (onApplyAndContinue) { + await onApplyAndContinue(); + } + setHasUnsavedChanges(false); + confirmNavigation(); + }; + + const handleExportAndContinue = async () => { + if (onExportAndContinue) { + await onExportAndContinue(); + } + setHasUnsavedChanges(false); + confirmNavigation(); + }; + + if (!hasUnsavedChanges) { + return null; + } + + return ( + + + + You have unsaved changes to your PDF. What would you like to do? + + + + + + + + {onApplyAndContinue && ( + + )} + + {onExportAndContinue && ( + + )} + + + + ); +}; + +export default NavigationWarningModal; \ No newline at end of file diff --git a/frontend/src/components/shared/SkeletonLoader.tsx b/frontend/src/components/shared/SkeletonLoader.tsx new file mode 100644 index 000000000..63c4bd22a --- /dev/null +++ b/frontend/src/components/shared/SkeletonLoader.tsx @@ -0,0 +1,104 @@ +import React from 'react'; +import { Box, Group, Stack } from '@mantine/core'; + +interface SkeletonLoaderProps { + type: 'pageGrid' | 'fileGrid' | 'controls' | 'viewer'; + count?: number; + animated?: boolean; +} + +const SkeletonLoader: React.FC = ({ + type, + count = 8, + animated = true +}) => { + const animationStyle = animated ? { animation: 'pulse 2s infinite' } : {}; + + const renderPageGridSkeleton = () => ( +
+ {Array.from({ length: count }).map((_, i) => ( + + ))} +
+ ); + + const renderFileGridSkeleton = () => ( +
+ {Array.from({ length: count }).map((_, i) => ( + + ))} +
+ ); + + const renderControlsSkeleton = () => ( + + + + + + ); + + const renderViewerSkeleton = () => ( + + {/* Toolbar skeleton */} + + + + + + + {/* Main content skeleton */} + + + ); + + switch (type) { + case 'pageGrid': + return renderPageGridSkeleton(); + case 'fileGrid': + return renderFileGridSkeleton(); + case 'controls': + return renderControlsSkeleton(); + case 'viewer': + return renderViewerSkeleton(); + default: + return null; + } +}; + +export default SkeletonLoader; \ No newline at end of file diff --git a/frontend/src/components/shared/TopControls.tsx b/frontend/src/components/shared/TopControls.tsx index 13c82103f..5b772c90a 100644 --- a/frontend/src/components/shared/TopControls.tsx +++ b/frontend/src/components/shared/TopControls.tsx @@ -1,5 +1,5 @@ -import React from "react"; -import { Button, SegmentedControl } from "@mantine/core"; +import React, { useState, useCallback } from "react"; +import { Button, SegmentedControl, Loader } from "@mantine/core"; import { useRainbowThemeContext } from "./RainbowThemeProvider"; import LanguageSelector from "./LanguageSelector"; import rainbowStyles from '../../styles/rainbow.module.css'; @@ -8,15 +8,19 @@ import LightModeIcon from '@mui/icons-material/LightMode'; import AutoAwesomeIcon from '@mui/icons-material/AutoAwesome'; import VisibilityIcon from "@mui/icons-material/Visibility"; import EditNoteIcon from "@mui/icons-material/EditNote"; -import InsertDriveFileIcon from "@mui/icons-material/InsertDriveFile"; import FolderIcon from "@mui/icons-material/Folder"; import { Group } from "@mantine/core"; -const VIEW_OPTIONS = [ +// This will be created inside the component to access switchingTo +const createViewOptions = (switchingTo: string | null) => [ { label: ( - + {switchingTo === "viewer" ? ( + + ) : ( + + )} ), value: "viewer", @@ -24,7 +28,11 @@ const VIEW_OPTIONS = [ { label: ( - + {switchingTo === "pageEditor" ? ( + + ) : ( + + )} ), value: "pageEditor", @@ -32,15 +40,11 @@ const VIEW_OPTIONS = [ { label: ( - - - ), - value: "fileManager", - }, - { - label: ( - - + {switchingTo === "fileEditor" ? ( + + ) : ( + + )} ), value: "fileEditor", @@ -50,13 +54,34 @@ const VIEW_OPTIONS = [ interface TopControlsProps { currentView: string; setCurrentView: (view: string) => void; + selectedToolKey?: string | null; } const TopControls = ({ currentView, setCurrentView, + selectedToolKey, }: TopControlsProps) => { const { themeMode, isRainbowMode, isToggleDisabled, toggleTheme } = useRainbowThemeContext(); + const [switchingTo, setSwitchingTo] = useState(null); + + const isToolSelected = selectedToolKey !== null; + + const handleViewChange = useCallback((view: string) => { + // Show immediate feedback + setSwitchingTo(view); + + // Defer the heavy view change to next frame so spinner can render + requestAnimationFrame(() => { + // Give the spinner one more frame to show + requestAnimationFrame(() => { + setCurrentView(view); + + // Clear the loading state after view change completes + setTimeout(() => setSwitchingTo(null), 300); + }); + }); + }, [setCurrentView]); const getThemeIcon = () => { if (isRainbowMode) return ; @@ -66,7 +91,9 @@ const TopControls = ({ return (
-
+
-
- -
+ {!isToolSelected && ( +
+ +
+ )}
); }; diff --git a/frontend/src/components/tools/ToolPicker.tsx b/frontend/src/components/tools/ToolPicker.tsx index acf86dd35..cfb2bd3d4 100644 --- a/frontend/src/components/tools/ToolPicker.tsx +++ b/frontend/src/components/tools/ToolPicker.tsx @@ -12,7 +12,7 @@ type ToolRegistry = { }; interface ToolPickerProps { - selectedToolKey: string; + selectedToolKey: string | null; onSelect: (id: string) => void; toolRegistry: ToolRegistry; } diff --git a/frontend/src/components/tools/ToolRenderer.tsx b/frontend/src/components/tools/ToolRenderer.tsx index 5ce167864..4a9b9901d 100644 --- a/frontend/src/components/tools/ToolRenderer.tsx +++ b/frontend/src/components/tools/ToolRenderer.tsx @@ -1,29 +1,30 @@ -import React from "react"; import { FileWithUrl } from "../../types/file"; +import { useToolManagement } from "../../hooks/useToolManagement"; interface ToolRendererProps { selectedToolKey: string; - selectedTool: any; pdfFile: any; files: FileWithUrl[]; - downloadUrl: string | null; - setDownloadUrl: (url: string | null) => void; toolParams: any; updateParams: (params: any) => void; + toolSelectedFiles?: File[]; + onPreviewFile?: (file: File | null) => void; } const ToolRenderer = ({ selectedToolKey, - selectedTool, - pdfFile, - files, - downloadUrl, - setDownloadUrl, +files, toolParams, updateParams, + toolSelectedFiles = [], + onPreviewFile, }: ToolRendererProps) => { + // Get the tool from registry + const { toolRegistry } = useToolManagement(); + const selectedTool = toolRegistry[selectedToolKey]; + if (!selectedTool || !selectedTool.component) { - return
Tool not found
; + return
Tool not found: {selectedToolKey}
; } const ToolComponent = selectedTool.component; @@ -33,19 +34,15 @@ const ToolRenderer = ({ case "split": return ( ); case "compress": return ( {}} // TODO: Add loading state + setLoading={(loading: boolean) => {}} params={toolParams} updateParams={updateParams} /> @@ -54,7 +51,6 @@ const ToolRenderer = ({ return ( @@ -63,7 +59,6 @@ const ToolRenderer = ({ return ( @@ -71,4 +66,4 @@ const ToolRenderer = ({ } }; -export default ToolRenderer; \ No newline at end of file +export default ToolRenderer; diff --git a/frontend/src/components/tools/shared/ErrorNotification.tsx b/frontend/src/components/tools/shared/ErrorNotification.tsx new file mode 100644 index 000000000..a1740a1f6 --- /dev/null +++ b/frontend/src/components/tools/shared/ErrorNotification.tsx @@ -0,0 +1,35 @@ +import { Notification } from '@mantine/core'; +import { useTranslation } from 'react-i18next'; + +export interface ErrorNotificationProps { + error: string | null; + onClose: () => void; + title?: string; + color?: string; + mb?: string; +} + +const ErrorNotification = ({ + error, + onClose, + title, + color = 'red', + mb = 'md' +}: ErrorNotificationProps) => { + const { t } = useTranslation(); + + if (!error) return null; + + return ( + + {error} + + ); +} + +export default ErrorNotification; diff --git a/frontend/src/components/tools/shared/FileStatusIndicator.tsx b/frontend/src/components/tools/shared/FileStatusIndicator.tsx new file mode 100644 index 000000000..5ff76c13c --- /dev/null +++ b/frontend/src/components/tools/shared/FileStatusIndicator.tsx @@ -0,0 +1,40 @@ +import React from 'react'; +import { Text } from '@mantine/core'; + +export interface FileStatusIndicatorProps { + selectedFiles?: File[]; + isCompleted?: boolean; + placeholder?: string; + showFileName?: boolean; +} + +const FileStatusIndicator = ({ + selectedFiles = [], + isCompleted = false, + placeholder = "Select a PDF file in the main view to get started", + showFileName = true +}: FileStatusIndicatorProps) => { + if (selectedFiles.length === 0) { + return ( + + {placeholder} + + ); + } + + if (isCompleted) { + return ( + + ✓ Selected: {showFileName ? selectedFiles[0]?.name : `${selectedFiles.length} file${selectedFiles.length > 1 ? 's' : ''}`} + + ); + } + + return ( + + Selected: {showFileName ? selectedFiles[0]?.name : `${selectedFiles.length} file${selectedFiles.length > 1 ? 's' : ''}`} + + ); +} + +export default FileStatusIndicator; \ No newline at end of file diff --git a/frontend/src/components/tools/shared/OperationButton.tsx b/frontend/src/components/tools/shared/OperationButton.tsx new file mode 100644 index 000000000..c356b2cc2 --- /dev/null +++ b/frontend/src/components/tools/shared/OperationButton.tsx @@ -0,0 +1,51 @@ +import React from 'react'; +import { Button } from '@mantine/core'; +import { useTranslation } from 'react-i18next'; + +export interface OperationButtonProps { + onClick?: () => void; + isLoading?: boolean; + disabled?: boolean; + loadingText?: string; + submitText?: string; + variant?: 'filled' | 'outline' | 'subtle'; + color?: string; + fullWidth?: boolean; + mt?: string; + type?: 'button' | 'submit' | 'reset'; +} + +const OperationButton = ({ + onClick, + isLoading = false, + disabled = false, + loadingText, + submitText, + variant = 'filled', + color = 'blue', + fullWidth = true, + mt = 'md', + type = 'button' +}: OperationButtonProps) => { + const { t } = useTranslation(); + + return ( + + ); +} + +export default OperationButton; \ No newline at end of file diff --git a/frontend/src/components/tools/shared/ResultsPreview.tsx b/frontend/src/components/tools/shared/ResultsPreview.tsx new file mode 100644 index 000000000..c13b0bac3 --- /dev/null +++ b/frontend/src/components/tools/shared/ResultsPreview.tsx @@ -0,0 +1,112 @@ +import { Grid, Paper, Box, Image, Text, Loader, Stack, Center } from '@mantine/core'; + +export interface ResultFile { + file: File; + thumbnail?: string; +} + +export interface ResultsPreviewProps { + files: ResultFile[]; + isGeneratingThumbnails?: boolean; + onFileClick?: (file: File) => void; + title?: string; + emptyMessage?: string; + loadingMessage?: string; +} + +const ResultsPreview = ({ + files, + isGeneratingThumbnails = false, + onFileClick, + title, + emptyMessage = "No files to preview", + loadingMessage = "Generating previews..." +}: ResultsPreviewProps) => { + const formatSize = (size: number) => { + if (size > 1024 * 1024) return `${(size / (1024 * 1024)).toFixed(1)} MB`; + if (size > 1024) return `${(size / 1024).toFixed(1)} KB`; + return `${size} B`; + }; + + if (files.length === 0 && !isGeneratingThumbnails) { + return ( + + {emptyMessage} + + ); + } + + return ( + + {title && ( + + {title} ({files.length} files) + + )} + + {isGeneratingThumbnails ? ( +
+ + + {loadingMessage} + +
+ ) : ( + + {files.map((result, index) => ( + + onFileClick?.(result.file)} + style={{ + textAlign: 'center', + height: '10rem', + width:'5rem', + display: 'flex', + flexDirection: 'column', + cursor: onFileClick ? 'pointer' : 'default', + transition: 'all 0.2s ease' + }} + > + + {result.thumbnail ? ( + {`Preview + ) : ( + No preview + )} + + + {result.file.name} + + + {formatSize(result.file.size)} + + + + ))} + + )} +
+ ); +} + +export default ResultsPreview; diff --git a/frontend/src/components/tools/shared/ToolStep.tsx b/frontend/src/components/tools/shared/ToolStep.tsx new file mode 100644 index 000000000..4ac22aa10 --- /dev/null +++ b/frontend/src/components/tools/shared/ToolStep.tsx @@ -0,0 +1,120 @@ +import React, { createContext, useContext, useMemo, useRef } from 'react'; +import { Paper, Text, Stack, Box } from '@mantine/core'; + +interface ToolStepContextType { + visibleStepCount: number; + getStepNumber: () => number; +} + +const ToolStepContext = createContext(null); + +export interface ToolStepProps { + title: string; + isVisible?: boolean; + isCollapsed?: boolean; + isCompleted?: boolean; + onCollapsedClick?: () => void; + children?: React.ReactNode; + completedMessage?: string; + helpText?: string; + showNumber?: boolean; +} + +const ToolStep = ({ + title, + isVisible = true, + isCollapsed = false, + isCompleted = false, + onCollapsedClick, + children, + completedMessage, + helpText, + showNumber +}: ToolStepProps) => { + if (!isVisible) return null; + + // Auto-detect if we should show numbers based on sibling count + const shouldShowNumber = useMemo(() => { + if (showNumber !== undefined) return showNumber; + const parent = useContext(ToolStepContext); + return parent ? parent.visibleStepCount >= 3 : false; + }, [showNumber]); + + const stepNumber = useContext(ToolStepContext)?.getStepNumber?.() || 1; + + return ( + + + {shouldShowNumber ? `${stepNumber}. ` : ''}{title} + + + {isCollapsed ? ( + + {isCompleted && completedMessage && ( + + ✓ {completedMessage} + {onCollapsedClick && ( + + (click to change) + + )} + + )} + + ) : ( + + {helpText && ( + + {helpText} + + )} + {children} + + )} + + ); +} + +export interface ToolStepContainerProps { + children: React.ReactNode; +} + +export const ToolStepContainer = ({ children }: ToolStepContainerProps) => { + const stepCounterRef = useRef(0); + + // Count visible ToolStep children + const visibleStepCount = useMemo(() => { + let count = 0; + React.Children.forEach(children, (child) => { + if (React.isValidElement(child) && child.type === ToolStep) { + const isVisible = child.props.isVisible !== false; + if (isVisible) count++; + } + }); + return count; + }, [children]); + + const contextValue = useMemo(() => ({ + visibleStepCount, + getStepNumber: () => ++stepCounterRef.current + }), [visibleStepCount]); + + stepCounterRef.current = 0; + + return ( + + {children} + + ); +} + +export default ToolStep; diff --git a/frontend/src/components/tools/split/SplitSettings.tsx b/frontend/src/components/tools/split/SplitSettings.tsx new file mode 100644 index 000000000..50ca49f20 --- /dev/null +++ b/frontend/src/components/tools/split/SplitSettings.tsx @@ -0,0 +1,148 @@ +import { Stack, TextInput, Select, Checkbox } from '@mantine/core'; +import { useTranslation } from 'react-i18next'; +import { SPLIT_MODES, SPLIT_TYPES, type SplitMode, type SplitType } from '../../../constants/splitConstants'; + +export interface SplitParameters { + pages: string; + hDiv: string; + vDiv: string; + merge: boolean; + splitType: SplitType | ''; + splitValue: string; + bookmarkLevel: string; + includeMetadata: boolean; + allowDuplicates: boolean; +} + +export interface SplitSettingsProps { + mode: SplitMode | ''; + onModeChange: (mode: SplitMode | '') => void; + parameters: SplitParameters; + onParameterChange: (parameter: keyof SplitParameters, value: string | boolean) => void; + disabled?: boolean; +} + +const SplitSettings = ({ + mode, + onModeChange, + parameters, + onParameterChange, + disabled = false +}: SplitSettingsProps) => { + const { t } = useTranslation(); + + const renderByPagesForm = () => ( + onParameterChange('pages', e.target.value)} + disabled={disabled} + /> + ); + + const renderBySectionsForm = () => ( + + onParameterChange('hDiv', e.target.value)} + placeholder={t("split-by-sections.horizontal.placeholder", "Enter number of horizontal divisions")} + disabled={disabled} + /> + onParameterChange('vDiv', e.target.value)} + placeholder={t("split-by-sections.vertical.placeholder", "Enter number of vertical divisions")} + disabled={disabled} + /> + onParameterChange('merge', e.currentTarget.checked)} + disabled={disabled} + /> + + ); + + const renderBySizeOrCountForm = () => ( + + v && onModeChange(v)} + disabled={disabled} + data={[ + { value: SPLIT_MODES.BY_PAGES, label: t("split.header", "Split by Pages") + " (e.g. 1,3,5-10)" }, + { value: SPLIT_MODES.BY_SECTIONS, label: t("split-by-sections.title", "Split by Grid Sections") }, + { value: SPLIT_MODES.BY_SIZE_OR_COUNT, label: t("split-by-size-or-count.title", "Split by Size or Count") }, + { value: SPLIT_MODES.BY_CHAPTERS, label: t("splitByChapters.title", "Split by Chapters") }, + ]} + /> + + {/* Parameter Form */} + {mode === SPLIT_MODES.BY_PAGES && renderByPagesForm()} + {mode === SPLIT_MODES.BY_SECTIONS && renderBySectionsForm()} + {mode === SPLIT_MODES.BY_SIZE_OR_COUNT && renderBySizeOrCountForm()} + {mode === SPLIT_MODES.BY_CHAPTERS && renderByChaptersForm()} + + ); +} + +export default SplitSettings; diff --git a/frontend/src/components/viewer/Viewer.tsx b/frontend/src/components/viewer/Viewer.tsx index 8c2e89c78..065ce5824 100644 --- a/frontend/src/components/viewer/Viewer.tsx +++ b/frontend/src/components/viewer/Viewer.tsx @@ -1,5 +1,5 @@ -import React, { useEffect, useState, useRef } from "react"; -import { Paper, Stack, Text, ScrollArea, Loader, Center, Button, Group, NumberInput, useMantineTheme } from "@mantine/core"; +import React, { useEffect, useState, useRef, useCallback } from "react"; +import { Paper, Stack, Text, ScrollArea, Loader, Center, Button, Group, NumberInput, useMantineTheme, ActionIcon, Box, Tabs } from "@mantine/core"; import { getDocument, GlobalWorkerOptions } from "pdfjs-dist"; import { useTranslation } from "react-i18next"; import ArrowBackIosNewIcon from "@mui/icons-material/ArrowBackIosNew"; @@ -9,8 +9,12 @@ import LastPageIcon from "@mui/icons-material/LastPage"; import ViewSidebarIcon from "@mui/icons-material/ViewSidebar"; import ViewWeekIcon from "@mui/icons-material/ViewWeek"; // for dual page (book) import DescriptionIcon from "@mui/icons-material/Description"; // for single page +import CloseIcon from "@mui/icons-material/Close"; import { useLocalStorage } from "@mantine/hooks"; import { fileStorage } from "../../services/fileStorage"; +import SkeletonLoader from '../shared/SkeletonLoader'; +import { useFileContext } from "../../contexts/FileContext"; +import { useFileWithUrl } from "../../hooks/useFileWithUrl"; GlobalWorkerOptions.workerSrc = "/pdf.worker.js"; @@ -29,7 +33,7 @@ const LazyPageImage = ({ pageIndex, zoom, theme, isFirst, renderPage, pageImages, setPageRef }: LazyPageImageProps) => { const [isVisible, setIsVisible] = useState(false); - const [imageUrl, setImageUrl] = useState(pageImages[pageIndex]); + const [imageUrl, setImageUrl] = useState(null); const imgRef = useRef(null); useEffect(() => { @@ -54,6 +58,13 @@ const LazyPageImage = ({ return () => observer.disconnect(); }, [imageUrl]); + // Update local state when pageImages changes (from preloading) + useEffect(() => { + if (pageImages[pageIndex]) { + setImageUrl(pageImages[pageIndex]); + } + }, [pageImages, pageIndex]); + useEffect(() => { if (isVisible && !imageUrl) { renderPage(pageIndex).then((url) => { @@ -123,20 +134,40 @@ const LazyPageImage = ({ }; export interface ViewerProps { - pdfFile: { file: File; url: string } | null; // First file in the array - setPdfFile: (file: { file: File; url: string } | null) => void; sidebarsVisible: boolean; setSidebarsVisible: (v: boolean) => void; + onClose?: () => void; + previewFile?: File; // For preview mode - bypasses context } const Viewer = ({ - pdfFile, - setPdfFile, sidebarsVisible, setSidebarsVisible, + onClose, + previewFile, }: ViewerProps) => { const { t } = useTranslation(); const theme = useMantineTheme(); + + // Get current file from FileContext + const { getCurrentFile, getCurrentProcessedFile, clearAllFiles, addFiles, activeFiles } = useFileContext(); + const currentFile = getCurrentFile(); + const processedFile = getCurrentProcessedFile(); + + // Convert File to FileWithUrl format for viewer + const pdfFile = useFileWithUrl(currentFile); + + // Tab management for multiple files + const [activeTab, setActiveTab] = useState("0"); + + // Reset PDF state when switching tabs + const handleTabChange = (newTab: string) => { + setActiveTab(newTab); + setNumPages(0); + setPageImages([]); + setCurrentPage(null); + setLoading(true); + }; const [numPages, setNumPages] = useState(0); const [pageImages, setPageImages] = useState([]); const [loading, setLoading] = useState(false); @@ -144,16 +175,50 @@ const Viewer = ({ const [dualPage, setDualPage] = useState(false); const [zoom, setZoom] = useState(1); // 1 = 100% const pageRefs = useRef<(HTMLImageElement | null)[]>([]); + + + // Get files with URLs for tabs - we'll need to create these individually + const file0WithUrl = useFileWithUrl(activeFiles[0]); + const file1WithUrl = useFileWithUrl(activeFiles[1]); + const file2WithUrl = useFileWithUrl(activeFiles[2]); + const file3WithUrl = useFileWithUrl(activeFiles[3]); + const file4WithUrl = useFileWithUrl(activeFiles[4]); + + const filesWithUrls = React.useMemo(() => { + return [file0WithUrl, file1WithUrl, file2WithUrl, file3WithUrl, file4WithUrl] + .slice(0, activeFiles.length) + .filter(Boolean); + }, [file0WithUrl, file1WithUrl, file2WithUrl, file3WithUrl, file4WithUrl, activeFiles.length]); + + // Use preview file if available, otherwise use active tab file + const effectiveFile = React.useMemo(() => { + if (previewFile) { + // Validate the preview file + if (!(previewFile instanceof File)) { + return null; + } + + if (previewFile.size === 0) { + return null; + } + + return { file: previewFile, url: null }; + } else { + // Use the file from the active tab + const tabIndex = parseInt(activeTab); + return filesWithUrls[tabIndex] || null; + } + }, [previewFile, filesWithUrls, activeTab]); + const scrollAreaRef = useRef(null); - const userInitiatedRef = useRef(false); - const suppressScrollRef = useRef(false); const pdfDocRef = useRef(null); const renderingPagesRef = useRef>(new Set()); const currentArrayBufferRef = useRef(null); + const preloadingRef = useRef(false); // Function to render a specific page on-demand const renderPage = async (pageIndex: number): Promise => { - if (!pdfFile || !pdfDocRef.current || renderingPagesRef.current.has(pageIndex)) { + if (!pdfDocRef.current || renderingPagesRef.current.has(pageIndex)) { return null; } @@ -194,70 +259,78 @@ const Viewer = ({ return null; }; - // Listen for hash changes and update currentPage - useEffect(() => { - function handleHashChange() { - if (window.location.hash.startsWith("#page=")) { - const page = parseInt(window.location.hash.replace("#page=", ""), 10); - if (!isNaN(page) && page >= 1 && page <= numPages) { - setCurrentPage(page); - } + // Progressive preloading function + const startProgressivePreload = async () => { + if (!pdfDocRef.current || preloadingRef.current || numPages === 0) return; + + preloadingRef.current = true; + + // Start with first few pages for immediate viewing + const priorityPages = [0, 1, 2, 3, 4]; // First 5 pages + + // Render priority pages first + for (const pageIndex of priorityPages) { + if (pageIndex < numPages && !pageImages[pageIndex]) { + await renderPage(pageIndex); + // Small delay to allow UI to update + await new Promise(resolve => setTimeout(resolve, 50)); } - userInitiatedRef.current = false; } - window.addEventListener("hashchange", handleHashChange); - handleHashChange(); // Run on mount - return () => window.removeEventListener("hashchange", handleHashChange); - }, [numPages]); - - // Scroll to the current page when it changes - useEffect(() => { - if (currentPage && pageRefs.current[currentPage - 1]) { - suppressScrollRef.current = true; - const el = pageRefs.current[currentPage - 1]; - el?.scrollIntoView({ behavior: "smooth", block: "center" }); - - // Try to use scrollend if supported - const viewport = scrollAreaRef.current; - let timeout: NodeJS.Timeout | null = null; - let scrollEndHandler: (() => void) | null = null; - - if (viewport && "onscrollend" in viewport) { - scrollEndHandler = () => { - suppressScrollRef.current = false; - viewport.removeEventListener("scrollend", scrollEndHandler!); - }; - viewport.addEventListener("scrollend", scrollEndHandler); - } else { - // Fallback for non-Chromium browsers - timeout = setTimeout(() => { - suppressScrollRef.current = false; - }, 1000); + + // Then render remaining pages in background + for (let pageIndex = 5; pageIndex < numPages; pageIndex++) { + if (!pageImages[pageIndex]) { + await renderPage(pageIndex); + // Longer delay for background loading to not block UI + await new Promise(resolve => setTimeout(resolve, 100)); } - - return () => { - if (viewport && scrollEndHandler) { - viewport.removeEventListener("scrollend", scrollEndHandler); - } - if (timeout) clearTimeout(timeout); - }; } - }, [currentPage, pageImages]); + + preloadingRef.current = false; + }; - // Detect visible page on scroll and update hash - const handleScroll = () => { - if (suppressScrollRef.current) return; + // Initialize current page when PDF loads + useEffect(() => { + if (numPages > 0 && !currentPage) { + setCurrentPage(1); + } + }, [numPages, currentPage]); + + // Function to scroll to a specific page + const scrollToPage = (pageNumber: number) => { + const el = pageRefs.current[pageNumber - 1]; + const scrollArea = scrollAreaRef.current; + + if (el && scrollArea) { + const scrollAreaRect = scrollArea.getBoundingClientRect(); + const elRect = el.getBoundingClientRect(); + const currentScrollTop = scrollArea.scrollTop; + + // Position page near top of viewport with some padding + const targetScrollTop = currentScrollTop + (elRect.top - scrollAreaRect.top) - 20; + + scrollArea.scrollTo({ + top: targetScrollTop, + behavior: "smooth" + }); + } + }; + + // Throttled scroll handler to prevent jerky updates + const handleScrollThrottled = useCallback(() => { const scrollArea = scrollAreaRef.current; if (!scrollArea || !pageRefs.current.length) return; const areaRect = scrollArea.getBoundingClientRect(); + const viewportCenter = areaRect.top + areaRect.height / 2; let closestIdx = 0; let minDist = Infinity; pageRefs.current.forEach((img, idx) => { if (img) { const imgRect = img.getBoundingClientRect(); - const dist = Math.abs(imgRect.top - areaRect.top); + const imgCenter = imgRect.top + imgRect.height / 2; + const dist = Math.abs(imgCenter - viewportCenter); if (dist < minDist) { minDist = dist; closestIdx = idx; @@ -265,30 +338,41 @@ const Viewer = ({ } }); + // Update page number display only if changed if (currentPage !== closestIdx + 1) { setCurrentPage(closestIdx + 1); - if (window.location.hash !== `#page=${closestIdx + 1}`) { - window.location.hash = `#page=${closestIdx + 1}`; - } } - }; + }, [currentPage]); + + // Throttle scroll events to reduce jerkiness + const handleScroll = useCallback(() => { + if (window.requestAnimationFrame) { + window.requestAnimationFrame(handleScrollThrottled); + } else { + handleScrollThrottled(); + } + }, [handleScrollThrottled]); useEffect(() => { let cancelled = false; async function loadPdfInfo() { - if (!pdfFile || !pdfFile.url) { + if (!effectiveFile) { setNumPages(0); setPageImages([]); return; } setLoading(true); try { - let pdfUrl = pdfFile.url; - + let pdfData; + + // For preview files, use ArrayBuffer directly to avoid blob URL issues + if (previewFile && effectiveFile.file === previewFile) { + const arrayBuffer = await previewFile.arrayBuffer(); + pdfData = { data: arrayBuffer }; + } // Handle special IndexedDB URLs for large files - if (pdfFile.url.startsWith('indexeddb:')) { - const fileId = pdfFile.url.replace('indexeddb:', ''); - console.log('Loading large file from IndexedDB:', fileId); + else if (effectiveFile.url?.startsWith('indexeddb:')) { + const fileId = effectiveFile.url.replace('indexeddb:', ''); // Get data directly from IndexedDB const arrayBuffer = await fileStorage.getFileData(fileId); @@ -298,21 +382,23 @@ const Viewer = ({ // Store reference for cleanup currentArrayBufferRef.current = arrayBuffer; - - // Use ArrayBuffer directly instead of creating blob URL - const pdf = await getDocument({ data: arrayBuffer }).promise; - pdfDocRef.current = pdf; - setNumPages(pdf.numPages); - if (!cancelled) setPageImages(new Array(pdf.numPages).fill(null)); - } else { + pdfData = { data: arrayBuffer }; + } else if (effectiveFile.url) { // Standard blob URL or regular URL - const pdf = await getDocument(pdfUrl).promise; - pdfDocRef.current = pdf; - setNumPages(pdf.numPages); - if (!cancelled) setPageImages(new Array(pdf.numPages).fill(null)); + pdfData = effectiveFile.url; + } else { + throw new Error('No valid PDF source available'); + } + + const pdf = await getDocument(pdfData).promise; + pdfDocRef.current = pdf; + setNumPages(pdf.numPages); + if (!cancelled) { + setPageImages(new Array(pdf.numPages).fill(null)); + // Start progressive preloading after a short delay + setTimeout(() => startProgressivePreload(), 100); } } catch (error) { - console.error('Failed to load PDF:', error); if (!cancelled) { setPageImages([]); setNumPages(0); @@ -323,10 +409,12 @@ const Viewer = ({ loadPdfInfo(); return () => { cancelled = true; + // Stop any ongoing preloading + preloadingRef.current = false; // Cleanup ArrayBuffer reference to help garbage collection currentArrayBufferRef.current = null; }; - }, [pdfFile]); + }, [effectiveFile, previewFile]); useEffect(() => { const viewport = scrollAreaRef.current; @@ -339,39 +427,62 @@ const Viewer = ({ }, [pageImages]); return ( - <> - {!pdfFile ? ( + + {/* Close Button - Only show in preview mode */} + {onClose && previewFile && ( + + + + )} + + {!effectiveFile ? (
- - {t("viewer.noPdfLoaded", "No PDF loaded. Click to upload a PDF.")} - - -
- ) : loading ? ( -
- + Error: No file provided to viewer
) : ( + <> + {/* Tabs for multiple files */} + {activeFiles.length > 1 && !previewFile && ( + + handleTabChange(value || "0")}> + + {activeFiles.map((file, index) => ( + + {file.name.length > 20 ? `${file.name.substring(0, 20)}...` : file.name} + + ))} + + + + )} + + {loading ? ( +
+ +
+ ) : ( @@ -456,7 +567,7 @@ const Viewer = ({ px={8} radius="xl" onClick={() => { - window.location.hash = `#page=1`; + scrollToPage(1); }} disabled={currentPage === 1} style={{ minWidth: 36 }} @@ -470,7 +581,8 @@ const Viewer = ({ px={8} radius="xl" onClick={() => { - window.location.hash = `#page=${Math.max(1, (currentPage || 1) - 1)}`; + const prevPage = Math.max(1, (currentPage || 1) - 1); + scrollToPage(prevPage); }} disabled={currentPage === 1} style={{ minWidth: 36 }} @@ -482,7 +594,7 @@ const Viewer = ({ onChange={value => { const page = Number(value); if (!isNaN(page) && page >= 1 && page <= numPages) { - window.location.hash = `#page=${page}`; + scrollToPage(page); } }} min={1} @@ -502,7 +614,8 @@ const Viewer = ({ px={8} radius="xl" onClick={() => { - window.location.hash = `#page=${Math.min(numPages, (currentPage || 1) + 1)}`; + const nextPage = Math.min(numPages, (currentPage || 1) + 1); + scrollToPage(nextPage); }} disabled={currentPage === numPages} style={{ minWidth: 36 }} @@ -516,7 +629,7 @@ const Viewer = ({ px={8} radius="xl" onClick={() => { - window.location.hash = `#page=${numPages}`; + scrollToPage(numPages); }} disabled={currentPage === numPages} style={{ minWidth: 36 }} @@ -558,9 +671,11 @@ const Viewer = ({
+ )} + )} - +
); }; diff --git a/frontend/src/constants/splitConstants.ts b/frontend/src/constants/splitConstants.ts new file mode 100644 index 000000000..0da43ac57 --- /dev/null +++ b/frontend/src/constants/splitConstants.ts @@ -0,0 +1,22 @@ +export const SPLIT_MODES = { + BY_PAGES: 'byPages', + BY_SECTIONS: 'bySections', + BY_SIZE_OR_COUNT: 'bySizeOrCount', + BY_CHAPTERS: 'byChapters' +} as const; + +export const SPLIT_TYPES = { + SIZE: 'size', + PAGES: 'pages', + DOCS: 'docs' +} as const; + +export const ENDPOINTS = { + [SPLIT_MODES.BY_PAGES]: 'split-pages', + [SPLIT_MODES.BY_SECTIONS]: 'split-pdf-by-sections', + [SPLIT_MODES.BY_SIZE_OR_COUNT]: 'split-by-size-or-count', + [SPLIT_MODES.BY_CHAPTERS]: 'split-pdf-by-chapters' +} as const; + +export type SplitMode = typeof SPLIT_MODES[keyof typeof SPLIT_MODES]; +export type SplitType = typeof SPLIT_TYPES[keyof typeof SPLIT_TYPES]; \ No newline at end of file diff --git a/frontend/src/contexts/FileContext.tsx b/frontend/src/contexts/FileContext.tsx new file mode 100644 index 000000000..811a49db7 --- /dev/null +++ b/frontend/src/contexts/FileContext.tsx @@ -0,0 +1,865 @@ +/** + * Global file context for managing files, edits, and navigation across all views and tools + */ + +import React, { createContext, useContext, useReducer, useCallback, useEffect, useRef } from 'react'; +import { + FileContextValue, + FileContextState, + FileContextProviderProps, + ModeType, + ViewType, + ToolType, + FileOperation, + FileEditHistory, + FileOperationHistory, + ViewerConfig, + FileContextUrlParams +} from '../types/fileContext'; +import { ProcessedFile } from '../types/processing'; +import { PageOperation, PDFDocument } from '../types/pageEditor'; +import { useEnhancedProcessedFiles } from '../hooks/useEnhancedProcessedFiles'; +import { fileStorage } from '../services/fileStorage'; +import { enhancedPDFProcessingService } from '../services/enhancedPDFProcessingService'; +import { thumbnailGenerationService } from '../services/thumbnailGenerationService'; + +// Initial state +const initialViewerConfig: ViewerConfig = { + zoom: 1.0, + currentPage: 1, + viewMode: 'single', + sidebarOpen: false +}; + +const initialState: FileContextState = { + activeFiles: [], + processedFiles: new Map(), + currentMode: 'pageEditor', + currentView: 'fileEditor', // Legacy field + currentTool: null, // Legacy field + fileEditHistory: new Map(), + globalFileOperations: [], + fileOperationHistory: new Map(), + selectedFileIds: [], + selectedPageNumbers: [], + viewerConfig: initialViewerConfig, + isProcessing: false, + processingProgress: 0, + lastExportConfig: undefined, + hasUnsavedChanges: false, + pendingNavigation: null, + showNavigationWarning: false +}; + +// Action types +type FileContextAction = + | { type: 'SET_ACTIVE_FILES'; payload: File[] } + | { type: 'ADD_FILES'; payload: File[] } + | { type: 'REMOVE_FILES'; payload: string[] } + | { type: 'SET_PROCESSED_FILES'; payload: Map } + | { type: 'UPDATE_PROCESSED_FILE'; payload: { file: File; processedFile: ProcessedFile } } + | { type: 'SET_CURRENT_MODE'; payload: ModeType } + | { type: 'SET_CURRENT_VIEW'; payload: ViewType } + | { type: 'SET_CURRENT_TOOL'; payload: ToolType } + | { type: 'SET_SELECTED_FILES'; payload: string[] } + | { type: 'SET_SELECTED_PAGES'; payload: number[] } + | { type: 'CLEAR_SELECTIONS' } + | { type: 'SET_PROCESSING'; payload: { isProcessing: boolean; progress: number } } + | { type: 'UPDATE_VIEWER_CONFIG'; payload: Partial } + | { type: 'ADD_PAGE_OPERATIONS'; payload: { fileId: string; operations: PageOperation[] } } + | { type: 'ADD_FILE_OPERATION'; payload: FileOperation } + | { type: 'RECORD_OPERATION'; payload: { fileId: string; operation: FileOperation | PageOperation } } + | { type: 'MARK_OPERATION_APPLIED'; payload: { fileId: string; operationId: string } } + | { type: 'MARK_OPERATION_FAILED'; payload: { fileId: string; operationId: string; error: string } } + | { type: 'CLEAR_FILE_HISTORY'; payload: string } + | { type: 'SET_EXPORT_CONFIG'; payload: FileContextState['lastExportConfig'] } + | { type: 'SET_UNSAVED_CHANGES'; payload: boolean } + | { type: 'SET_PENDING_NAVIGATION'; payload: (() => void) | null } + | { type: 'SHOW_NAVIGATION_WARNING'; payload: boolean } + | { type: 'RESET_CONTEXT' } + | { type: 'LOAD_STATE'; payload: Partial }; + +// Reducer +function fileContextReducer(state: FileContextState, action: FileContextAction): FileContextState { + switch (action.type) { + case 'SET_ACTIVE_FILES': + return { + ...state, + activeFiles: action.payload, + selectedFileIds: [], // Clear selections when files change + selectedPageNumbers: [] + }; + + case 'ADD_FILES': + return { + ...state, + activeFiles: [...state.activeFiles, ...action.payload] + }; + + case 'REMOVE_FILES': + const remainingFiles = state.activeFiles.filter(file => { + const fileId = (file as any).id || file.name; + return !action.payload.includes(fileId); + }); + const safeSelectedFileIds = Array.isArray(state.selectedFileIds) ? state.selectedFileIds : []; + return { + ...state, + activeFiles: remainingFiles, + selectedFileIds: safeSelectedFileIds.filter(id => !action.payload.includes(id)) + }; + + case 'SET_PROCESSED_FILES': + return { + ...state, + processedFiles: action.payload + }; + + case 'UPDATE_PROCESSED_FILE': + const updatedProcessedFiles = new Map(state.processedFiles); + updatedProcessedFiles.set(action.payload.file, action.payload.processedFile); + return { + ...state, + processedFiles: updatedProcessedFiles + }; + + case 'SET_CURRENT_MODE': + const coreViews = ['viewer', 'pageEditor', 'fileEditor']; + const isToolMode = !coreViews.includes(action.payload); + + return { + ...state, + currentMode: action.payload, + // Update legacy fields for backward compatibility + currentView: isToolMode ? 'fileEditor' : action.payload as ViewType, + currentTool: isToolMode ? action.payload as ToolType : null + }; + + case 'SET_CURRENT_VIEW': + // Legacy action - just update currentMode + return { + ...state, + currentMode: action.payload as ModeType, + currentView: action.payload, + currentTool: null + }; + + case 'SET_CURRENT_TOOL': + // Legacy action - just update currentMode + return { + ...state, + currentMode: action.payload ? action.payload as ModeType : 'pageEditor', + currentView: action.payload ? 'fileEditor' : 'pageEditor', + currentTool: action.payload + }; + + case 'SET_SELECTED_FILES': + return { + ...state, + selectedFileIds: action.payload + }; + + case 'SET_SELECTED_PAGES': + return { + ...state, + selectedPageNumbers: action.payload + }; + + case 'CLEAR_SELECTIONS': + return { + ...state, + selectedFileIds: [], + selectedPageNumbers: [] + }; + + case 'SET_PROCESSING': + return { + ...state, + isProcessing: action.payload.isProcessing, + processingProgress: action.payload.progress + }; + + case 'UPDATE_VIEWER_CONFIG': + return { + ...state, + viewerConfig: { + ...state.viewerConfig, + ...action.payload + } + }; + + case 'ADD_PAGE_OPERATIONS': + const newHistory = new Map(state.fileEditHistory); + const existing = newHistory.get(action.payload.fileId); + newHistory.set(action.payload.fileId, { + fileId: action.payload.fileId, + pageOperations: existing ? + [...existing.pageOperations, ...action.payload.operations] : + action.payload.operations, + lastModified: Date.now() + }); + return { + ...state, + fileEditHistory: newHistory + }; + + case 'ADD_FILE_OPERATION': + return { + ...state, + globalFileOperations: [...state.globalFileOperations, action.payload] + }; + + case 'RECORD_OPERATION': + const { fileId, operation } = action.payload; + const newOperationHistory = new Map(state.fileOperationHistory); + const existingHistory = newOperationHistory.get(fileId); + + if (existingHistory) { + // Add operation to existing history + newOperationHistory.set(fileId, { + ...existingHistory, + operations: [...existingHistory.operations, operation], + lastModified: Date.now() + }); + } else { + // Create new history for this file + newOperationHistory.set(fileId, { + fileId, + fileName: fileId, // Will be updated with actual filename when available + operations: [operation], + createdAt: Date.now(), + lastModified: Date.now() + }); + } + + return { + ...state, + fileOperationHistory: newOperationHistory + }; + + case 'MARK_OPERATION_APPLIED': + const appliedHistory = new Map(state.fileOperationHistory); + const appliedFileHistory = appliedHistory.get(action.payload.fileId); + + if (appliedFileHistory) { + const updatedOperations = appliedFileHistory.operations.map(op => + op.id === action.payload.operationId + ? { ...op, status: 'applied' as const } + : op + ); + appliedHistory.set(action.payload.fileId, { + ...appliedFileHistory, + operations: updatedOperations, + lastModified: Date.now() + }); + } + + return { + ...state, + fileOperationHistory: appliedHistory + }; + + case 'MARK_OPERATION_FAILED': + const failedHistory = new Map(state.fileOperationHistory); + const failedFileHistory = failedHistory.get(action.payload.fileId); + + if (failedFileHistory) { + const updatedOperations = failedFileHistory.operations.map(op => + op.id === action.payload.operationId + ? { + ...op, + status: 'failed' as const, + metadata: { ...op.metadata, error: action.payload.error } + } + : op + ); + failedHistory.set(action.payload.fileId, { + ...failedFileHistory, + operations: updatedOperations, + lastModified: Date.now() + }); + } + + return { + ...state, + fileOperationHistory: failedHistory + }; + + case 'CLEAR_FILE_HISTORY': + const clearedHistory = new Map(state.fileOperationHistory); + clearedHistory.delete(action.payload); + return { + ...state, + fileOperationHistory: clearedHistory + }; + + case 'SET_EXPORT_CONFIG': + return { + ...state, + lastExportConfig: action.payload + }; + + case 'SET_UNSAVED_CHANGES': + return { + ...state, + hasUnsavedChanges: action.payload + }; + + case 'SET_PENDING_NAVIGATION': + return { + ...state, + pendingNavigation: action.payload + }; + + case 'SHOW_NAVIGATION_WARNING': + return { + ...state, + showNavigationWarning: action.payload + }; + + case 'RESET_CONTEXT': + return { + ...initialState + }; + + case 'LOAD_STATE': + return { + ...state, + ...action.payload + }; + + default: + return state; + } +} + +// Context +const FileContext = createContext(undefined); + +// Provider component +export function FileContextProvider({ + children, + enableUrlSync = true, + enablePersistence = true, + maxCacheSize = 1024 * 1024 * 1024 // 1GB +}: FileContextProviderProps) { + const [state, dispatch] = useReducer(fileContextReducer, initialState); + + // Cleanup timers and refs + const cleanupTimers = useRef>(new Map()); + const blobUrls = useRef>(new Set()); + const pdfDocuments = useRef>(new Map()); + + // Enhanced file processing hook + const { + processedFiles, + processingStates, + isProcessing: globalProcessing, + processingProgress, + actions: processingActions + } = useEnhancedProcessedFiles(state.activeFiles, { + strategy: 'progressive_chunked', + thumbnailQuality: 'medium', + chunkSize: 5, // Process 5 pages at a time for smooth progress + priorityPageCount: 0 // No special priority pages + }); + + // Update processed files when they change + useEffect(() => { + dispatch({ type: 'SET_PROCESSED_FILES', payload: processedFiles }); + dispatch({ + type: 'SET_PROCESSING', + payload: { + isProcessing: globalProcessing, + progress: processingProgress.overall + } + }); + }, [processedFiles, globalProcessing, processingProgress.overall]); + + + // Centralized memory management + const trackBlobUrl = useCallback((url: string) => { + blobUrls.current.add(url); + }, []); + + const trackPdfDocument = useCallback((fileId: string, pdfDoc: any) => { + // Clean up existing document for this file if any + const existing = pdfDocuments.current.get(fileId); + if (existing && existing.destroy) { + try { + existing.destroy(); + } catch (error) { + console.warn('Error destroying existing PDF document:', error); + } + } + pdfDocuments.current.set(fileId, pdfDoc); + }, []); + + const cleanupFile = useCallback(async (fileId: string) => { + console.log('Cleaning up file:', fileId); + + try { + // Cancel any pending cleanup timer + const timer = cleanupTimers.current.get(fileId); + if (timer) { + clearTimeout(timer); + cleanupTimers.current.delete(fileId); + } + + // Cleanup PDF document instances (but preserve processed file cache) + const pdfDoc = pdfDocuments.current.get(fileId); + if (pdfDoc && pdfDoc.destroy) { + pdfDoc.destroy(); + pdfDocuments.current.delete(fileId); + } + + // IMPORTANT: Don't cancel processing or clear cache during normal view switches + // Only do this when file is actually being removed + // enhancedPDFProcessingService.cancelProcessing(fileId); + // thumbnailGenerationService.stopGeneration(); + + } catch (error) { + console.warn('Error during file cleanup:', error); + } + }, []); + + const cleanupAllFiles = useCallback(() => { + console.log('Cleaning up all files'); + + try { + // Clear all timers + cleanupTimers.current.forEach(timer => clearTimeout(timer)); + cleanupTimers.current.clear(); + + // Destroy all PDF documents + pdfDocuments.current.forEach((pdfDoc, fileId) => { + if (pdfDoc && pdfDoc.destroy) { + try { + pdfDoc.destroy(); + } catch (error) { + console.warn(`Error destroying PDF document for ${fileId}:`, error); + } + } + }); + pdfDocuments.current.clear(); + + // Revoke all blob URLs + blobUrls.current.forEach(url => { + try { + URL.revokeObjectURL(url); + } catch (error) { + console.warn('Error revoking blob URL:', error); + } + }); + blobUrls.current.clear(); + + // Clear all processing + enhancedPDFProcessingService.clearAllProcessing(); + + // Destroy thumbnails + thumbnailGenerationService.destroy(); + + // Force garbage collection hint + if (typeof window !== 'undefined' && window.gc) { + setTimeout(() => window.gc(), 100); + } + + } catch (error) { + console.warn('Error during cleanup all files:', error); + } + }, []); + + const scheduleCleanup = useCallback((fileId: string, delay: number = 30000) => { + // Cancel existing timer + const existingTimer = cleanupTimers.current.get(fileId); + if (existingTimer) { + clearTimeout(existingTimer); + cleanupTimers.current.delete(fileId); + } + + // If delay is negative, just cancel (don't reschedule) + if (delay < 0) { + return; + } + + // Schedule new cleanup + const timer = setTimeout(() => { + cleanupFile(fileId); + }, delay); + + cleanupTimers.current.set(fileId, timer); + }, [cleanupFile]); + + // Action implementations + const addFiles = useCallback(async (files: File[]) => { + dispatch({ type: 'ADD_FILES', payload: files }); + + // Auto-save to IndexedDB if persistence enabled + if (enablePersistence) { + for (const file of files) { + try { + // Check if file already has an ID (already in IndexedDB) + const fileId = (file as any).id; + if (!fileId) { + // File doesn't have ID, store it and get the ID + const storedFile = await fileStorage.storeFile(file); + // Add the ID to the file object + Object.defineProperty(file, 'id', { value: storedFile.id, writable: false }); + } + } catch (error) { + console.error('Failed to store file:', error); + } + } + } + }, [enablePersistence]); + + const removeFiles = useCallback((fileIds: string[], deleteFromStorage: boolean = true) => { + // FULL cleanup for actually removed files (including cache) + fileIds.forEach(fileId => { + // Cancel processing and clear caches when file is actually removed + enhancedPDFProcessingService.cancelProcessing(fileId); + cleanupFile(fileId); + }); + + dispatch({ type: 'REMOVE_FILES', payload: fileIds }); + + // Remove from IndexedDB only if requested + if (enablePersistence && deleteFromStorage) { + fileIds.forEach(async (fileId) => { + try { + await fileStorage.deleteFile(fileId); + } catch (error) { + console.error('Failed to remove file from storage:', error); + } + }); + } + }, [enablePersistence, cleanupFile]); + + + const replaceFile = useCallback(async (oldFileId: string, newFile: File) => { + // Remove old file and add new one + removeFiles([oldFileId]); + await addFiles([newFile]); + }, [removeFiles, addFiles]); + + const clearAllFiles = useCallback(() => { + // Cleanup all memory before clearing files + cleanupAllFiles(); + + dispatch({ type: 'SET_ACTIVE_FILES', payload: [] }); + dispatch({ type: 'CLEAR_SELECTIONS' }); + }, [cleanupAllFiles]); + + // Navigation guard system functions + const setHasUnsavedChanges = useCallback((hasChanges: boolean) => { + dispatch({ type: 'SET_UNSAVED_CHANGES', payload: hasChanges }); + }, []); + + const requestNavigation = useCallback((navigationFn: () => void): boolean => { + if (state.hasUnsavedChanges) { + dispatch({ type: 'SET_PENDING_NAVIGATION', payload: navigationFn }); + dispatch({ type: 'SHOW_NAVIGATION_WARNING', payload: true }); + return false; + } else { + navigationFn(); + return true; + } + }, [state.hasUnsavedChanges]); + + const confirmNavigation = useCallback(() => { + if (state.pendingNavigation) { + state.pendingNavigation(); + dispatch({ type: 'SET_PENDING_NAVIGATION', payload: null }); + } + dispatch({ type: 'SHOW_NAVIGATION_WARNING', payload: false }); + }, [state.pendingNavigation]); + + const cancelNavigation = useCallback(() => { + dispatch({ type: 'SET_PENDING_NAVIGATION', payload: null }); + dispatch({ type: 'SHOW_NAVIGATION_WARNING', payload: false }); + }, []); + + const setCurrentMode = useCallback((mode: ModeType) => { + requestNavigation(() => { + dispatch({ type: 'SET_CURRENT_MODE', payload: mode }); + + if (state.currentMode !== mode && state.activeFiles.length > 0) { + if (window.requestIdleCallback && typeof window !== 'undefined' && window.gc) { + window.requestIdleCallback(() => { + window.gc(); + }, { timeout: 5000 }); + } + } + }); + }, [requestNavigation, state.currentMode, state.activeFiles]); + + const setCurrentView = useCallback((view: ViewType) => { + requestNavigation(() => { + dispatch({ type: 'SET_CURRENT_VIEW', payload: view }); + + if (state.currentView !== view && state.activeFiles.length > 0) { + if (window.requestIdleCallback && typeof window !== 'undefined' && window.gc) { + window.requestIdleCallback(() => { + window.gc(); + }, { timeout: 5000 }); + } + } + }); + }, [requestNavigation, state.currentView, state.activeFiles]); + + const setCurrentTool = useCallback((tool: ToolType) => { + requestNavigation(() => { + dispatch({ type: 'SET_CURRENT_TOOL', payload: tool }); + }); + }, [requestNavigation]); + + const setSelectedFiles = useCallback((fileIds: string[]) => { + dispatch({ type: 'SET_SELECTED_FILES', payload: fileIds }); + }, []); + + const setSelectedPages = useCallback((pageNumbers: number[]) => { + dispatch({ type: 'SET_SELECTED_PAGES', payload: pageNumbers }); + }, []); + + const updateProcessedFile = useCallback((file: File, processedFile: ProcessedFile) => { + dispatch({ type: 'UPDATE_PROCESSED_FILE', payload: { file, processedFile } }); + }, []); + + const clearSelections = useCallback(() => { + dispatch({ type: 'CLEAR_SELECTIONS' }); + }, []); + + const applyPageOperations = useCallback((fileId: string, operations: PageOperation[]) => { + dispatch({ + type: 'ADD_PAGE_OPERATIONS', + payload: { fileId, operations } + }); + }, []); + + const applyFileOperation = useCallback((operation: FileOperation) => { + dispatch({ type: 'ADD_FILE_OPERATION', payload: operation }); + }, []); + + const undoLastOperation = useCallback((fileId?: string) => { + console.warn('Undo not yet implemented'); + }, []); + + const updateViewerConfig = useCallback((config: Partial) => { + dispatch({ type: 'UPDATE_VIEWER_CONFIG', payload: config }); + }, []); + + const setExportConfig = useCallback((config: FileContextState['lastExportConfig']) => { + dispatch({ type: 'SET_EXPORT_CONFIG', payload: config }); + }, []); + + // Operation history management functions + const recordOperation = useCallback((fileId: string, operation: FileOperation | PageOperation) => { + dispatch({ type: 'RECORD_OPERATION', payload: { fileId, operation } }); + }, []); + + const markOperationApplied = useCallback((fileId: string, operationId: string) => { + dispatch({ type: 'MARK_OPERATION_APPLIED', payload: { fileId, operationId } }); + }, []); + + const markOperationFailed = useCallback((fileId: string, operationId: string, error: string) => { + dispatch({ type: 'MARK_OPERATION_FAILED', payload: { fileId, operationId, error } }); + }, []); + + const getFileHistory = useCallback((fileId: string): FileOperationHistory | undefined => { + return state.fileOperationHistory.get(fileId); + }, [state.fileOperationHistory]); + + const getAppliedOperations = useCallback((fileId: string): (FileOperation | PageOperation)[] => { + const history = state.fileOperationHistory.get(fileId); + return history ? history.operations.filter(op => op.status === 'applied') : []; + }, [state.fileOperationHistory]); + + const clearFileHistory = useCallback((fileId: string) => { + dispatch({ type: 'CLEAR_FILE_HISTORY', payload: fileId }); + }, []); + + // Utility functions + const getFileById = useCallback((fileId: string): File | undefined => { + return state.activeFiles.find(file => { + const actualFileId = (file as any).id || file.name; + return actualFileId === fileId; + }); + }, [state.activeFiles]); + + const getProcessedFileById = useCallback((fileId: string): ProcessedFile | undefined => { + const file = getFileById(fileId); + return file ? state.processedFiles.get(file) : undefined; + }, [getFileById, state.processedFiles]); + + const getCurrentFile = useCallback((): File | undefined => { + if (state.selectedFileIds.length > 0) { + return getFileById(state.selectedFileIds[0]); + } + return state.activeFiles[0]; // Default to first file + }, [state.selectedFileIds, state.activeFiles, getFileById]); + + const getCurrentProcessedFile = useCallback((): ProcessedFile | undefined => { + const file = getCurrentFile(); + return file ? state.processedFiles.get(file) : undefined; + }, [getCurrentFile, state.processedFiles]); + + // Context persistence + const saveContext = useCallback(async () => { + if (!enablePersistence) return; + + try { + const contextData = { + currentView: state.currentView, + currentTool: state.currentTool, + selectedFileIds: state.selectedFileIds, + selectedPageIds: state.selectedPageIds, + viewerConfig: state.viewerConfig, + lastExportConfig: state.lastExportConfig, + timestamp: Date.now() + }; + + localStorage.setItem('fileContext', JSON.stringify(contextData)); + } catch (error) { + console.error('Failed to save context:', error); + } + }, [state, enablePersistence]); + + const loadContext = useCallback(async () => { + if (!enablePersistence) return; + + try { + const saved = localStorage.getItem('fileContext'); + if (saved) { + const contextData = JSON.parse(saved); + dispatch({ type: 'LOAD_STATE', payload: contextData }); + } + } catch (error) { + console.error('Failed to load context:', error); + } + }, [enablePersistence]); + + const resetContext = useCallback(() => { + dispatch({ type: 'RESET_CONTEXT' }); + if (enablePersistence) { + localStorage.removeItem('fileContext'); + } + }, [enablePersistence]); + + + // Auto-save context when it changes + useEffect(() => { + saveContext(); + }, [saveContext]); + + // Load context on mount + useEffect(() => { + loadContext(); + }, [loadContext]); + + // Cleanup on unmount + useEffect(() => { + return () => { + console.log('FileContext unmounting - cleaning up all resources'); + cleanupAllFiles(); + }; + }, [cleanupAllFiles]); + + const contextValue: FileContextValue = { + // State + ...state, + + // Actions + addFiles, + removeFiles, + replaceFile, + clearAllFiles, + setCurrentMode, + setCurrentView, + setCurrentTool, + setSelectedFiles, + setSelectedPages, + updateProcessedFile, + clearSelections, + applyPageOperations, + applyFileOperation, + undoLastOperation, + updateViewerConfig, + setExportConfig, + getFileById, + getProcessedFileById, + getCurrentFile, + getCurrentProcessedFile, + saveContext, + loadContext, + resetContext, + + // Operation history management + recordOperation, + markOperationApplied, + markOperationFailed, + getFileHistory, + getAppliedOperations, + clearFileHistory, + + // Navigation guard system + setHasUnsavedChanges, + requestNavigation, + confirmNavigation, + cancelNavigation, + + // Memory management + trackBlobUrl, + trackPdfDocument, + cleanupFile, + scheduleCleanup + }; + + return ( + + {children} + + ); +} + +// Custom hook to use the context +export function useFileContext(): FileContextValue { + const context = useContext(FileContext); + if (!context) { + throw new Error('useFileContext must be used within a FileContextProvider'); + } + return context; +} + +// Helper hooks for specific aspects +export function useCurrentFile() { + const { getCurrentFile, getCurrentProcessedFile } = useFileContext(); + return { + file: getCurrentFile(), + processedFile: getCurrentProcessedFile() + }; +} + +export function useFileSelection() { + const { + selectedFileIds, + selectedPageIds, + setSelectedFiles, + setSelectedPages, + clearSelections + } = useFileContext(); + + return { + selectedFileIds, + selectedPageIds, + setSelectedFiles, + setSelectedPages, + clearSelections + }; +} + +export function useViewerState() { + const { viewerConfig, updateViewerConfig } = useFileContext(); + return { + config: viewerConfig, + updateConfig: updateViewerConfig + }; +} \ No newline at end of file diff --git a/frontend/src/hooks/tools/shared/useOperationResults.ts b/frontend/src/hooks/tools/shared/useOperationResults.ts new file mode 100644 index 000000000..fca4f922a --- /dev/null +++ b/frontend/src/hooks/tools/shared/useOperationResults.ts @@ -0,0 +1,67 @@ +import { useState, useCallback } from 'react'; + +export interface OperationResult { + files: File[]; + thumbnails: string[]; + isGeneratingThumbnails: boolean; +} + +export interface OperationResultsHook { + results: OperationResult; + downloadUrl: string | null; + status: string; + errorMessage: string | null; + isLoading: boolean; + + setResults: (results: OperationResult) => void; + setDownloadUrl: (url: string | null) => void; + setStatus: (status: string) => void; + setErrorMessage: (error: string | null) => void; + setIsLoading: (loading: boolean) => void; + + resetResults: () => void; + clearError: () => void; +} + +const initialResults: OperationResult = { + files: [], + thumbnails: [], + isGeneratingThumbnails: false, +}; + +export const useOperationResults = (): OperationResultsHook => { + const [results, setResults] = useState(initialResults); + const [downloadUrl, setDownloadUrl] = useState(null); + const [status, setStatus] = useState(''); + const [errorMessage, setErrorMessage] = useState(null); + const [isLoading, setIsLoading] = useState(false); + + const resetResults = useCallback(() => { + setResults(initialResults); + setDownloadUrl(null); + setStatus(''); + setErrorMessage(null); + setIsLoading(false); + }, []); + + const clearError = useCallback(() => { + setErrorMessage(null); + }, []); + + return { + results, + downloadUrl, + status, + errorMessage, + isLoading, + + setResults, + setDownloadUrl, + setStatus, + setErrorMessage, + setIsLoading, + + resetResults, + clearError, + }; +}; \ No newline at end of file diff --git a/frontend/src/hooks/tools/split/useSplitOperation.ts b/frontend/src/hooks/tools/split/useSplitOperation.ts new file mode 100644 index 000000000..3abf2981e --- /dev/null +++ b/frontend/src/hooks/tools/split/useSplitOperation.ts @@ -0,0 +1,242 @@ +import { useCallback, useState } from 'react'; +import axios from 'axios'; +import { useTranslation } from 'react-i18next'; +import { useFileContext } from '../../../contexts/FileContext'; +import { FileOperation } from '../../../types/fileContext'; +import { zipFileService } from '../../../services/zipFileService'; +import { generateThumbnailForFile } from '../../../utils/thumbnailUtils'; +import { SplitParameters } from '../../../components/tools/split/SplitSettings'; +import { SPLIT_MODES, ENDPOINTS, type SplitMode } from '../../../constants/splitConstants'; + +export interface SplitOperationHook { + executeOperation: ( + mode: SplitMode | '', + parameters: SplitParameters, + selectedFiles: File[] + ) => Promise; + + // Flattened result properties for cleaner access + files: File[]; + thumbnails: string[]; + isGeneratingThumbnails: boolean; + downloadUrl: string | null; + status: string; + errorMessage: string | null; + isLoading: boolean; + + // Result management functions + resetResults: () => void; + clearError: () => void; +} + +export const useSplitOperation = (): SplitOperationHook => { + const { t } = useTranslation(); + const { + recordOperation, + markOperationApplied, + markOperationFailed, + addFiles + } = useFileContext(); + + // Internal state management (replacing useOperationResults) + const [files, setFiles] = useState([]); + const [thumbnails, setThumbnails] = useState([]); + const [isGeneratingThumbnails, setIsGeneratingThumbnails] = useState(false); + const [downloadUrl, setDownloadUrl] = useState(null); + const [status, setStatus] = useState(''); + const [errorMessage, setErrorMessage] = useState(null); + const [isLoading, setIsLoading] = useState(false); + + const buildFormData = useCallback(( + mode: SplitMode | '', + parameters: SplitParameters, + selectedFiles: File[] + ) => { + const formData = new FormData(); + + selectedFiles.forEach(file => { + formData.append("fileInput", file); + }); + + if (!mode) { + throw new Error('Split mode is required'); + } + + let endpoint = ""; + + switch (mode) { + case SPLIT_MODES.BY_PAGES: + formData.append("pageNumbers", parameters.pages); + endpoint = "/api/v1/general/split-pages"; + break; + case SPLIT_MODES.BY_SECTIONS: + formData.append("horizontalDivisions", parameters.hDiv); + formData.append("verticalDivisions", parameters.vDiv); + formData.append("merge", parameters.merge.toString()); + endpoint = "/api/v1/general/split-pdf-by-sections"; + break; + case SPLIT_MODES.BY_SIZE_OR_COUNT: + formData.append( + "splitType", + parameters.splitType === "size" ? "0" : parameters.splitType === "pages" ? "1" : "2" + ); + formData.append("splitValue", parameters.splitValue); + endpoint = "/api/v1/general/split-by-size-or-count"; + break; + case SPLIT_MODES.BY_CHAPTERS: + formData.append("bookmarkLevel", parameters.bookmarkLevel); + formData.append("includeMetadata", parameters.includeMetadata.toString()); + formData.append("allowDuplicates", parameters.allowDuplicates.toString()); + endpoint = "/api/v1/general/split-pdf-by-chapters"; + break; + default: + throw new Error(`Unknown split mode: ${mode}`); + } + + return { formData, endpoint }; + }, []); + + const createOperation = useCallback(( + mode: SplitMode | '', + parameters: SplitParameters, + selectedFiles: File[] + ): { operation: FileOperation; operationId: string; fileId: string } => { + const operationId = `split-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; + const fileId = selectedFiles[0].name; + + const operation: FileOperation = { + id: operationId, + type: 'split', + timestamp: Date.now(), + fileIds: selectedFiles.map(f => f.name), + status: 'pending', + metadata: { + originalFileName: selectedFiles[0].name, + parameters: { + mode, + pages: mode === SPLIT_MODES.BY_PAGES ? parameters.pages : undefined, + hDiv: mode === SPLIT_MODES.BY_SECTIONS ? parameters.hDiv : undefined, + vDiv: mode === SPLIT_MODES.BY_SECTIONS ? parameters.vDiv : undefined, + merge: mode === SPLIT_MODES.BY_SECTIONS ? parameters.merge : undefined, + splitType: mode === SPLIT_MODES.BY_SIZE_OR_COUNT ? parameters.splitType : undefined, + splitValue: mode === SPLIT_MODES.BY_SIZE_OR_COUNT ? parameters.splitValue : undefined, + bookmarkLevel: mode === SPLIT_MODES.BY_CHAPTERS ? parameters.bookmarkLevel : undefined, + includeMetadata: mode === SPLIT_MODES.BY_CHAPTERS ? parameters.includeMetadata : undefined, + allowDuplicates: mode === SPLIT_MODES.BY_CHAPTERS ? parameters.allowDuplicates : undefined, + }, + fileSize: selectedFiles[0].size + } + }; + + return { operation, operationId, fileId }; + }, []); + + const processResults = useCallback(async (blob: Blob) => { + try { + const zipFile = new File([blob], "split_result.zip", { type: "application/zip" }); + const extractionResult = await zipFileService.extractPdfFiles(zipFile); + + if (extractionResult.success && extractionResult.extractedFiles.length > 0) { + // Set local state for preview + setFiles(extractionResult.extractedFiles); + setThumbnails([]); + setIsGeneratingThumbnails(true); + + // Add extracted files to FileContext for future use + await addFiles(extractionResult.extractedFiles); + + const thumbnails = await Promise.all( + extractionResult.extractedFiles.map(async (file) => { + try { + return await generateThumbnailForFile(file); + } catch (error) { + console.warn(`Failed to generate thumbnail for ${file.name}:`, error); + return ''; + } + }) + ); + + setThumbnails(thumbnails); + setIsGeneratingThumbnails(false); + } + } catch (extractError) { + console.warn('Failed to extract files for preview:', extractError); + } + }, [addFiles]); + + const executeOperation = useCallback(async ( + mode: SplitMode | '', + parameters: SplitParameters, + selectedFiles: File[] + ) => { + if (selectedFiles.length === 0) { + setStatus(t("noFileSelected")); + return; + } + + const { operation, operationId, fileId } = createOperation(mode, parameters, selectedFiles); + const { formData, endpoint } = buildFormData(mode, parameters, selectedFiles); + + recordOperation(fileId, operation); + + setStatus(t("loading")); + setIsLoading(true); + setErrorMessage(null); + + try { + const response = await axios.post(endpoint, formData, { responseType: "blob" }); + const blob = new Blob([response.data], { type: "application/zip" }); + const url = window.URL.createObjectURL(blob); + + setDownloadUrl(url); + setStatus(t("downloadComplete")); + + await processResults(blob); + markOperationApplied(fileId, operationId); + } catch (error: any) { + console.error(error); + let errorMsg = t("error.pdfPassword", "An error occurred while splitting the PDF."); + if (error.response?.data && typeof error.response.data === 'string') { + errorMsg = error.response.data; + } else if (error.message) { + errorMsg = error.message; + } + setErrorMessage(errorMsg); + setStatus(t("error._value", "Split failed.")); + markOperationFailed(fileId, operationId, errorMsg); + } finally { + setIsLoading(false); + } + }, [t, createOperation, buildFormData, recordOperation, markOperationApplied, markOperationFailed, processResults]); + + const resetResults = useCallback(() => { + setFiles([]); + setThumbnails([]); + setIsGeneratingThumbnails(false); + setDownloadUrl(null); + setStatus(''); + setErrorMessage(null); + setIsLoading(false); + }, []); + + const clearError = useCallback(() => { + setErrorMessage(null); + }, []); + + return { + executeOperation, + + // Flattened result properties for cleaner access + files, + thumbnails, + isGeneratingThumbnails, + downloadUrl, + status, + errorMessage, + isLoading, + + // Result management functions + resetResults, + clearError, + }; +}; \ No newline at end of file diff --git a/frontend/src/hooks/tools/split/useSplitParameters.ts b/frontend/src/hooks/tools/split/useSplitParameters.ts new file mode 100644 index 000000000..4fac0b9d1 --- /dev/null +++ b/frontend/src/hooks/tools/split/useSplitParameters.ts @@ -0,0 +1,71 @@ +import { useState } from 'react'; +import { SPLIT_MODES, SPLIT_TYPES, ENDPOINTS, type SplitMode, type SplitType } from '../../../constants/splitConstants'; +import { SplitParameters } from '../../../components/tools/split/SplitSettings'; + +export interface SplitParametersHook { + mode: SplitMode | ''; + parameters: SplitParameters; + setMode: (mode: SplitMode | '') => void; + updateParameter: (parameter: keyof SplitParameters, value: string | boolean) => void; + resetParameters: () => void; + validateParameters: () => boolean; + getEndpointName: () => string; +} + +const initialParameters: SplitParameters = { + pages: '', + hDiv: '2', + vDiv: '2', + merge: false, + splitType: SPLIT_TYPES.SIZE, + splitValue: '', + bookmarkLevel: '1', + includeMetadata: false, + allowDuplicates: false, +}; + +export const useSplitParameters = (): SplitParametersHook => { + const [mode, setMode] = useState(''); + const [parameters, setParameters] = useState(initialParameters); + + const updateParameter = (parameter: keyof SplitParameters, value: string | boolean) => { + setParameters(prev => ({ ...prev, [parameter]: value })); + }; + + const resetParameters = () => { + setParameters(initialParameters); + setMode(''); + }; + + const validateParameters = () => { + if (!mode) return false; + + switch (mode) { + case SPLIT_MODES.BY_PAGES: + return parameters.pages.trim() !== ""; + case SPLIT_MODES.BY_SECTIONS: + return parameters.hDiv !== "" && parameters.vDiv !== ""; + case SPLIT_MODES.BY_SIZE_OR_COUNT: + return parameters.splitValue.trim() !== ""; + case SPLIT_MODES.BY_CHAPTERS: + return parameters.bookmarkLevel !== ""; + default: + return false; + } + }; + + const getEndpointName = () => { + if (!mode) return ENDPOINTS[SPLIT_MODES.BY_PAGES]; + return ENDPOINTS[mode as SplitMode]; + }; + + return { + mode, + parameters, + setMode, + updateParameter, + resetParameters, + validateParameters, + getEndpointName, + }; +}; \ No newline at end of file diff --git a/frontend/src/hooks/useEnhancedProcessedFiles.ts b/frontend/src/hooks/useEnhancedProcessedFiles.ts new file mode 100644 index 000000000..ebdff4bf5 --- /dev/null +++ b/frontend/src/hooks/useEnhancedProcessedFiles.ts @@ -0,0 +1,312 @@ +import { useState, useEffect, useRef } from 'react'; +import { ProcessedFile, ProcessingState, ProcessingConfig } from '../types/processing'; +import { enhancedPDFProcessingService } from '../services/enhancedPDFProcessingService'; +import { FileHasher } from '../utils/fileHash'; + +interface UseEnhancedProcessedFilesResult { + processedFiles: Map; + processingStates: Map; + isProcessing: boolean; + hasProcessingErrors: boolean; + processingProgress: { + overall: number; + fileProgress: Map; + estimatedTimeRemaining: number; + }; + cacheStats: { + entries: number; + totalSizeBytes: number; + maxSizeBytes: number; + }; + metrics: { + totalFiles: number; + completedFiles: number; + failedFiles: number; + averageProcessingTime: number; + cacheHitRate: number; + }; + actions: { + cancelProcessing: (fileKey: string) => void; + retryProcessing: (file: File) => void; + clearCache: () => void; + }; +} + +export function useEnhancedProcessedFiles( + activeFiles: File[], + config?: Partial +): UseEnhancedProcessedFilesResult { + const [processedFiles, setProcessedFiles] = useState>(new Map()); + const fileHashMapRef = useRef>(new Map()); // Use ref to avoid state update loops + const [processingStates, setProcessingStates] = useState>(new Map()); + + // Subscribe to processing state changes once + useEffect(() => { + const unsubscribe = enhancedPDFProcessingService.onProcessingChange(setProcessingStates); + return unsubscribe; + }, []); + + // Process files when activeFiles changes + useEffect(() => { + console.log('useEnhancedProcessedFiles: activeFiles changed', activeFiles.length, 'files'); + + if (activeFiles.length === 0) { + console.log('useEnhancedProcessedFiles: No active files, clearing processed cache'); + setProcessedFiles(new Map()); + // Clear any ongoing processing when no files + enhancedPDFProcessingService.clearAllProcessing(); + return; + } + + const processFiles = async () => { + const newProcessedFiles = new Map(); + + for (const file of activeFiles) { + // Generate hash for this file + const fileHash = await FileHasher.generateHybridHash(file); + fileHashMapRef.current.set(file, fileHash); + + // First, check if we have this exact File object cached + let existing = processedFiles.get(file); + + // If not found by File object, try to find by hash in case File was recreated + if (!existing) { + for (const [cachedFile, processed] of processedFiles.entries()) { + const cachedHash = fileHashMapRef.current.get(cachedFile); + if (cachedHash === fileHash) { + existing = processed; + break; + } + } + } + + if (existing) { + newProcessedFiles.set(file, existing); + continue; + } + + try { + const processed = await enhancedPDFProcessingService.processFile(file, config); + if (processed) { + newProcessedFiles.set(file, processed); + } + } catch (error) { + console.error(`Failed to start processing for ${file.name}:`, error); + } + } + + // Only update if the content actually changed + const hasChanged = newProcessedFiles.size !== processedFiles.size || + Array.from(newProcessedFiles.keys()).some(file => !processedFiles.has(file)); + + if (hasChanged) { + setProcessedFiles(newProcessedFiles); + } + }; + + processFiles(); + }, [activeFiles]); // Only depend on activeFiles to avoid infinite loops + + // Listen for processing completion + useEffect(() => { + const checkForCompletedFiles = async () => { + let hasNewFiles = false; + const updatedFiles = new Map(processedFiles); + + // Generate file keys for all files first + const fileKeyPromises = activeFiles.map(async (file) => ({ + file, + key: await FileHasher.generateHybridHash(file) + })); + + const fileKeyPairs = await Promise.all(fileKeyPromises); + + for (const { file, key } of fileKeyPairs) { + // Only check files that don't have processed results yet + if (!updatedFiles.has(file)) { + const processingState = processingStates.get(key); + + // Check for both processing and recently completed files + // This ensures we catch completed files before they're cleaned up + if (processingState?.status === 'processing' || processingState?.status === 'completed') { + try { + const processed = await enhancedPDFProcessingService.processFile(file, config); + if (processed) { + updatedFiles.set(file, processed); + hasNewFiles = true; + } + } catch (error) { + // Ignore errors in completion check + } + } + } + } + + if (hasNewFiles) { + setProcessedFiles(updatedFiles); + } + }; + + // Check every 500ms for completed processing + const interval = setInterval(checkForCompletedFiles, 500); + return () => clearInterval(interval); + }, [activeFiles, processingStates]); + + + // Cleanup when activeFiles changes + useEffect(() => { + const currentFiles = new Set(activeFiles); + const previousFiles = Array.from(processedFiles.keys()); + const removedFiles = previousFiles.filter(file => !currentFiles.has(file)); + + if (removedFiles.length > 0) { + // Clean up processing service cache + enhancedPDFProcessingService.cleanup(removedFiles); + + // Update local state + setProcessedFiles(prev => { + const updated = new Map(); + for (const [file, processed] of prev) { + if (currentFiles.has(file)) { + updated.set(file, processed); + } + } + return updated; + }); + } + }, [activeFiles]); + + // Calculate derived state + const isProcessing = processingStates.size > 0; + const hasProcessingErrors = Array.from(processingStates.values()).some(state => state.status === 'error'); + + // Calculate overall progress + const processingProgress = calculateProcessingProgress(processingStates); + + // Get cache stats and metrics + const cacheStats = enhancedPDFProcessingService.getCacheStats(); + const metrics = enhancedPDFProcessingService.getMetrics(); + + // Action handlers + const actions = { + cancelProcessing: (fileKey: string) => { + enhancedPDFProcessingService.cancelProcessing(fileKey); + }, + + retryProcessing: async (file: File) => { + try { + await enhancedPDFProcessingService.processFile(file, config); + } catch (error) { + console.error(`Failed to retry processing for ${file.name}:`, error); + } + }, + + clearCache: () => { + enhancedPDFProcessingService.clearAll(); + } + }; + + // Cleanup on unmount + useEffect(() => { + return () => { + enhancedPDFProcessingService.clearAllProcessing(); + }; + }, []); + + return { + processedFiles, + processingStates, + isProcessing, + hasProcessingErrors, + processingProgress, + cacheStats, + metrics, + actions + }; +} + +/** + * Calculate overall processing progress from individual file states + */ +function calculateProcessingProgress(states: Map): { + overall: number; + fileProgress: Map; + estimatedTimeRemaining: number; +} { + if (states.size === 0) { + return { + overall: 100, + fileProgress: new Map(), + estimatedTimeRemaining: 0 + }; + } + + const fileProgress = new Map(); + let totalProgress = 0; + let totalEstimatedTime = 0; + + for (const [fileKey, state] of states) { + fileProgress.set(fileKey, state.progress); + totalProgress += state.progress; + totalEstimatedTime += state.estimatedTimeRemaining || 0; + } + + const overall = totalProgress / states.size; + const estimatedTimeRemaining = totalEstimatedTime; + + return { + overall, + fileProgress, + estimatedTimeRemaining + }; +} + +/** + * Hook for getting a single processed file with enhanced features + */ +export function useEnhancedProcessedFile( + file: File | null, + config?: Partial +): { + processedFile: ProcessedFile | null; + isProcessing: boolean; + processingState: ProcessingState | null; + error: string | null; + canRetry: boolean; + actions: { + cancel: () => void; + retry: () => void; + }; +} { + const result = useEnhancedProcessedFiles(file ? [file] : [], config); + + const processedFile = file ? result.processedFiles.get(file) || null : null; + // Note: This is async but we can't await in hook return - consider refactoring if needed + const fileKey = file ? '' : ''; + const processingState = fileKey ? result.processingStates.get(fileKey) || null : null; + const isProcessing = !!processingState; + const error = processingState?.error?.message || null; + const canRetry = processingState?.error?.recoverable || false; + + const actions = { + cancel: () => { + if (fileKey) { + result.actions.cancelProcessing(fileKey); + } + }, + retry: () => { + if (file) { + result.actions.retryProcessing(file); + } + } + }; + + return { + processedFile, + isProcessing, + processingState, + error, + canRetry, + actions + }; +} \ No newline at end of file diff --git a/frontend/src/hooks/useFileManager.ts b/frontend/src/hooks/useFileManager.ts new file mode 100644 index 000000000..d8e776f75 --- /dev/null +++ b/frontend/src/hooks/useFileManager.ts @@ -0,0 +1,122 @@ +import { useState, useCallback } from 'react'; +import { fileStorage } from '../services/fileStorage'; +import { FileWithUrl } from '../types/file'; + +export const useFileManager = () => { + const [loading, setLoading] = useState(false); + + const convertToFile = useCallback(async (fileWithUrl: FileWithUrl): Promise => { + if (fileWithUrl.url && fileWithUrl.url.startsWith('blob:')) { + const response = await fetch(fileWithUrl.url); + const data = await response.arrayBuffer(); + const file = new File([data], fileWithUrl.name, { + type: fileWithUrl.type || 'application/pdf', + lastModified: fileWithUrl.lastModified || Date.now() + }); + // Preserve the ID if it exists + if (fileWithUrl.id) { + Object.defineProperty(file, 'id', { value: fileWithUrl.id, writable: false }); + } + return file; + } + + // Always use ID first, fallback to name only if ID doesn't exist + const lookupKey = fileWithUrl.id || fileWithUrl.name; + const storedFile = await fileStorage.getFile(lookupKey); + if (storedFile) { + const file = new File([storedFile.data], storedFile.name, { + type: storedFile.type, + lastModified: storedFile.lastModified + }); + // Add the ID to the file object + Object.defineProperty(file, 'id', { value: storedFile.id, writable: false }); + return file; + } + + throw new Error('File not found in storage'); + }, []); + + const loadRecentFiles = useCallback(async (): Promise => { + setLoading(true); + try { + const files = await fileStorage.getAllFiles(); + const sortedFiles = files.sort((a, b) => (b.lastModified || 0) - (a.lastModified || 0)); + return sortedFiles; + } catch (error) { + console.error('Failed to load recent files:', error); + return []; + } finally { + setLoading(false); + } + }, []); + + const handleRemoveFile = useCallback(async (index: number, files: FileWithUrl[], setFiles: (files: FileWithUrl[]) => void) => { + const file = files[index]; + try { + await fileStorage.deleteFile(file.id || file.name); + setFiles(files.filter((_, i) => i !== index)); + } catch (error) { + console.error('Failed to remove file:', error); + throw error; + } + }, []); + + const storeFile = useCallback(async (file: File) => { + try { + const storedFile = await fileStorage.storeFile(file); + // Add the ID to the file object + Object.defineProperty(file, 'id', { value: storedFile.id, writable: false }); + return storedFile; + } catch (error) { + console.error('Failed to store file:', error); + throw error; + } + }, []); + + const createFileSelectionHandlers = useCallback(( + selectedFiles: string[], + setSelectedFiles: (files: string[]) => void + ) => { + const toggleSelection = (fileId: string) => { + setSelectedFiles( + selectedFiles.includes(fileId) + ? selectedFiles.filter(id => id !== fileId) + : [...selectedFiles, fileId] + ); + }; + + const clearSelection = () => { + setSelectedFiles([]); + }; + + const selectMultipleFiles = async (files: FileWithUrl[], onFilesSelect: (files: File[]) => void) => { + if (selectedFiles.length === 0) return; + + try { + const selectedFileObjects = files.filter(f => selectedFiles.includes(f.id || f.name)); + const filePromises = selectedFileObjects.map(convertToFile); + const convertedFiles = await Promise.all(filePromises); + onFilesSelect(convertedFiles); + clearSelection(); + } catch (error) { + console.error('Failed to load selected files:', error); + throw error; + } + }; + + return { + toggleSelection, + clearSelection, + selectMultipleFiles + }; + }, [convertToFile]); + + return { + loading, + convertToFile, + loadRecentFiles, + handleRemoveFile, + storeFile, + createFileSelectionHandlers + }; +}; \ No newline at end of file diff --git a/frontend/src/hooks/useMemoryManagement.ts b/frontend/src/hooks/useMemoryManagement.ts new file mode 100644 index 000000000..d27e5ed56 --- /dev/null +++ b/frontend/src/hooks/useMemoryManagement.ts @@ -0,0 +1,30 @@ +import { useCallback } from 'react'; +import { useFileContext } from '../contexts/FileContext'; + +/** + * Hook for components that need to register resources with centralized memory management + */ +export function useMemoryManagement() { + const { trackBlobUrl, trackPdfDocument, scheduleCleanup } = useFileContext(); + + const registerBlobUrl = useCallback((url: string) => { + trackBlobUrl(url); + return url; + }, [trackBlobUrl]); + + const registerPdfDocument = useCallback((fileId: string, pdfDoc: any) => { + trackPdfDocument(fileId, pdfDoc); + return pdfDoc; + }, [trackPdfDocument]); + + const cancelCleanup = useCallback((fileId: string) => { + // Cancel scheduled cleanup (user is actively using the file) + scheduleCleanup(fileId, -1); // -1 cancels the timer + }, [scheduleCleanup]); + + return { + registerBlobUrl, + registerPdfDocument, + cancelCleanup + }; +} \ No newline at end of file diff --git a/frontend/src/hooks/usePDFProcessor.ts b/frontend/src/hooks/usePDFProcessor.ts index 7b1cc0c4b..0a717a3a9 100644 --- a/frontend/src/hooks/usePDFProcessor.ts +++ b/frontend/src/hooks/usePDFProcessor.ts @@ -50,18 +50,28 @@ export function usePDFProcessor() { const pages: PDFPage[] = []; - // Generate thumbnails for all pages + // Create pages without thumbnails initially - load them lazily for (let i = 1; i <= totalPages; i++) { - const thumbnail = await generatePageThumbnail(file, i); pages.push({ id: `${file.name}-page-${i}`, pageNumber: i, - thumbnail, + thumbnail: null, // Will be loaded lazily rotation: 0, selected: false }); } + // Generate thumbnails for first 10 pages immediately for better UX + const priorityPages = Math.min(10, totalPages); + for (let i = 1; i <= priorityPages; i++) { + try { + const thumbnail = await generatePageThumbnail(file, i); + pages[i - 1].thumbnail = thumbnail; + } catch (error) { + console.warn(`Failed to generate thumbnail for page ${i}:`, error); + } + } + // Clean up pdf.destroy(); diff --git a/frontend/src/hooks/useProcessedFiles.ts b/frontend/src/hooks/useProcessedFiles.ts new file mode 100644 index 000000000..a7db9b07e --- /dev/null +++ b/frontend/src/hooks/useProcessedFiles.ts @@ -0,0 +1,125 @@ +import { useState, useEffect } from 'react'; +import { ProcessedFile, ProcessingState } from '../types/processing'; +import { pdfProcessingService } from '../services/pdfProcessingService'; + +interface UseProcessedFilesResult { + processedFiles: Map; + processingStates: Map; + isProcessing: boolean; + hasProcessingErrors: boolean; + cacheStats: { + entries: number; + totalSizeBytes: number; + maxSizeBytes: number; + }; +} + +export function useProcessedFiles(activeFiles: File[]): UseProcessedFilesResult { + const [processedFiles, setProcessedFiles] = useState>(new Map()); + const [processingStates, setProcessingStates] = useState>(new Map()); + + useEffect(() => { + // Subscribe to processing state changes + const unsubscribe = pdfProcessingService.onProcessingChange(setProcessingStates); + + // Check/start processing for each active file + const checkProcessing = async () => { + const newProcessedFiles = new Map(); + + for (const file of activeFiles) { + const processed = await pdfProcessingService.getProcessedFile(file); + if (processed) { + newProcessedFiles.set(file, processed); + } + } + + setProcessedFiles(newProcessedFiles); + }; + + checkProcessing(); + + return unsubscribe; + }, [activeFiles]); + + // Listen for processing completion and update processed files + useEffect(() => { + const updateProcessedFiles = async () => { + const updated = new Map(); + + for (const file of activeFiles) { + const existing = processedFiles.get(file); + if (existing) { + updated.set(file, existing); + } else { + // Check if processing just completed + const processed = await pdfProcessingService.getProcessedFile(file); + if (processed) { + updated.set(file, processed); + } + } + } + + setProcessedFiles(updated); + }; + + // Small delay to allow processing state to settle + const timeoutId = setTimeout(updateProcessedFiles, 100); + return () => clearTimeout(timeoutId); + }, [processingStates, activeFiles]); + + // Cleanup when activeFiles changes + useEffect(() => { + const currentFiles = new Set(activeFiles); + const previousFiles = Array.from(processedFiles.keys()); + const removedFiles = previousFiles.filter(file => !currentFiles.has(file)); + + if (removedFiles.length > 0) { + // Clean up processing service cache + pdfProcessingService.cleanup(removedFiles); + + // Update local state + setProcessedFiles(prev => { + const updated = new Map(); + for (const [file, processed] of prev) { + if (currentFiles.has(file)) { + updated.set(file, processed); + } + } + return updated; + }); + } + }, [activeFiles]); + + // Derived state + const isProcessing = processingStates.size > 0; + const hasProcessingErrors = Array.from(processingStates.values()).some(state => state.status === 'error'); + const cacheStats = pdfProcessingService.getCacheStats(); + + return { + processedFiles, + processingStates, + isProcessing, + hasProcessingErrors, + cacheStats + }; +} + +// Hook for getting a single processed file +export function useProcessedFile(file: File | null): { + processedFile: ProcessedFile | null; + isProcessing: boolean; + processingState: ProcessingState | null; +} { + const result = useProcessedFiles(file ? [file] : []); + + const processedFile = file ? result.processedFiles.get(file) || null : null; + const fileKey = file ? pdfProcessingService.generateFileKey(file) : ''; + const processingState = fileKey ? result.processingStates.get(fileKey) || null : null; + const isProcessing = !!processingState; + + return { + processedFile, + isProcessing, + processingState + }; +} \ No newline at end of file diff --git a/frontend/src/hooks/useThumbnailGeneration.ts b/frontend/src/hooks/useThumbnailGeneration.ts new file mode 100644 index 000000000..2d1138401 --- /dev/null +++ b/frontend/src/hooks/useThumbnailGeneration.ts @@ -0,0 +1,56 @@ +import { useCallback } from 'react'; +import { thumbnailGenerationService } from '../services/thumbnailGenerationService'; + +/** + * Hook for tools that want to use thumbnail generation + * Tools can choose whether to include visual features + */ +export function useThumbnailGeneration() { + const generateThumbnails = useCallback(async ( + pdfArrayBuffer: ArrayBuffer, + pageNumbers: number[], + options: { + scale?: number; + quality?: number; + batchSize?: number; + parallelBatches?: number; + } = {}, + onProgress?: (progress: { completed: number; total: number; thumbnails: any[] }) => void + ) => { + return thumbnailGenerationService.generateThumbnails( + pdfArrayBuffer, + pageNumbers, + options, + onProgress + ); + }, []); + + const addThumbnailToCache = useCallback((pageId: string, thumbnail: string) => { + thumbnailGenerationService.addThumbnailToCache(pageId, thumbnail); + }, []); + + const getThumbnailFromCache = useCallback((pageId: string): string | null => { + return thumbnailGenerationService.getThumbnailFromCache(pageId); + }, []); + + const getCacheStats = useCallback(() => { + return thumbnailGenerationService.getCacheStats(); + }, []); + + const stopGeneration = useCallback(() => { + thumbnailGenerationService.stopGeneration(); + }, []); + + const destroyThumbnails = useCallback(() => { + thumbnailGenerationService.destroy(); + }, []); + + return { + generateThumbnails, + addThumbnailToCache, + getThumbnailFromCache, + getCacheStats, + stopGeneration, + destroyThumbnails + }; +} \ No newline at end of file diff --git a/frontend/src/hooks/useToolManagement.tsx b/frontend/src/hooks/useToolManagement.tsx new file mode 100644 index 000000000..31e63e93d --- /dev/null +++ b/frontend/src/hooks/useToolManagement.tsx @@ -0,0 +1,96 @@ +import React, { useState, useCallback, useMemo, useEffect } from 'react'; +import { useTranslation } from 'react-i18next'; +import AddToPhotosIcon from "@mui/icons-material/AddToPhotos"; +import ContentCutIcon from "@mui/icons-material/ContentCut"; +import ZoomInMapIcon from "@mui/icons-material/ZoomInMap"; +import SplitPdfPanel from "../tools/Split"; +import CompressPdfPanel from "../tools/Compress"; +import MergePdfPanel from "../tools/Merge"; +import { useMultipleEndpointsEnabled } from "./useEndpointConfig"; + +type ToolRegistryEntry = { + icon: React.ReactNode; + name: string; + component: React.ComponentType; + view: string; +}; + +type ToolRegistry = { + [key: string]: ToolRegistryEntry; +}; + +const baseToolRegistry = { + split: { icon: , component: SplitPdfPanel, view: "split" }, + compress: { icon: , component: CompressPdfPanel, view: "viewer" }, + merge: { icon: , component: MergePdfPanel, view: "pageEditor" }, +}; + +// Tool endpoint mappings +const toolEndpoints: Record = { + split: ["split-pages", "split-pdf-by-sections", "split-by-size-or-count", "split-pdf-by-chapters"], + compress: ["compress-pdf"], + merge: ["merge-pdfs"], +}; + + +export const useToolManagement = () => { + const { t } = useTranslation(); + + const [selectedToolKey, setSelectedToolKey] = useState(null); + const [toolSelectedFileIds, setToolSelectedFileIds] = useState([]); + + const allEndpoints = Array.from(new Set(Object.values(toolEndpoints).flat())); + const { endpointStatus, loading: endpointsLoading } = useMultipleEndpointsEnabled(allEndpoints); + + const isToolAvailable = useCallback((toolKey: string): boolean => { + if (endpointsLoading) return true; + const endpoints = toolEndpoints[toolKey] || []; + return endpoints.some(endpoint => endpointStatus[endpoint] === true); + }, [endpointsLoading, endpointStatus]); + + const toolRegistry: ToolRegistry = useMemo(() => { + const availableToolRegistry: ToolRegistry = {}; + Object.keys(baseToolRegistry).forEach(toolKey => { + if (isToolAvailable(toolKey)) { + availableToolRegistry[toolKey] = { + ...baseToolRegistry[toolKey as keyof typeof baseToolRegistry], + name: t(`home.${toolKey}.title`, toolKey.charAt(0).toUpperCase() + toolKey.slice(1)) + }; + } + }); + return availableToolRegistry; + }, [t, isToolAvailable]); + + useEffect(() => { + if (!endpointsLoading && selectedToolKey && !toolRegistry[selectedToolKey]) { + const firstAvailableTool = Object.keys(toolRegistry)[0]; + if (firstAvailableTool) { + setSelectedToolKey(firstAvailableTool); + } else { + setSelectedToolKey(null); + } + } + }, [endpointsLoading, selectedToolKey, toolRegistry]); + + const selectTool = useCallback((toolKey: string) => { + setSelectedToolKey(toolKey); + }, []); + + const clearToolSelection = useCallback(() => { + setSelectedToolKey(null); + }, []); + + const selectedTool = selectedToolKey ? toolRegistry[selectedToolKey] : null; + + return { + selectedToolKey, + selectedTool, + toolSelectedFileIds, + toolRegistry, + + selectTool, + clearToolSelection, + setToolSelectedFileIds, + + }; +}; diff --git a/frontend/src/hooks/useToolParameters.ts b/frontend/src/hooks/useToolParameters.ts new file mode 100644 index 000000000..d6eae6d8b --- /dev/null +++ b/frontend/src/hooks/useToolParameters.ts @@ -0,0 +1,51 @@ +/** + * React hooks for tool parameter management (URL logic removed) + */ + +import { useCallback, useMemo } from 'react'; + +type ToolParameterValues = Record; + +/** + * Register tool parameters and get current values + */ +export function useToolParameters( + toolName: string, + parameters: Record +): [ToolParameterValues, (updates: Partial) => void] { + + // Return empty values and noop updater + const currentValues = useMemo(() => ({}), []); + const updateParameters = useCallback(() => {}, []); + + return [currentValues, updateParameters]; +} + +/** + * Hook for managing a single tool parameter + */ +export function useToolParameter( + toolName: string, + paramName: string, + definition: any +): [T, (value: T) => void] { + const [allParams, updateParams] = useToolParameters(toolName, { [paramName]: definition }); + + const value = allParams[paramName] as T; + + const setValue = useCallback((newValue: T) => { + updateParams({ [paramName]: newValue }); + }, [paramName, updateParams]); + + return [value, setValue]; +} + +/** + * Hook for getting/setting global parameters (zoom, page, etc.) + */ +export function useGlobalParameters() { + const currentValues = useMemo(() => ({}), []); + const updateParameters = useCallback(() => {}, []); + + return [currentValues, updateParameters]; +} \ No newline at end of file diff --git a/frontend/src/hooks/useToolParams.ts b/frontend/src/hooks/useToolParams.ts deleted file mode 100644 index 9c422da14..000000000 --- a/frontend/src/hooks/useToolParams.ts +++ /dev/null @@ -1,130 +0,0 @@ -import { useSearchParams } from "react-router-dom"; -import { useEffect } from "react"; - -// Tool parameter definitions (shortened URLs) -const TOOL_PARAMS = { - split: [ - "mode", "p", "hd", "vd", "m", - "type", "val", "level", "meta", "dupes" - ], - compress: [ - "level", "gray", "rmeta", "size", "agg" - ], - merge: [ - "order", "rdupes" - ] -}; - -// Extract params for a specific tool from URL -function getToolParams(toolKey: string, searchParams: URLSearchParams) { - switch (toolKey) { - case "split": - return { - mode: searchParams.get("mode") || "byPages", - pages: searchParams.get("p") || "", - hDiv: searchParams.get("hd") || "", - vDiv: searchParams.get("vd") || "", - merge: searchParams.get("m") === "true", - splitType: searchParams.get("type") || "size", - splitValue: searchParams.get("val") || "", - bookmarkLevel: searchParams.get("level") || "0", - includeMetadata: searchParams.get("meta") === "true", - allowDuplicates: searchParams.get("dupes") === "true", - }; - case "compress": - return { - compressionLevel: parseInt(searchParams.get("level") || "5"), - grayscale: searchParams.get("gray") === "true", - removeMetadata: searchParams.get("rmeta") === "true", - expectedSize: searchParams.get("size") || "", - aggressive: searchParams.get("agg") === "true", - }; - case "merge": - return { - order: searchParams.get("order") || "default", - removeDuplicates: searchParams.get("rdupes") === "true", - }; - default: - return {}; - } -} - -// Update tool-specific params in URL -function updateToolParams(toolKey: string, searchParams: URLSearchParams, setSearchParams: any, newParams: any) { - const params = new URLSearchParams(searchParams); - - // Clear tool-specific params - if (toolKey === "split") { - ["mode", "p", "hd", "vd", "m", "type", "val", "level", "meta", "dupes"].forEach((k) => params.delete(k)); - // Set new split params - const merged = { ...getToolParams("split", searchParams), ...newParams }; - params.set("mode", merged.mode); - if (merged.mode === "byPages") params.set("p", merged.pages); - else if (merged.mode === "bySections") { - params.set("hd", merged.hDiv); - params.set("vd", merged.vDiv); - params.set("m", String(merged.merge)); - } else if (merged.mode === "bySizeOrCount") { - params.set("type", merged.splitType); - params.set("val", merged.splitValue); - } else if (merged.mode === "byChapters") { - params.set("level", merged.bookmarkLevel); - params.set("meta", String(merged.includeMetadata)); - params.set("dupes", String(merged.allowDuplicates)); - } - } else if (toolKey === "compress") { - ["level", "gray", "rmeta", "size", "agg"].forEach((k) => params.delete(k)); - const merged = { ...getToolParams("compress", searchParams), ...newParams }; - params.set("level", String(merged.compressionLevel)); - params.set("gray", String(merged.grayscale)); - params.set("rmeta", String(merged.removeMetadata)); - if (merged.expectedSize) params.set("size", merged.expectedSize); - params.set("agg", String(merged.aggressive)); - } else if (toolKey === "merge") { - ["order", "rdupes"].forEach((k) => params.delete(k)); - const merged = { ...getToolParams("merge", searchParams), ...newParams }; - params.set("order", merged.order); - params.set("rdupes", String(merged.removeDuplicates)); - } - - setSearchParams(params, { replace: true }); -} - -export function useToolParams(selectedToolKey: string, currentView: string) { - const [searchParams, setSearchParams] = useSearchParams(); - - const toolParams = getToolParams(selectedToolKey, searchParams); - - const updateParams = (newParams: any) => - updateToolParams(selectedToolKey, searchParams, setSearchParams, newParams); - - // Update URL when core state changes - useEffect(() => { - const params = new URLSearchParams(searchParams); - - // Remove all tool-specific params except for the current tool - Object.entries(TOOL_PARAMS).forEach(([tool, keys]) => { - if (tool !== selectedToolKey) { - keys.forEach((k) => params.delete(k)); - } - }); - - // Collect all params except 'v' - const entries = Array.from(params.entries()).filter(([key]) => key !== "v"); - - // Rebuild params with 'v' first - const newParams = new URLSearchParams(); - newParams.set("v", currentView); - newParams.set("t", selectedToolKey); - entries.forEach(([key, value]) => { - if (key !== "t") newParams.set(key, value); - }); - - setSearchParams(newParams, { replace: true }); - }, [selectedToolKey, currentView, setSearchParams]); - - return { - toolParams, - updateParams, - }; -} \ No newline at end of file diff --git a/frontend/src/pages/HomePage.tsx b/frontend/src/pages/HomePage.tsx index a6b5277f6..f858a5db9 100644 --- a/frontend/src/pages/HomePage.tsx +++ b/frontend/src/pages/HomePage.tsx @@ -1,322 +1,83 @@ -import React, { useState, useCallback, useEffect } from "react"; +import React, { useState, useCallback} from "react"; import { useTranslation } from 'react-i18next'; -import { useSearchParams } from "react-router-dom"; -import { useToolParams } from "../hooks/useToolParams"; -import { useFileWithUrl } from "../hooks/useFileWithUrl"; -import { fileStorage } from "../services/fileStorage"; -import AddToPhotosIcon from "@mui/icons-material/AddToPhotos"; -import ContentCutIcon from "@mui/icons-material/ContentCut"; -import ZoomInMapIcon from "@mui/icons-material/ZoomInMap"; -import { Group, Paper, Box, Button, useMantineTheme, Container } from "@mantine/core"; +import { useFileContext } from "../contexts/FileContext"; +import { useToolManagement } from "../hooks/useToolManagement"; +import { Group, Box, Button, Container } from "@mantine/core"; import { useRainbowThemeContext } from "../components/shared/RainbowThemeProvider"; import rainbowStyles from '../styles/rainbow.module.css'; import ToolPicker from "../components/tools/ToolPicker"; import TopControls from "../components/shared/TopControls"; -import FileManager from "../components/fileManagement/FileManager"; -import FileEditor from "../components/pageEditor/FileEditor"; +import FileEditor from "../components/fileEditor/FileEditor"; import PageEditor from "../components/pageEditor/PageEditor"; import PageEditorControls from "../components/pageEditor/PageEditorControls"; import Viewer from "../components/viewer/Viewer"; import FileUploadSelector from "../components/shared/FileUploadSelector"; -import SplitPdfPanel from "../tools/Split"; -import CompressPdfPanel from "../tools/Compress"; -import MergePdfPanel from "../tools/Merge"; import ToolRenderer from "../components/tools/ToolRenderer"; import QuickAccessBar from "../components/shared/QuickAccessBar"; import { useMultipleEndpointsEnabled } from "../hooks/useEndpointConfig"; -type ToolRegistryEntry = { - icon: React.ReactNode; - name: string; - component: React.ComponentType; - view: string; -}; - -type ToolRegistry = { - [key: string]: ToolRegistryEntry; -}; - -// Base tool registry without translations -const baseToolRegistry = { - split: { icon: , component: SplitPdfPanel, view: "viewer" }, - compress: { icon: , component: CompressPdfPanel, view: "viewer" }, - merge: { icon: , component: MergePdfPanel, view: "fileManager" }, -}; - -// Tool endpoint mappings -const toolEndpoints: Record = { - split: ["split-pages", "split-pdf-by-sections", "split-by-size-or-count", "split-pdf-by-chapters"], - compress: ["compress-pdf"], - merge: ["merge-pdfs"], -}; - export default function HomePage() { const { t } = useTranslation(); - const [searchParams] = useSearchParams(); - const theme = useMantineTheme(); const { isRainbowMode } = useRainbowThemeContext(); - // Core app state - const [selectedToolKey, setSelectedToolKey] = useState(searchParams.get("t") || "split"); - const [currentView, setCurrentView] = useState(searchParams.get("v") || "viewer"); + // Get file context + const fileContext = useFileContext(); + const { activeFiles, currentView, currentMode, setCurrentView, addFiles } = fileContext; - // File state separation - const [storedFiles, setStoredFiles] = useState([]); // IndexedDB files (FileManager) - const [activeFiles, setActiveFiles] = useState([]); // Active working set (persisted) - const [preSelectedFiles, setPreSelectedFiles] = useState([]); + const { + selectedToolKey, + selectedTool, + toolParams, + toolRegistry, + selectTool, + clearToolSelection, + updateToolParams, + } = useToolManagement(); - const [downloadUrl, setDownloadUrl] = useState(null); + const [toolSelectedFiles, setToolSelectedFiles] = useState([]); const [sidebarsVisible, setSidebarsVisible] = useState(true); const [leftPanelView, setLeftPanelView] = useState<'toolPicker' | 'toolContent'>('toolPicker'); const [readerMode, setReaderMode] = useState(false); - - // Page editor functions const [pageEditorFunctions, setPageEditorFunctions] = useState(null); + const [previewFile, setPreviewFile] = useState(null); - // URL parameter management - const { toolParams, updateParams } = useToolParams(selectedToolKey, currentView); - // Get all unique endpoints for batch checking - const allEndpoints = Array.from(new Set(Object.values(toolEndpoints).flat())); - const { endpointStatus, loading: endpointsLoading } = useMultipleEndpointsEnabled(allEndpoints); - // Persist active files across reloads - useEffect(() => { - // Save active files to localStorage (just metadata) - const activeFileData = activeFiles.map(file => ({ - name: file.name, - size: file.size, - type: file.type, - lastModified: file.lastModified - })); - localStorage.setItem('activeFiles', JSON.stringify(activeFileData)); - }, [activeFiles]); - // Load stored files from IndexedDB on mount - useEffect(() => { - const loadStoredFiles = async () => { - try { - const files = await fileStorage.getAllFiles(); - setStoredFiles(files); - } catch (error) { - console.warn('Failed to load stored files:', error); - } - }; - loadStoredFiles(); - }, []); - // Restore active files on load - useEffect(() => { - const restoreActiveFiles = async () => { - try { - const savedFileData = JSON.parse(localStorage.getItem('activeFiles') || '[]'); - if (savedFileData.length > 0) { - // TODO: Reconstruct files from IndexedDB when fileStorage is available - console.log('Would restore active files:', savedFileData); - } - } catch (error) { - console.warn('Failed to restore active files:', error); - } - }; - restoreActiveFiles(); - }, []); - - // Helper function to check if a tool is available - const isToolAvailable = (toolKey: string): boolean => { - if (endpointsLoading) return true; // Show tools while loading - const endpoints = toolEndpoints[toolKey] || []; - // Tool is available if at least one of its endpoints is enabled - return endpoints.some(endpoint => endpointStatus[endpoint] === true); - }; - - // Filter tool registry to only show available tools - const availableToolRegistry: ToolRegistry = {}; - Object.keys(baseToolRegistry).forEach(toolKey => { - if (isToolAvailable(toolKey)) { - availableToolRegistry[toolKey] = { - ...baseToolRegistry[toolKey as keyof typeof baseToolRegistry], - name: t(`home.${toolKey}.title`, toolKey.charAt(0).toUpperCase() + toolKey.slice(1)) - }; - } - }); - - const toolRegistry = availableToolRegistry; - - // Handle case where selected tool becomes unavailable - useEffect(() => { - if (!endpointsLoading && selectedToolKey && !toolRegistry[selectedToolKey]) { - // If current tool is not available, select the first available tool - const firstAvailableTool = Object.keys(toolRegistry)[0]; - if (firstAvailableTool) { - setSelectedToolKey(firstAvailableTool); - if (toolRegistry[firstAvailableTool]?.view) { - setCurrentView(toolRegistry[firstAvailableTool].view); - } - } - } - }, [endpointsLoading, selectedToolKey, toolRegistry]); - - // Handle tool selection const handleToolSelect = useCallback( (id: string) => { - setSelectedToolKey(id); + selectTool(id); if (toolRegistry[id]?.view) setCurrentView(toolRegistry[id].view); - setLeftPanelView('toolContent'); // Switch to tool content view when a tool is selected - setReaderMode(false); // Exit reader mode when selecting a tool + setLeftPanelView('toolContent'); + setReaderMode(false); }, - [toolRegistry] + [selectTool, toolRegistry, setCurrentView] ); - // Handle quick access actions const handleQuickAccessTools = useCallback(() => { setLeftPanelView('toolPicker'); setReaderMode(false); - }, []); + clearToolSelection(); + }, [clearToolSelection]); const handleReaderToggle = useCallback(() => { setReaderMode(!readerMode); }, [readerMode]); - // Update URL when view changes const handleViewChange = useCallback((view: string) => { - setCurrentView(view); - const params = new URLSearchParams(window.location.search); - params.set('view', view); - const newUrl = `${window.location.pathname}?${params.toString()}`; - window.history.replaceState({}, '', newUrl); - }, []); + setCurrentView(view as any); + }, [setCurrentView]); - // Active file management - const addToActiveFiles = useCallback((file: File) => { - setActiveFiles(prev => { - // Avoid duplicates based on name and size - const exists = prev.some(f => f.name === file.name && f.size === file.size); - if (exists) return prev; - return [file, ...prev]; - }); - }, []); - - const removeFromActiveFiles = useCallback((file: File) => { - setActiveFiles(prev => prev.filter(f => !(f.name === file.name && f.size === file.size))); - }, []); - - const setCurrentActiveFile = useCallback((file: File) => { - setActiveFiles(prev => { - const filtered = prev.filter(f => !(f.name === file.name && f.size === file.size)); - return [file, ...filtered]; - }); - }, []); - - // Handle file selection from upload (adds to active files) - const handleFileSelect = useCallback((file: File) => { - addToActiveFiles(file); - }, [addToActiveFiles]); - - // Handle opening file editor with selected files - const handleOpenFileEditor = useCallback(async (selectedFiles) => { - if (!selectedFiles || selectedFiles.length === 0) { - setPreSelectedFiles([]); - handleViewChange("fileEditor"); - return; + const addToActiveFiles = useCallback(async (file: File) => { + const exists = activeFiles.some(f => f.name === file.name && f.size === file.size); + if (!exists) { + await addFiles([file]); } + }, [activeFiles, addFiles]); - // Convert FileWithUrl[] to File[] and add to activeFiles - try { - const convertedFiles = await Promise.all( - selectedFiles.map(async (fileItem) => { - // If it's already a File, return as is - if (fileItem instanceof File) { - return fileItem; - } - // If it has a file property, use that - if (fileItem.file && fileItem.file instanceof File) { - return fileItem.file; - } - - // If it's from IndexedDB storage, reconstruct the File - if (fileItem.arrayBuffer && typeof fileItem.arrayBuffer === 'function') { - const arrayBuffer = await fileItem.arrayBuffer(); - const blob = new Blob([arrayBuffer], { type: fileItem.type || 'application/pdf' }); - const file = new File([blob], fileItem.name, { - type: fileItem.type || 'application/pdf', - lastModified: fileItem.lastModified || Date.now() - }); - // Mark as from storage to avoid re-storing - (file as any).storedInIndexedDB = true; - return file; - } - - console.warn('Could not convert file item:', fileItem); - return null; - }) - ); - - // Filter out nulls and add to activeFiles - const validFiles = convertedFiles.filter((f): f is File => f !== null); - setActiveFiles(validFiles); - setPreSelectedFiles([]); // Clear preselected since we're using activeFiles now - handleViewChange("fileEditor"); - } catch (error) { - console.error('Error converting selected files:', error); - } - }, [handleViewChange, setActiveFiles]); - - // Handle opening page editor with selected files - const handleOpenPageEditor = useCallback(async (selectedFiles) => { - if (!selectedFiles || selectedFiles.length === 0) { - handleViewChange("pageEditor"); - return; - } - - // Convert FileWithUrl[] to File[] and add to activeFiles - try { - const convertedFiles = await Promise.all( - selectedFiles.map(async (fileItem) => { - // If it's already a File, return as is - if (fileItem instanceof File) { - return fileItem; - } - - // If it has a file property, use that - if (fileItem.file && fileItem.file instanceof File) { - return fileItem.file; - } - - // If it's from IndexedDB storage, reconstruct the File - if (fileItem.arrayBuffer && typeof fileItem.arrayBuffer === 'function') { - const arrayBuffer = await fileItem.arrayBuffer(); - const blob = new Blob([arrayBuffer], { type: fileItem.type || 'application/pdf' }); - const file = new File([blob], fileItem.name, { - type: fileItem.type || 'application/pdf', - lastModified: fileItem.lastModified || Date.now() - }); - // Mark as from storage to avoid re-storing - (file as any).storedInIndexedDB = true; - return file; - } - - console.warn('Could not convert file item:', fileItem); - return null; - }) - ); - - // Filter out nulls and add to activeFiles - const validFiles = convertedFiles.filter((f): f is File => f !== null); - setActiveFiles(validFiles); - handleViewChange("pageEditor"); - } catch (error) { - console.error('Error converting selected files for page editor:', error); - } - }, [handleViewChange, setActiveFiles]); - - const selectedTool = toolRegistry[selectedToolKey]; - - // For Viewer - convert first active file to expected format (only when needed) - const currentFileWithUrl = useFileWithUrl( - (currentView === "viewer" && activeFiles[0]) ? activeFiles[0] : null - ); return ( - {/* Left: Tool Picker OR Selected Tool Panel */} + {/* Left: Tool Picker or Selected Tool Panel */}
setLeftPanelView('toolPicker')} + onClick={handleQuickAccessTools} className="text-sm" > ← {t("fileUpload.backToTools", "Back to Tools")} @@ -389,13 +145,8 @@ export default function HomePage() {
@@ -414,19 +165,16 @@ export default function HomePage() { {/* Main content area */} - - {currentView === "fileManager" ? ( - - ) : (currentView != "fileManager") && !activeFiles[0] ? ( + + {!activeFiles[0] ? ( { addToActiveFiles(file); }} - allowMultiple={false} + onFilesSelect={(files) => { + files.forEach(addToActiveFiles); + }} accept={["application/pdf"]} loading={false} + showRecentFiles={true} + maxRecentFiles={8} /> ) : currentView === "fileEditor" ? ( setPreSelectedFiles([])} onOpenPageEditor={(file) => { - setCurrentActiveFile(file); handleViewChange("pageEditor"); }} onMergeFiles={(filesToMerge) => { @@ -461,28 +207,30 @@ export default function HomePage() { /> ) : currentView === "viewer" ? ( { - if (fileObj) { - setCurrentActiveFile(fileObj.file); - } else { - setActiveFiles([]); - } - }} sidebarsVisible={sidebarsVisible} setSidebarsVisible={setSidebarsVisible} + previewFile={previewFile} + {...(previewFile && { + onClose: () => { + setPreviewFile(null); // Clear preview file + const previousMode = sessionStorage.getItem('previousMode'); + if (previousMode === 'split') { + selectTool('split'); + setCurrentView('split'); + setLeftPanelView('toolContent'); + sessionStorage.removeItem('previousMode'); + } else { + setCurrentView('fileEditor'); + } + } + })} /> ) : currentView === "pageEditor" ? ( <> - {activeFiles[0] && pageEditorFunctions && ( + {pageEditorFunctions && ( )} - ) : ( - { + setToolSelectedFiles(files); + }} /> + ) : selectedToolKey && selectedTool ? ( + + ) : ( + + { + addToActiveFiles(file); + }} + onFilesSelect={(files) => { + files.forEach(addToActiveFiles); + }} + accept={["application/pdf"]} + loading={false} + showRecentFiles={true} + maxRecentFiles={8} + /> + )} diff --git a/frontend/src/services/enhancedPDFProcessingService.ts b/frontend/src/services/enhancedPDFProcessingService.ts new file mode 100644 index 000000000..ea825e353 --- /dev/null +++ b/frontend/src/services/enhancedPDFProcessingService.ts @@ -0,0 +1,546 @@ +import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist'; +import { ProcessedFile, ProcessingState, PDFPage, ProcessingStrategy, ProcessingConfig, ProcessingMetrics } from '../types/processing'; +import { ProcessingCache } from './processingCache'; +import { FileHasher } from '../utils/fileHash'; +import { FileAnalyzer } from './fileAnalyzer'; +import { ProcessingErrorHandler } from './processingErrorHandler'; + +// Set up PDF.js worker +GlobalWorkerOptions.workerSrc = '/pdf.worker.js'; + +export class EnhancedPDFProcessingService { + private static instance: EnhancedPDFProcessingService; + private cache = new ProcessingCache(); + private processing = new Map(); + private processingListeners = new Set<(states: Map) => void>(); + private metrics: ProcessingMetrics = { + totalFiles: 0, + completedFiles: 0, + failedFiles: 0, + averageProcessingTime: 0, + cacheHitRate: 0, + memoryUsage: 0 + }; + + private defaultConfig: ProcessingConfig = { + strategy: 'immediate_full', + chunkSize: 20, + thumbnailQuality: 'medium', + priorityPageCount: 10, + useWebWorker: false, + maxRetries: 3, + timeoutMs: 300000 // 5 minutes + }; + + private constructor() {} + + static getInstance(): EnhancedPDFProcessingService { + if (!EnhancedPDFProcessingService.instance) { + EnhancedPDFProcessingService.instance = new EnhancedPDFProcessingService(); + } + return EnhancedPDFProcessingService.instance; + } + + /** + * Process a file with intelligent strategy selection + */ + async processFile(file: File, customConfig?: Partial): Promise { + const fileKey = await this.generateFileKey(file); + + // Check cache first + const cached = this.cache.get(fileKey); + if (cached) { + this.updateMetrics('cacheHit'); + return cached; + } + + // Check if already processing + if (this.processing.has(fileKey)) { + return null; + } + + // Analyze file to determine optimal strategy + const analysis = await FileAnalyzer.analyzeFile(file); + if (analysis.isCorrupted) { + throw new Error(`File ${file.name} appears to be corrupted`); + } + + // Create processing config + const config: ProcessingConfig = { + ...this.defaultConfig, + strategy: analysis.recommendedStrategy, + ...customConfig + }; + + // Start processing + this.startProcessing(file, fileKey, config, analysis.estimatedProcessingTime); + return null; + } + + /** + * Start processing a file with the specified configuration + */ + private async startProcessing( + file: File, + fileKey: string, + config: ProcessingConfig, + estimatedTime: number + ): Promise { + // Create cancellation token + const cancellationToken = ProcessingErrorHandler.createTimeoutController(config.timeoutMs); + + // Set initial state + const state: ProcessingState = { + fileKey, + fileName: file.name, + status: 'processing', + progress: 0, + strategy: config.strategy, + startedAt: Date.now(), + estimatedTimeRemaining: estimatedTime, + cancellationToken + }; + + this.processing.set(fileKey, state); + this.notifyListeners(); + this.updateMetrics('started'); + + try { + // Execute processing with retry logic + const processedFile = await ProcessingErrorHandler.executeWithRetry( + () => this.executeProcessingStrategy(file, config, state), + (error) => { + state.error = error; + this.notifyListeners(); + }, + config.maxRetries + ); + + // Cache the result + this.cache.set(fileKey, processedFile); + + // Update state to completed + state.status = 'completed'; + state.progress = 100; + state.completedAt = Date.now(); + this.notifyListeners(); + this.updateMetrics('completed', Date.now() - state.startedAt); + + // Remove from processing map after brief delay + setTimeout(() => { + this.processing.delete(fileKey); + this.notifyListeners(); + }, 2000); + + } catch (error) { + console.error('Processing failed for', file.name, ':', error); + + const processingError = ProcessingErrorHandler.createProcessingError(error); + state.status = 'error'; + state.error = processingError; + this.notifyListeners(); + this.updateMetrics('failed'); + + // Remove failed processing after delay + setTimeout(() => { + this.processing.delete(fileKey); + this.notifyListeners(); + }, 10000); + } + } + + /** + * Execute the actual processing based on strategy + */ + private async executeProcessingStrategy( + file: File, + config: ProcessingConfig, + state: ProcessingState + ): Promise { + switch (config.strategy) { + case 'immediate_full': + return this.processImmediateFull(file, config, state); + + case 'priority_pages': + return this.processPriorityPages(file, config, state); + + case 'progressive_chunked': + return this.processProgressiveChunked(file, config, state); + + case 'metadata_only': + return this.processMetadataOnly(file, config, state); + + default: + return this.processImmediateFull(file, config, state); + } + } + + /** + * Process all pages immediately (for small files) + */ + private async processImmediateFull( + file: File, + config: ProcessingConfig, + state: ProcessingState + ): Promise { + const arrayBuffer = await file.arrayBuffer(); + const pdf = await getDocument({ data: arrayBuffer }).promise; + const totalPages = pdf.numPages; + + state.progress = 10; + this.notifyListeners(); + + const pages: PDFPage[] = []; + + for (let i = 1; i <= totalPages; i++) { + // Check for cancellation + if (state.cancellationToken?.signal.aborted) { + pdf.destroy(); + throw new Error('Processing cancelled'); + } + + const page = await pdf.getPage(i); + const thumbnail = await this.renderPageThumbnail(page, config.thumbnailQuality); + + pages.push({ + id: `${file.name}-page-${i}`, + pageNumber: i, + thumbnail, + rotation: 0, + selected: false + }); + + // Update progress + state.progress = 10 + (i / totalPages) * 85; + state.currentPage = i; + this.notifyListeners(); + } + + pdf.destroy(); + state.progress = 100; + this.notifyListeners(); + + return this.createProcessedFile(file, pages, totalPages); + } + + /** + * Process priority pages first, then queue the rest + */ + private async processPriorityPages( + file: File, + config: ProcessingConfig, + state: ProcessingState + ): Promise { + const arrayBuffer = await file.arrayBuffer(); + const pdf = await getDocument({ data: arrayBuffer }).promise; + const totalPages = pdf.numPages; + + state.progress = 10; + this.notifyListeners(); + + const pages: PDFPage[] = []; + const priorityCount = Math.min(config.priorityPageCount, totalPages); + + // Process priority pages first + for (let i = 1; i <= priorityCount; i++) { + if (state.cancellationToken?.signal.aborted) { + pdf.destroy(); + throw new Error('Processing cancelled'); + } + + const page = await pdf.getPage(i); + const thumbnail = await this.renderPageThumbnail(page, config.thumbnailQuality); + + pages.push({ + id: `${file.name}-page-${i}`, + pageNumber: i, + thumbnail, + rotation: 0, + selected: false + }); + + state.progress = 10 + (i / priorityCount) * 60; + state.currentPage = i; + this.notifyListeners(); + } + + // Create placeholder pages for remaining pages + for (let i = priorityCount + 1; i <= totalPages; i++) { + pages.push({ + id: `${file.name}-page-${i}`, + pageNumber: i, + thumbnail: null, // Will be loaded lazily + rotation: 0, + selected: false + }); + } + + pdf.destroy(); + state.progress = 100; + this.notifyListeners(); + + return this.createProcessedFile(file, pages, totalPages); + } + + /** + * Process in chunks with breaks between chunks + */ + private async processProgressiveChunked( + file: File, + config: ProcessingConfig, + state: ProcessingState + ): Promise { + const arrayBuffer = await file.arrayBuffer(); + const pdf = await getDocument({ data: arrayBuffer }).promise; + const totalPages = pdf.numPages; + + state.progress = 10; + this.notifyListeners(); + + const pages: PDFPage[] = []; + const chunkSize = config.chunkSize; + let processedPages = 0; + + // Process first chunk immediately + const firstChunkEnd = Math.min(chunkSize, totalPages); + + for (let i = 1; i <= firstChunkEnd; i++) { + if (state.cancellationToken?.signal.aborted) { + pdf.destroy(); + throw new Error('Processing cancelled'); + } + + const page = await pdf.getPage(i); + const thumbnail = await this.renderPageThumbnail(page, config.thumbnailQuality); + + pages.push({ + id: `${file.name}-page-${i}`, + pageNumber: i, + thumbnail, + rotation: 0, + selected: false + }); + + processedPages++; + state.progress = 10 + (processedPages / totalPages) * 70; + state.currentPage = i; + this.notifyListeners(); + + // Small delay to prevent UI blocking + if (i % 5 === 0) { + await new Promise(resolve => setTimeout(resolve, 10)); + } + } + + // Create placeholders for remaining pages + for (let i = firstChunkEnd + 1; i <= totalPages; i++) { + pages.push({ + id: `${file.name}-page-${i}`, + pageNumber: i, + thumbnail: null, + rotation: 0, + selected: false + }); + } + + pdf.destroy(); + state.progress = 100; + this.notifyListeners(); + + return this.createProcessedFile(file, pages, totalPages); + } + + /** + * Process metadata only (for very large files) + */ + private async processMetadataOnly( + file: File, + config: ProcessingConfig, + state: ProcessingState + ): Promise { + const arrayBuffer = await file.arrayBuffer(); + const pdf = await getDocument({ data: arrayBuffer }).promise; + const totalPages = pdf.numPages; + + state.progress = 50; + this.notifyListeners(); + + // Create placeholder pages without thumbnails + const pages: PDFPage[] = []; + for (let i = 1; i <= totalPages; i++) { + pages.push({ + id: `${file.name}-page-${i}`, + pageNumber: i, + thumbnail: null, + rotation: 0, + selected: false + }); + } + + pdf.destroy(); + state.progress = 100; + this.notifyListeners(); + + return this.createProcessedFile(file, pages, totalPages); + } + + /** + * Render a page thumbnail with specified quality + */ + private async renderPageThumbnail(page: any, quality: 'low' | 'medium' | 'high'): Promise { + const scales = { low: 0.2, medium: 0.5, high: 0.8 }; // Reduced low quality for page editor + const scale = scales[quality]; + + const viewport = page.getViewport({ scale }); + const canvas = document.createElement('canvas'); + canvas.width = viewport.width; + canvas.height = viewport.height; + + const context = canvas.getContext('2d'); + if (!context) { + throw new Error('Could not get canvas context'); + } + + await page.render({ canvasContext: context, viewport }).promise; + return canvas.toDataURL('image/jpeg', 0.8); // Use JPEG for better compression + } + + /** + * Create a ProcessedFile object + */ + private createProcessedFile(file: File, pages: PDFPage[], totalPages: number): ProcessedFile { + return { + id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`, + pages, + totalPages, + metadata: { + title: file.name, + createdAt: new Date().toISOString(), + modifiedAt: new Date().toISOString() + } + }; + } + + + /** + * Generate a unique, collision-resistant cache key + */ + private async generateFileKey(file: File): Promise { + return await FileHasher.generateHybridHash(file); + } + + /** + * Cancel processing for a specific file + */ + cancelProcessing(fileKey: string): void { + const state = this.processing.get(fileKey); + if (state && state.cancellationToken) { + state.cancellationToken.abort(); + state.status = 'cancelled'; + this.notifyListeners(); + } + } + + /** + * Update processing metrics + */ + private updateMetrics(event: 'started' | 'completed' | 'failed' | 'cacheHit', processingTime?: number): void { + switch (event) { + case 'started': + this.metrics.totalFiles++; + break; + case 'completed': + this.metrics.completedFiles++; + if (processingTime) { + // Update rolling average + const totalProcessingTime = this.metrics.averageProcessingTime * (this.metrics.completedFiles - 1) + processingTime; + this.metrics.averageProcessingTime = totalProcessingTime / this.metrics.completedFiles; + } + break; + case 'failed': + this.metrics.failedFiles++; + break; + case 'cacheHit': + // Update cache hit rate + const totalAttempts = this.metrics.totalFiles + 1; + this.metrics.cacheHitRate = (this.metrics.cacheHitRate * this.metrics.totalFiles + 1) / totalAttempts; + break; + } + } + + /** + * Get processing metrics + */ + getMetrics(): ProcessingMetrics { + return { ...this.metrics }; + } + + /** + * State subscription for components + */ + onProcessingChange(callback: (states: Map) => void): () => void { + this.processingListeners.add(callback); + return () => this.processingListeners.delete(callback); + } + + getProcessingStates(): Map { + return new Map(this.processing); + } + + private notifyListeners(): void { + this.processingListeners.forEach(callback => callback(this.processing)); + } + + /** + * Cleanup method for removed files + */ + cleanup(removedFiles: File[]): void { + removedFiles.forEach(async (file) => { + const key = await this.generateFileKey(file); + this.cache.delete(key); + this.cancelProcessing(key); + this.processing.delete(key); + }); + this.notifyListeners(); + } + + /** + * Clear all processing for view switches + */ + clearAllProcessing(): void { + // Cancel all ongoing processing + this.processing.forEach((state, key) => { + if (state.cancellationToken) { + state.cancellationToken.abort(); + } + }); + + // Clear processing states + this.processing.clear(); + this.notifyListeners(); + + // Force memory cleanup hint + if (typeof window !== 'undefined' && window.gc) { + setTimeout(() => window.gc(), 100); + } + } + + /** + * Get cache statistics + */ + getCacheStats() { + return this.cache.getStats(); + } + + /** + * Clear all cache and processing + */ + clearAll(): void { + this.cache.clear(); + this.processing.clear(); + this.notifyListeners(); + } +} + +// Export singleton instance +export const enhancedPDFProcessingService = EnhancedPDFProcessingService.getInstance(); \ No newline at end of file diff --git a/frontend/src/services/fileAnalyzer.ts b/frontend/src/services/fileAnalyzer.ts new file mode 100644 index 000000000..2a9f15cff --- /dev/null +++ b/frontend/src/services/fileAnalyzer.ts @@ -0,0 +1,240 @@ +import { getDocument } from 'pdfjs-dist'; +import { FileAnalysis, ProcessingStrategy } from '../types/processing'; + +export class FileAnalyzer { + private static readonly SIZE_THRESHOLDS = { + SMALL: 10 * 1024 * 1024, // 10MB + MEDIUM: 50 * 1024 * 1024, // 50MB + LARGE: 200 * 1024 * 1024, // 200MB + }; + + private static readonly PAGE_THRESHOLDS = { + FEW: 10, // < 10 pages - immediate full processing + MANY: 50, // < 50 pages - priority pages + MASSIVE: 100, // < 100 pages - progressive chunked + // >100 pages = metadata only + }; + + /** + * Analyze a file to determine optimal processing strategy + */ + static async analyzeFile(file: File): Promise { + const analysis: FileAnalysis = { + fileSize: file.size, + isEncrypted: false, + isCorrupted: false, + recommendedStrategy: 'metadata_only', + estimatedProcessingTime: 0, + }; + + try { + // Quick validation and page count estimation + const quickAnalysis = await this.quickPDFAnalysis(file); + analysis.estimatedPageCount = quickAnalysis.pageCount; + analysis.isEncrypted = quickAnalysis.isEncrypted; + analysis.isCorrupted = quickAnalysis.isCorrupted; + + // Determine strategy based on file characteristics + analysis.recommendedStrategy = this.determineStrategy(file.size, quickAnalysis.pageCount); + + // Estimate processing time + analysis.estimatedProcessingTime = this.estimateProcessingTime( + file.size, + quickAnalysis.pageCount, + analysis.recommendedStrategy + ); + + } catch (error) { + console.error('File analysis failed:', error); + analysis.isCorrupted = true; + analysis.recommendedStrategy = 'metadata_only'; + } + + return analysis; + } + + /** + * Quick PDF analysis without full processing + */ + private static async quickPDFAnalysis(file: File): Promise<{ + pageCount: number; + isEncrypted: boolean; + isCorrupted: boolean; + }> { + try { + // For small files, read the whole file + // For large files, try the whole file first (PDF.js needs the complete structure) + const arrayBuffer = await file.arrayBuffer(); + + const pdf = await getDocument({ + data: arrayBuffer, + stopAtErrors: false, // Don't stop at minor errors + verbosity: 0 // Suppress PDF.js warnings + }).promise; + + const pageCount = pdf.numPages; + const isEncrypted = pdf.isEncrypted; + + // Clean up + pdf.destroy(); + + return { + pageCount, + isEncrypted, + isCorrupted: false + }; + + } catch (error) { + // Try to determine if it's corruption vs encryption + const errorMessage = error instanceof Error ? error.message.toLowerCase() : ''; + const isEncrypted = errorMessage.includes('password') || errorMessage.includes('encrypted'); + + return { + pageCount: 0, + isEncrypted, + isCorrupted: !isEncrypted // If not encrypted, probably corrupted + }; + } + } + + /** + * Determine the best processing strategy based on file characteristics + */ + private static determineStrategy(fileSize: number, pageCount?: number): ProcessingStrategy { + // Handle corrupted or encrypted files + if (!pageCount || pageCount === 0) { + return 'metadata_only'; + } + + // Small files with few pages - process everything immediately + if (fileSize <= this.SIZE_THRESHOLDS.SMALL && pageCount <= this.PAGE_THRESHOLDS.FEW) { + return 'immediate_full'; + } + + // Medium files or many pages - priority pages first, then progressive + if (fileSize <= this.SIZE_THRESHOLDS.MEDIUM && pageCount <= this.PAGE_THRESHOLDS.MANY) { + return 'priority_pages'; + } + + // Large files or massive page counts - chunked processing + if (fileSize <= this.SIZE_THRESHOLDS.LARGE && pageCount <= this.PAGE_THRESHOLDS.MASSIVE) { + return 'progressive_chunked'; + } + + // Very large files - metadata only + return 'metadata_only'; + } + + /** + * Estimate processing time based on file characteristics and strategy + */ + private static estimateProcessingTime( + fileSize: number, + pageCount: number = 0, + strategy: ProcessingStrategy + ): number { + const baseTimes = { + immediate_full: 200, // 200ms per page + priority_pages: 150, // 150ms per page (optimized) + progressive_chunked: 100, // 100ms per page (chunked) + metadata_only: 50 // 50ms total + }; + + const baseTime = baseTimes[strategy]; + + switch (strategy) { + case 'metadata_only': + return baseTime; + + case 'immediate_full': + return pageCount * baseTime; + + case 'priority_pages': + // Estimate time for priority pages (first 10) + const priorityPages = Math.min(pageCount, 10); + return priorityPages * baseTime; + + case 'progressive_chunked': + // Estimate time for first chunk (20 pages) + const firstChunk = Math.min(pageCount, 20); + return firstChunk * baseTime; + + default: + return pageCount * baseTime; + } + } + + /** + * Get processing recommendations for a set of files + */ + static async analyzeMultipleFiles(files: File[]): Promise<{ + analyses: Map; + recommendations: { + totalEstimatedTime: number; + suggestedBatchSize: number; + shouldUseWebWorker: boolean; + memoryWarning: boolean; + }; + }> { + const analyses = new Map(); + let totalEstimatedTime = 0; + let totalSize = 0; + let totalPages = 0; + + // Analyze each file + for (const file of files) { + const analysis = await this.analyzeFile(file); + analyses.set(file, analysis); + totalEstimatedTime += analysis.estimatedProcessingTime; + totalSize += file.size; + totalPages += analysis.estimatedPageCount || 0; + } + + // Generate recommendations + const recommendations = { + totalEstimatedTime, + suggestedBatchSize: this.calculateBatchSize(files.length, totalSize), + shouldUseWebWorker: totalPages > 100 || totalSize > this.SIZE_THRESHOLDS.MEDIUM, + memoryWarning: totalSize > this.SIZE_THRESHOLDS.LARGE || totalPages > this.PAGE_THRESHOLDS.MASSIVE + }; + + return { analyses, recommendations }; + } + + /** + * Calculate optimal batch size for processing multiple files + */ + private static calculateBatchSize(fileCount: number, totalSize: number): number { + // Process small batches for large total sizes + if (totalSize > this.SIZE_THRESHOLDS.LARGE) { + return Math.max(1, Math.floor(fileCount / 4)); + } + + if (totalSize > this.SIZE_THRESHOLDS.MEDIUM) { + return Math.max(2, Math.floor(fileCount / 2)); + } + + // Process all at once for smaller total sizes + return fileCount; + } + + /** + * Check if a file appears to be a valid PDF + */ + static async isValidPDF(file: File): Promise { + if (file.type !== 'application/pdf' && !file.name.toLowerCase().endsWith('.pdf')) { + return false; + } + + try { + // Read first few bytes to check PDF header + const header = file.slice(0, 8); + const headerBytes = new Uint8Array(await header.arrayBuffer()); + const headerString = String.fromCharCode(...headerBytes); + + return headerString.startsWith('%PDF-'); + } catch (error) { + return false; + } + } +} \ No newline at end of file diff --git a/frontend/src/services/pdfExportService.ts b/frontend/src/services/pdfExportService.ts index e26037c66..b0662437e 100644 --- a/frontend/src/services/pdfExportService.ts +++ b/frontend/src/services/pdfExportService.ts @@ -12,12 +12,12 @@ export class PDFExportService { * Export PDF document with applied operations */ async exportPDF( - pdfDocument: PDFDocument, + pdfDocument: PDFDocument, selectedPageIds: string[] = [], options: ExportOptions = {} ): Promise<{ blob: Blob; filename: string } | { blobs: Blob[]; filenames: string[] }> { const { selectedOnly = false, filename, splitDocuments = false } = options; - + try { // Determine which pages to export const pagesToExport = selectedOnly && selectedPageIds.length > 0 @@ -57,16 +57,16 @@ export class PDFExportService { for (const page of pages) { // Get the original page from source document const sourcePageIndex = page.pageNumber - 1; - + if (sourcePageIndex >= 0 && sourcePageIndex < sourceDoc.getPageCount()) { // Copy the page const [copiedPage] = await newDoc.copyPages(sourceDoc, [sourcePageIndex]); - + // Apply rotation if (page.rotation !== 0) { copiedPage.setRotation(degrees(page.rotation)); } - + newDoc.addPage(copiedPage); } } @@ -108,20 +108,20 @@ export class PDFExportService { for (const endIndex of splitPoints) { const segmentPages = pages.slice(startIndex, endIndex); - + if (segmentPages.length > 0) { const newDoc = await PDFLibDocument.create(); - + for (const page of segmentPages) { const sourcePageIndex = page.pageNumber - 1; - + if (sourcePageIndex >= 0 && sourcePageIndex < sourceDoc.getPageCount()) { const [copiedPage] = await newDoc.copyPages(sourceDoc, [sourcePageIndex]); - + if (page.rotation !== 0) { copiedPage.setRotation(degrees(page.rotation)); } - + newDoc.addPage(copiedPage); } } @@ -130,16 +130,16 @@ export class PDFExportService { newDoc.setCreator('Stirling PDF'); newDoc.setProducer('Stirling PDF'); newDoc.setTitle(`${baseFilename} - Part ${partNumber}`); - + const pdfBytes = await newDoc.save(); const blob = new Blob([pdfBytes], { type: 'application/pdf' }); const filename = this.generateSplitFilename(baseFilename, partNumber); - + blobs.push(blob); filenames.push(filename); partNumber++; } - + startIndex = endIndex; } @@ -172,11 +172,11 @@ export class PDFExportService { link.href = url; link.download = filename; link.style.display = 'none'; - + document.body.appendChild(link); link.click(); document.body.removeChild(link); - + // Clean up the URL after a short delay setTimeout(() => URL.revokeObjectURL(url), 1000); } @@ -185,8 +185,7 @@ export class PDFExportService { * Download multiple files as a ZIP */ async downloadAsZip(blobs: Blob[], filenames: string[], zipFilename: string): Promise { - // For now, download files individually - // TODO: Implement ZIP creation when needed + // For now, download files wherindividually blobs.forEach((blob, index) => { setTimeout(() => { this.downloadFile(blob, filenames[index]); @@ -208,7 +207,7 @@ export class PDFExportService { errors.push('No pages available to export'); } - const pagesToExport = selectedOnly + const pagesToExport = selectedOnly ? pdfDocument.pages.filter(page => selectedPageIds.includes(page.id)) : pdfDocument.pages; @@ -227,7 +226,7 @@ export class PDFExportService { splitCount: number; estimatedSize: string; } { - const pagesToExport = selectedOnly + const pagesToExport = selectedOnly ? pdfDocument.pages.filter(page => selectedPageIds.includes(page.id)) : pdfDocument.pages; @@ -260,4 +259,4 @@ export class PDFExportService { } // Export singleton instance -export const pdfExportService = new PDFExportService(); \ No newline at end of file +export const pdfExportService = new PDFExportService(); diff --git a/frontend/src/services/pdfProcessingService.ts b/frontend/src/services/pdfProcessingService.ts new file mode 100644 index 000000000..5bb6f2ce3 --- /dev/null +++ b/frontend/src/services/pdfProcessingService.ts @@ -0,0 +1,188 @@ +import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist'; +import { ProcessedFile, ProcessingState, PDFPage } from '../types/processing'; +import { ProcessingCache } from './processingCache'; + +// Set up PDF.js worker +GlobalWorkerOptions.workerSrc = '/pdf.worker.js'; + +export class PDFProcessingService { + private static instance: PDFProcessingService; + private cache = new ProcessingCache(); + private processing = new Map(); + private processingListeners = new Set<(states: Map) => void>(); + + private constructor() {} + + static getInstance(): PDFProcessingService { + if (!PDFProcessingService.instance) { + PDFProcessingService.instance = new PDFProcessingService(); + } + return PDFProcessingService.instance; + } + + async getProcessedFile(file: File): Promise { + const fileKey = this.generateFileKey(file); + + // Check cache first + const cached = this.cache.get(fileKey); + if (cached) { + console.log('Cache hit for:', file.name); + return cached; + } + + // Check if already processing + if (this.processing.has(fileKey)) { + console.log('Already processing:', file.name); + return null; // Will be available when processing completes + } + + // Start processing + this.startProcessing(file, fileKey); + return null; + } + + private async startProcessing(file: File, fileKey: string): Promise { + // Set initial state + const state: ProcessingState = { + fileKey, + fileName: file.name, + status: 'processing', + progress: 0, + startedAt: Date.now() + }; + + this.processing.set(fileKey, state); + this.notifyListeners(); + + try { + // Process the file with progress updates + const processedFile = await this.processFileWithProgress(file, (progress) => { + state.progress = progress; + this.notifyListeners(); + }); + + // Cache the result + this.cache.set(fileKey, processedFile); + + // Update state to completed + state.status = 'completed'; + state.progress = 100; + state.completedAt = Date.now(); + this.notifyListeners(); + + // Remove from processing map after brief delay + setTimeout(() => { + this.processing.delete(fileKey); + this.notifyListeners(); + }, 2000); + + } catch (error) { + console.error('Processing failed for', file.name, ':', error); + state.status = 'error'; + state.error = error instanceof Error ? error.message : 'Unknown error'; + this.notifyListeners(); + + // Remove failed processing after delay + setTimeout(() => { + this.processing.delete(fileKey); + this.notifyListeners(); + }, 5000); + } + } + + private async processFileWithProgress( + file: File, + onProgress: (progress: number) => void + ): Promise { + const arrayBuffer = await file.arrayBuffer(); + const pdf = await getDocument({ data: arrayBuffer }).promise; + const totalPages = pdf.numPages; + + onProgress(10); // PDF loaded + + const pages: PDFPage[] = []; + + for (let i = 1; i <= totalPages; i++) { + const page = await pdf.getPage(i); + const viewport = page.getViewport({ scale: 0.5 }); + const canvas = document.createElement('canvas'); + canvas.width = viewport.width; + canvas.height = viewport.height; + + const context = canvas.getContext('2d'); + if (context) { + await page.render({ canvasContext: context, viewport }).promise; + const thumbnail = canvas.toDataURL(); + + pages.push({ + id: `${file.name}-page-${i}`, + pageNumber: i, + thumbnail, + rotation: 0, + selected: false + }); + } + + // Update progress + const progress = 10 + (i / totalPages) * 85; // 10-95% + onProgress(progress); + } + + pdf.destroy(); + onProgress(100); + + return { + id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`, + pages, + totalPages, + metadata: { + title: file.name, + createdAt: new Date().toISOString(), + modifiedAt: new Date().toISOString() + } + }; + } + + // State subscription for components + onProcessingChange(callback: (states: Map) => void): () => void { + this.processingListeners.add(callback); + return () => this.processingListeners.delete(callback); + } + + getProcessingStates(): Map { + return new Map(this.processing); + } + + private notifyListeners(): void { + this.processingListeners.forEach(callback => callback(this.processing)); + } + + generateFileKey(file: File): string { + return `${file.name}-${file.size}-${file.lastModified}`; + } + + // Cleanup method for activeFiles changes + cleanup(removedFiles: File[]): void { + removedFiles.forEach(file => { + const key = this.generateFileKey(file); + this.cache.delete(key); + this.processing.delete(key); + }); + this.notifyListeners(); + } + + // Get cache stats (for debugging) + getCacheStats() { + return this.cache.getStats(); + } + + // Clear all cache and processing + clearAll(): void { + this.cache.clear(); + this.processing.clear(); + this.notifyListeners(); + } +} + +// Export singleton instance +export const pdfProcessingService = PDFProcessingService.getInstance(); \ No newline at end of file diff --git a/frontend/src/services/processingCache.ts b/frontend/src/services/processingCache.ts new file mode 100644 index 000000000..820cfcbef --- /dev/null +++ b/frontend/src/services/processingCache.ts @@ -0,0 +1,138 @@ +import { ProcessedFile, CacheConfig, CacheEntry, CacheStats } from '../types/processing'; + +export class ProcessingCache { + private cache = new Map(); + private totalSize = 0; + + constructor(private config: CacheConfig = { + maxFiles: 20, + maxSizeBytes: 2 * 1024 * 1024 * 1024, // 2GB + ttlMs: 30 * 60 * 1000 // 30 minutes + }) {} + + set(key: string, data: ProcessedFile): void { + // Remove expired entries first + this.cleanup(); + + // Calculate entry size (rough estimate) + const size = this.calculateSize(data); + + // Make room if needed + this.makeRoom(size); + + this.cache.set(key, { + data, + size, + lastAccessed: Date.now(), + createdAt: Date.now() + }); + + this.totalSize += size; + } + + get(key: string): ProcessedFile | null { + const entry = this.cache.get(key); + if (!entry) return null; + + // Check TTL + if (Date.now() - entry.createdAt > this.config.ttlMs) { + this.delete(key); + return null; + } + + // Update last accessed + entry.lastAccessed = Date.now(); + return entry.data; + } + + has(key: string): boolean { + const entry = this.cache.get(key); + if (!entry) return false; + + // Check TTL + if (Date.now() - entry.createdAt > this.config.ttlMs) { + this.delete(key); + return false; + } + + return true; + } + + private makeRoom(neededSize: number): void { + // Remove oldest entries until we have space + while ( + this.cache.size >= this.config.maxFiles || + this.totalSize + neededSize > this.config.maxSizeBytes + ) { + const oldestKey = this.findOldestEntry(); + if (oldestKey) { + this.delete(oldestKey); + } else break; + } + } + + private findOldestEntry(): string | null { + let oldest: { key: string; lastAccessed: number } | null = null; + + for (const [key, entry] of this.cache) { + if (!oldest || entry.lastAccessed < oldest.lastAccessed) { + oldest = { key, lastAccessed: entry.lastAccessed }; + } + } + + return oldest?.key || null; + } + + private cleanup(): void { + const now = Date.now(); + for (const [key, entry] of this.cache) { + if (now - entry.createdAt > this.config.ttlMs) { + this.delete(key); + } + } + } + + private calculateSize(data: ProcessedFile): number { + // Rough size estimation + let size = 0; + + // Estimate size of thumbnails (main memory consumer) + data.pages.forEach(page => { + if (page.thumbnail) { + // Base64 thumbnail is roughly 50KB each + size += 50 * 1024; + } + }); + + // Add some overhead for other data + size += 10 * 1024; // 10KB overhead + + return size; + } + + delete(key: string): void { + const entry = this.cache.get(key); + if (entry) { + this.totalSize -= entry.size; + this.cache.delete(key); + } + } + + clear(): void { + this.cache.clear(); + this.totalSize = 0; + } + + getStats(): CacheStats { + return { + entries: this.cache.size, + totalSizeBytes: this.totalSize, + maxSizeBytes: this.config.maxSizeBytes + }; + } + + // Get all cached keys (for debugging and cleanup) + getKeys(): string[] { + return Array.from(this.cache.keys()); + } +} \ No newline at end of file diff --git a/frontend/src/services/processingErrorHandler.ts b/frontend/src/services/processingErrorHandler.ts new file mode 100644 index 000000000..f6871008d --- /dev/null +++ b/frontend/src/services/processingErrorHandler.ts @@ -0,0 +1,282 @@ +import { ProcessingError } from '../types/processing'; + +export class ProcessingErrorHandler { + private static readonly DEFAULT_MAX_RETRIES = 3; + private static readonly RETRY_DELAYS = [1000, 2000, 4000]; // Progressive backoff in ms + + /** + * Create a ProcessingError from an unknown error + */ + static createProcessingError( + error: unknown, + retryCount: number = 0, + maxRetries: number = this.DEFAULT_MAX_RETRIES + ): ProcessingError { + const originalError = error instanceof Error ? error : new Error(String(error)); + const message = originalError.message; + + // Determine error type based on error message and properties + const errorType = this.determineErrorType(originalError, message); + + // Determine if error is recoverable + const recoverable = this.isRecoverable(errorType, retryCount, maxRetries); + + return { + type: errorType, + message: this.formatErrorMessage(errorType, message), + recoverable, + retryCount, + maxRetries, + originalError + }; + } + + /** + * Determine the type of error based on error characteristics + */ + private static determineErrorType(error: Error, message: string): ProcessingError['type'] { + const lowerMessage = message.toLowerCase(); + + // Network-related errors + if (lowerMessage.includes('network') || + lowerMessage.includes('fetch') || + lowerMessage.includes('connection')) { + return 'network'; + } + + // Memory-related errors + if (lowerMessage.includes('memory') || + lowerMessage.includes('quota') || + lowerMessage.includes('allocation') || + error.name === 'QuotaExceededError') { + return 'memory'; + } + + // Timeout errors + if (lowerMessage.includes('timeout') || + lowerMessage.includes('aborted') || + error.name === 'AbortError') { + return 'timeout'; + } + + // Cancellation + if (lowerMessage.includes('cancel') || + lowerMessage.includes('abort') || + error.name === 'AbortError') { + return 'cancelled'; + } + + // PDF corruption/parsing errors + if (lowerMessage.includes('pdf') || + lowerMessage.includes('parse') || + lowerMessage.includes('invalid') || + lowerMessage.includes('corrupt') || + lowerMessage.includes('malformed')) { + return 'corruption'; + } + + // Default to parsing error + return 'parsing'; + } + + /** + * Determine if an error is recoverable based on type and retry count + */ + private static isRecoverable( + errorType: ProcessingError['type'], + retryCount: number, + maxRetries: number + ): boolean { + // Never recoverable + if (errorType === 'cancelled' || errorType === 'corruption') { + return false; + } + + // Recoverable if we haven't exceeded retry count + if (retryCount >= maxRetries) { + return false; + } + + // Memory errors are usually not recoverable + if (errorType === 'memory') { + return retryCount < 1; // Only one retry for memory errors + } + + // Network and timeout errors are usually recoverable + return errorType === 'network' || errorType === 'timeout' || errorType === 'parsing'; + } + + /** + * Format error message for user display + */ + private static formatErrorMessage(errorType: ProcessingError['type'], originalMessage: string): string { + switch (errorType) { + case 'network': + return 'Network connection failed. Please check your internet connection and try again.'; + + case 'memory': + return 'Insufficient memory to process this file. Try closing other applications or processing a smaller file.'; + + case 'timeout': + return 'Processing timed out. This file may be too large or complex to process.'; + + case 'cancelled': + return 'Processing was cancelled by user.'; + + case 'corruption': + return 'This PDF file appears to be corrupted or encrypted. Please try a different file.'; + + case 'parsing': + return `Failed to process PDF: ${originalMessage}`; + + default: + return `Processing failed: ${originalMessage}`; + } + } + + /** + * Execute an operation with automatic retry logic + */ + static async executeWithRetry( + operation: () => Promise, + onError?: (error: ProcessingError) => void, + maxRetries: number = this.DEFAULT_MAX_RETRIES + ): Promise { + let lastError: ProcessingError | null = null; + + for (let attempt = 0; attempt <= maxRetries; attempt++) { + try { + return await operation(); + } catch (error) { + lastError = this.createProcessingError(error, attempt, maxRetries); + + // Notify error handler + if (onError) { + onError(lastError); + } + + // Don't retry if not recoverable + if (!lastError.recoverable) { + break; + } + + // Don't retry on last attempt + if (attempt === maxRetries) { + break; + } + + // Wait before retry with progressive backoff + const delay = this.RETRY_DELAYS[Math.min(attempt, this.RETRY_DELAYS.length - 1)]; + await this.delay(delay); + + console.log(`Retrying operation (attempt ${attempt + 2}/${maxRetries + 1}) after ${delay}ms delay`); + } + } + + // All retries exhausted + throw lastError || new Error('Operation failed after all retries'); + } + + /** + * Create a timeout wrapper for operations + */ + static withTimeout( + operation: () => Promise, + timeoutMs: number, + timeoutMessage: string = 'Operation timed out' + ): Promise { + return new Promise((resolve, reject) => { + const timeoutId = setTimeout(() => { + reject(new Error(timeoutMessage)); + }, timeoutMs); + + operation() + .then(result => { + clearTimeout(timeoutId); + resolve(result); + }) + .catch(error => { + clearTimeout(timeoutId); + reject(error); + }); + }); + } + + /** + * Create an AbortController that times out after specified duration + */ + static createTimeoutController(timeoutMs: number): AbortController { + const controller = new AbortController(); + + setTimeout(() => { + controller.abort(); + }, timeoutMs); + + return controller; + } + + /** + * Check if an error indicates the operation should be retried + */ + static shouldRetry(error: ProcessingError): boolean { + return error.recoverable && error.retryCount < error.maxRetries; + } + + /** + * Get user-friendly suggestions based on error type + */ + static getErrorSuggestions(error: ProcessingError): string[] { + switch (error.type) { + case 'network': + return [ + 'Check your internet connection', + 'Try refreshing the page', + 'Try again in a few moments' + ]; + + case 'memory': + return [ + 'Close other browser tabs or applications', + 'Try processing a smaller file', + 'Restart your browser', + 'Use a device with more memory' + ]; + + case 'timeout': + return [ + 'Try processing a smaller file', + 'Break large files into smaller sections', + 'Check your internet connection speed' + ]; + + case 'corruption': + return [ + 'Verify the PDF file opens in other applications', + 'Try re-downloading the file', + 'Try a different PDF file', + 'Contact the file creator if it appears corrupted' + ]; + + case 'parsing': + return [ + 'Verify this is a valid PDF file', + 'Try a different PDF file', + 'Contact support if the problem persists' + ]; + + default: + return [ + 'Try refreshing the page', + 'Try again in a few moments', + 'Contact support if the problem persists' + ]; + } + } + + /** + * Utility function for delays + */ + private static delay(ms: number): Promise { + return new Promise(resolve => setTimeout(resolve, ms)); + } +} \ No newline at end of file diff --git a/frontend/src/services/thumbnailGenerationService.ts b/frontend/src/services/thumbnailGenerationService.ts new file mode 100644 index 000000000..3f16a23ea --- /dev/null +++ b/frontend/src/services/thumbnailGenerationService.ts @@ -0,0 +1,450 @@ +/** + * High-performance thumbnail generation service using Web Workers + */ + +interface ThumbnailResult { + pageNumber: number; + thumbnail: string; + success: boolean; + error?: string; +} + +interface ThumbnailGenerationOptions { + scale?: number; + quality?: number; + batchSize?: number; + parallelBatches?: number; +} + +interface CachedThumbnail { + thumbnail: string; + lastUsed: number; + sizeBytes: number; +} + +export class ThumbnailGenerationService { + private workers: Worker[] = []; + private activeJobs = new Map(); + private jobCounter = 0; + private isGenerating = false; + + // Session-based thumbnail cache + private thumbnailCache = new Map(); + private maxCacheSizeBytes = 1024 * 1024 * 1024; // 1GB cache limit + private currentCacheSize = 0; + + constructor(private maxWorkers: number = 3) { + this.initializeWorkers(); + } + + private initializeWorkers(): void { + const workerPromises: Promise[] = []; + + for (let i = 0; i < this.maxWorkers; i++) { + const workerPromise = new Promise((resolve) => { + try { + console.log(`Attempting to create worker ${i}...`); + const worker = new Worker('/thumbnailWorker.js'); + let workerReady = false; + let pingTimeout: NodeJS.Timeout; + + worker.onmessage = (e) => { + const { type, data, jobId } = e.data; + + // Handle PONG response to confirm worker is ready + if (type === 'PONG') { + workerReady = true; + clearTimeout(pingTimeout); + console.log(`✓ Worker ${i} is ready and responsive`); + resolve(worker); + return; + } + + const job = this.activeJobs.get(jobId); + if (!job) return; + + switch (type) { + case 'PROGRESS': + if (job.onProgress) { + job.onProgress(data); + } + break; + + case 'COMPLETE': + job.resolve(data.thumbnails); + this.activeJobs.delete(jobId); + break; + + case 'ERROR': + job.reject(new Error(data.error)); + this.activeJobs.delete(jobId); + break; + } + }; + + worker.onerror = (error) => { + console.error(`✗ Worker ${i} failed with error:`, error); + clearTimeout(pingTimeout); + worker.terminate(); + resolve(null); + }; + + // Test worker with timeout + pingTimeout = setTimeout(() => { + if (!workerReady) { + console.warn(`✗ Worker ${i} timed out (no PONG response)`); + worker.terminate(); + resolve(null); + } + }, 3000); // Reduced timeout for faster feedback + + // Send PING to test worker + try { + worker.postMessage({ type: 'PING' }); + } catch (pingError) { + console.error(`✗ Failed to send PING to worker ${i}:`, pingError); + clearTimeout(pingTimeout); + worker.terminate(); + resolve(null); + } + + } catch (error) { + console.error(`✗ Failed to create worker ${i}:`, error); + resolve(null); + } + }); + + workerPromises.push(workerPromise); + } + + // Wait for all workers to initialize or fail + Promise.all(workerPromises).then((workers) => { + this.workers = workers.filter((w): w is Worker => w !== null); + const successCount = this.workers.length; + const failCount = this.maxWorkers - successCount; + + console.log(`🔧 Worker initialization complete: ${successCount}/${this.maxWorkers} workers ready`); + + if (failCount > 0) { + console.warn(`⚠️ ${failCount} workers failed to initialize - will use main thread fallback`); + } + + if (successCount === 0) { + console.warn('🚨 No Web Workers available - all thumbnail generation will use main thread'); + } + }); + } + + /** + * Generate thumbnails for multiple pages using Web Workers + */ + async generateThumbnails( + pdfArrayBuffer: ArrayBuffer, + pageNumbers: number[], + options: ThumbnailGenerationOptions = {}, + onProgress?: (progress: { completed: number; total: number; thumbnails: ThumbnailResult[] }) => void + ): Promise { + if (this.isGenerating) { + console.warn('🚨 ThumbnailService: Thumbnail generation already in progress, rejecting new request'); + throw new Error('Thumbnail generation already in progress'); + } + + console.log(`🎬 ThumbnailService: Starting thumbnail generation for ${pageNumbers.length} pages`); + this.isGenerating = true; + + const { + scale = 0.2, + quality = 0.8, + batchSize = 20, // Pages per worker + parallelBatches = this.maxWorkers + } = options; + + try { + // Check if workers are available, fallback to main thread if not + if (this.workers.length === 0) { + console.warn('No Web Workers available, falling back to main thread processing'); + return await this.generateThumbnailsMainThread(pdfArrayBuffer, pageNumbers, scale, quality, onProgress); + } + + // Split pages across workers + const workerBatches = this.distributeWork(pageNumbers, this.workers.length); + console.log(`🔧 ThumbnailService: Distributing ${pageNumbers.length} pages across ${this.workers.length} workers:`, workerBatches.map(batch => batch.length)); + const jobPromises: Promise[] = []; + + for (let i = 0; i < workerBatches.length; i++) { + const batch = workerBatches[i]; + if (batch.length === 0) continue; + + const worker = this.workers[i % this.workers.length]; + const jobId = `job-${++this.jobCounter}`; + console.log(`🔧 ThumbnailService: Sending job ${jobId} with ${batch.length} pages to worker ${i}:`, batch); + + const promise = new Promise((resolve, reject) => { + // Add timeout for worker jobs + const timeout = setTimeout(() => { + console.error(`⏰ ThumbnailService: Worker job ${jobId} timed out`); + this.activeJobs.delete(jobId); + reject(new Error(`Worker job ${jobId} timed out`)); + }, 60000); // 1 minute timeout + + // Create job with timeout handling + this.activeJobs.set(jobId, { + resolve: (result: any) => { + console.log(`✅ ThumbnailService: Job ${jobId} completed with ${result.length} thumbnails`); + clearTimeout(timeout); + resolve(result); + }, + reject: (error: any) => { + console.error(`❌ ThumbnailService: Job ${jobId} failed:`, error); + clearTimeout(timeout); + reject(error); + }, + onProgress: onProgress ? (progressData: any) => { + console.log(`📊 ThumbnailService: Job ${jobId} progress - ${progressData.completed}/${progressData.total} (${progressData.thumbnails.length} new)`); + onProgress(progressData); + } : undefined + }); + + worker.postMessage({ + type: 'GENERATE_THUMBNAILS', + jobId, + data: { + pdfArrayBuffer, + pageNumbers: batch, + scale, + quality + } + }); + }); + + jobPromises.push(promise); + } + + // Wait for all workers to complete + const results = await Promise.all(jobPromises); + + // Flatten and sort results by page number + const allThumbnails = results.flat().sort((a, b) => a.pageNumber - b.pageNumber); + console.log(`🎯 ThumbnailService: All workers completed, returning ${allThumbnails.length} thumbnails`); + + return allThumbnails; + + } catch (error) { + console.error('Web Worker thumbnail generation failed, falling back to main thread:', error); + return await this.generateThumbnailsMainThread(pdfArrayBuffer, pageNumbers, scale, quality, onProgress); + } finally { + console.log('🔄 ThumbnailService: Resetting isGenerating flag'); + this.isGenerating = false; + } + } + + /** + * Fallback thumbnail generation on main thread + */ + private async generateThumbnailsMainThread( + pdfArrayBuffer: ArrayBuffer, + pageNumbers: number[], + scale: number, + quality: number, + onProgress?: (progress: { completed: number; total: number; thumbnails: ThumbnailResult[] }) => void + ): Promise { + console.log(`🔧 ThumbnailService: Fallback to main thread for ${pageNumbers.length} pages`); + + // Import PDF.js dynamically for main thread + const { getDocument } = await import('pdfjs-dist'); + + // Load PDF once + const pdf = await getDocument({ data: pdfArrayBuffer }).promise; + console.log(`✓ ThumbnailService: PDF loaded on main thread`); + + + const allResults: ThumbnailResult[] = []; + let completed = 0; + const batchSize = 5; // Small batches for UI responsiveness + + // Process pages in small batches + for (let i = 0; i < pageNumbers.length; i += batchSize) { + const batch = pageNumbers.slice(i, i + batchSize); + + // Process batch sequentially (to avoid canvas conflicts) + for (const pageNumber of batch) { + try { + const page = await pdf.getPage(pageNumber); + const viewport = page.getViewport({ scale }); + + const canvas = document.createElement('canvas'); + canvas.width = viewport.width; + canvas.height = viewport.height; + + const context = canvas.getContext('2d'); + if (!context) { + throw new Error('Could not get canvas context'); + } + + await page.render({ canvasContext: context, viewport }).promise; + const thumbnail = canvas.toDataURL('image/jpeg', quality); + + allResults.push({ pageNumber, thumbnail, success: true }); + + } catch (error) { + console.error(`Failed to generate thumbnail for page ${pageNumber}:`, error); + allResults.push({ + pageNumber, + thumbnail: '', + success: false, + error: error instanceof Error ? error.message : 'Unknown error' + }); + } + } + + completed += batch.length; + + // Report progress + if (onProgress) { + onProgress({ + completed, + total: pageNumbers.length, + thumbnails: allResults.slice(-batch.length).filter(r => r.success) + }); + } + + // Small delay to keep UI responsive + if (i + batchSize < pageNumbers.length) { + await new Promise(resolve => setTimeout(resolve, 10)); + } + } + + // Clean up + pdf.destroy(); + + return allResults.filter(r => r.success); + } + + /** + * Distribute work evenly across workers + */ + private distributeWork(pageNumbers: number[], numWorkers: number): number[][] { + const batches: number[][] = Array(numWorkers).fill(null).map(() => []); + + pageNumbers.forEach((pageNum, index) => { + const workerIndex = index % numWorkers; + batches[workerIndex].push(pageNum); + }); + + return batches; + } + + /** + * Generate a single thumbnail (fallback for individual pages) + */ + async generateSingleThumbnail( + pdfArrayBuffer: ArrayBuffer, + pageNumber: number, + options: ThumbnailGenerationOptions = {} + ): Promise { + const results = await this.generateThumbnails(pdfArrayBuffer, [pageNumber], options); + + if (results.length === 0 || !results[0].success) { + throw new Error(`Failed to generate thumbnail for page ${pageNumber}`); + } + + return results[0].thumbnail; + } + + /** + * Add thumbnail to cache with size management + */ + addThumbnailToCache(pageId: string, thumbnail: string): void { + const thumbnailSizeBytes = thumbnail.length * 0.75; // Rough base64 size estimate + const now = Date.now(); + + // Add new thumbnail + this.thumbnailCache.set(pageId, { + thumbnail, + lastUsed: now, + sizeBytes: thumbnailSizeBytes + }); + + this.currentCacheSize += thumbnailSizeBytes; + + // If we exceed 1GB, trigger cleanup + if (this.currentCacheSize > this.maxCacheSizeBytes) { + this.cleanupThumbnailCache(); + } + } + + /** + * Get thumbnail from cache and update last used timestamp + */ + getThumbnailFromCache(pageId: string): string | null { + const cached = this.thumbnailCache.get(pageId); + if (!cached) return null; + + // Update last used timestamp + cached.lastUsed = Date.now(); + + return cached.thumbnail; + } + + /** + * Clean up cache using LRU eviction + */ + private cleanupThumbnailCache(): void { + const entries = Array.from(this.thumbnailCache.entries()); + + // Sort by last used (oldest first) + entries.sort(([, a], [, b]) => a.lastUsed - b.lastUsed); + + this.thumbnailCache.clear(); + this.currentCacheSize = 0; + const targetSize = this.maxCacheSizeBytes * 0.8; // Clean to 80% of limit + + // Keep most recently used entries until we hit target size + for (let i = entries.length - 1; i >= 0 && this.currentCacheSize < targetSize; i--) { + const [key, value] = entries[i]; + this.thumbnailCache.set(key, value); + this.currentCacheSize += value.sizeBytes; + } + } + + /** + * Clear all cached thumbnails + */ + clearThumbnailCache(): void { + this.thumbnailCache.clear(); + this.currentCacheSize = 0; + } + + /** + * Get cache statistics + */ + getCacheStats() { + return { + entries: this.thumbnailCache.size, + totalSizeBytes: this.currentCacheSize, + maxSizeBytes: this.maxCacheSizeBytes + }; + } + + /** + * Stop generation but keep cache and workers alive + */ + stopGeneration(): void { + this.activeJobs.clear(); + this.isGenerating = false; + } + + /** + * Terminate all workers and clear cache (only on explicit cleanup) + */ + destroy(): void { + this.workers.forEach(worker => worker.terminate()); + this.workers = []; + this.activeJobs.clear(); + this.isGenerating = false; + this.clearThumbnailCache(); + } +} + +// Export singleton instance +export const thumbnailGenerationService = new ThumbnailGenerationService(); \ No newline at end of file diff --git a/frontend/src/services/zipFileService.ts b/frontend/src/services/zipFileService.ts new file mode 100644 index 000000000..3c238e159 --- /dev/null +++ b/frontend/src/services/zipFileService.ts @@ -0,0 +1,300 @@ +import JSZip from 'jszip'; + +export interface ZipExtractionResult { + success: boolean; + extractedFiles: File[]; + errors: string[]; + totalFiles: number; + extractedCount: number; +} + +export interface ZipValidationResult { + isValid: boolean; + fileCount: number; + totalSizeBytes: number; + containsPDFs: boolean; + errors: string[]; +} + +export interface ZipExtractionProgress { + currentFile: string; + extractedCount: number; + totalFiles: number; + progress: number; +} + +export class ZipFileService { + private readonly maxFileSize = 100 * 1024 * 1024; // 100MB per file + private readonly maxTotalSize = 500 * 1024 * 1024; // 500MB total extraction limit + private readonly supportedExtensions = ['.pdf']; + + /** + * Validate a ZIP file without extracting it + */ + async validateZipFile(file: File): Promise { + const result: ZipValidationResult = { + isValid: false, + fileCount: 0, + totalSizeBytes: 0, + containsPDFs: false, + errors: [] + }; + + try { + // Check file size + if (file.size > this.maxTotalSize) { + result.errors.push(`ZIP file too large: ${this.formatFileSize(file.size)} (max: ${this.formatFileSize(this.maxTotalSize)})`); + return result; + } + + // Check file type + if (!this.isZipFile(file)) { + result.errors.push('File is not a valid ZIP archive'); + return result; + } + + // Load and validate ZIP contents + const zip = new JSZip(); + const zipContents = await zip.loadAsync(file); + + let totalSize = 0; + let fileCount = 0; + let containsPDFs = false; + + // Analyze ZIP contents + for (const [filename, zipEntry] of Object.entries(zipContents.files)) { + if (zipEntry.dir) { + continue; // Skip directories + } + + fileCount++; + const uncompressedSize = zipEntry._data?.uncompressedSize || 0; + totalSize += uncompressedSize; + + // Check if file is a PDF + if (this.isPdfFile(filename)) { + containsPDFs = true; + } + + // Check individual file size + if (uncompressedSize > this.maxFileSize) { + result.errors.push(`File "${filename}" too large: ${this.formatFileSize(uncompressedSize)} (max: ${this.formatFileSize(this.maxFileSize)})`); + } + } + + // Check total uncompressed size + if (totalSize > this.maxTotalSize) { + result.errors.push(`Total uncompressed size too large: ${this.formatFileSize(totalSize)} (max: ${this.formatFileSize(this.maxTotalSize)})`); + } + + result.fileCount = fileCount; + result.totalSizeBytes = totalSize; + result.containsPDFs = containsPDFs; + result.isValid = result.errors.length === 0 && containsPDFs; + + if (!containsPDFs) { + result.errors.push('ZIP file does not contain any PDF files'); + } + + return result; + } catch (error) { + result.errors.push(`Failed to validate ZIP file: ${error instanceof Error ? error.message : 'Unknown error'}`); + return result; + } + } + + /** + * Extract PDF files from a ZIP archive + */ + async extractPdfFiles( + file: File, + onProgress?: (progress: ZipExtractionProgress) => void + ): Promise { + const result: ZipExtractionResult = { + success: false, + extractedFiles: [], + errors: [], + totalFiles: 0, + extractedCount: 0 + }; + + try { + // Validate ZIP file first + const validation = await this.validateZipFile(file); + if (!validation.isValid) { + result.errors = validation.errors; + return result; + } + + // Load ZIP contents + const zip = new JSZip(); + const zipContents = await zip.loadAsync(file); + + // Get all PDF files + const pdfFiles = Object.entries(zipContents.files).filter(([filename, zipEntry]) => + !zipEntry.dir && this.isPdfFile(filename) + ); + + result.totalFiles = pdfFiles.length; + + // Extract each PDF file + for (let i = 0; i < pdfFiles.length; i++) { + const [filename, zipEntry] = pdfFiles[i]; + + try { + // Report progress + if (onProgress) { + onProgress({ + currentFile: filename, + extractedCount: i, + totalFiles: pdfFiles.length, + progress: (i / pdfFiles.length) * 100 + }); + } + + // Extract file content + const content = await zipEntry.async('uint8array'); + + // Create File object + const extractedFile = new File([content], this.sanitizeFilename(filename), { + type: 'application/pdf', + lastModified: zipEntry.date?.getTime() || Date.now() + }); + + // Validate extracted PDF + if (await this.isValidPdfFile(extractedFile)) { + result.extractedFiles.push(extractedFile); + result.extractedCount++; + } else { + result.errors.push(`File "${filename}" is not a valid PDF`); + } + } catch (error) { + result.errors.push(`Failed to extract "${filename}": ${error instanceof Error ? error.message : 'Unknown error'}`); + } + } + + // Final progress report + if (onProgress) { + onProgress({ + currentFile: '', + extractedCount: result.extractedCount, + totalFiles: result.totalFiles, + progress: 100 + }); + } + + result.success = result.extractedCount > 0; + return result; + } catch (error) { + result.errors.push(`Failed to extract ZIP file: ${error instanceof Error ? error.message : 'Unknown error'}`); + return result; + } + } + + /** + * Check if a file is a ZIP file based on type and extension + */ + private isZipFile(file: File): boolean { + const validTypes = [ + 'application/zip', + 'application/x-zip-compressed', + 'application/x-zip', + 'application/octet-stream' // Some browsers use this for ZIP files + ]; + + const validExtensions = ['.zip']; + const hasValidType = validTypes.includes(file.type); + const hasValidExtension = validExtensions.some(ext => + file.name.toLowerCase().endsWith(ext) + ); + + return hasValidType || hasValidExtension; + } + + /** + * Check if a filename indicates a PDF file + */ + private isPdfFile(filename: string): boolean { + return filename.toLowerCase().endsWith('.pdf'); + } + + /** + * Validate that a file is actually a PDF by checking its header + */ + private async isValidPdfFile(file: File): Promise { + try { + // Read first few bytes to check PDF header + const buffer = await file.slice(0, 8).arrayBuffer(); + const bytes = new Uint8Array(buffer); + + // Check for PDF header: %PDF- + return bytes[0] === 0x25 && // % + bytes[1] === 0x50 && // P + bytes[2] === 0x44 && // D + bytes[3] === 0x46 && // F + bytes[4] === 0x2D; // - + } catch (error) { + return false; + } + } + + /** + * Sanitize filename for safe use + */ + private sanitizeFilename(filename: string): string { + // Remove directory path and get just the filename + const basename = filename.split('/').pop() || filename; + + // Remove or replace unsafe characters + return basename + .replace(/[<>:"/\\|?*]/g, '_') // Replace unsafe chars with underscore + .replace(/\s+/g, '_') // Replace spaces with underscores + .replace(/_{2,}/g, '_') // Replace multiple underscores with single + .replace(/^_|_$/g, ''); // Remove leading/trailing underscores + } + + /** + * Format file size for display + */ + private formatFileSize(bytes: number): string { + if (bytes === 0) return '0 B'; + const k = 1024; + const sizes = ['B', 'KB', 'MB', 'GB']; + const i = Math.floor(Math.log(bytes) / Math.log(k)); + return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; + } + + /** + * Get file extension from filename + */ + private getFileExtension(filename: string): string { + return filename.substring(filename.lastIndexOf('.')).toLowerCase(); + } + + /** + * Check if ZIP file contains password protection + */ + private async isPasswordProtected(file: File): Promise { + try { + const zip = new JSZip(); + await zip.loadAsync(file); + + // Check if any files are encrypted + for (const [filename, zipEntry] of Object.entries(zip.files)) { + if (zipEntry.options?.compression === 'STORE' && zipEntry._data?.compressedSize === 0) { + // This might indicate encryption, but JSZip doesn't provide direct encryption detection + // We'll handle this in the extraction phase + } + } + + return false; // JSZip will throw an error if password is required + } catch (error) { + // If we can't load the ZIP, it might be password protected + const errorMessage = error instanceof Error ? error.message : ''; + return errorMessage.includes('password') || errorMessage.includes('encrypted'); + } + } +} + +// Export singleton instance +export const zipFileService = new ZipFileService(); \ No newline at end of file diff --git a/frontend/src/tools/Merge.tsx b/frontend/src/tools/Merge.tsx index 2e33ad046..582a3d6ec 100644 --- a/frontend/src/tools/Merge.tsx +++ b/frontend/src/tools/Merge.tsx @@ -1,6 +1,5 @@ import React, { useState, useEffect } from "react"; import { Paper, Button, Checkbox, Stack, Text, Group, Loader, Alert } from "@mantine/core"; -import { useSearchParams } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { FileWithUrl } from "../types/file"; import { fileStorage } from "../services/fileStorage"; diff --git a/frontend/src/tools/Split.tsx b/frontend/src/tools/Split.tsx index 1d0ffb343..e691d216a 100644 --- a/frontend/src/tools/Split.tsx +++ b/frontend/src/tools/Split.tsx @@ -1,308 +1,163 @@ -import React, { useState } from "react"; -import axios from "axios"; -import { - Button, - Select, - TextInput, - Checkbox, - Notification, - Stack, - Loader, - Alert, - Text, -} from "@mantine/core"; -import { useSearchParams } from "react-router-dom"; +import React, { useEffect, useMemo } from "react"; +import { Button, Stack, Text } from "@mantine/core"; import { useTranslation } from "react-i18next"; import DownloadIcon from "@mui/icons-material/Download"; -import { FileWithUrl } from "../types/file"; -import { fileStorage } from "../services/fileStorage"; import { useEndpointEnabled } from "../hooks/useEndpointConfig"; +import { useFileContext } from "../contexts/FileContext"; -export interface SplitPdfPanelProps { - file: { file: FileWithUrl; url: string } | null; - downloadUrl?: string | null; - setDownloadUrl: (url: string | null) => void; - params: { - mode: string; - pages: string; - hDiv: string; - vDiv: string; - merge: boolean; - splitType: string; - splitValue: string; - bookmarkLevel: string; - includeMetadata: boolean; - allowDuplicates: boolean; - }; - updateParams: (newParams: Partial) => void; +import ToolStep, { ToolStepContainer } from "../components/tools/shared/ToolStep"; +import OperationButton from "../components/tools/shared/OperationButton"; +import ErrorNotification from "../components/tools/shared/ErrorNotification"; +import FileStatusIndicator from "../components/tools/shared/FileStatusIndicator"; +import ResultsPreview from "../components/tools/shared/ResultsPreview"; + +import SplitSettings from "../components/tools/split/SplitSettings"; + +import { useSplitParameters } from "../hooks/tools/split/useSplitParameters"; +import { useSplitOperation } from "../hooks/tools/split/useSplitOperation"; + +interface SplitProps { + selectedFiles?: File[]; + onPreviewFile?: (file: File | null) => void; } -const SplitPdfPanel: React.FC = ({ - file, - downloadUrl, - setDownloadUrl, - params, - updateParams, -}) => { +const Split = ({ selectedFiles = [], onPreviewFile }: SplitProps) => { const { t } = useTranslation(); - const [searchParams] = useSearchParams(); + const { setCurrentMode } = useFileContext(); - const [status, setStatus] = useState(""); - const [isLoading, setIsLoading] = useState(false); - const [errorMessage, setErrorMessage] = useState(null); + const splitParams = useSplitParameters(); + const splitOperation = useSplitOperation(); - // Map mode to endpoint name for checking - const getEndpointName = (mode: string) => { - switch (mode) { - case "byPages": - return "split-pages"; - case "bySections": - return "split-pdf-by-sections"; - case "bySizeOrCount": - return "split-by-size-or-count"; - case "byChapters": - return "split-pdf-by-chapters"; - default: - return "split-pages"; - } + // Endpoint validation + const { enabled: endpointEnabled, loading: endpointLoading } = useEndpointEnabled( + splitParams.getEndpointName() + ); + + useEffect(() => { + splitOperation.resetResults(); + onPreviewFile?.(null); + }, [splitParams.mode, splitParams.parameters, selectedFiles]); + + const handleSplit = async () => { + await splitOperation.executeOperation( + splitParams.mode, + splitParams.parameters, + selectedFiles + ); }; - - const { - mode, - pages, - hDiv, - vDiv, - merge, - splitType, - splitValue, - bookmarkLevel, - includeMetadata, - allowDuplicates, - } = params; - - - const { enabled: endpointEnabled, loading: endpointLoading } = useEndpointEnabled(getEndpointName(mode)); - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - if (!file) { - setStatus(t("noFileSelected")); - return; - } - - const formData = new FormData(); - - // Handle IndexedDB files - if (!file.file.id) { - setStatus(t("noFileSelected")); - return; - } - const storedFile = await fileStorage.getFile(file.file.id); - if (storedFile) { - const blob = new Blob([storedFile.data], { type: storedFile.type }); - const actualFile = new File([blob], storedFile.name, { - type: storedFile.type, - lastModified: storedFile.lastModified - }); - formData.append("fileInput", actualFile); - } - - let endpoint = ""; - - switch (mode) { - case "byPages": - formData.append("pageNumbers", pages); - endpoint = "/api/v1/general/split-pages"; - break; - case "bySections": - formData.append("horizontalDivisions", hDiv); - formData.append("verticalDivisions", vDiv); - formData.append("merge", merge.toString()); - endpoint = "/api/v1/general/split-pdf-by-sections"; - break; - case "bySizeOrCount": - formData.append( - "splitType", - splitType === "size" ? "0" : splitType === "pages" ? "1" : "2" - ); - formData.append("splitValue", splitValue); - endpoint = "/api/v1/general/split-by-size-or-count"; - break; - case "byChapters": - formData.append("bookmarkLevel", bookmarkLevel); - formData.append("includeMetadata", includeMetadata.toString()); - formData.append("allowDuplicates", allowDuplicates.toString()); - endpoint = "/api/v1/general/split-pdf-by-chapters"; - break; - default: - return; - } - - setStatus(t("loading")); - setIsLoading(true); - setErrorMessage(null); - - try { - const response = await axios.post(endpoint, formData, { responseType: "blob" }); - const blob = new Blob([response.data], { type: "application/zip" }); - const url = window.URL.createObjectURL(blob); - setDownloadUrl(url); - setStatus(t("downloadComplete")); - } catch (error: any) { - console.error(error); - let errorMsg = t("error.pdfPassword", "An error occurred while splitting the PDF."); - if (error.response?.data && typeof error.response.data === 'string') { - errorMsg = error.response.data; - } else if (error.message) { - errorMsg = error.message; - } - setErrorMessage(errorMsg); - setStatus(t("error._value", "Split failed.")); - } finally { - setIsLoading(false); - } + const handleThumbnailClick = (file: File) => { + onPreviewFile?.(file); + sessionStorage.setItem('previousMode', 'split'); + setCurrentMode('viewer'); }; - if (endpointLoading) { - return ( - - - {t("loading", "Loading...")} - - ); - } + const handleSettingsReset = () => { + splitOperation.resetResults(); + onPreviewFile?.(null); + setCurrentMode('split'); + }; - if (endpointEnabled === false) { - return ( - - - {t("endpointDisabled", "This feature is currently disabled.")} - - - ); - } + const hasFiles = selectedFiles.length > 0; + const hasResults = splitOperation.downloadUrl !== null; + const filesCollapsed = hasFiles; + const settingsCollapsed = hasResults; + + const previewResults = useMemo(() => + splitOperation.files?.map((file, index) => ({ + file, + thumbnail: splitOperation.thumbnails[index] + })) || [], + [splitOperation.files, splitOperation.thumbnails] + ); return ( -
- - v && updateParams({ splitType: v })} - data={[ - { value: "size", label: t("split-by-size-or-count.type.size", "By Size") }, - { value: "pages", label: t("split-by-size-or-count.type.pageCount", "By Page Count") }, - { value: "docs", label: t("split-by-size-or-count.type.docCount", "By Document Count") }, - ]} - /> - updateParams({ splitValue: e.target.value })} - /> - - )} + {/* Results Step */} + + + {splitOperation.status && ( + {splitOperation.status} + )} - {mode === "byChapters" && ( - - updateParams({ bookmarkLevel: e.target.value })} - /> - updateParams({ includeMetadata: e.currentTarget.checked })} - /> - updateParams({ allowDuplicates: e.currentTarget.checked })} - /> - - )} + - + {splitOperation.downloadUrl && ( + + )} - {status &&

{status}

} - - {errorMessage && ( - setErrorMessage(null)}> - {errorMessage} - - )} - - {status === t("downloadComplete") && downloadUrl && ( - - )} -
- + + +
+ + ); -}; +} -export default SplitPdfPanel; +export default Split; diff --git a/frontend/src/types/fileContext.ts b/frontend/src/types/fileContext.ts new file mode 100644 index 000000000..d28490277 --- /dev/null +++ b/frontend/src/types/fileContext.ts @@ -0,0 +1,178 @@ +/** + * Types for global file context management across views and tools + */ + +import { ProcessedFile } from './processing'; +import { PDFDocument, PDFPage, PageOperation } from './pageEditor'; + +export type ModeType = 'viewer' | 'pageEditor' | 'fileEditor' | 'merge' | 'split' | 'compress'; + +// Legacy types for backward compatibility during transition +export type ViewType = 'viewer' | 'pageEditor' | 'fileEditor'; +export type ToolType = 'merge' | 'split' | 'compress' | null; + +export interface FileOperation { + id: string; + type: 'merge' | 'split' | 'compress' | 'add' | 'remove' | 'replace' | 'convert' | 'upload'; + timestamp: number; + fileIds: string[]; + status: 'pending' | 'applied' | 'failed'; + data?: any; + metadata?: { + originalFileName?: string; + outputFileNames?: string[]; + parameters?: Record; + fileSize?: number; + pageCount?: number; + error?: string; + }; +} + +export interface FileOperationHistory { + fileId: string; + fileName: string; + operations: (FileOperation | PageOperation)[]; + createdAt: number; + lastModified: number; +} + +export interface ViewerConfig { + zoom: number; + currentPage: number; + viewMode: 'single' | 'continuous' | 'facing'; + sidebarOpen: boolean; +} + +export interface FileEditHistory { + fileId: string; + pageOperations: PageOperation[]; + lastModified: number; +} + +export interface FileContextState { + // Core file management + activeFiles: File[]; + processedFiles: Map; + + // Current navigation state + currentMode: ModeType; + // Legacy fields for backward compatibility + currentView: ViewType; + currentTool: ToolType; + + // Edit history and state + fileEditHistory: Map; + globalFileOperations: FileOperation[]; + // New comprehensive operation history + fileOperationHistory: Map; + + // UI state that persists across views + selectedFileIds: string[]; + selectedPageNumbers: number[]; + viewerConfig: ViewerConfig; + + // Processing state + isProcessing: boolean; + processingProgress: number; + + // Export state + lastExportConfig?: { + filename: string; + selectedOnly: boolean; + splitDocuments: boolean; + }; + + // Navigation guard system + hasUnsavedChanges: boolean; + pendingNavigation: (() => void) | null; + showNavigationWarning: boolean; +} + +export interface FileContextActions { + // File management + addFiles: (files: File[]) => Promise; + removeFiles: (fileIds: string[], deleteFromStorage?: boolean) => void; + replaceFile: (oldFileId: string, newFile: File) => Promise; + clearAllFiles: () => void; + + // Navigation + setCurrentMode: (mode: ModeType) => void; + // Legacy navigation functions for backward compatibility + setCurrentView: (view: ViewType) => void; + setCurrentTool: (tool: ToolType) => void; + + // Selection management + setSelectedFiles: (fileIds: string[]) => void; + setSelectedPages: (pageNumbers: number[]) => void; + updateProcessedFile: (file: File, processedFile: ProcessedFile) => void; + clearSelections: () => void; + + // Edit operations + applyPageOperations: (fileId: string, operations: PageOperation[]) => void; + applyFileOperation: (operation: FileOperation) => void; + undoLastOperation: (fileId?: string) => void; + + // Operation history management + recordOperation: (fileId: string, operation: FileOperation | PageOperation) => void; + markOperationApplied: (fileId: string, operationId: string) => void; + markOperationFailed: (fileId: string, operationId: string, error: string) => void; + getFileHistory: (fileId: string) => FileOperationHistory | undefined; + getAppliedOperations: (fileId: string) => (FileOperation | PageOperation)[]; + clearFileHistory: (fileId: string) => void; + + // Viewer state + updateViewerConfig: (config: Partial) => void; + + // Export configuration + setExportConfig: (config: FileContextState['lastExportConfig']) => void; + + + // Utility + getFileById: (fileId: string) => File | undefined; + getProcessedFileById: (fileId: string) => ProcessedFile | undefined; + getCurrentFile: () => File | undefined; + getCurrentProcessedFile: () => ProcessedFile | undefined; + + // Context persistence + saveContext: () => Promise; + loadContext: () => Promise; + resetContext: () => void; + + // Navigation guard system + setHasUnsavedChanges: (hasChanges: boolean) => void; + requestNavigation: (navigationFn: () => void) => boolean; + confirmNavigation: () => void; + cancelNavigation: () => void; + + // Memory management + trackBlobUrl: (url: string) => void; + trackPdfDocument: (fileId: string, pdfDoc: any) => void; + cleanupFile: (fileId: string) => Promise; + scheduleCleanup: (fileId: string, delay?: number) => void; +} + +export interface FileContextValue extends FileContextState, FileContextActions {} + +export interface FileContextProviderProps { + children: React.ReactNode; + enableUrlSync?: boolean; + enablePersistence?: boolean; + maxCacheSize?: number; +} + +// Helper types for component props +export interface WithFileContext { + fileContext: FileContextValue; +} + +// URL parameter types for deep linking +export interface FileContextUrlParams { + mode?: ModeType; + // Legacy parameters for backward compatibility + view?: ViewType; + tool?: ToolType; + fileIds?: string[]; + pageIds?: string[]; + zoom?: number; + page?: number; +} \ No newline at end of file diff --git a/frontend/src/types/pageEditor.ts b/frontend/src/types/pageEditor.ts index c4fc19bdd..7e0dda16e 100644 --- a/frontend/src/types/pageEditor.ts +++ b/frontend/src/types/pageEditor.ts @@ -1,7 +1,7 @@ export interface PDFPage { id: string; pageNumber: number; - thumbnail: string; + thumbnail: string | null; rotation: number; selected: boolean; splitBefore?: boolean; @@ -16,12 +16,23 @@ export interface PDFDocument { } export interface PageOperation { - type: 'rotate' | 'delete' | 'move' | 'split' | 'insert'; + id: string; + type: 'rotate' | 'delete' | 'move' | 'split' | 'insert' | 'reorder'; pageIds: string[]; + timestamp: number; + status: 'pending' | 'applied' | 'failed'; data?: any; + metadata?: { + rotation?: number; + fromPosition?: number; + toPosition?: number; + splitType?: string; + insertAfterPage?: number; + error?: string; + }; } export interface UndoRedoState { operations: PageOperation[]; currentIndex: number; -} \ No newline at end of file +} diff --git a/frontend/src/types/processing.ts b/frontend/src/types/processing.ts new file mode 100644 index 000000000..65b996d7f --- /dev/null +++ b/frontend/src/types/processing.ts @@ -0,0 +1,91 @@ +export interface ProcessingError { + type: 'network' | 'parsing' | 'memory' | 'corruption' | 'timeout' | 'cancelled'; + message: string; + recoverable: boolean; + retryCount: number; + maxRetries: number; + originalError?: Error; +} + +export interface ProcessingState { + fileKey: string; + fileName: string; + status: 'pending' | 'processing' | 'completed' | 'error' | 'cancelled'; + progress: number; // 0-100 + strategy: ProcessingStrategy; + error?: ProcessingError; + startedAt: number; + completedAt?: number; + estimatedTimeRemaining?: number; + currentPage?: number; + cancellationToken?: AbortController; +} + +export interface ProcessedFile { + id: string; + pages: PDFPage[]; + totalPages: number; + metadata: { + title: string; + createdAt: string; + modifiedAt: string; + }; +} + +export interface PDFPage { + id: string; + pageNumber: number; + thumbnail: string | null; + rotation: number; + selected: boolean; + splitBefore?: boolean; +} + +export interface CacheConfig { + maxFiles: number; + maxSizeBytes: number; + ttlMs: number; +} + +export interface CacheEntry { + data: ProcessedFile; + size: number; + lastAccessed: number; + createdAt: number; +} + +export interface CacheStats { + entries: number; + totalSizeBytes: number; + maxSizeBytes: number; +} + +export type ProcessingStrategy = 'immediate_full' | 'progressive_chunked' | 'metadata_only' | 'priority_pages'; + +export interface ProcessingConfig { + strategy: ProcessingStrategy; + chunkSize: number; // Pages per chunk + thumbnailQuality: 'low' | 'medium' | 'high'; + priorityPageCount: number; // Number of priority pages to process first + useWebWorker: boolean; + maxRetries: number; + timeoutMs: number; +} + +export interface FileAnalysis { + fileSize: number; + estimatedPageCount?: number; + isEncrypted: boolean; + isCorrupted: boolean; + recommendedStrategy: ProcessingStrategy; + estimatedProcessingTime: number; // milliseconds +} + +export interface ProcessingMetrics { + totalFiles: number; + completedFiles: number; + failedFiles: number; + averageProcessingTime: number; + cacheHitRate: number; + memoryUsage: number; +} \ No newline at end of file diff --git a/frontend/src/utils/fileHash.ts b/frontend/src/utils/fileHash.ts new file mode 100644 index 000000000..3ff911a56 --- /dev/null +++ b/frontend/src/utils/fileHash.ts @@ -0,0 +1,127 @@ +/** + * File hashing utilities for cache key generation + */ + +export class FileHasher { + private static readonly CHUNK_SIZE = 64 * 1024; // 64KB chunks for hashing + + /** + * Generate a content-based hash for a file + * Uses first + last + middle chunks to create a reasonably unique hash + * without reading the entire file (which would be expensive for large files) + */ + static async generateContentHash(file: File): Promise { + const chunks = await this.getFileChunks(file); + const combined = await this.combineChunks(chunks); + return await this.hashArrayBuffer(combined); + } + + /** + * Generate a fast hash based on file metadata + * Faster but less collision-resistant than content hash + */ + static generateMetadataHash(file: File): string { + const data = `${file.name}-${file.size}-${file.lastModified}-${file.type}`; + return this.simpleHash(data); + } + + /** + * Generate a hybrid hash that balances speed and uniqueness + * Uses metadata + small content sample + */ + static async generateHybridHash(file: File): Promise { + const metadataHash = this.generateMetadataHash(file); + + // For small files, use full content hash + if (file.size <= 1024 * 1024) { // 1MB + const contentHash = await this.generateContentHash(file); + return `${metadataHash}-${contentHash}`; + } + + // For large files, use first chunk only + const firstChunk = file.slice(0, this.CHUNK_SIZE); + const firstChunkBuffer = await firstChunk.arrayBuffer(); + const firstChunkHash = await this.hashArrayBuffer(firstChunkBuffer); + + return `${metadataHash}-${firstChunkHash}`; + } + + private static async getFileChunks(file: File): Promise { + const chunks: ArrayBuffer[] = []; + + // First chunk + if (file.size > 0) { + const firstChunk = file.slice(0, Math.min(this.CHUNK_SIZE, file.size)); + chunks.push(await firstChunk.arrayBuffer()); + } + + // Middle chunk (if file is large enough) + if (file.size > this.CHUNK_SIZE * 2) { + const middleStart = Math.floor(file.size / 2) - Math.floor(this.CHUNK_SIZE / 2); + const middleEnd = middleStart + this.CHUNK_SIZE; + const middleChunk = file.slice(middleStart, middleEnd); + chunks.push(await middleChunk.arrayBuffer()); + } + + // Last chunk (if file is large enough and different from first) + if (file.size > this.CHUNK_SIZE) { + const lastStart = Math.max(file.size - this.CHUNK_SIZE, this.CHUNK_SIZE); + const lastChunk = file.slice(lastStart); + chunks.push(await lastChunk.arrayBuffer()); + } + + return chunks; + } + + private static async combineChunks(chunks: ArrayBuffer[]): Promise { + const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0); + const combined = new Uint8Array(totalLength); + + let offset = 0; + for (const chunk of chunks) { + combined.set(new Uint8Array(chunk), offset); + offset += chunk.byteLength; + } + + return combined.buffer; + } + + private static async hashArrayBuffer(buffer: ArrayBuffer): Promise { + // Use Web Crypto API for proper hashing + if (crypto.subtle) { + const hashBuffer = await crypto.subtle.digest('SHA-256', buffer); + const hashArray = Array.from(new Uint8Array(hashBuffer)); + return hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); + } + + // Fallback for environments without crypto.subtle + return this.simpleHash(Array.from(new Uint8Array(buffer)).join('')); + } + + private static simpleHash(str: string): string { + let hash = 0; + if (str.length === 0) return hash.toString(); + + for (let i = 0; i < str.length; i++) { + const char = str.charCodeAt(i); + hash = ((hash << 5) - hash) + char; + hash = hash & hash; // Convert to 32-bit integer + } + + return Math.abs(hash).toString(16); + } + + /** + * Validate that a file matches its expected hash + * Useful for detecting file corruption or changes + */ + static async validateFileHash(file: File, expectedHash: string): Promise { + try { + const actualHash = await this.generateHybridHash(file); + return actualHash === expectedHash; + } catch (error) { + console.error('Hash validation failed:', error); + return false; + } + } +} \ No newline at end of file diff --git a/frontend/src/utils/thumbnailUtils.ts b/frontend/src/utils/thumbnailUtils.ts index 1bc9bf069..f0c28631a 100644 --- a/frontend/src/utils/thumbnailUtils.ts +++ b/frontend/src/utils/thumbnailUtils.ts @@ -1,5 +1,19 @@ import { getDocument } from "pdfjs-dist"; +/** + * Calculate thumbnail scale based on file size + * Smaller files get higher quality, larger files get lower quality + */ +export function calculateScaleFromFileSize(fileSize: number): number { + const MB = 1024 * 1024; + + if (fileSize < 1 * MB) return 0.6; // < 1MB: High quality + if (fileSize < 5 * MB) return 0.4; // 1-5MB: Medium-high quality + if (fileSize < 15 * MB) return 0.3; // 5-15MB: Medium quality + if (fileSize < 30 * MB) return 0.2; // 15-30MB: Low-medium quality + return 0.15; // 30MB+: Low quality +} + /** * Generate thumbnail for a PDF file during upload * Returns base64 data URL or undefined if generation fails @@ -14,6 +28,10 @@ export async function generateThumbnailForFile(file: File): Promise