Skip to content

Commit

Permalink
feat(viewer): Upgrade pdf.js to v2.14.305 for modern browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
karelee7 authored and jstoffan committed Jun 30, 2022
1 parent 46cecdc commit b9b1e4b
Show file tree
Hide file tree
Showing 13 changed files with 91,224 additions and 112,882 deletions.
9 changes: 2 additions & 7 deletions build/upgrade_pdfjs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,19 @@ rm -rf ./pdfjs-dist/
echo "-----------------------------------------------------------------------------------"
echo "Decreasing # of cached pages on mobile web..."
echo "-----------------------------------------------------------------------------------"
sed -e 's@var DEFAULT_CACHE_SIZE = 10;@var DEFAULT_CACHE_SIZE = /iphone|ipad|ipod|android|blackberry|bb10|mini|windows\sce|palm/i.test(navigator.userAgent) ? 5 : 10;@' -i '' ${DOC_STATIC_ASSETS_PATH}/pdf_viewer.js
sed -e 's@const DEFAULT_CACHE_SIZE = 10;@const DEFAULT_CACHE_SIZE = /iphone|ipad|ipod|android|blackberry|bb10|mini|windows\sce|palm/i.test(navigator.userAgent) ? 5 : 10;@' -i '' ${DOC_STATIC_ASSETS_PATH}/pdf_viewer.js

# Render e-signatures without validation
echo "-----------------------------------------------------------------------------------"
echo "Enabling e-signature rendering without validation..."
echo "-----------------------------------------------------------------------------------"
sed -e 's@;r.setFlags(o.AnnotationFlag.HIDDEN)@@' -i '' ${DOC_STATIC_ASSETS_PATH}/pdf.worker.min.js

# Fix for Courier font PDF not rendering applied to src/third-party/doc/2.76.0/pdf.worker*.js which is PDFJS v2.2.228
# Link to issue: https://github.com/mozilla/pdf.js/issues/13771
# This fix may not need to be applied if next upgrade is >= 2.5.x
# See https://github.com/box/box-content-preview/pull/1414 for more details

# Minify using Babel
echo "-----------------------------------------------------------------------------------"
echo "Minifying pdf.js files with Babel"
echo "-----------------------------------------------------------------------------------"
babel ${DOC_STATIC_ASSETS_PATH}/pdf_viewer.js --out-file ${DOC_STATIC_ASSETS_PATH}/pdf_viewer.min.js
./node_modules/@babel/cli/bin/babel.js ${DOC_STATIC_ASSETS_PATH}/pdf_viewer.js --out-file ${DOC_STATIC_ASSETS_PATH}/pdf_viewer.min.js

echo "-----------------------------------------------------------------------------------"
echo "Minifying pdf.js CSS with cssnano"
Expand Down
3 changes: 2 additions & 1 deletion src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ class DocBaseViewer extends BaseViewer {
this.pdfLinkService = new this.pdfjsViewer.PDFLinkService({
eventBus: this.pdfEventBus,
externalLinkRel: 'noopener noreferrer nofollow', // Prevent referrer hijacking
externalLinkTarget: this.pdfjsLib.LinkTarget.BLANK, // Open links in new tab
externalLinkTarget: Browser.isIE() ? this.pdfjsLib.LinkTarget.BLANK : this.pdfjsViewer.LinkTarget.BLANK, // Open links in new tab
});

this.pdfFindController = new this.pdfjsViewer.PDFFindController({
Expand Down Expand Up @@ -783,6 +783,7 @@ class DocBaseViewer extends BaseViewer {
imageResourcesPath: assetUrlCreator(IMAGES),
linkService: this.pdfLinkService,
maxCanvasPixels: this.isMobile ? MOBILE_MAX_CANVAS_SIZE : -1,
renderInteractiveForms: false, // Enabling prevents unverified signatures from being displayed
textLayerMode: hasTextLayer ? textLayerMode : PDFJS_TEXT_LAYER_MODE.DISABLE,
});
}
Expand Down
8 changes: 6 additions & 2 deletions src/lib/viewers/doc/DocFindBar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EventEmitter from 'events';
import Browser from '../../Browser';
import { decodeKeydown } from '../../util';
import { USER_DOCUMENT_FIND_EVENTS, VIEWER_EVENT } from '../../events';
import { CLASS_BOX_PREVIEW_FIND_BAR, CLASS_HIDDEN } from '../../constants';
Expand Down Expand Up @@ -137,12 +138,15 @@ class DocFindBar extends EventEmitter {
* @return {void}
*/
dispatchFindEvent(type, findPrev) {
this.findController.executeCommand(type, {
const options = {
query: this.findFieldEl.value,
phraseSearch: true, // true by default
highlightAll: true, // true by default
findPrevious: findPrev,
});
};
return Browser.isIE()
? this.findController.executeCommand(type, options)
: this.eventBus.dispatch(type, options);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/lib/viewers/doc/PresentationViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class PresentationViewer extends DocBaseViewer {

return {
first: currentPageObj,
ids: new Set([currentPageObj.id]),
last: currentPageObj,
views: visible,
};
Expand Down
20 changes: 18 additions & 2 deletions src/lib/viewers/doc/__tests__/DocFindBar-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-unused-expressions */
import * as util from '../../../util';
import Browser from '../../../Browser';
import DocFindBar from '../DocFindBar';
import { CLASS_BOX_PREVIEW_FIND_BAR, CLASS_HIDDEN } from '../../../constants';
import { VIEWER_EVENT, USER_DOCUMENT_FIND_EVENTS } from '../../../events';
Expand All @@ -24,7 +25,7 @@ describe('lib/viewers/doc/DocFindBar', () => {
fixture.load('viewers/doc/__tests__/DocFindBar-test.html');

containerEl = document.querySelector('.test-container');
eventBus = { off: jest.fn(), on: jest.fn() };
eventBus = { dispatch: jest.fn(), off: jest.fn(), on: jest.fn() };
findController = {
executeCommand: jest.fn(),
linkService: {},
Expand Down Expand Up @@ -149,7 +150,8 @@ describe('lib/viewers/doc/DocFindBar', () => {
});

describe('dispatchFindEvent()', () => {
test('should execute the find controller command with the given params', () => {
test('should execute the find controller command with the given params on IE', () => {
jest.spyOn(Browser, 'isIE').mockImplementation(() => true);
docFindBar.findFieldEl.value = 'value';
const params = {
query: docFindBar.findFieldEl.value,
Expand All @@ -161,6 +163,20 @@ describe('lib/viewers/doc/DocFindBar', () => {
docFindBar.dispatchFindEvent('string', 'test');
expect(findController.executeCommand).toBeCalledWith('string', params);
});

test('should execute the find controller command with the given params on modern browsers', () => {
jest.spyOn(Browser, 'isIE').mockImplementation(() => false);
docFindBar.findFieldEl.value = 'value';
const params = {
query: docFindBar.findFieldEl.value,
phraseSearch: true,
highlightAll: true,
findPrevious: 'test',
};

docFindBar.dispatchFindEvent('string', 'test');
expect(eventBus.dispatch).toBeCalledWith('string', params);
});
});

describe('updateUIState()', () => {
Expand Down
Loading

0 comments on commit b9b1e4b

Please sign in to comment.