From 5280c125d7bbda4bd569d0aa1ffe19fdf783ea29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Wed, 8 Jan 2025 17:54:15 +0100 Subject: [PATCH] Move PDFViewerApplication setup from helper script to Vue component MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Calviño Sánchez [skip ci] --- src/views/PDFView.vue | 43 +++++++++++++++++++++++++++++++++++-- src/workersrc.js | 50 ------------------------------------------- 2 files changed, 41 insertions(+), 52 deletions(-) diff --git a/src/views/PDFView.vue b/src/views/PDFView.vue index 62d9dff8..cb170388 100644 --- a/src/views/PDFView.vue +++ b/src/views/PDFView.vue @@ -53,8 +53,7 @@ export default { computed: { iframeSrc() { - return generateUrl('/apps/files_pdfviewer/?file={file}&hideDownload={hideDownload}', { - hideDownload: hideDownload() ? 1 : 0, + return generateUrl('/apps/files_pdfviewer/?file={file}', { file: this.source ?? this.davPath, }) }, @@ -202,6 +201,46 @@ export default { this.getDownloadElement().removeAttribute('disabled') } }) + + if (hideDownload()) { + const pdfViewer = this.getIframeDocument().querySelector('.pdfViewer') + + if (pdfViewer) { + pdfViewer.classList.add('disabledTextSelection') + } + + // Disable download function when downloads are hidden, as even + // if the buttons in the UI are hidden the download could still + // be triggered with Ctrl|Meta+S. + this.PDFViewerApplication.download = () => { + } + + // Disable printing service when downloads are hidden, as even + // if the buttons in the UI are hidden the printing could still + // be triggered with Ctrl|Meta+P. + // Abuse the "supportsPrinting" parameter, which signals that + // the browser does not fully support printing, to make + // PDFViewer disable the printing service. + // "supportsPrinting" is a getter function, so it needs to be + // deleted before replacing it with a simple value. + delete this.PDFViewerApplication.supportsPrinting + this.PDFViewerApplication.supportsPrinting = false + + // When printing is not supported a warning is shown by the + // default "beforePrint" function when trying to print. That + // function needs to be replaced with an empty one to prevent + // that warning to be shown. + this.PDFViewerApplication.beforePrint = () => { + } + + logger.info('Download, print and user interaction disabled') + } else { + logger.info('Download and print available') + } + + const PDFViewerApplicationOptions = this.$refs.iframe.contentWindow.PDFViewerApplicationOptions + + logger.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll()) }, handleWebviewerloaded() { diff --git a/src/workersrc.js b/src/workersrc.js index a2ebcc51..87e68bb3 100644 --- a/src/workersrc.js +++ b/src/workersrc.js @@ -21,57 +21,7 @@ * */ -import logger from './services/logger.js' import redirectIfNotIframe from './utils/redirectIfNotIframe.js' // Checks if the page is displayed in an iframe. If not redirect to /. redirectIfNotIframe() - -// Retrieve the hideDownload from the url, this is -// the most easy way to pass the prop to this iframe -const queryString = window.location.search -const urlParams = new URLSearchParams(queryString) -const hideDownload = urlParams.get('hideDownload') - -function initializeCustomPDFViewerApplication() { - if (hideDownload === '1') { - const pdfViewer = window.document.querySelector('.pdfViewer') - - if (pdfViewer) { - pdfViewer.classList.add('disabledTextSelection') - } - - if (PDFViewerApplication) { - // Disable download function when downloads are hidden, as even if the - // buttons in the UI are hidden the download could still be triggered - // with Ctrl|Meta+S. - PDFViewerApplication.download = function() { - } - - // Disable printing service when downloads are hidden, as even if the - // buttons in the UI are hidden the printing could still be triggered - // with Ctrl|Meta+P. - // Abuse the "supportsPrinting" parameter, which signals that the - // browser does not fully support printing, to make PDFViewer disable - // the printing service. - // "supportsPrinting" is a getter function, so it needs to be deleted - // before replacing it with a simple value. - delete PDFViewerApplication.supportsPrinting - PDFViewerApplication.supportsPrinting = false - - // When printing is not supported a warning is shown by the default - // "beforePrint" function when trying to print. That function needs to - // be replaced with an empty one to prevent that warning to be shown. - PDFViewerApplication.beforePrint = function() { - } - } - - logger.info('Download, print and user interaction disabled') - } else { - logger.info('Download and print available') - } - - logger.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll()) -} - -document.addEventListener('DOMContentLoaded', initializeCustomPDFViewerApplication, true)