Skip to content

Commit

Permalink
New: Add in SinglePageViewer for pdfs (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiverson authored and tonyjin committed Feb 2, 2018
1 parent 01b7d83 commit 1ad415c
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions src/lib/viewers/doc/DocLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -29,6 +30,12 @@ const VIEWERS = [
REP: 'pdf',
EXT: DOCUMENT_EXTENSIONS
},
{
NAME: 'SinglePage',
CONSTRUCTOR: SinglePageViewer,
REP: 'pdf',
EXT: DOCUMENT_EXTENSIONS
},
{
NAME: 'Document',
CONSTRUCTOR: DocumentViewer,
Expand All @@ -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']
}
];

Expand Down
25 changes: 25 additions & 0 deletions src/lib/viewers/doc/SinglePageViewer.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions src/lib/viewers/doc/__tests__/SinglePageViewer-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="container"><div class="bp-container"><div class="bp"></div></div></div>
68 changes: 68 additions & 0 deletions src/lib/viewers/doc/__tests__/SinglePageViewer-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 1ad415c

Please sign in to comment.