Skip to content

Commit

Permalink
Update: Add disableTextLayer option for doc and text viewers (#71)
Browse files Browse the repository at this point in the history
- Doc and text viewers will use this option and download permissions to determine whether text layer is usable
  • Loading branch information
tonyjin authored Apr 17, 2017
1 parent 5b5807b commit a36f735
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,8 @@ class DocBaseViewer extends BaseViewer {
(file.watermark_info && file.watermark_info.is_watermarked);

// Disable text layer if user doesn't have download permissions
PDFJS.disableTextLayer = !checkPermission(file, PERMISSION_DOWNLOAD);
PDFJS.disableTextLayer = !checkPermission(file, PERMISSION_DOWNLOAD) ||
!!this.getViewerOption('disableTextLayer');

// Decrease mobile canvas size to ~3MP (1920x1536)
PDFJS.maxCanvasPixels = Browser.isMobile() ? MOBILE_MAX_CANVAS_SIZE : PDFJS.maxCanvasPixels;
Expand Down
10 changes: 10 additions & 0 deletions src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,7 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
stubs.urlCreator = sandbox.stub(util, 'createAssetUrlCreator').returns(() => { return 'asset'; });
stubs.browser = sandbox.stub(Browser, 'getName').returns('Safari');
stubs.checkPermission = sandbox.stub(file, 'checkPermission');
stubs.getViewerOption = sandbox.stub(docBase, 'getViewerOption');
docBase.options = {
location: {
staticBaseURI: 'test/'
Expand Down Expand Up @@ -1095,6 +1096,15 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
expect(PDFJS.disableTextLayer).to.be.true;
});

it('should disable the text layer if disableTextLayer viewer option is set', () => {
stubs.checkPermission.withArgs(docBase.options.file, PERMISSION_DOWNLOAD).returns(true);
stubs.getViewerOption.withArgs('disableTextLayer').returns(true);

docBase.setupPdfjs();

expect(PDFJS.disableTextLayer).to.be.true;
});

it('should decrease max canvas size to 3MP if on mobile', () => {
sandbox.stub(Browser, 'isMobile').returns(true);
docBase.setupPdfjs();
Expand Down
5 changes: 3 additions & 2 deletions src/lib/viewers/text/TextBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ class TextBaseViewer extends BaseViewer {
* @return {void}
*/
load() {
// Enable text selection if user has download permissions
if (checkPermission(this.options.file, PERMISSION_DOWNLOAD)) {
// Enable text selection if user has download permissions and 'disableTextLayer' option is not true
if (checkPermission(this.options.file, PERMISSION_DOWNLOAD) &&
!this.getViewerOption('disableTextLayer')) {
this.containerEl.classList.add(CLASS_IS_SELECTABLE);
}

Expand Down
13 changes: 11 additions & 2 deletions src/lib/viewers/text/__tests__/TextBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('lib/viewers/text/TextBaseViewer', () => {
});

beforeEach(() => {
fixture.load('viewers/text/__tests__/TextBase-test.html');
fixture.load('viewers/text/__tests__/TextBaseViewer-test.html');
containerEl = document.querySelector('.container');
textBase = new TextBaseViewer({
file: {
Expand Down Expand Up @@ -91,7 +91,16 @@ describe('lib/viewers/text/TextBaseViewer', () => {
it('should add selectable class if user has download permissions', () => {
sandbox.stub(file, 'checkPermission').withArgs(textBase.options.file, PERMISSION_DOWNLOAD).returns(true);
textBase.load();
expect(textBase.containerEl.classList.contains('bp-is-selectable')).to.be.true;
expect(textBase.containerEl).to.have.class('bp-is-selectable');
});

it('should not add selectable class if disableTextViewer option is true', () => {
sandbox.stub(file, 'checkPermission').withArgs(textBase.options.file, PERMISSION_DOWNLOAD).returns(true);
sandbox.stub(textBase, 'getViewerOption').withArgs('disableTextLayer').returns(true);

textBase.load();

expect(textBase.containerEl).to.not.have.class('bp-is-selectable');
});
});

Expand Down

0 comments on commit a36f735

Please sign in to comment.