Merge pull request #3963 from overleaf/revert-3819-msm-update-pdfjs-2-6

Revert "Update `pdf.js` to `2.6.347`"

GitOrigin-RevId: 62402d8e0b99b302206f0edc61411c9477d29249
This commit is contained in:
Miguel Serrano
2021-04-28 02:10:27 +00:00
committed by Copybot
parent bac110ee46
commit 8fbd4e3340
7 changed files with 70 additions and 75 deletions
@@ -1,5 +1,5 @@
import App from '../../../base'
import { Util } from './pdfJsLoader'
import PDFJS from './pdfJsLoader'
const EXTERNAL_LINK_TARGET = '_blank'
const REL_NOOPENER = 'noreferrer noopener'
@@ -34,7 +34,7 @@ App.factory('pdfAnnotations', function () {
buildLinkElementFromRect(rect) {
rect = this.viewport.convertToViewportRectangle(rect)
rect = Util.normalizeRect(rect)
rect = PDFJS.Util.normalizeRect(rect)
const element = document.createElement('a')
element.style.left = Math.floor(rect[0]) + 'px'
element.style.top = Math.floor(rect[1]) + 'px'
@@ -13,7 +13,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import App from '../../../base'
import { Util } from './pdfJsLoader'
import PDFJS from './pdfJsLoader'
export default App.factory('pdfHighlights', function () {
let pdfHighlights
@@ -30,7 +30,7 @@ export default App.factory('pdfHighlights', function () {
left + width,
top + height,
])
rect = Util.normalizeRect(rect)
rect = PDFJS.Util.normalizeRect(rect)
const element = document.createElement('div')
element.style.left = Math.floor(rect[0]) + 'px'
element.style.top = Math.floor(rect[1]) + 'px'
@@ -8,9 +8,18 @@
* to customise the loader's options. However the rest of the file is identical
* to the one provided by PDF.js.
*/
const pdfjs = require('pdfjs-dist/es5/build/pdf.js')
const PdfjsWorker = require('pdfjs-dist/es5/build/pdf.worker.js')
/*
* Adapted from https://github.com/mozilla/pdfjs-dist/blob/e9492b7a725ec4edd466880223474f4295a5fb45/webpack.js
* The PDF.js worker needs to be loaded in a Web Worker. This can be done
* automatically with webpack via worker-loader.
* PDF.js has the above file to do this, however it uses the webpack loader
* module loading syntax, which prevents us from customising the loader.
* We need to output the worker file to the public/js directory, and so we need
* to customise the loader's options. However the rest of the file is identical
* to the one provided by PDF.js.
*/
var pdfjs = require('pdfjs-dist/build/pdf.js')
var PdfjsWorker = require('pdfjs-dist/build/pdf.worker.js')
if (typeof window !== 'undefined' && 'Worker' in window) {
pdfjs.GlobalWorkerOptions.workerPort = new PdfjsWorker()
@@ -18,9 +18,8 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import App from '../../../base'
import { getDocument, renderTextLayer } from './pdfJsLoader'
import PDFJS from './pdfJsLoader'
import { captureMessage } from '../../../infrastructure/error-reporter'
import OError from '@overleaf/o-error'
export default App.factory(
'PDFRenderer',
@@ -54,7 +53,7 @@ export default App.factory(
} else {
disableFontFace = false
}
this.pdfjs = getDocument({
this.pdfjs = PDFJS.getDocument({
url: this.url,
cMapUrl: window.pdfCMapsPath,
cMapPacked: true,
@@ -84,13 +83,9 @@ export default App.factory(
})
this.errorCallback = this.options.errorCallback
this.pageSizeChangeCallback = this.options.pageSizeChangeCallback
this.pdfjs.promise.catch(error => {
this.pdfjs.promise.catch(exception => {
// error getting document
this.errorCallback(
error
? OError.tag(error)
: new UnknownPDFError('pdfJsLoader.getDocument()')
)
return this.errorCallback(exception)
})
}
@@ -137,14 +132,9 @@ export default App.factory(
return (viewport = page.getViewport({ scale: scale }))
},
error => {
this.errorCallback(
error
? OError.tag(error)
: new UnknownPDFError('getPdfViewport', {
pageNum,
scale,
})
)
return typeof this.errorCallback === 'function'
? this.errorCallback(error)
: undefined
}
)
})
@@ -160,11 +150,9 @@ export default App.factory(
return this.document.promise.then(
pdfDocument => pdfDocument.getDestination(dest),
error => {
this.errorCallback(
error
? OError.tag(error)
: new UnknownPDFError('getDestination', { dest })
)
return typeof this.errorCallback === 'function'
? this.errorCallback(error)
: undefined
}
)
}
@@ -174,11 +162,9 @@ export default App.factory(
return pdfDocument.getPageIndex(ref).then(
idx => idx,
error => {
this.errorCallback(
error
? OError.tag(error)
: new UnknownPDFError('getPageIndex', { ref })
)
return typeof this.errorCallback === 'function'
? this.errorCallback(error)
: undefined
}
)
})
@@ -356,7 +342,9 @@ export default App.factory(
this.clearIndicator(page)
// @jobs = @jobs - 1
// @triggerRenderQueue(0)
this.errorCallback(new OError('timeout'))
return typeof this.errorCallback === 'function'
? this.errorCallback('timeout')
: undefined
}, this.PAGE_LOAD_TIMEOUT)
var loadTask = this.getPage(pagenum)
@@ -459,7 +447,7 @@ export default App.factory(
const textLayer = new pdfTextLayer({
textLayerDiv: element.text[0],
viewport,
renderer: renderTextLayer,
renderer: PDFJS.renderTextLayer,
})
const annotationsLayer = new pdfAnnotations({
@@ -486,20 +474,16 @@ export default App.factory(
return textLayer.render(textLayerTimeout)
},
error =>
self.errorCallback(
error
? OError.tag(error)
: new UnknownPDFError('page.getTextContent')
)
typeof self.errorCallback === 'function'
? self.errorCallback(error)
: undefined
)
return page.getAnnotations().then(
annotations => annotationsLayer.setAnnotations(annotations),
error =>
self.errorCallback(
error
? OError.tag(error)
: new UnknownPDFError('page.getAnnotations')
)
typeof self.errorCallback === 'function'
? self.errorCallback(error)
: undefined
)
})
.catch(function (error) {
@@ -507,9 +491,9 @@ export default App.factory(
if (error.name === 'RenderingCancelledException') {
// do nothing when cancelled
} else {
self.errorCallback(
error ? OError.tag(error) : new UnknownPDFError('page.render')
)
return typeof self.errorCallback === 'function'
? self.errorCallback(error)
: undefined
}
})
@@ -547,9 +531,3 @@ function __guardMethod__(obj, methodName, transform) {
return undefined
}
}
class UnknownPDFError extends OError {
constructor(location, info) {
super(`Unknown pdfRenderer error from ${location}`, info)
}
}
@@ -75,7 +75,7 @@ App.controller(
// but we plan to add this in the future
// (https://github.com/overleaf/issues/issues/2985) and this error
// is causing noise in Sentry so ignore it
if (error.name !== 'MissingPDFException') {
if (!error || error.name !== 'MissingPDFException') {
captureMessage(`pdfng error ${error}`)
}
return $scope.$emit('pdf:error', error)
@@ -495,11 +495,11 @@ export default App.directive('pdfViewer', ($q, $timeout, pdfSpinner) => ({
)
scope.$on('pdf:error', function (event, error) {
if (error?.name === 'RenderingCancelledException') {
if (error.name === 'RenderingCancelledException') {
return
}
// check if too many retries or file is missing
const message = error?.message
const message = (error != null ? error.message : undefined) || error
if (
scope.loadCount > 3 ||
/^Missing PDF/i.test(message) ||
@@ -516,7 +516,7 @@ export default App.directive('pdfViewer', ($q, $timeout, pdfSpinner) => ({
// trigger a redraw
(scope.scale = angular.copy(scope.scale))
)
.catch(() => scope.$emit('pdf:error:display'))
.catch(error => scope.$emit('pdf:error:display'))
} else {
scope.$emit('pdf:error:display')
}