Move the isPortraitOrientation helper function from web/base_viewer.js to web/ui_utils.js

A couple of basic unit-tests are added, and a manual `isLandscape` check (in `web/base_viewer.js`) is also converted to use the helper function instead.
This commit is contained in:
Jonas Jenwald 2018-03-20 13:25:13 +01:00
parent 5c1a16ba6e
commit 77d025dc14
3 changed files with 33 additions and 11 deletions

View File

@ -15,7 +15,7 @@
import { import {
binarySearchFirstItem, EventBus, getPageSizeInches, getPDFFileNameFromURL, binarySearchFirstItem, EventBus, getPageSizeInches, getPDFFileNameFromURL,
isValidRotation, waitOnEventOrTimeout, WaitOnType isPortraitOrientation, isValidRotation, waitOnEventOrTimeout, WaitOnType
} from '../../web/ui_utils'; } from '../../web/ui_utils';
import { createObjectURL } from '../../src/shared/util'; import { createObjectURL } from '../../src/shared/util';
import isNodeJS from '../../src/shared/is_node'; import isNodeJS from '../../src/shared/is_node';
@ -285,6 +285,27 @@ describe('ui_utils', function() {
}); });
}); });
describe('isPortraitOrientation', function() {
it('should be portrait orientation', function() {
expect(isPortraitOrientation({
width: 200,
height: 400,
})).toEqual(true);
expect(isPortraitOrientation({
width: 500,
height: 500,
})).toEqual(true);
});
it('should be landscape orientation', function() {
expect(isPortraitOrientation({
width: 600,
height: 300,
})).toEqual(false);
});
});
describe('waitOnEventOrTimeout', function() { describe('waitOnEventOrTimeout', function() {
let eventBus; let eventBus;

View File

@ -14,9 +14,10 @@
*/ */
import { import {
CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, isValidRotation, CSS_UNITS, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, isPortraitOrientation,
MAX_AUTO_SCALE, NullL10n, PresentationModeState, RendererType, isValidRotation, MAX_AUTO_SCALE, NullL10n, PresentationModeState,
SCROLLBAR_PADDING, TextLayerMode, UNKNOWN_SCALE, VERTICAL_PADDING, watchScroll RendererType, SCROLLBAR_PADDING, TextLayerMode, UNKNOWN_SCALE,
VERTICAL_PADDING, watchScroll
} from './ui_utils'; } from './ui_utils';
import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue'; import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue';
import { AnnotationLayerBuilder } from './annotation_layer_builder'; import { AnnotationLayerBuilder } from './annotation_layer_builder';
@ -94,10 +95,6 @@ function isSameScale(oldScale, newScale) {
return false; return false;
} }
function isPortraitOrientation(size) {
return size.width <= size.height;
}
/** /**
* Simple viewer control to display PDF content/pages. * Simple viewer control to display PDF content/pages.
* @implements {IRenderableView} * @implements {IRenderableView}
@ -578,11 +575,10 @@ class BaseViewer {
scale = Math.min(pageWidthScale, pageHeightScale); scale = Math.min(pageWidthScale, pageHeightScale);
break; break;
case 'auto': case 'auto':
let isLandscape = (currentPage.width > currentPage.height);
// For pages in landscape mode, fit the page height to the viewer // For pages in landscape mode, fit the page height to the viewer
// *unless* the page would thus become too wide to fit horizontally. // *unless* the page would thus become too wide to fit horizontally.
let horizontalScale = isLandscape ? let horizontalScale = isPortraitOrientation(currentPage) ?
Math.min(pageHeightScale, pageWidthScale) : pageWidthScale; pageWidthScale : Math.min(pageHeightScale, pageWidthScale);
scale = Math.min(MAX_AUTO_SCALE, horizontalScale); scale = Math.min(MAX_AUTO_SCALE, horizontalScale);
break; break;
default: default:

View File

@ -439,6 +439,10 @@ function isValidRotation(angle) {
return Number.isInteger(angle) && angle % 90 === 0; return Number.isInteger(angle) && angle % 90 === 0;
} }
function isPortraitOrientation(size) {
return size.width <= size.height;
}
function cloneObj(obj) { function cloneObj(obj) {
let result = Object.create(null); let result = Object.create(null);
for (let i in obj) { for (let i in obj) {
@ -642,6 +646,7 @@ export {
SCROLLBAR_PADDING, SCROLLBAR_PADDING,
VERTICAL_PADDING, VERTICAL_PADDING,
isValidRotation, isValidRotation,
isPortraitOrientation,
isFileSchema, isFileSchema,
cloneObj, cloneObj,
PresentationModeState, PresentationModeState,