Skip to content

Commit

Permalink
Merge pull request #10502 from Snuffleupagus/adjust-onLoad-prefs
Browse files Browse the repository at this point in the history
Modify a number of the viewer preferences, whose current default value is `0`, such that they behave as expected with the view history
  • Loading branch information
timvandermeij authored Feb 4, 2019
2 parents 9f9d87c + ef634b5 commit c0d6e46
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 140 deletions.
17 changes: 17 additions & 0 deletions extensions/chromium/options/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ limitations under the License.
'disableTextLayer',
'enhanceTextSelection',
'textLayerMode',
'showPreviousViewOnLoad',
'disablePageMode',
'viewOnLoad',
], function(items) {
// Migration code for https://github.com/mozilla/pdf.js/pull/7635.
if (typeof items.enableHandToolOnLoad === 'boolean') {
Expand Down Expand Up @@ -113,6 +116,20 @@ limitations under the License.
storageSync.remove(['disableTextLayer', 'enhanceTextSelection']);
}
}
// Migration code for https://github.com/mozilla/pdf.js/pull/10502.
if (typeof items.showPreviousViewOnLoad === 'boolean') {
if (!items.showPreviousViewOnLoad) {
storageSync.set({
viewOnLoad: 1,
}, function() {
if (!chrome.runtime.lastError) {
storageSync.remove(['showPreviousViewOnLoad', 'disablePageMode']);
}
});
} else {
storageSync.remove(['showPreviousViewOnLoad', 'disablePageMode']);
}
}
});
}
})();
16 changes: 16 additions & 0 deletions extensions/chromium/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@
</div>
</template>

<template id="viewOnLoad-template">
<div class="settings-row">
<label>
<span></span>
<select>
<option value="-1">Default</option>
<option value="0">Show previous position</option>
<option value="1">Show initial position</option>
</select>
</label>
</div>
</template>

<template id="defaultZoomValue-template">
<div class="settings-row">
<label>
Expand Down Expand Up @@ -71,6 +84,7 @@
<label>
<span></span>
<select>
<option value="-1">Default</option>
<option value="0">Do not show sidebar</option>
<option value="1">Show thumbnails in sidebar</option>
<option value="2">Show document outline in sidebar</option>
Expand Down Expand Up @@ -125,6 +139,7 @@
<label>
<span></span>
<select>
<option value="-1">Default</option>
<option value="0">Vertical scrolling</option>
<option value="1">Horizontal scrolling</option>
<option value="2">Wrapped scrolling</option>
Expand All @@ -138,6 +153,7 @@
<label>
<span></span>
<select>
<option value="-1">Default</option>
<option value="0">No spreads</option>
<option value="1">Odd spreads</option>
<option value="2">Even spreads</option>
Expand Down
33 changes: 21 additions & 12 deletions extensions/chromium/preferences_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
"type": "object",
"properties": {
"showPreviousViewOnLoad": {
"title": "Show previous position of PDF upon load",
"description": "Whether to view PDF documents in the last page and position upon opening the viewer.",
"description": "DEPRECATED. Set viewOnLoad to 1 to disable showing the last page/position on load.",
"type": "boolean",
"default": true
},
"viewOnLoad": {
"title": "View position on load",
"description": "The position in the document upon load.\n -1 = Default (uses OpenAction if available, otherwise equal to `viewOnLoad = 0`).\n 0 = The last viewed page/position.\n 1 = The initial page/position.",
"enum": [
-1,
0,
1
],
"default": 0
},
"defaultZoomValue": {
"title": "Default zoom level",
"description": "Default zoom level of the viewer. Accepted values: 'auto', 'page-actual', 'page-width', 'page-height', 'page-fit', or a zoom level in percents.",
Expand All @@ -16,15 +25,16 @@
},
"sidebarViewOnLoad": {
"title": "Sidebar state on load",
"description": "Controls the state of the sidebar upon load.\n 0 = do not show sidebar.\n 1 = show thumbnails in sidebar.\n 2 = show document outline in sidebar.\n 3 = Show attachments in sidebar.",
"description": "Controls the state of the sidebar upon load.\n -1 = Default (uses PageMode if available, otherwise the last position if available/enabled).\n 0 = Do not show sidebar.\n 1 = Show thumbnails in sidebar.\n 2 = Show document outline in sidebar.\n 3 = Show attachments in sidebar.",
"type": "integer",
"enum": [
-1,
0,
1,
2,
3
],
"default": 0
"default": -1
},
"enableHandToolOnLoad": {
"description": "DEPRECATED. Set cursorToolOnLoad to 1 to enable the hand tool by default.",
Expand Down Expand Up @@ -117,15 +127,12 @@
],
"default": 0
},
"disableOpenActionDestination": {
"type": "boolean",
"default": true
},
"disablePageLabels": {
"type": "boolean",
"default": false
},
"disablePageMode": {
"description": "DEPRECATED.",
"type": "boolean",
"default": false
},
Expand Down Expand Up @@ -159,25 +166,27 @@
},
"scrollModeOnLoad": {
"title": "Scroll mode on load",
"description": "Controls how the viewer scrolls upon load.\n 0 = Vertical scrolling.\n 1 = Horizontal scrolling.\n 2 = Wrapped scrolling.",
"description": "Controls how the viewer scrolls upon load.\n -1 = Default (uses the last position if available/enabled).\n 0 = Vertical scrolling.\n 1 = Horizontal scrolling.\n 2 = Wrapped scrolling.",
"type": "integer",
"enum": [
-1,
0,
1,
2
],
"default": 0
"default": -1
},
"spreadModeOnLoad": {
"title": "Spread mode on load",
"description": "Whether the viewer should join pages into spreads upon load.\n 0 = No spreads.\n 1 = Odd spreads.\n 2 = Even spreads.",
"description": "Whether the viewer should join pages into spreads upon load.\n -1 = Default (uses the last position if available/enabled).\n 0 = No spreads.\n 1 = Odd spreads.\n 2 = Even spreads.",
"type": "integer",
"enum": [
-1,
0,
1,
2
],
"default": 0
"default": -1
}
}
}
126 changes: 75 additions & 51 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

import {
animationStarted, DEFAULT_SCALE_VALUE, getGlobalEventBus,
getPDFFileNameFromURL, isValidRotation, MAX_SCALE, MIN_SCALE,
noContextMenuHandler, normalizeWheelEventDelta, parseQueryString,
PresentationModeState, ProgressBar, RendererType, TextLayerMode
getPDFFileNameFromURL, isValidRotation, isValidScrollMode, isValidSpreadMode,
MAX_SCALE, MIN_SCALE, noContextMenuHandler, normalizeWheelEventDelta,
parseQueryString, PresentationModeState, ProgressBar, RendererType,
ScrollMode, SpreadMode, TextLayerMode
} from './ui_utils';
import {
build, createObjectURL, getDocument, getFilenameFromUrl, GlobalWorkerOptions,
Expand Down Expand Up @@ -52,6 +53,12 @@ const DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000; // ms
const FORCE_PAGES_LOADED_TIMEOUT = 10000; // ms
const WHEEL_ZOOM_DISABLED_TIMEOUT = 1000; // ms

const ViewOnLoad = {
UNKNOWN: -1,
PREVIOUS: 0, // Default value.
INITIAL: 1,
};

const DefaultExternalServices = {
updateFindControlState(data) {},
updateFindMatchesCount(data) {},
Expand Down Expand Up @@ -900,46 +907,29 @@ let PDFViewerApplication = {
firstPagePromise.then((pdfPage) => {
this.loadingBar.setWidth(this.appConfig.viewerContainer);

if (!AppOptions.get('disableHistory') && !this.isViewerEmbedded) {
// The browsing history is only enabled when the viewer is standalone,
// i.e. not when it is embedded in a web page.
this.pdfHistory.initialize({
fingerprint: pdfDocument.fingerprint,
resetHistory: !AppOptions.get('showPreviousViewOnLoad'),
updateUrl: AppOptions.get('historyUpdateUrl'),
});

if (this.pdfHistory.initialBookmark) {
this.initialBookmark = this.pdfHistory.initialBookmark;

this.initialRotation = this.pdfHistory.initialRotation;
}
}

let storePromise = store.getMultiple({
const storePromise = store.getMultiple({
page: null,
zoom: DEFAULT_SCALE_VALUE,
scrollLeft: '0',
scrollTop: '0',
rotation: null,
sidebarView: SidebarView.NONE,
scrollMode: null,
spreadMode: null,
sidebarView: SidebarView.UNKNOWN,
scrollMode: ScrollMode.UNKNOWN,
spreadMode: SpreadMode.UNKNOWN,
}).catch(() => { /* Unable to read from storage; ignoring errors. */ });

Promise.all([
storePromise, pageModePromise, openActionDestPromise,
]).then(async ([values = {}, pageMode, openActionDest]) => {
if (openActionDest && !this.initialBookmark &&
!AppOptions.get('disableOpenActionDestination')) {
// Always let the browser history/document hash take precedence.
this.initialBookmark = JSON.stringify(openActionDest);
// TODO: Re-factor the `PDFHistory` initialization to remove this hack
// that's currently necessary to prevent weird initial history state.
this.pdfHistory.push({ explicitDest: openActionDest,
pageNumber: null, });
}
const viewOnLoad = AppOptions.get('viewOnLoad');

this._initializePdfHistory({
fingerprint: pdfDocument.fingerprint,
viewOnLoad,
initialDest: openActionDest,
});
const initialBookmark = this.initialBookmark;

// Initialize the default values, from user preferences.
const zoom = AppOptions.get('defaultZoomValue');
let hash = zoom ? `zoom=${zoom}` : null;
Expand All @@ -949,18 +939,25 @@ let PDFViewerApplication = {
let scrollMode = AppOptions.get('scrollModeOnLoad');
let spreadMode = AppOptions.get('spreadModeOnLoad');

if (values.page && AppOptions.get('showPreviousViewOnLoad')) {
hash = 'page=' + values.page + '&zoom=' + (zoom || values.zoom) +
',' + values.scrollLeft + ',' + values.scrollTop;
if (values.page && viewOnLoad !== ViewOnLoad.INITIAL) {
hash = `page=${values.page}&zoom=${zoom || values.zoom},` +
`${values.scrollLeft},${values.scrollTop}`;

rotation = parseInt(values.rotation, 10);
sidebarView = sidebarView || (values.sidebarView | 0);
scrollMode = scrollMode || (values.scrollMode | 0);
spreadMode = spreadMode || (values.spreadMode | 0);
// Always let user preferences take precedence over the view history.
if (sidebarView === SidebarView.UNKNOWN) {
sidebarView = (values.sidebarView | 0);
}
if (scrollMode === ScrollMode.UNKNOWN) {
scrollMode = (values.scrollMode | 0);
}
if (spreadMode === SpreadMode.UNKNOWN) {
spreadMode = (values.spreadMode | 0);
}
}
if (pageMode && !AppOptions.get('disablePageMode')) {
// Always let the user preference/history take precedence.
sidebarView = sidebarView || apiPageModeToSidebarView(pageMode);
// Always let the user preference/view history take precedence.
if (pageMode && sidebarView === SidebarView.UNKNOWN) {
sidebarView = apiPageModeToSidebarView(pageMode);
}

this.setInitialView(hash, {
Expand Down Expand Up @@ -1147,30 +1144,57 @@ let PDFViewerApplication = {
});
},

/**
* @private
*/
_initializePdfHistory({ fingerprint, viewOnLoad, initialDest = null, }) {
if (AppOptions.get('disableHistory') || this.isViewerEmbedded) {
// The browsing history is only enabled when the viewer is standalone,
// i.e. not when it is embedded in a web page.
return;
}
this.pdfHistory.initialize({
fingerprint,
resetHistory: viewOnLoad === ViewOnLoad.INITIAL,
updateUrl: AppOptions.get('historyUpdateUrl'),
});

if (this.pdfHistory.initialBookmark) {
this.initialBookmark = this.pdfHistory.initialBookmark;

this.initialRotation = this.pdfHistory.initialRotation;
}

// Always let the browser history/document hash take precedence.
if (initialDest && !this.initialBookmark &&
viewOnLoad === ViewOnLoad.UNKNOWN) {
this.initialBookmark = JSON.stringify(initialDest);
// TODO: Re-factor the `PDFHistory` initialization to remove this hack
// that's currently necessary to prevent weird initial history state.
this.pdfHistory.push({ explicitDest: initialDest, pageNumber: null, });
}
},

setInitialView(storedHash, { rotation, sidebarView,
scrollMode, spreadMode, } = {}) {
let setRotation = (angle) => {
const setRotation = (angle) => {
if (isValidRotation(angle)) {
this.pdfViewer.pagesRotation = angle;
}
};
let setViewerModes = (scroll, spread) => {
if (Number.isInteger(scroll)) {
const setViewerModes = (scroll, spread) => {
if (isValidScrollMode(scroll)) {
this.pdfViewer.scrollMode = scroll;
}
if (Number.isInteger(spread)) {
if (isValidSpreadMode(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.
setViewerModes(scrollMode, spreadMode);

this.isInitialViewSet = true;
this.pdfSidebar.setInitialView(sidebarView);

setViewerModes(scrollMode, spreadMode);

if (this.initialBookmark) {
setRotation(this.initialRotation);
delete this.initialRotation;
Expand Down
Loading

0 comments on commit c0d6e46

Please sign in to comment.