From 1ad415cdd6d56b0b120d0e5f3661439130011485 Mon Sep 17 00:00:00 2001 From: Josh Iverson Date: Fri, 2 Feb 2018 16:25:15 -0700 Subject: [PATCH] New: Add in SinglePageViewer for pdfs (#606) --- src/lib/viewers/doc/DocBaseViewer.js | 23 +++++-- src/lib/viewers/doc/DocLoader.js | 13 ++++ src/lib/viewers/doc/SinglePageViewer.js | 25 +++++++ .../doc/__tests__/SinglePageViewer-test.html | 1 + .../doc/__tests__/SinglePageViewer-test.js | 68 +++++++++++++++++++ 5 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 src/lib/viewers/doc/SinglePageViewer.js create mode 100644 src/lib/viewers/doc/__tests__/SinglePageViewer-test.html create mode 100644 src/lib/viewers/doc/__tests__/SinglePageViewer-test.js diff --git a/src/lib/viewers/doc/DocBaseViewer.js b/src/lib/viewers/doc/DocBaseViewer.js index 42fd64c82..c9bbba934 100644 --- a/src/lib/viewers/doc/DocBaseViewer.js +++ b/src/lib/viewers/doc/DocBaseViewer.js @@ -497,12 +497,7 @@ class DocBaseViewer extends BaseViewer { this.bindDOMListeners(); // Initialize pdf.js in container - this.pdfViewer = new PDFJS.PDFViewer({ - container: this.docEl, - linkService: new PDFJS.PDFLinkService(), - // Enhanced text selection uses more memory, so disable on mobile - enhanceTextSelection: !this.isMobile - }); + this.pdfViewer = this.initPdfViewer(); // Use chunk size set in viewer options if available let rangeChunkSize = this.getViewerOption('rangeChunkSize'); @@ -558,6 +553,22 @@ class DocBaseViewer extends BaseViewer { }); } + /** + * Initialize pdf.js viewer. + * + * @protected + * @override + * @return {PDFJS.PDFViewer} PDF viewer type + */ + initPdfViewer() { + return new PDFJS.PDFViewer({ + container: this.docEl, + linkService: new PDFJS.PDFLinkService(), + // Enhanced text selection uses more memory, so disable on mobile + enhanceTextSelection: !this.isMobile + }); + } + /** * Re-sizing logic. * diff --git a/src/lib/viewers/doc/DocLoader.js b/src/lib/viewers/doc/DocLoader.js index 895fc2619..a132d10ba 100644 --- a/src/lib/viewers/doc/DocLoader.js +++ b/src/lib/viewers/doc/DocLoader.js @@ -2,6 +2,7 @@ import AssetLoader from '../AssetLoader'; import { getRepresentation } from '../../file'; import DocumentViewer from './DocumentViewer'; import PresentationViewer from './PresentationViewer'; +import SinglePageViewer from './SinglePageViewer'; import RepStatus from '../../RepStatus'; import { ORIGINAL_REP_NAME, STATUS_SUCCESS } from '../../constants'; import { DOCUMENT_EXTENSIONS } from '../../extensions'; @@ -29,6 +30,12 @@ const VIEWERS = [ REP: 'pdf', EXT: DOCUMENT_EXTENSIONS }, + { + NAME: 'SinglePage', + CONSTRUCTOR: SinglePageViewer, + REP: 'pdf', + EXT: DOCUMENT_EXTENSIONS + }, { NAME: 'Document', CONSTRUCTOR: DocumentViewer, @@ -42,6 +49,12 @@ const VIEWERS = [ CONSTRUCTOR: PresentationViewer, REP: ORIGINAL_REP_NAME, EXT: ['pdf', 'lcdpdf'] + }, + { + NAME: 'SinglePage', + CONSTRUCTOR: SinglePageViewer, + REP: ORIGINAL_REP_NAME, + EXT: ['pdf', 'lcdpdf'] } ]; diff --git a/src/lib/viewers/doc/SinglePageViewer.js b/src/lib/viewers/doc/SinglePageViewer.js new file mode 100644 index 000000000..7a21512d7 --- /dev/null +++ b/src/lib/viewers/doc/SinglePageViewer.js @@ -0,0 +1,25 @@ +import DocumentViewer from './DocumentViewer'; + +class SinglePageViewer extends DocumentViewer { + //-------------------------------------------------------------------------- + // Protected + //-------------------------------------------------------------------------- + + /** + * Initialize pdf.js viewer. + * + * @protected + * @override + * @return {PDFJS.PDFViewer} PDF viewer type + */ + initPdfViewer() { + return new PDFJS.PDFSinglePageViewer({ + container: this.docEl, + linkService: new PDFJS.PDFLinkService(), + // Enhanced text selection uses more memory, so disable on mobile + enhanceTextSelection: !this.isMobile + }); + } +} + +export default SinglePageViewer; diff --git a/src/lib/viewers/doc/__tests__/SinglePageViewer-test.html b/src/lib/viewers/doc/__tests__/SinglePageViewer-test.html new file mode 100644 index 000000000..7dd9073c1 --- /dev/null +++ b/src/lib/viewers/doc/__tests__/SinglePageViewer-test.html @@ -0,0 +1 @@ +
diff --git a/src/lib/viewers/doc/__tests__/SinglePageViewer-test.js b/src/lib/viewers/doc/__tests__/SinglePageViewer-test.js new file mode 100644 index 000000000..17a2fcef1 --- /dev/null +++ b/src/lib/viewers/doc/__tests__/SinglePageViewer-test.js @@ -0,0 +1,68 @@ +/* eslint-disable no-unused-expressions */ +import SinglePageViewer from '../SinglePageViewer'; +import BaseViewer from '../../BaseViewer'; + +const sandbox = sinon.sandbox.create(); +let containerEl; +let doc; +let stubs = {}; + +describe('lib/viewers/doc/SinglePageViewer', () => { + const setupFunc = BaseViewer.prototype.setup; + + before(() => { + fixture.setBase('src/lib'); + }); + + beforeEach(() => { + fixture.load('viewers/doc/__tests__/SinglePageViewer-test.html'); + + containerEl = document.querySelector('.container'); + doc = new SinglePageViewer({ + container: containerEl, + file: { + id: '0' + } + }); + + Object.defineProperty(BaseViewer.prototype, 'setup', { value: sandbox.mock() }); + doc.containerEl = containerEl; + doc.setup(); + }); + + afterEach(() => { + sandbox.verifyAndRestore(); + fixture.cleanup(); + + Object.defineProperty(BaseViewer.prototype, 'setup', { value: setupFunc }); + + if (doc && typeof doc.destroy === 'function') { + doc.destroy(); + } + + doc = null; + stubs = {}; + }); + + describe('initPdfViewer()', () => { + const pdfViewer = { + linkService: new PDFJS.PDFLinkService(), + setDocument: sandbox.stub(), + enhanceTextSelection: true + }; + + beforeEach(() => { + stubs.pdfViewerStub = sandbox.stub(PDFJS, 'PDFSinglePageViewer').returns(pdfViewer); + }); + + it('should return the default pdfViewer', () => { + const result = doc.initPdfViewer(); + expect(stubs.pdfViewerStub).to.be.calledWith({ + container: sinon.match.any, + linkService: sinon.match.any, + enhanceTextSelection: true + }); + expect(result).to.equal(pdfViewer); + }); + }); +});