Skip to content

Commit f5c69f0

Browse files
committed
Move PDFViewerApplication setup from helper script to Vue component
Signed-off-by: Daniel Calviño Sánchez <[email protected]>
1 parent 438c630 commit f5c69f0

File tree

2 files changed

+41
-52
lines changed

2 files changed

+41
-52
lines changed

src/views/PDFView.vue

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ export default {
3535

3636
computed: {
3737
iframeSrc() {
38-
return generateUrl('/apps/files_pdfviewer/?file={file}&hideDownload={hideDownload}', {
39-
hideDownload: hideDownload() ? 1 : 0,
38+
return generateUrl('/apps/files_pdfviewer/?file={file}', {
4039
file: this.source ?? this.davPath,
4140
})
4241
},
@@ -184,6 +183,46 @@ export default {
184183
this.getDownloadElement().removeAttribute('disabled')
185184
}
186185
})
186+
187+
if (hideDownload()) {
188+
const pdfViewer = this.getIframeDocument().querySelector('.pdfViewer')
189+
190+
if (pdfViewer) {
191+
pdfViewer.classList.add('disabledTextSelection')
192+
}
193+
194+
// Disable download function when downloads are hidden, as even
195+
// if the buttons in the UI are hidden the download could still
196+
// be triggered with Ctrl|Meta+S.
197+
this.PDFViewerApplication.download = () => {
198+
}
199+
200+
// Disable printing service when downloads are hidden, as even
201+
// if the buttons in the UI are hidden the printing could still
202+
// be triggered with Ctrl|Meta+P.
203+
// Abuse the "supportsPrinting" parameter, which signals that
204+
// the browser does not fully support printing, to make
205+
// PDFViewer disable the printing service.
206+
// "supportsPrinting" is a getter function, so it needs to be
207+
// deleted before replacing it with a simple value.
208+
delete this.PDFViewerApplication.supportsPrinting
209+
this.PDFViewerApplication.supportsPrinting = false
210+
211+
// When printing is not supported a warning is shown by the
212+
// default "beforePrint" function when trying to print. That
213+
// function needs to be replaced with an empty one to prevent
214+
// that warning to be shown.
215+
this.PDFViewerApplication.beforePrint = () => {
216+
}
217+
218+
logger.info('Download, print and user interaction disabled')
219+
} else {
220+
logger.info('Download and print available')
221+
}
222+
223+
const PDFViewerApplicationOptions = this.$refs.iframe.contentWindow.PDFViewerApplicationOptions
224+
225+
logger.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll())
187226
},
188227

189228
handleWebviewerloaded() {

src/workersrc.js

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,7 @@
44
* SPDX-License-Identifier: AGPL-3.0-or-later
55
*/
66

7-
import logger from './services/logger.js'
87
import redirectIfNotIframe from './utils/redirectIfNotIframe.js'
98

109
// Checks if the page is displayed in an iframe. If not redirect to /.
1110
redirectIfNotIframe()
12-
13-
// Retrieve the hideDownload from the url, this is
14-
// the most easy way to pass the prop to this iframe
15-
const queryString = window.location.search
16-
const urlParams = new URLSearchParams(queryString)
17-
const hideDownload = urlParams.get('hideDownload')
18-
19-
function initializeCustomPDFViewerApplication() {
20-
if (hideDownload === '1') {
21-
const pdfViewer = window.document.querySelector('.pdfViewer')
22-
23-
if (pdfViewer) {
24-
pdfViewer.classList.add('disabledTextSelection')
25-
}
26-
27-
if (PDFViewerApplication) {
28-
// Disable download function when downloads are hidden, as even if the
29-
// buttons in the UI are hidden the download could still be triggered
30-
// with Ctrl|Meta+S.
31-
PDFViewerApplication.download = function() {
32-
}
33-
34-
// Disable printing service when downloads are hidden, as even if the
35-
// buttons in the UI are hidden the printing could still be triggered
36-
// with Ctrl|Meta+P.
37-
// Abuse the "supportsPrinting" parameter, which signals that the
38-
// browser does not fully support printing, to make PDFViewer disable
39-
// the printing service.
40-
// "supportsPrinting" is a getter function, so it needs to be deleted
41-
// before replacing it with a simple value.
42-
delete PDFViewerApplication.supportsPrinting
43-
PDFViewerApplication.supportsPrinting = false
44-
45-
// When printing is not supported a warning is shown by the default
46-
// "beforePrint" function when trying to print. That function needs to
47-
// be replaced with an empty one to prevent that warning to be shown.
48-
PDFViewerApplication.beforePrint = function() {
49-
}
50-
}
51-
52-
logger.info('Download, print and user interaction disabled')
53-
} else {
54-
logger.info('Download and print available')
55-
}
56-
57-
logger.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll())
58-
}
59-
60-
document.addEventListener('DOMContentLoaded', initializeCustomPDFViewerApplication, true)

0 commit comments

Comments
 (0)