Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(text): Hide print content for users without download permission #1076

Merged
merged 1 commit into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const CLASS_MULTI_IMAGE_PAGE = 'bp-multi-image-page';
export const CLASS_INVISIBLE = 'bp-is-invisible';
export const CLASS_IS_TRANSPARENT = 'bp-is-transparent';
export const CLASS_IS_VISIBLE = 'bp-is-visible';
export const CLASS_IS_PRINTABLE = 'bp-is-printable';
export const CLASS_IS_SCROLLABLE = 'bp-is-scrollable';
export const CLASS_IS_SELECTABLE = 'bp-is-selectable';
export const CLASS_IS_BUFFERING = 'bp-is-buffering';
Expand Down
6 changes: 1 addition & 5 deletions src/lib/viewers/text/CSV.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import '../../boxuiVariables';
@import 'base';

.bp-text-csv {
position: absolute;
Expand All @@ -17,11 +18,6 @@
white-space: pre;
word-wrap: normal;
word-break: normal;
user-select: none;

.bp-is-selectable & {
user-select: auto;
}

.bp-text-csv-grid {
width: 100%;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/viewers/text/PlainTextViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './Text.scss';
import TextBaseViewer from './TextBaseViewer';
import Browser from '../../Browser';
import Popup from '../../Popup';
import { CLASS_HIDDEN, TEXT_STATIC_ASSETS_VERSION, CLASS_IS_SCROLLABLE } from '../../constants';
import { CLASS_HIDDEN, CLASS_IS_SCROLLABLE, TEXT_STATIC_ASSETS_VERSION } from '../../constants';
import { ICON_PRINT_CHECKMARK } from '../../icons/icons';
import { HIGHLIGHTTABLE_EXTENSIONS } from '../../extensions';
import { openContentInsideIframe, createAssetUrlCreator, createStylesheet } from '../../util';
Expand Down Expand Up @@ -116,7 +116,7 @@ class PlainTextViewer extends TextBaseViewer {
this.textEl.tabIndex = '0';

this.codeEl = this.textEl.appendChild(document.createElement('code'));
this.codeEl.classList.add(this.options.file.extension);
this.codeEl.className = `bp-text-code ${this.options.file.extension}`;

// Whether or not we truncated text shown due to performance issues
this.truncated = false;
Expand Down
6 changes: 1 addition & 5 deletions src/lib/viewers/text/Text.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import '../../boxuiVariables';
@import 'base';

.bp-text-plain {
position: absolute;
Expand All @@ -12,11 +13,6 @@
text-align: left;
word-wrap: break-word;
background-color: $white;
user-select: none;

.bp-is-selectable & {
user-select: auto;
}

/* Override Github CSS */
.bp & {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/viewers/text/TextBaseViewer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Controls from '../../Controls';
import BaseViewer from '../BaseViewer';
import { checkPermission } from '../../file';
import { CLASS_IS_SELECTABLE, PERMISSION_DOWNLOAD } from '../../constants';
import { CLASS_IS_PRINTABLE, CLASS_IS_SELECTABLE, PERMISSION_DOWNLOAD } from '../../constants';

import { ICON_ZOOM_IN, ICON_ZOOM_OUT, ICON_FULLSCREEN_IN, ICON_FULLSCREEN_OUT } from '../../icons/icons';

Expand Down Expand Up @@ -97,6 +97,7 @@ class TextBaseViewer extends BaseViewer {
load() {
// 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_PRINTABLE);
this.containerEl.classList.add(CLASS_IS_SELECTABLE);
}

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

describe('load()', () => {
it('should add selectable class if user has download permissions', () => {
it('should add selectable/printable classes if user has download permissions', () => {
sandbox
.stub(file, 'checkPermission')
.withArgs(textBase.options.file, PERMISSION_DOWNLOAD)
.returns(true);
textBase.load();

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

it('should not add selectable class if disableTextViewer option is true', () => {
it('should not add selectable/printable classes if user does not have download permissions', () => {
sandbox
.stub(file, 'checkPermission')
.withArgs(textBase.options.file, PERMISSION_DOWNLOAD)
.returns(false);
textBase.load();

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

it('should not add selectable/printable classes if disableTextViewer option is true', () => {
sandbox
.stub(file, 'checkPermission')
.withArgs(textBase.options.file, PERMISSION_DOWNLOAD)
Expand All @@ -116,6 +129,7 @@ describe('lib/viewers/text/TextBaseViewer', () => {

textBase.load();

expect(textBase.containerEl).to.not.have.class('bp-is-printable');
expect(textBase.containerEl).to.not.have.class('bp-is-selectable');
});
});
Expand Down
27 changes: 27 additions & 0 deletions src/lib/viewers/text/_base.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.bp-content {
.bp-text {
user-select: none;
}

&.bp-is-selectable {
.bp-text {
user-select: auto;
}

.bp-text-code {
cursor: text;
}
}
}

@media print {
.bp-content {
.bp-text {
display: none; // Hide text in app-level print preview window
}

&.bp-is-printable .bp-text {
display: block;
}
}
}