Skip to content

Commit

Permalink
Merge pull request #9858 from Snuffleupagus/scrollMode-refactor
Browse files Browse the repository at this point in the history
Additional Scroll/Spread mode clean-up (PR 9832 follow-up)
  • Loading branch information
timvandermeij authored Jun 30, 2018
2 parents 4fbad9a + ff26d41 commit 872c6e4
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 127 deletions.
36 changes: 17 additions & 19 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,7 @@ let PDFViewerApplication = {
this.toolbar.setPagesCount(pdfDocument.numPages, false);
this.secondaryToolbar.setPagesCount(pdfDocument.numPages);

let id = this.documentFingerprint = pdfDocument.fingerprint;
let store = this.store = new ViewHistory(id);
const store = this.store = new ViewHistory(pdfDocument.fingerprint);

let baseDocumentUrl;
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
Expand Down Expand Up @@ -1003,7 +1002,7 @@ let PDFViewerApplication = {
// The browsing history is only enabled when the viewer is standalone,
// i.e. not when it is embedded in a web page.
let resetHistory = !AppOptions.get('showPreviousViewOnLoad');
this.pdfHistory.initialize(id, resetHistory);
this.pdfHistory.initialize(pdfDocument.fingerprint, resetHistory);

if (this.pdfHistory.initialBookmark) {
this.initialBookmark = this.pdfHistory.initialBookmark;
Expand Down Expand Up @@ -1044,12 +1043,8 @@ let PDFViewerApplication = {

rotation = parseInt(values.rotation, 10);
sidebarView = sidebarView || (values.sidebarView | 0);
if (values.scrollMode !== null) {
scrollMode = values.scrollMode;
}
if (values.spreadMode !== null) {
spreadMode = values.spreadMode;
}
scrollMode = scrollMode || (values.scrollMode | 0);
spreadMode = spreadMode || (values.spreadMode | 0);
}
if (pageMode && !AppOptions.get('disablePageMode')) {
// Always let the user preference/history take precedence.
Expand Down Expand Up @@ -1246,23 +1241,26 @@ let PDFViewerApplication = {
});
},

setInitialView(storedHash, values = {}) {
let { rotation, sidebarView, scrollMode, spreadMode, } = values;
setInitialView(storedHash, { rotation, sidebarView,
scrollMode, spreadMode, } = {}) {
let setRotation = (angle) => {
if (isValidRotation(angle)) {
this.pdfViewer.pagesRotation = angle;
}
};
let setViewerModes = (scroll, spread) => {
if (Number.isInteger(scroll)) {
this.pdfViewer.scrollMode = scroll;
}
if (Number.isInteger(spread)) {
this.pdfViewer.spreadMode = spread;
}
};

// Putting these before isInitialViewSet = true prevents these values from
// being stored in the document history (and overriding any future changes
// made to the corresponding global preferences), just this once.
if (Number.isInteger(scrollMode)) {
this.pdfViewer.setScrollMode(scrollMode);
}
if (Number.isInteger(spreadMode)) {
this.pdfViewer.setSpreadMode(spreadMode);
}
setViewerModes(scrollMode, spreadMode);

this.isInitialViewSet = true;
this.pdfSidebar.setInitialView(sidebarView);
Expand Down Expand Up @@ -2021,10 +2019,10 @@ function webViewerRotateCcw() {
PDFViewerApplication.rotatePages(-90);
}
function webViewerSwitchScrollMode(evt) {
PDFViewerApplication.pdfViewer.setScrollMode(evt.mode);
PDFViewerApplication.pdfViewer.scrollMode = evt.mode;
}
function webViewerSwitchSpreadMode(evt) {
PDFViewerApplication.pdfViewer.setSpreadMode(evt.mode);
PDFViewerApplication.pdfViewer.spreadMode = evt.mode;
}
function webViewerDocumentProperties() {
PDFViewerApplication.pdfDocumentProperties.open();
Expand Down
10 changes: 10 additions & 0 deletions web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ const defaultOptions = {
value: 0,
kind: OptionKind.VIEWER,
},
scrollModeOnLoad: {
/** @type {number} */
value: 0,
kind: OptionKind.VIEWER,
},
spreadModeOnLoad: {
/** @type {number} */
value: 0,
kind: OptionKind.VIEWER,
},
textLayerMode: {
/** @type {number} */
value: 1,
Expand Down
155 changes: 132 additions & 23 deletions web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ const SpreadMode = {
* size in total pixels, i.e. width * height. Use -1 for no limit.
* The default value is 4096 * 4096 (16 mega-pixels).
* @property {IL10n} l10n - Localization service.
* @property {number} scrollMode - (optional) The direction in which the
* document pages should be laid out within the scrolling container. The
* constants from {ScrollMode} should be used. The default value is
* `ScrollMode.VERTICAL`.
* @property {number} spreadMode - (optional) If not `SpreadMode.NONE`, groups
* pages into spreads, starting with odd- or even-numbered pages. The
* constants from {SpreadMode} should be used. The default value is
* `SpreadMode.NONE`.
*/

function PDFPageViewBuffer(size) {
Expand Down Expand Up @@ -162,8 +154,6 @@ class BaseViewer {
this.useOnlyCssZoom = options.useOnlyCssZoom || false;
this.maxCanvasPixels = options.maxCanvasPixels;
this.l10n = options.l10n || NullL10n;
this.scrollMode = options.scrollMode || ScrollMode.VERTICAL;
this.spreadMode = options.spreadMode || SpreadMode.NONE;

this.defaultRenderingQueue = !options.renderingQueue;
if (this.defaultRenderingQueue) {
Expand All @@ -181,8 +171,17 @@ class BaseViewer {
if (this.removePageBorders) {
this.viewer.classList.add('removePageBorders');
}
if (this.scrollMode !== ScrollMode.VERTICAL) {
this._updateScrollModeClasses();

if ((typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) &&
('scrollMode' in options || 'spreadMode' in options)) {
console.error(`The ${this._name} constructor options ` +
'`scrollMode`/`spreadMode` are deprecated, use the setters instead.');
if (options.scrollMode !== undefined) {
this.scrollMode = options.scrollMode;
}
if (options.spreadMode !== undefined) {
this.spreadMode = options.spreadMode;
}
}
}

Expand Down Expand Up @@ -441,8 +440,8 @@ class BaseViewer {
bindOnAfterAndBeforeDraw(pageView);
this._pages.push(pageView);
}
if (this.spreadMode !== SpreadMode.NONE) {
this._regroupSpreads();
if (this._spreadMode !== SpreadMode.NONE) {
this._updateSpreadMode();
}

// Fetch all the pages since the viewport is needed before printing
Expand Down Expand Up @@ -524,9 +523,13 @@ class BaseViewer {
this._pagesRotation = 0;
this._pagesRequests = [];
this._pageViewsReady = false;
this._scrollMode = ScrollMode.VERTICAL;
this._spreadMode = SpreadMode.NONE;

// Remove the pages from the DOM.
// Remove the pages from the DOM...
this.viewer.textContent = '';
// ... and reset the Scroll mode CSS class(es) afterwards.
this._updateScrollMode();
}

_scrollUpdate() {
Expand Down Expand Up @@ -1023,26 +1026,132 @@ class BaseViewer {
});
}

setScrollMode(mode) {
/**
* @return {number} One of the values in {ScrollMode}.
*/
get scrollMode() {
return this._scrollMode;
}

/**
* @param {number} mode - The direction in which the document pages should be
* laid out within the scrolling container.
* The constants from {ScrollMode} should be used.
*/
set scrollMode(mode) {
if (this._scrollMode === mode) {
return; // The Scroll mode didn't change.
}
if (!Number.isInteger(mode) || !Object.values(ScrollMode).includes(mode)) {
throw new Error(`Invalid scroll mode: ${mode}`);
}
this.scrollMode = mode;
this._scrollMode = mode;
this.eventBus.dispatch('scrollmodechanged', { source: this, mode, });

this._updateScrollMode(/* pageNumber = */ this._currentPageNumber);
}

_updateScrollModeClasses() {
// No-op in the base class.
_updateScrollMode(pageNumber = null) {
const scrollMode = this._scrollMode, viewer = this.viewer;

if (scrollMode === ScrollMode.HORIZONTAL) {
viewer.classList.add('scrollHorizontal');
} else {
viewer.classList.remove('scrollHorizontal');
}
if (scrollMode === ScrollMode.WRAPPED) {
viewer.classList.add('scrollWrapped');
} else {
viewer.classList.remove('scrollWrapped');
}

if (!this.pdfDocument || !pageNumber) {
return;
}
// Non-numeric scale values can be sensitive to the scroll orientation.
// Call this before re-scrolling to the current page, to ensure that any
// changes in scale don't move the current page.
if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
this._setScale(this._currentScaleValue, true);
}
this.scrollPageIntoView({ pageNumber, });
this.update();
}

setSpreadMode(mode) {
setScrollMode(mode) {
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
console.error(`${this._name}.setScrollMode() is deprecated, ` +
`use the ${this._name}.scrollMode setter instead.`);
this.scrollMode = mode;
}
}

/**
* @return {number} One of the values in {SpreadMode}.
*/
get spreadMode() {
return this._spreadMode;
}

/**
* @param {number} mode - Group the pages in spreads, starting with odd- or
* even-number pages (unless `SpreadMode.NONE` is used).
* The constants from {SpreadMode} should be used.
*/
set spreadMode(mode) {
if (this._spreadMode === mode) {
return; // The Spread mode didn't change.
}
if (!Number.isInteger(mode) || !Object.values(SpreadMode).includes(mode)) {
throw new Error(`Invalid spread mode: ${mode}`);
}
this.spreadMode = mode;
this._spreadMode = mode;
this.eventBus.dispatch('spreadmodechanged', { source: this, mode, });

this._updateSpreadMode(/* pageNumber = */ this._currentPageNumber);
}

_updateSpreadMode(pageNumber = null) {
if (!this.pdfDocument) {
return;
}
const viewer = this.viewer, pages = this._pages;
// Temporarily remove all the pages from the DOM.
viewer.textContent = '';

if (this._spreadMode === SpreadMode.NONE) {
for (let i = 0, iMax = pages.length; i < iMax; ++i) {
viewer.appendChild(pages[i].div);
}
} else {
const parity = this._spreadMode - 1;
let spread = null;
for (let i = 0, iMax = pages.length; i < iMax; ++i) {
if (spread === null) {
spread = document.createElement('div');
spread.className = 'spread';
viewer.appendChild(spread);
} else if (i % 2 === parity) {
spread = spread.cloneNode(false);
viewer.appendChild(spread);
}
spread.appendChild(pages[i].div);
}
}

if (!pageNumber) {
return;
}
this.scrollPageIntoView({ pageNumber, });
this.update();
}

_regroupSpreads() {
// No-op in the base class.
setSpreadMode(mode) {
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
console.error(`${this._name}.setSpreadMode() is deprecated, ` +
`use the ${this._name}.spreadMode setter instead.`);
this.spreadMode = mode;
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions web/pdf_single_page_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ class PDFSinglePageViewer extends BaseViewer {
// The Scroll/Spread modes are never used in `PDFSinglePageViewer`.
return shadow(this, '_isScrollModeHorizontal', false);
}

_updateScrollMode() { }

_updateSpreadMode() { }
}

export {
Expand Down
Loading

0 comments on commit 872c6e4

Please sign in to comment.