@@ -46,7 +46,7 @@ export default createStore({
46
46
loading : false ,
47
47
logedin : false ,
48
48
processing : false ,
49
- pageDimenssion : [ ] ,
49
+ pageDimension : [ ] ,
50
50
mode : allowedModes . selection ,
51
51
existingMusicMode : false ,
52
52
selectedZoneId : null ,
@@ -796,77 +796,152 @@ export default createStore({
796
796
commit ( 'SET_BRANCH' , branch )
797
797
} ,
798
798
importIIIF ( { commit, dispatch, state } , url ) {
799
- commit ( 'SET_LOADING' , true )
799
+ commit ( 'SET_LOADING' , true ) ;
800
+
801
+ // Fetch the IIIF manifest
800
802
fetch ( url )
801
- . then ( res => {
802
- return res . json ( )
803
- } )
803
+ . then ( res => res . json ( ) )
804
804
. then ( json => {
805
- commit ( 'SET_LOADING' , false )
806
- commit ( 'SET_PROCESSING' , true )
807
-
805
+ commit ( 'SET_LOADING' , false ) ;
806
+ commit ( 'SET_PROCESSING' , true ) ;
807
+
808
808
let canvases = json . sequences [ 0 ] . canvases ;
809
- for ( let i = 0 ; i < canvases . length ; i ++ ) {
810
- // Do something with canvas
811
- state . infoJson . push ( canvases [ i ] . images [ 0 ] . resource . service [ '@id' ] + "/info.json" )
812
-
813
- }
814
- let fetchPromises = [ ] ;
815
-
816
- for ( let i = 0 ; i < state . infoJson . length ; i ++ ) {
817
- // Add each fetch promise to the array
818
- fetchPromises . push (
819
- fetch ( state . infoJson [ i ] )
820
- . then ( res => res . json ( ) ) // Parse the JSON from the response
821
- . then ( result => {
822
- // check if this is a proper IIIF Manifest, then convert to MEI
823
- const isManifest = checkIiifManifest ( json )
824
- if ( ! isManifest ) {
825
- // do some error handling
826
- return false
827
- }
828
-
829
- // Access the width and height from the result
830
- const width = result . width ;
831
- const height = result . height ;
832
-
833
- // Push the dimensions into the state
834
- state . pageDimenssion . push ( [ width , height ] ) ;
835
- } )
836
- . catch ( error => {
837
- console . error ( "Error:" , error ) ;
838
- } )
839
- ) ;
840
- }
841
-
842
- // Use Promise.all to wait for all fetch requests to complete
843
- Promise . all ( fetchPromises )
809
+
810
+ // Process each canvas one by one, sequentially
811
+ const processCanvasesSequentially = async ( ) => {
812
+ for ( let i = 0 ; i < canvases . length ; i ++ ) {
813
+ try {
814
+ // Build the info.json URL for the current canvas
815
+ const infoUrl = canvases [ i ] . images [ 0 ] . resource . service [ '@id' ] + "/info.json" ;
816
+
817
+ // Push the URL to the state.infoJson array
818
+ state . infoJson . push ( infoUrl ) ;
819
+
820
+ // Fetch the info.json
821
+ const res = await fetch ( infoUrl ) ;
822
+ const result = await res . json ( ) ;
823
+
824
+ // Check if this is a proper IIIF Manifest, then convert to MEI
825
+ const isManifest = checkIiifManifest ( json ) ;
826
+ if ( ! isManifest ) {
827
+ throw new Error ( "Invalid IIIF manifest" ) ;
828
+ }
829
+
830
+ // Extract the width and height from the result
831
+ const width = result . width ;
832
+ const height = result . height ;
833
+
834
+ // Push the dimensions into the state.pageDimension array
835
+ state . pageDimension . push ( [ width , height ] ) ;
836
+
837
+ } catch ( error ) {
838
+ console . error ( `Error processing canvas ${ i } :` , error ) ;
839
+ throw error ; // This will stop the loop and be caught in the outer catch block
840
+ }
841
+ }
842
+ } ;
843
+
844
+ // Call the sequential processing function
845
+ processCanvasesSequentially ( )
844
846
. then ( ( ) => {
845
-
846
- iiifManifest2mei ( json , url , parser , state )
847
-
848
- . then ( mei => {
849
- dispatch ( 'setData' , mei )
850
- } )
851
- } )
852
- . catch ( err => {
853
- commit ( 'SET_LOADING' , false )
854
- console . log ( err )
855
- // add some error message
847
+ // After processing all canvases, convert the manifest to MEI
848
+ return iiifManifest2mei ( json , url , parser , state ) ;
849
+ } )
850
+ . then ( mei => {
851
+ // Dispatch setData with the generated MEI
852
+ dispatch ( 'setData' , mei ) ;
853
+ } )
854
+ . catch ( err => {
855
+ console . error ( 'Error processing IIIF manifest or canvases:' , err ) ;
856
+ commit ( 'SET_LOADING' , false ) ;
857
+ // Add any additional error messaging here
858
+ } )
859
+ . finally ( ( ) => {
860
+ commit ( 'SET_PROCESSING' , false ) ; // Ensure processing is set to false after completion
861
+ } ) ;
862
+ } )
863
+ . catch ( error => {
864
+ // Handle errors in the initial IIIF manifest fetch
865
+ console . error ( 'Error fetching IIIF manifest:' , error ) ;
866
+ commit ( 'SET_LOADING' , false ) ;
867
+ } ) ;
868
+ } ,
869
+ importIIIF ( { commit, dispatch, state } , url ) {
870
+ commit ( 'SET_LOADING' , true ) ;
871
+
872
+ // Fetch the IIIF manifest
873
+ fetch ( url )
874
+ . then ( res => res . json ( ) )
875
+ . then ( json => {
876
+ commit ( 'SET_LOADING' , false ) ;
877
+ commit ( 'SET_PROCESSING' , true ) ;
878
+
879
+ let canvases = json . sequences [ 0 ] . canvases ;
880
+
881
+ // Map all canvas images to their info.json URLs
882
+ const infoJsonUrls = canvases . map ( canvas => {
883
+ return canvas . images [ 0 ] . resource . service [ '@id' ] + "/info.json" ;
884
+ } ) ;
885
+
886
+ // Store all fetch promises for info.json
887
+ const fetchPromises = infoJsonUrls . map ( ( infoUrl ) => {
888
+ return fetch ( infoUrl )
889
+ . then ( res => res . json ( ) )
890
+ . then ( result => {
891
+ // Check if this is a proper IIIF Manifest
892
+ const isManifest = checkIiifManifest ( json ) ;
893
+ if ( ! isManifest ) {
894
+ throw new Error ( "Invalid IIIF manifest" ) ;
895
+ }
896
+
897
+ // Extract the width and height from the result
898
+ const width = result . width ;
899
+ const height = result . height ;
900
+
901
+ // Return both the infoUrl and the dimensions
902
+ return { infoUrl, dimensions : [ width , height ] } ;
856
903
} )
904
+ . catch ( error => {
905
+ console . error ( `Error fetching ${ infoUrl } :` , error ) ;
906
+ // Return a fallback object in case of error, to keep the promise chain going
907
+ return { infoUrl, dimensions : [ null , null ] , error : true } ;
908
+ } ) ;
909
+ } ) ;
910
+
911
+ // Use Promise.allSettled to fetch all info.json files concurrently and wait for all of them
912
+ Promise . allSettled ( fetchPromises )
913
+ . then ( ( results ) => {
914
+ // Update state.infoJson and state.pageDimension in batches
915
+ results . forEach ( result => {
916
+ if ( result . status === 'fulfilled' ) {
917
+ state . infoJson . push ( result . value . infoUrl ) ;
918
+ state . pageDimension . push ( result . value . dimensions ) ;
919
+ }
920
+ } ) ;
921
+
922
+ // After processing all canvases, convert the manifest to MEI
923
+ return iiifManifest2mei ( json , url , parser , state ) ;
857
924
} )
858
- . catch ( error => {
859
- console . error ( "Error with one of the promises:" , error ) ;
925
+ . then ( mei => {
926
+ // Dispatch setData with the generated MEI
927
+ dispatch ( 'setData' , mei ) ;
928
+ } )
929
+ . catch ( err => {
930
+ console . error ( 'Error processing IIIF manifest or canvases:' , err ) ;
931
+ commit ( 'SET_LOADING' , false ) ;
932
+ // Add any additional error messaging here
933
+ } )
934
+ . finally ( ( ) => {
935
+ commit ( 'SET_PROCESSING' , false ) ; // Ensure processing is set to false after completion
860
936
} ) ;
861
-
862
-
863
-
864
-
865
-
866
-
867
-
868
-
937
+ } )
938
+ . catch ( error => {
939
+ // Handle errors in the initial IIIF manifest fetch
940
+ console . error ( 'Error fetching IIIF manifest:' , error ) ;
941
+ commit ( 'SET_LOADING' , false ) ;
942
+ } ) ;
869
943
} ,
944
+
870
945
importXML ( { commit, dispatch } , mei ) {
871
946
fetch ( mei )
872
947
. then ( res => {
0 commit comments