Merge pull request #10423 from Snuffleupagus/historyUpdateUrl

Add support for updating the document hash, off by default, when the browser history is updated (issue 5753)
This commit is contained in:
Tim van der Meij 2019-01-06 20:18:12 +01:00 committed by GitHub
commit e4d2a1604e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 73 additions and 10 deletions

View File

@ -147,6 +147,10 @@
"type": "boolean",
"default": false
},
"historyUpdateUrl": {
"type": "boolean",
"default": false
},
"enablePrintAutoRotate": {
"title": "Automatically rotate printed pages",
"description": "When enabled, pages whose orientation differ from the first page are rotated when printed.",

View File

@ -112,6 +112,24 @@ const hasDOM = typeof window === 'object' && typeof document === 'object';
};
})();
// Provides support for String.prototype.startsWith in legacy browsers.
// Support: IE, Chrome<41
(function checkStringStartsWith() {
if (String.prototype.startsWith) {
return;
}
require('core-js/fn/string/starts-with');
})();
// Provides support for String.prototype.endsWith in legacy browsers.
// Support: IE, Chrome<41
(function checkStringEndsWith() {
if (String.prototype.endsWith) {
return;
}
require('core-js/fn/string/ends-with');
})();
// Provides support for String.prototype.includes in legacy browsers.
// Support: IE, Chrome<41
(function checkStringIncludes() {
@ -223,6 +241,24 @@ const hasDOM = typeof window === 'object' && typeof document === 'object';
} // End of !PDFJSDev.test('CHROME')
// Provides support for String.prototype.padStart in legacy browsers.
// Support: IE, Chrome<57
(function checkStringPadStart() {
if (String.prototype.padStart) {
return;
}
require('core-js/fn/string/pad-start');
})();
// Provides support for String.prototype.padEnd in legacy browsers.
// Support: IE, Chrome<57
(function checkStringPadEnd() {
if (String.prototype.padEnd) {
return;
}
require('core-js/fn/string/pad-end');
})();
// Provides support for Object.values in legacy browsers.
// Support: IE, Chrome<54
(function checkObjectValues() {

View File

@ -903,8 +903,11 @@ let PDFViewerApplication = {
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.
let resetHistory = !AppOptions.get('showPreviousViewOnLoad');
this.pdfHistory.initialize(pdfDocument.fingerprint, resetHistory);
this.pdfHistory.initialize({
fingerprint: pdfDocument.fingerprint,
resetHistory: !AppOptions.get('showPreviousViewOnLoad'),
updateUrl: AppOptions.get('historyUpdateUrl'),
});
if (this.pdfHistory.initialBookmark) {
this.initialBookmark = this.pdfHistory.initialBookmark;

View File

@ -91,6 +91,11 @@ const defaultOptions = {
value: 0,
kind: OptionKind.VIEWER,
},
historyUpdateUrl: {
/** @type {boolean} */
value: false,
kind: OptionKind.VIEWER,
},
imageResourcesPath: {
/** @type {string} */
value: './images/',

View File

@ -19,6 +19,7 @@
"disableOpenActionDestination": true,
"disablePageMode": false,
"disablePageLabels": false,
"historyUpdateUrl": false,
"scrollModeOnLoad": 0,
"spreadModeOnLoad": 0
}

View File

@ -86,10 +86,9 @@ class IPDFLinkService {
*/
class IPDFHistory {
/**
* @param {string} fingerprint - The PDF document's unique fingerprint.
* @param {boolean} resetHistory - (optional) Reset the browsing history.
* @param {Object} params
*/
initialize(fingerprint, resetHistory = false) {}
initialize({ fingerprint, resetHistory = false, updateUrl = false, }) {}
/**
* @param {Object} params

View File

@ -30,6 +30,14 @@ const UPDATE_VIEWAREA_TIMEOUT = 1000; // milliseconds
* @property {EventBus} eventBus - The application event bus.
*/
/**
* @typedef {Object} InitializeParameters
* @property {string} fingerprint - The PDF document's unique fingerprint.
* @property {boolean} resetHistory - (optional) Reset the browsing history.
* @property {boolean} updateUrl - (optional) Attempt to update the document
* URL, with the current hash, when pushing/replacing browser history entries.
*/
/**
* @typedef {Object} PushParameters
* @property {string} namedDest - (optional) The named destination. If absent,
@ -82,10 +90,9 @@ class PDFHistory {
/**
* Initialize the history for the PDF document, using either the current
* browser history entry or the document hash, whichever is present.
* @param {string} fingerprint - The PDF document's unique fingerprint.
* @param {boolean} resetHistory - (optional) Reset the browsing history.
* @param {InitializeParameters} params
*/
initialize(fingerprint, resetHistory = false) {
initialize({ fingerprint, resetHistory = false, updateUrl = false, }) {
if (!fingerprint || typeof fingerprint !== 'string') {
console.error(
'PDFHistory.initialize: The "fingerprint" must be a non-empty string.');
@ -93,6 +100,7 @@ class PDFHistory {
}
let reInitialized = this.initialized && this.fingerprint !== fingerprint;
this.fingerprint = fingerprint;
this._updateUrl = (updateUrl === true);
if (!this.initialized) {
this._bindEvents();
@ -290,11 +298,18 @@ class PDFHistory {
}
this._updateInternalState(destination, newState.uid);
let newUrl;
if (this._updateUrl && destination && destination.hash) {
const baseUrl = document.location.href.split('#')[0];
if (!baseUrl.startsWith('file://')) { // Prevent errors in Firefox.
newUrl = `${baseUrl}#${destination.hash}`;
}
}
if (shouldReplace) {
window.history.replaceState(newState, '');
window.history.replaceState(newState, '', newUrl);
} else {
this._maxUid = this._uid;
window.history.pushState(newState, '');
window.history.pushState(newState, '', newUrl);
}
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME') &&