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", "type": "boolean",
"default": false "default": false
}, },
"historyUpdateUrl": {
"type": "boolean",
"default": false
},
"enablePrintAutoRotate": { "enablePrintAutoRotate": {
"title": "Automatically rotate printed pages", "title": "Automatically rotate printed pages",
"description": "When enabled, pages whose orientation differ from the first page are rotated when printed.", "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. // Provides support for String.prototype.includes in legacy browsers.
// Support: IE, Chrome<41 // Support: IE, Chrome<41
(function checkStringIncludes() { (function checkStringIncludes() {
@ -223,6 +241,24 @@ const hasDOM = typeof window === 'object' && typeof document === 'object';
} // End of !PDFJSDev.test('CHROME') } // 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. // Provides support for Object.values in legacy browsers.
// Support: IE, Chrome<54 // Support: IE, Chrome<54
(function checkObjectValues() { (function checkObjectValues() {

View File

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

View File

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

View File

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

View File

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

View File

@ -30,6 +30,14 @@ const UPDATE_VIEWAREA_TIMEOUT = 1000; // milliseconds
* @property {EventBus} eventBus - The application event bus. * @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 * @typedef {Object} PushParameters
* @property {string} namedDest - (optional) The named destination. If absent, * @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 * Initialize the history for the PDF document, using either the current
* browser history entry or the document hash, whichever is present. * browser history entry or the document hash, whichever is present.
* @param {string} fingerprint - The PDF document's unique fingerprint. * @param {InitializeParameters} params
* @param {boolean} resetHistory - (optional) Reset the browsing history.
*/ */
initialize(fingerprint, resetHistory = false) { initialize({ fingerprint, resetHistory = false, updateUrl = false, }) {
if (!fingerprint || typeof fingerprint !== 'string') { if (!fingerprint || typeof fingerprint !== 'string') {
console.error( console.error(
'PDFHistory.initialize: The "fingerprint" must be a non-empty string.'); 'PDFHistory.initialize: The "fingerprint" must be a non-empty string.');
@ -93,6 +100,7 @@ class PDFHistory {
} }
let reInitialized = this.initialized && this.fingerprint !== fingerprint; let reInitialized = this.initialized && this.fingerprint !== fingerprint;
this.fingerprint = fingerprint; this.fingerprint = fingerprint;
this._updateUrl = (updateUrl === true);
if (!this.initialized) { if (!this.initialized) {
this._bindEvents(); this._bindEvents();
@ -290,11 +298,18 @@ class PDFHistory {
} }
this._updateInternalState(destination, newState.uid); 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) { if (shouldReplace) {
window.history.replaceState(newState, ''); window.history.replaceState(newState, '', newUrl);
} else { } else {
this._maxUid = this._uid; this._maxUid = this._uid;
window.history.pushState(newState, ''); window.history.pushState(newState, '', newUrl);
} }
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME') && if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME') &&