Merge pull request #11725 from Snuffleupagus/app-ViewHistory-init

Re-factor `PDFViewerApplication.load` such that `{PDFViewer, PDFThumbnailViewer}.setDocument` happens slightly earlier
This commit is contained in:
Tim van der Meij 2020-03-22 17:15:44 +01:00 committed by GitHub
commit b86df97725
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 109 deletions

View File

@ -1040,8 +1040,6 @@ const PDFViewerApplication = {
this.toolbar.setPagesCount(pdfDocument.numPages, false); this.toolbar.setPagesCount(pdfDocument.numPages, false);
this.secondaryToolbar.setPagesCount(pdfDocument.numPages); this.secondaryToolbar.setPagesCount(pdfDocument.numPages);
const store = (this.store = new ViewHistory(pdfDocument.fingerprint));
let baseDocumentUrl; let baseDocumentUrl;
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
baseDocumentUrl = null; baseDocumentUrl = null;
@ -1060,123 +1058,115 @@ const PDFViewerApplication = {
const pdfThumbnailViewer = this.pdfThumbnailViewer; const pdfThumbnailViewer = this.pdfThumbnailViewer;
pdfThumbnailViewer.setDocument(pdfDocument); pdfThumbnailViewer.setDocument(pdfDocument);
const storedPromise = (this.store = new ViewHistory(
pdfDocument.fingerprint
))
.getMultiple({
page: null,
zoom: DEFAULT_SCALE_VALUE,
scrollLeft: "0",
scrollTop: "0",
rotation: null,
sidebarView: SidebarView.UNKNOWN,
scrollMode: ScrollMode.UNKNOWN,
spreadMode: SpreadMode.UNKNOWN,
})
.catch(() => {
/* Unable to read from storage; ignoring errors. */
return Object.create(null);
});
firstPagePromise.then(pdfPage => { firstPagePromise.then(pdfPage => {
this.loadingBar.setWidth(this.appConfig.viewerContainer); this.loadingBar.setWidth(this.appConfig.viewerContainer);
const storePromise = store
.getMultiple({
page: null,
zoom: DEFAULT_SCALE_VALUE,
scrollLeft: "0",
scrollTop: "0",
rotation: null,
sidebarView: SidebarView.UNKNOWN,
scrollMode: ScrollMode.UNKNOWN,
spreadMode: SpreadMode.UNKNOWN,
})
.catch(() => {
/* Unable to read from storage; ignoring errors. */
});
Promise.all([ Promise.all([
animationStarted, animationStarted,
storePromise, storedPromise,
pageLayoutPromise, pageLayoutPromise,
pageModePromise, pageModePromise,
openActionPromise, openActionPromise,
]) ])
.then( .then(async ([timeStamp, stored, pageLayout, pageMode, openAction]) => {
async ([ const viewOnLoad = AppOptions.get("viewOnLoad");
timeStamp,
values = {},
pageLayout,
pageMode,
openAction,
]) => {
const viewOnLoad = AppOptions.get("viewOnLoad");
this._initializePdfHistory({ this._initializePdfHistory({
fingerprint: pdfDocument.fingerprint, fingerprint: pdfDocument.fingerprint,
viewOnLoad, viewOnLoad,
initialDest: openAction && openAction.dest, initialDest: openAction && openAction.dest,
}); });
const initialBookmark = this.initialBookmark; const initialBookmark = this.initialBookmark;
// Initialize the default values, from user preferences. // Initialize the default values, from user preferences.
const zoom = AppOptions.get("defaultZoomValue"); const zoom = AppOptions.get("defaultZoomValue");
let hash = zoom ? `zoom=${zoom}` : null; let hash = zoom ? `zoom=${zoom}` : null;
let rotation = null; let rotation = null;
let sidebarView = AppOptions.get("sidebarViewOnLoad"); let sidebarView = AppOptions.get("sidebarViewOnLoad");
let scrollMode = AppOptions.get("scrollModeOnLoad"); let scrollMode = AppOptions.get("scrollModeOnLoad");
let spreadMode = AppOptions.get("spreadModeOnLoad"); let spreadMode = AppOptions.get("spreadModeOnLoad");
if (values.page && viewOnLoad !== ViewOnLoad.INITIAL) { if (stored.page && viewOnLoad !== ViewOnLoad.INITIAL) {
hash = hash =
`page=${values.page}&zoom=${zoom || values.zoom},` + `page=${stored.page}&zoom=${zoom || stored.zoom},` +
`${values.scrollLeft},${values.scrollTop}`; `${stored.scrollLeft},${stored.scrollTop}`;
rotation = parseInt(values.rotation, 10); rotation = parseInt(stored.rotation, 10);
// Always let user preferences take precedence over the view // Always let user preference take precedence over the view history.
// history. if (sidebarView === SidebarView.UNKNOWN) {
if (sidebarView === SidebarView.UNKNOWN) { sidebarView = stored.sidebarView | 0;
sidebarView = values.sidebarView | 0;
}
if (scrollMode === ScrollMode.UNKNOWN) {
scrollMode = values.scrollMode | 0;
}
if (spreadMode === SpreadMode.UNKNOWN) {
spreadMode = values.spreadMode | 0;
}
} }
// Always let the user preference/view history take precedence. if (scrollMode === ScrollMode.UNKNOWN) {
if (pageMode && sidebarView === SidebarView.UNKNOWN) { scrollMode = stored.scrollMode | 0;
sidebarView = apiPageModeToSidebarView(pageMode);
} }
if (pageLayout && spreadMode === SpreadMode.UNKNOWN) { if (spreadMode === SpreadMode.UNKNOWN) {
spreadMode = apiPageLayoutToSpreadMode(pageLayout); spreadMode = stored.spreadMode | 0;
} }
this.setInitialView(hash, {
rotation,
sidebarView,
scrollMode,
spreadMode,
});
this.eventBus.dispatch("documentinit", { source: this });
// Make all navigation keys work on document load,
// unless the viewer is embedded in a web page.
if (!this.isViewerEmbedded) {
pdfViewer.focus();
}
// 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.)
await Promise.race([
pagesPromise,
new Promise(resolve => {
setTimeout(resolve, FORCE_PAGES_LOADED_TIMEOUT);
}),
]);
if (!initialBookmark && !hash) {
return;
}
if (pdfViewer.hasEqualPageSizes) {
return;
}
this.initialBookmark = initialBookmark;
// eslint-disable-next-line no-self-assign
pdfViewer.currentScaleValue = pdfViewer.currentScaleValue;
// Re-apply the initial document location.
this.setInitialView(hash);
} }
) // Always let the user preference/view history take precedence.
if (pageMode && sidebarView === SidebarView.UNKNOWN) {
sidebarView = apiPageModeToSidebarView(pageMode);
}
if (pageLayout && spreadMode === SpreadMode.UNKNOWN) {
spreadMode = apiPageLayoutToSpreadMode(pageLayout);
}
this.setInitialView(hash, {
rotation,
sidebarView,
scrollMode,
spreadMode,
});
this.eventBus.dispatch("documentinit", { source: this });
// Make all navigation keys work on document load,
// unless the viewer is embedded in a web page.
if (!this.isViewerEmbedded) {
pdfViewer.focus();
}
// 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.)
await Promise.race([
pagesPromise,
new Promise(resolve => {
setTimeout(resolve, FORCE_PAGES_LOADED_TIMEOUT);
}),
]);
if (!initialBookmark && !hash) {
return;
}
if (pdfViewer.hasEqualPageSizes) {
return;
}
this.initialBookmark = initialBookmark;
// eslint-disable-next-line no-self-assign
pdfViewer.currentScaleValue = pdfViewer.currentScaleValue;
// Re-apply the initial document location.
this.setInitialView(hash);
})
.catch(() => { .catch(() => {
// Ensure that the document is always completely initialized, // Ensure that the document is always completely initialized,
// even if there are any errors thrown above. // even if there are any errors thrown above.

View File

@ -31,19 +31,20 @@ class ViewHistory {
this._initializedPromise = this._readFromStorage().then(databaseStr => { this._initializedPromise = this._readFromStorage().then(databaseStr => {
const database = JSON.parse(databaseStr || "{}"); const database = JSON.parse(databaseStr || "{}");
if (!("files" in database)) { let index = -1;
if (!Array.isArray(database.files)) {
database.files = []; database.files = [];
} else { } else {
while (database.files.length >= this.cacheSize) { while (database.files.length >= this.cacheSize) {
database.files.shift(); database.files.shift();
} }
}
let index = -1; for (let i = 0, ii = database.files.length; i < ii; i++) {
for (let i = 0, length = database.files.length; i < length; i++) { const branch = database.files[i];
const branch = database.files[i]; if (branch.fingerprint === this.fingerprint) {
if (branch.fingerprint === this.fingerprint) { index = i;
index = i; break;
break; }
} }
} }
if (index === -1) { if (index === -1) {