Skip to content

Commit

Permalink
Enable "Save" button when there is an unsaved annotation
Browse files Browse the repository at this point in the history
The "Save" button was always enabled, but saving and uploading the file
would be needed only if there is an unsaved annotation. Therefore now
the button is initially disabled, and it is enabled only when an
annotation is added; once the file is saved the button is disabled and
it will be enabled again once a new annotation is added.

Signed-off-by: Daniel Calviño Sánchez <[email protected]>
  • Loading branch information
danxuliu committed Oct 23, 2023
1 parent 17a3550 commit ef2f09c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions js/files_pdfviewer-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/files_pdfviewer-main.js.map

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion src/views/PDFView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ export default {
this.PDFViewerApplication = this.$refs.iframe.contentWindow.PDFViewerApplication
this.PDFViewerApplication.save = this.handleSave
// Not all fields of PDFViewerApplication are reactive.
// Specifically, it can not be known if annotations were created
// by watching "pdfDocument.annotationStorage.size" (maybe
// because "size" is a getter based on a private field, so it
// does not work even if the rest of the chain is skipped and
// "size" is directly watched). However, "annotationStorage" has
// callbacks used by PDFViewerApplication to know when an
// annotation was set, so that callback can be wrapped to also
// enable the save button.
this.PDFViewerApplication.eventBus.on('documentinit', () => {
const annotationStorage = this.PDFViewerApplication.pdfDocument.annotationStorage
const onSetModifiedOriginal = annotationStorage.onSetModified
annotationStorage.onSetModified = () => {
onSetModifiedOriginal.apply(null, arguments)
this.getDownloadElement().removeAttribute('disabled')
}
})
})
},
Expand All @@ -138,9 +158,13 @@ export default {
logger.error('Error uploading file:', error)
showError(t('files_pdfviewer', 'File upload failed.'))
// Enable button again only if the upload failed; if it was
// successful it will be enabled again when a new annotation is
// added.
downloadElement.removeAttribute('disabled')
}).finally(() => {
downloadElement.classList.remove('icon-loading-small')
downloadElement.removeAttribute('disabled')
})
},
},
Expand Down
2 changes: 1 addition & 1 deletion templates/viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
<span data-l10n-id="print_label">Print</span>
</button>

<button id="download" class="toolbarButton hiddenMediumView" title="Save" tabindex="33" data-l10n-id="save" hidden="true">
<button id="download" class="toolbarButton hiddenMediumView" disabled="disabled" title="Save" tabindex="33" data-l10n-id="save" hidden="true">
<span data-l10n-id="save_label">Save</span>
</button>

Expand Down

0 comments on commit ef2f09c

Please sign in to comment.