66 * - npm package: Uses ES6 import (when USE_NPM=true)
77 *
88 * Features:
9- * - Drag and drop PDF loading
10- * - File picker for PDF selection
11- * - Tracks unsaved annotations
129 * - Loads a default sample PDF on page load
10+ * - Simple button to load another document
1311 */
1412
15- import dragDrop from "drag-drop" ;
16- import { processFiles } from "./utils.js" ;
17-
1813// Dynamic import for npm package mode
1914let NutrientViewer ;
2015
@@ -39,68 +34,30 @@ async function initializeNutrientViewer() {
3934 return true ;
4035}
4136
42- let hasUnsavedAnnotations = false ;
43- let isAlreadyLoaded = false ;
4437let currentInstance = null ;
4538
4639// Load the default sample PDF on page load
4740window . addEventListener ( "DOMContentLoaded" , async ( ) => {
4841 const initialized = await initializeNutrientViewer ( ) ;
4942 if ( initialized ) {
50- loadDefaultPDF ( ) ;
43+ loadDocument ( "nutrient-web-demo.pdf" ) ;
5144 }
5245} ) ;
5346
5447/**
55- * Loads the default sample PDF from the public/assets folder
56- */
57- async function loadDefaultPDF ( ) {
58- try {
59- const response = await fetch ( "/assets/nutrient-web-demo.pdf" ) ;
60- const arrayBuffer = await response . arrayBuffer ( ) ;
61- load ( [ arrayBuffer ] ) ;
62- } catch ( error ) {
63- console . error ( "Failed to load default PDF:" , error ) ;
64- console . log ( "You can still upload your own PDF using the controls above." ) ;
65- }
66- }
67-
68- /**
69- * Creates an onAnnotationsChange handler that keeps track of changes.
70- * Skips the first call since `annotations.change` fires when the PDF viewer
71- * is initialized and populated with annotations.
48+ * Loads a PDF document from the public/assets folder
49+ * @param {string } documentName - The name of the PDF file to load
7250 */
73- const createOnAnnotationsChange = ( ) => {
74- let initialized = false ;
75-
76- return ( ) => {
77- if ( initialized ) {
78- hasUnsavedAnnotations = true ;
79- } else {
80- initialized = true ;
81- }
82- } ;
83- } ;
84-
85- /**
86- * Main load function invoked when a dropped or selected file (PDF)
87- * has been successfully read as ArrayBuffer.
88- */
89- async function load ( pdfArrayBuffers ) {
90- const pdfArrayBuffer = pdfArrayBuffers [ 0 ] ;
91-
92- if ( isAlreadyLoaded && currentInstance ) {
51+ async function loadDocument ( documentName ) {
52+ if ( currentInstance ) {
9353 console . info ( "Destroyed previous instance" ) ;
9454 NutrientViewer . unload ( ".App" ) ;
95- hasUnsavedAnnotations = false ;
9655 currentInstance = null ;
9756 }
9857
99- isAlreadyLoaded = true ;
100-
10158 const configuration = {
10259 container : ".App" ,
103- document : pdfArrayBuffer ,
60+ document : `/assets/ ${ documentName } ` ,
10461 } ;
10562
10663 // Add baseUrl for npm mode
@@ -110,64 +67,19 @@ async function load(pdfArrayBuffers) {
11067
11168 try {
11269 currentInstance = await NutrientViewer . load ( configuration ) ;
113- currentInstance . addEventListener (
114- "annotations.change" ,
115- createOnAnnotationsChange ( ) ,
116- ) ;
11770 console . log ( "Nutrient loaded" , currentInstance ) ;
11871 } catch ( error ) {
11972 console . error ( "Failed to load Nutrient:" , error ) ;
120- isAlreadyLoaded = false ;
12173 }
12274}
12375
124- /**
125- * Helper functions for file handling
126- */
127- function onFail ( { message } ) {
128- alert ( message ) ;
129- }
130-
131- function shouldPreventLoad ( ) {
132- return (
133- hasUnsavedAnnotations &&
134- ! window . confirm (
135- "You have unsaved changes. By continuing, you will lose those changes." ,
136- )
137- ) ;
138- }
139-
140- /**
141- * Handle drag and drop functionality
142- */
143- const destroyListener = dragDrop ( "body" , {
144- onDrop : ( files ) => {
145- if ( shouldPreventLoad ( ) ) {
146- return ;
147- }
148-
149- processFiles ( files )
150- . then ( ( arrayBuffers ) => {
151- load ( arrayBuffers ) ;
152- } )
153- . catch ( onFail ) ;
154- } ,
155- } ) ;
156-
157- /**
158- * Handle file picker functionality
159- */
160- document . querySelector ( "#selectFile" ) . addEventListener ( "change" , ( event ) => {
161- if ( ! event . target . files . length || shouldPreventLoad ( ) ) {
162- event . target . value = null ;
163- return ;
76+ // Add button handler for switching documents
77+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
78+ const openButton = document . querySelector ( "#openAnotherDoc" ) ;
79+ if ( openButton ) {
80+ openButton . addEventListener ( "click" , ( ) => {
81+ // Switch to another document (you can change this filename as needed)
82+ loadDocument ( "another-example.pdf" ) ;
83+ } ) ;
16484 }
165-
166- processFiles ( [ ...event . target . files ] )
167- . then ( ( arrayBuffers ) => {
168- load ( arrayBuffers ) ;
169- } )
170- . catch ( onFail ) ;
171-
172- event . target . value = null ;
17385} ) ;
0 commit comments