Skip to content

Commit 14b67e9

Browse files
authored
Merge pull request #1130 from nextcloud/backport/1107/stable31
[stable31] Move initialization from helper script to Vue component
2 parents 02815fd + 4fd96d1 commit 14b67e9

7 files changed

+97
-116
lines changed

js/files_pdfviewer-main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/files_pdfviewer-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/files_pdfviewer-workersrc.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
1-
SPDX-License-Identifier: MIT
2-
SPDX-License-Identifier: ISC
3-
SPDX-License-Identifier: GPL-3.0-or-later
41
SPDX-License-Identifier: AGPL-3.0-or-later
5-
SPDX-FileCopyrightText: Roman Shtylman <[email protected]>
62
SPDX-FileCopyrightText: John Molakvoæ <[email protected]>
7-
SPDX-FileCopyrightText: GitHub Inc.
8-
SPDX-FileCopyrightText: Christoph Wurst
93

104

115
This file is generated from multiple sources. Included packages:
12-
- @nextcloud/browser-storage
13-
- version: 0.4.0
14-
- license: GPL-3.0-or-later
15-
- semver
16-
- version: 7.6.2
17-
- license: ISC
18-
- process
19-
- version: 0.11.10
20-
- license: MIT
216
- files_pdfviewer
227
- version: 4.0.0
238
- license: AGPL-3.0-or-later

js/files_pdfviewer-workersrc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/views/PDFView.vue

Lines changed: 91 additions & 30 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
},
@@ -111,9 +110,28 @@ export default {
111110
return this.getIframeDocument().getElementById('download')
112111
},
113112

114-
handleWebviewerloaded() {
113+
getViewerTemplateParameter(parameterName) {
114+
// templates/viewer.php provides the PDF viewer parameters in the
115+
// data attributes of the head element.
116+
return this.getIframeDocument().getElementsByTagName('head')[0].getAttribute('data-' + parameterName)
117+
},
118+
119+
initializePDFViewerApplicationOptions() {
115120
const PDFViewerApplicationOptions = this.$refs.iframe.contentWindow.PDFViewerApplicationOptions
116121

122+
// Preferences override options, so they must be disabled for
123+
// "externalLinkTarget" and "annotationMode" to take effect.
124+
PDFViewerApplicationOptions.set('disablePreferences', true)
125+
// TODO https://github.com/mozilla/pdf.js/pull/14424#issuecomment-1092947792
126+
PDFViewerApplicationOptions.set('externalLinkTarget', 2)
127+
PDFViewerApplicationOptions.set('isEvalSupported', false)
128+
PDFViewerApplicationOptions.set('workerSrc', this.getViewerTemplateParameter('workersrc'))
129+
PDFViewerApplicationOptions.set('cMapUrl', this.getViewerTemplateParameter('cmapurl'))
130+
PDFViewerApplicationOptions.set('sandboxBundleSrc', this.getViewerTemplateParameter('sandbox'))
131+
PDFViewerApplicationOptions.set('enablePermissions', true)
132+
PDFViewerApplicationOptions.set('imageResourcesPath', this.getViewerTemplateParameter('imageresourcespath'))
133+
PDFViewerApplicationOptions.set('enableScripting', this.getViewerTemplateParameter('enableScripting') === true)
134+
117135
const language = getLanguage()
118136
const supportedLanguages = SUPPORTED_LANGUAGES
119137
// If the user language is supported we use that language,
@@ -132,10 +150,6 @@ export default {
132150
}
133151

134152
if (!this.isEditable) {
135-
// Preferences override options, so they must be disabled for
136-
// "annotationMode" to take effect.
137-
PDFViewerApplicationOptions.set('disablePreferences', true)
138-
139153
// AnnotationMode.ENABLE value is 1 in PDF.js, which shows
140154
// forms, but does not allow to interact with them
141155
PDFViewerApplicationOptions.set('annotationMode', 1)
@@ -144,6 +158,75 @@ export default {
144158
// prevents editing annotations
145159
PDFViewerApplicationOptions.set('annotationEditorMode', -1)
146160
}
161+
},
162+
163+
initializePDFViewerApplication() {
164+
this.PDFViewerApplication = this.$refs.iframe.contentWindow.PDFViewerApplication
165+
166+
this.PDFViewerApplication.save = this.handleSave
167+
168+
// Not all fields of PDFViewerApplication are reactive.
169+
// Specifically, it can not be known if annotations were created by
170+
// watching "pdfDocument.annotationStorage.size" (maybe because
171+
// "size" is a getter based on a private field, so it does not work
172+
// even if the rest of the chain is skipped and "size" is directly
173+
// watched). However, "annotationStorage" has callbacks used by
174+
// PDFViewerApplication to know when an annotation was set, so that
175+
// callback can be wrapped to also enable the save button.
176+
this.PDFViewerApplication.eventBus.on('documentinit', () => {
177+
const annotationStorage = this.PDFViewerApplication.pdfDocument.annotationStorage
178+
179+
const onSetModifiedOriginal = annotationStorage.onSetModified
180+
annotationStorage.onSetModified = () => {
181+
onSetModifiedOriginal.apply(null, arguments)
182+
183+
this.getDownloadElement().removeAttribute('disabled')
184+
}
185+
})
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())
226+
},
227+
228+
handleWebviewerloaded() {
229+
this.initializePDFViewerApplicationOptions()
147230

148231
// PDFViewerApplication can not be set when the "webviewerloaded"
149232
// event is dispatched, as at this point the application was not
@@ -155,29 +238,7 @@ export default {
155238
// PDFViewerApplication.pdfViewer to be set already and thus uses it
156239
// unconditionally).
157240
this.$refs.iframe.contentWindow.PDFViewerApplication.initializedPromise.then(() => {
158-
this.PDFViewerApplication = this.$refs.iframe.contentWindow.PDFViewerApplication
159-
160-
this.PDFViewerApplication.save = this.handleSave
161-
162-
// Not all fields of PDFViewerApplication are reactive.
163-
// Specifically, it can not be known if annotations were created
164-
// by watching "pdfDocument.annotationStorage.size" (maybe
165-
// because "size" is a getter based on a private field, so it
166-
// does not work even if the rest of the chain is skipped and
167-
// "size" is directly watched). However, "annotationStorage" has
168-
// callbacks used by PDFViewerApplication to know when an
169-
// annotation was set, so that callback can be wrapped to also
170-
// enable the save button.
171-
this.PDFViewerApplication.eventBus.on('documentinit', () => {
172-
const annotationStorage = this.PDFViewerApplication.pdfDocument.annotationStorage
173-
174-
const onSetModifiedOriginal = annotationStorage.onSetModified
175-
annotationStorage.onSetModified = () => {
176-
onSetModifiedOriginal.apply(null, arguments)
177-
178-
this.getDownloadElement().removeAttribute('disabled')
179-
}
180-
})
241+
this.initializePDFViewerApplication()
181242
})
182243
},
183244

src/workersrc.js

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +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-
const head = document.getElementsByTagName('head')[0]
21-
22-
// Preferences override options, so they must be disabled for
23-
// "externalLinkTarget" to take effect.
24-
PDFViewerApplicationOptions.set('disablePreferences', true)
25-
// TODO https://github.com/mozilla/pdf.js/pull/14424#issuecomment-1092947792
26-
PDFViewerApplicationOptions.set('externalLinkTarget', 2)
27-
PDFViewerApplicationOptions.set('isEvalSupported', false)
28-
PDFViewerApplicationOptions.set('workerSrc', head.getAttribute('data-workersrc'))
29-
PDFViewerApplicationOptions.set('cMapUrl', head.getAttribute('data-cmapurl'))
30-
PDFViewerApplicationOptions.set('sandboxBundleSrc', head.getAttribute('data-sandbox'))
31-
PDFViewerApplicationOptions.set('enablePermissions', true)
32-
PDFViewerApplicationOptions.set('imageResourcesPath', head.getAttribute('data-imageresourcespath'))
33-
PDFViewerApplicationOptions.set('enableScripting', head.getAttribute('data-enableScripting') === true)
34-
35-
if (hideDownload === '1') {
36-
const pdfViewer = window.document.querySelector('.pdfViewer')
37-
38-
if (pdfViewer) {
39-
pdfViewer.classList.add('disabledTextSelection')
40-
}
41-
42-
if (PDFViewerApplication) {
43-
// Disable download function when downloads are hidden, as even if the
44-
// buttons in the UI are hidden the download could still be triggered
45-
// with Ctrl|Meta+S.
46-
PDFViewerApplication.download = function() {
47-
}
48-
49-
// Disable printing service when downloads are hidden, as even if the
50-
// buttons in the UI are hidden the printing could still be triggered
51-
// with Ctrl|Meta+P.
52-
// Abuse the "supportsPrinting" parameter, which signals that the
53-
// browser does not fully support printing, to make PDFViewer disable
54-
// the printing service.
55-
// "supportsPrinting" is a getter function, so it needs to be deleted
56-
// before replacing it with a simple value.
57-
delete PDFViewerApplication.supportsPrinting
58-
PDFViewerApplication.supportsPrinting = false
59-
60-
// When printing is not supported a warning is shown by the default
61-
// "beforePrint" function when trying to print. That function needs to
62-
// be replaced with an empty one to prevent that warning to be shown.
63-
PDFViewerApplication.beforePrint = function() {
64-
}
65-
}
66-
67-
logger.info('Download, print and user interaction disabled')
68-
} else {
69-
logger.info('Download and print available')
70-
}
71-
72-
logger.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll())
73-
}
74-
75-
document.addEventListener('DOMContentLoaded', initializeCustomPDFViewerApplication, true)

0 commit comments

Comments
 (0)