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

New: centering image prints #18

Merged
merged 2 commits into from
Mar 24, 2017
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
4 changes: 4 additions & 0 deletions src/lib/viewers/image/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ class Image extends Base {
this.printframe = openContentInsideIframe(this.imageEl.outerHTML);
this.printframe.contentWindow.focus();

this.printImage = this.printframe.contentDocument.querySelector('img');
this.printImage.style.display = 'block';
this.printImage.style.margin = '0 auto';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative way to do this would be to load in a stylesheet like we do in preparePrint() in Text.js. Generally if possible, move styles to a stylesheet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth loading the style sheet for 2 lines?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on the fence about it, you can make the judgement call.

Pro in JS:

  • Tons of other places already set styles in JS
  • Only 2 lines

Pro in CSS:

  • Slippery slope to add any styles that we could use CSS for in JS
  • In case future styles need to be added for multi image, we'd be adding to CSS not more to JS

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm going to stick with JS. If multi-image printing is introduced I'll happily refactor.


if (Browser.getName() === 'Explorer' || Browser.getName() === 'Edge') {
this.printframe.contentWindow.document.execCommand('print', false, null);
} else {
Expand Down
27 changes: 12 additions & 15 deletions src/lib/viewers/image/__tests__/Image-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,45 +416,42 @@ describe('lib/viewers/image/Image', () => {

describe('print()', () => {
beforeEach(() => {
stubs.iframe = {
contentWindow: {
focus: sandbox.stub(),
print: sandbox.stub(),
document: {
execCommand: sandbox.stub()
}
}
};
stubs.mockIframe = util.openContentInsideIframe(image.imageEl.outerHTML);
stubs.focus = sandbox.stub(stubs.mockIframe.contentWindow, 'focus');
stubs.execCommand = sandbox.stub(stubs.mockIframe.contentWindow.document, 'execCommand');
stubs.print = sandbox.stub(stubs.mockIframe.contentWindow, 'print');

stubs.openContentInsideIframe = sandbox.stub(util, 'openContentInsideIframe').returns(stubs.iframe);
stubs.openContentInsideIframe = sandbox.stub(util, 'openContentInsideIframe').returns(stubs.mockIframe);
stubs.getName = sandbox.stub(Browser, 'getName');
});

it('should open the content inside an iframe, and focus', () => {
it('should open the content inside an iframe, center, and focus', () => {
image.print();
expect(stubs.openContentInsideIframe).to.be.called;
expect(stubs.iframe.contentWindow.focus).to.be.called;
expect(image.printImage.style.display).to.equal('block');
expect(image.printImage.style.margin).to.equal('0px auto');
expect(stubs.focus).to.be.called;
});

it('should execute the print command if the browser is Explorer', () => {
stubs.getName.returns('Explorer');

image.print();
expect(stubs.iframe.contentWindow.document.execCommand).to.be.calledWith('print', false, null);
expect(stubs.execCommand).to.be.calledWith('print', false, null);
});

it('should execute the print command if the browser is Edge', () => {
stubs.getName.returns('Edge');

image.print();
expect(stubs.iframe.contentWindow.document.execCommand).to.be.calledWith('print', false, null);
expect(stubs.execCommand).to.be.calledWith('print', false, null);
});

it('should call the contentWindow print for other browsers', () => {
stubs.getName.returns('Chrome');

image.print();
expect(stubs.iframe.contentWindow.print).to.be.called;
expect(stubs.print).to.be.called;
});
});

Expand Down