@@ -1072,9 +1072,9 @@ const PDFViewerApplication = {
10721072 // Embedded PDF viewers should not be changing their parent page's title.
10731073 return ;
10741074 }
1075- const editorIndicator =
1076- this . _hasAnnotationEditors && ! this . pdfRenderingQueue . printing ;
1077- document . title = `${ editorIndicator ? "* " : "" } ${ title } ` ;
1075+ const hasChangesIndicator =
1076+ this . _hasChanges ( ) && ! this . pdfRenderingQueue . printing ;
1077+ document . title = `${ hasChangesIndicator ? "* " : "" } ${ title } ` ;
10781078 } ,
10791079
10801080 get _docFilename ( ) {
@@ -1129,12 +1129,12 @@ const PDFViewerApplication = {
11291129 if (
11301130 ( typeof PDFJSDev === "undefined" ||
11311131 PDFJSDev . test ( "GENERIC && !TESTING" ) ) &&
1132- this . pdfDocument ?. annotationStorage . size > 0 &&
1132+ this . _hasChanges ( ) &&
11331133 this . _annotationStorageModified
11341134 ) {
11351135 try {
11361136 // Trigger saving, to prevent data loss in forms; see issue 12257.
1137- await this . save ( ) ;
1137+ await this . downloadOrSave ( ) ;
11381138 } catch {
11391139 // Ignoring errors, to ensure that document closing won't break.
11401140 }
@@ -1315,9 +1315,15 @@ const PDFViewerApplication = {
13151315 // a message and change PdfjsChild.sys.mjs to take it into account.
13161316 const { classList } = this . appConfig . appContainer ;
13171317 classList . add ( "wait" ) ;
1318- await ( this . pdfDocument ?. annotationStorage . size > 0
1319- ? this . save ( )
1320- : this . download ( ) ) ;
1318+
1319+ const structuralChanges = this . pdfThumbnailViewer ?. getStructuralChanges ( ) ;
1320+ if ( structuralChanges ) {
1321+ await this . onSavePages ( { data : structuralChanges } ) ;
1322+ } else {
1323+ await ( this . pdfDocument ?. annotationStorage . size > 0
1324+ ? this . save ( )
1325+ : this . download ( ) ) ;
1326+ }
13211327 classList . remove ( "wait" ) ;
13221328 } ,
13231329
@@ -1862,6 +1868,13 @@ const PDFViewerApplication = {
18621868 }
18631869 } ,
18641870
1871+ _hasChanges ( ) {
1872+ return (
1873+ this . pdfDocument ?. annotationStorage . size > 0 ||
1874+ this . pdfThumbnailViewer ?. hasStructuralChanges ( )
1875+ ) ;
1876+ } ,
1877+
18651878 /**
18661879 * @private
18671880 */
@@ -1872,15 +1885,11 @@ const PDFViewerApplication = {
18721885 const { annotationStorage } = pdfDocument ;
18731886
18741887 annotationStorage . onSetModified = ( ) => {
1875- window . addEventListener ( "beforeunload" , beforeUnload ) ;
1876-
18771888 if ( typeof PDFJSDev === "undefined" || PDFJSDev . test ( "GENERIC" ) ) {
18781889 this . _annotationStorageModified = true ;
18791890 }
18801891 } ;
18811892 annotationStorage . onResetModified = ( ) => {
1882- window . removeEventListener ( "beforeunload" , beforeUnload ) ;
1883-
18841893 if ( typeof PDFJSDev === "undefined" || PDFJSDev . test ( "GENERIC" ) ) {
18851894 delete this . _annotationStorageModified ;
18861895 }
@@ -2185,11 +2194,7 @@ const PDFViewerApplication = {
21852194 ) ;
21862195 }
21872196 eventBus . _on ( "pagesedited" , this . onPagesEdited . bind ( this ) , opts ) ;
2188- eventBus . _on (
2189- "savepageseditedpdf" ,
2190- this . onSavePagesEditedPDF . bind ( this ) ,
2191- opts
2192- ) ;
2197+ eventBus . _on ( "saveextractedpages" , this . onSavePages . bind ( this ) , opts ) ;
21932198 } ,
21942199
21952200 bindWindowEvents ( ) {
@@ -2270,6 +2275,9 @@ const PDFViewerApplication = {
22702275 } ,
22712276 { signal }
22722277 ) ;
2278+ window . addEventListener ( "beforeunload" , onBeforeUnload . bind ( this ) , {
2279+ signal,
2280+ } ) ;
22732281
22742282 if (
22752283 ( typeof PDFJSDev === "undefined" || ! PDFJSDev . test ( "MOZCENTRAL" ) ) &&
@@ -2368,7 +2376,7 @@ const PDFViewerApplication = {
23682376 this . pdfViewer . onPagesEdited ( data ) ;
23692377 } ,
23702378
2371- async onSavePagesEditedPDF ( { data : extractParams } ) {
2379+ async onSavePages ( { data : extractParams } ) {
23722380 if ( typeof PDFJSDev !== "undefined" && PDFJSDev . test ( "TESTING" ) ) {
23732381 return ;
23742382 }
@@ -2876,6 +2884,15 @@ function closeEditorUndoBar(evt) {
28762884 }
28772885}
28782886
2887+ function onBeforeUnload ( evt ) {
2888+ if ( this . _hasChanges ( ) ) {
2889+ evt . preventDefault ( ) ;
2890+ evt . returnValue = "" ;
2891+ return false ;
2892+ }
2893+ return true ;
2894+ }
2895+
28792896function onClick ( evt ) {
28802897 closeSecondaryToolbar . call ( this , evt ) ;
28812898 closeEditorUndoBar . call ( this , evt ) ;
@@ -3230,10 +3247,4 @@ function onKeyDown(evt) {
32303247 }
32313248}
32323249
3233- function beforeUnload ( evt ) {
3234- evt . preventDefault ( ) ;
3235- evt . returnValue = "" ;
3236- return false ;
3237- }
3238-
32393250export { PDFViewerApplication } ;
0 commit comments