Skip to content

Commit

Permalink
Merge pull request #9841 from Snuffleupagus/initial-position-misc-cle…
Browse files Browse the repository at this point in the history
…anup

Various (small) cleanup related to setting the initial document position on load
  • Loading branch information
timvandermeij authored Jun 26, 2018
2 parents 14b69a4 + 95a4fa2 commit 40d577d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
30 changes: 20 additions & 10 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ import { Toolbar } from './toolbar';
import { ViewHistory } from './view_history';

const DEFAULT_SCALE_DELTA = 1.1;
const DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000;
const DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000; // ms
const FORCE_PAGES_LOADED_TIMEOUT = 10000; // ms

const DefaultExternalServices = {
updateFindControlState(data) {},
Expand Down Expand Up @@ -1016,8 +1017,7 @@ let PDFViewerApplication = {
hash: null,
};
let storePromise = store.getMultiple({
exists: false,
page: '1',
page: null,
zoom: DEFAULT_SCALE_VALUE,
scrollLeft: '0',
scrollTop: '0',
Expand All @@ -1030,17 +1030,18 @@ let PDFViewerApplication = {
Promise.all([storePromise, pageModePromise]).then(
([values = {}, pageMode]) => {
// Initialize the default values, from user preferences.
let hash = AppOptions.get('defaultZoomValue') ?
('zoom=' + AppOptions.get('defaultZoomValue')) : null;
const zoom = AppOptions.get('defaultZoomValue');
let hash = zoom ? `zoom=${zoom}` : null;

let rotation = null;
let sidebarView = AppOptions.get('sidebarViewOnLoad');
let scrollMode = AppOptions.get('scrollModeOnLoad');
let spreadMode = AppOptions.get('spreadModeOnLoad');

if (values.exists && AppOptions.get('showPreviousViewOnLoad')) {
hash = 'page=' + values.page +
'&zoom=' + (AppOptions.get('defaultZoomValue') || values.zoom) +
if (values.page && AppOptions.get('showPreviousViewOnLoad')) {
hash = 'page=' + values.page + '&zoom=' + (zoom || values.zoom) +
',' + values.scrollLeft + ',' + values.scrollTop;

rotation = parseInt(values.rotation, 10);
sidebarView = sidebarView || (values.sidebarView | 0);
if (values.scrollMode !== null) {
Expand Down Expand Up @@ -1074,10 +1075,20 @@ let PDFViewerApplication = {
if (!this.isViewerEmbedded) {
pdfViewer.focus();
}
return pagesPromise;

return Promise.race([
pagesPromise,
new Promise((resolve) => {
setTimeout(resolve, FORCE_PAGES_LOADED_TIMEOUT);
}),
]);
}).then(() => {
// For documents with different page sizes, once all pages are resolved,
// ensure that the correct location becomes visible on load.
// To reduce the risk, in very large and/or slow loading documents,
// that the location changes *after* the user has started interacting
// with the viewer, wait for either `pagesPromise` or a timeout above.

if (!initialParams.bookmark && !initialParams.hash) {
return;
}
Expand Down Expand Up @@ -1852,7 +1863,6 @@ function webViewerUpdateViewarea(evt) {

if (store && PDFViewerApplication.isInitialViewSet) {
store.setMultiple({
'exists': true,
'page': location.pageNumber,
'zoom': location.scale,
'scrollLeft': location.left,
Expand Down
11 changes: 6 additions & 5 deletions web/view_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ class ViewHistory {
let database = JSON.parse(databaseStr || '{}');
if (!('files' in database)) {
database.files = [];
} else {
while (database.files.length >= this.cacheSize) {
database.files.shift();
}
}
if (database.files.length >= this.cacheSize) {
database.files.shift();
}
let index;
let index = -1;
for (let i = 0, length = database.files.length; i < length; i++) {
let branch = database.files[i];
if (branch.fingerprint === this.fingerprint) {
index = i;
break;
}
}
if (typeof index !== 'number') {
if (index === -1) {
index = database.files.push({ fingerprint: this.fingerprint, }) - 1;
}
this.file = database.files[index];
Expand Down

0 comments on commit 40d577d

Please sign in to comment.