Generic build of PDF.js library.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

498 lines
14 KiB

/**
* @licstart The following is the entire license notice for the
* Javascript code in this page
*
* Copyright 2019 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @licend The above is the entire license notice for the
* Javascript code in this page
*/
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SimpleLinkService = exports.PDFLinkService = void 0;
var _ui_utils = require("./ui_utils");
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var PDFLinkService =
/*#__PURE__*/
function () {
function PDFLinkService() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
eventBus = _ref.eventBus,
_ref$externalLinkTarg = _ref.externalLinkTarget,
externalLinkTarget = _ref$externalLinkTarg === void 0 ? null : _ref$externalLinkTarg,
_ref$externalLinkRel = _ref.externalLinkRel,
externalLinkRel = _ref$externalLinkRel === void 0 ? null : _ref$externalLinkRel;
_classCallCheck(this, PDFLinkService);
this.eventBus = eventBus || (0, _ui_utils.getGlobalEventBus)();
this.externalLinkTarget = externalLinkTarget;
this.externalLinkRel = externalLinkRel;
this.baseUrl = null;
this.pdfDocument = null;
this.pdfViewer = null;
this.pdfHistory = null;
this._pagesRefCache = null;
}
_createClass(PDFLinkService, [{
key: "setDocument",
value: function setDocument(pdfDocument) {
var baseUrl = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
this.baseUrl = baseUrl;
this.pdfDocument = pdfDocument;
this._pagesRefCache = Object.create(null);
}
}, {
key: "setViewer",
value: function setViewer(pdfViewer) {
this.pdfViewer = pdfViewer;
}
}, {
key: "setHistory",
value: function setHistory(pdfHistory) {
this.pdfHistory = pdfHistory;
}
}, {
key: "navigateTo",
value: function navigateTo(dest) {
var _this = this;
var goToDestination = function goToDestination(_ref2) {
var namedDest = _ref2.namedDest,
explicitDest = _ref2.explicitDest;
var destRef = explicitDest[0],
pageNumber;
if (destRef instanceof Object) {
pageNumber = _this._cachedPageNumber(destRef);
if (pageNumber === null) {
_this.pdfDocument.getPageIndex(destRef).then(function (pageIndex) {
_this.cachePageRef(pageIndex + 1, destRef);
goToDestination({
namedDest: namedDest,
explicitDest: explicitDest
});
})["catch"](function () {
console.error("PDFLinkService.navigateTo: \"".concat(destRef, "\" is not ") + "a valid page reference, for dest=\"".concat(dest, "\"."));
});
return;
}
} else if (Number.isInteger(destRef)) {
pageNumber = destRef + 1;
} else {
console.error("PDFLinkService.navigateTo: \"".concat(destRef, "\" is not ") + "a valid destination reference, for dest=\"".concat(dest, "\"."));
return;
}
if (!pageNumber || pageNumber < 1 || pageNumber > _this.pagesCount) {
console.error("PDFLinkService.navigateTo: \"".concat(pageNumber, "\" is not ") + "a valid page number, for dest=\"".concat(dest, "\"."));
return;
}
if (_this.pdfHistory) {
_this.pdfHistory.pushCurrentPosition();
_this.pdfHistory.push({
namedDest: namedDest,
explicitDest: explicitDest,
pageNumber: pageNumber
});
}
_this.pdfViewer.scrollPageIntoView({
pageNumber: pageNumber,
destArray: explicitDest
});
};
new Promise(function (resolve, reject) {
if (typeof dest === 'string') {
_this.pdfDocument.getDestination(dest).then(function (destArray) {
resolve({
namedDest: dest,
explicitDest: destArray
});
});
return;
}
resolve({
namedDest: '',
explicitDest: dest
});
}).then(function (data) {
if (!Array.isArray(data.explicitDest)) {
console.error("PDFLinkService.navigateTo: \"".concat(data.explicitDest, "\" is") + " not a valid destination array, for dest=\"".concat(dest, "\"."));
return;
}
goToDestination(data);
});
}
}, {
key: "getDestinationHash",
value: function getDestinationHash(dest) {
if (typeof dest === 'string') {
return this.getAnchorUrl('#' + escape(dest));
}
if (Array.isArray(dest)) {
var str = JSON.stringify(dest);
return this.getAnchorUrl('#' + escape(str));
}
return this.getAnchorUrl('');
}
}, {
key: "getAnchorUrl",
value: function getAnchorUrl(anchor) {
return (this.baseUrl || '') + anchor;
}
}, {
key: "setHash",
value: function setHash(hash) {
var pageNumber, dest;
if (hash.includes('=')) {
var params = (0, _ui_utils.parseQueryString)(hash);
if ('search' in params) {
this.eventBus.dispatch('findfromurlhash', {
source: this,
query: params['search'].replace(/"/g, ''),
phraseSearch: params['phrase'] === 'true'
});
}
if ('nameddest' in params) {
this.navigateTo(params.nameddest);
return;
}
if ('page' in params) {
pageNumber = params.page | 0 || 1;
}
if ('zoom' in params) {
var zoomArgs = params.zoom.split(',');
var zoomArg = zoomArgs[0];
var zoomArgNumber = parseFloat(zoomArg);
if (!zoomArg.includes('Fit')) {
dest = [null, {
name: 'XYZ'
}, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null, zoomArgs.length > 2 ? zoomArgs[2] | 0 : null, zoomArgNumber ? zoomArgNumber / 100 : zoomArg];
} else {
if (zoomArg === 'Fit' || zoomArg === 'FitB') {
dest = [null, {
name: zoomArg
}];
} else if (zoomArg === 'FitH' || zoomArg === 'FitBH' || zoomArg === 'FitV' || zoomArg === 'FitBV') {
dest = [null, {
name: zoomArg
}, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null];
} else if (zoomArg === 'FitR') {
if (zoomArgs.length !== 5) {
console.error('PDFLinkService.setHash: Not enough parameters for "FitR".');
} else {
dest = [null, {
name: zoomArg
}, zoomArgs[1] | 0, zoomArgs[2] | 0, zoomArgs[3] | 0, zoomArgs[4] | 0];
}
} else {
console.error("PDFLinkService.setHash: \"".concat(zoomArg, "\" is not ") + 'a valid zoom value.');
}
}
}
if (dest) {
this.pdfViewer.scrollPageIntoView({
pageNumber: pageNumber || this.page,
destArray: dest,
allowNegativeOffset: true
});
} else if (pageNumber) {
this.page = pageNumber;
}
if ('pagemode' in params) {
this.eventBus.dispatch('pagemode', {
source: this,
mode: params.pagemode
});
}
} else {
dest = unescape(hash);
try {
dest = JSON.parse(dest);
if (!Array.isArray(dest)) {
dest = dest.toString();
}
} catch (ex) {}
if (typeof dest === 'string' || isValidExplicitDestination(dest)) {
this.navigateTo(dest);
return;
}
console.error("PDFLinkService.setHash: \"".concat(unescape(hash), "\" is not ") + 'a valid destination.');
}
}
}, {
key: "executeNamedAction",
value: function executeNamedAction(action) {
switch (action) {
case 'GoBack':
if (this.pdfHistory) {
this.pdfHistory.back();
}
break;
case 'GoForward':
if (this.pdfHistory) {
this.pdfHistory.forward();
}
break;
case 'NextPage':
if (this.page < this.pagesCount) {
this.page++;
}
break;
case 'PrevPage':
if (this.page > 1) {
this.page--;
}
break;
case 'LastPage':
this.page = this.pagesCount;
break;
case 'FirstPage':
this.page = 1;
break;
default:
break;
}
this.eventBus.dispatch('namedaction', {
source: this,
action: action
});
}
}, {
key: "cachePageRef",
value: function cachePageRef(pageNum, pageRef) {
if (!pageRef) {
return;
}
var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
this._pagesRefCache[refStr] = pageNum;
}
}, {
key: "_cachedPageNumber",
value: function _cachedPageNumber(pageRef) {
var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
return this._pagesRefCache && this._pagesRefCache[refStr] || null;
}
}, {
key: "isPageVisible",
value: function isPageVisible(pageNumber) {
return this.pdfViewer.isPageVisible(pageNumber);
}
}, {
key: "pagesCount",
get: function get() {
return this.pdfDocument ? this.pdfDocument.numPages : 0;
}
}, {
key: "page",
get: function get() {
return this.pdfViewer.currentPageNumber;
},
set: function set(value) {
this.pdfViewer.currentPageNumber = value;
}
}, {
key: "rotation",
get: function get() {
return this.pdfViewer.pagesRotation;
},
set: function set(value) {
this.pdfViewer.pagesRotation = value;
}
}]);
return PDFLinkService;
}();
exports.PDFLinkService = PDFLinkService;
function isValidExplicitDestination(dest) {
if (!Array.isArray(dest)) {
return false;
}
var destLength = dest.length,
allowNull = true;
if (destLength < 2) {
return false;
}
var page = dest[0];
if (!(_typeof(page) === 'object' && Number.isInteger(page.num) && Number.isInteger(page.gen)) && !(Number.isInteger(page) && page >= 0)) {
return false;
}
var zoom = dest[1];
if (!(_typeof(zoom) === 'object' && typeof zoom.name === 'string')) {
return false;
}
switch (zoom.name) {
case 'XYZ':
if (destLength !== 5) {
return false;
}
break;
case 'Fit':
case 'FitB':
return destLength === 2;
case 'FitH':
case 'FitBH':
case 'FitV':
case 'FitBV':
if (destLength !== 3) {
return false;
}
break;
case 'FitR':
if (destLength !== 6) {
return false;
}
allowNull = false;
break;
default:
return false;
}
for (var i = 2; i < destLength; i++) {
var param = dest[i];
if (!(typeof param === 'number' || allowNull && param === null)) {
return false;
}
}
return true;
}
var SimpleLinkService =
/*#__PURE__*/
function () {
function SimpleLinkService() {
_classCallCheck(this, SimpleLinkService);
this.externalLinkTarget = null;
this.externalLinkRel = null;
}
_createClass(SimpleLinkService, [{
key: "navigateTo",
value: function navigateTo(dest) {}
}, {
key: "getDestinationHash",
value: function getDestinationHash(dest) {
return '#';
}
}, {
key: "getAnchorUrl",
value: function getAnchorUrl(hash) {
return '#';
}
}, {
key: "setHash",
value: function setHash(hash) {}
}, {
key: "executeNamedAction",
value: function executeNamedAction(action) {}
}, {
key: "cachePageRef",
value: function cachePageRef(pageNum, pageRef) {}
}, {
key: "isPageVisible",
value: function isPageVisible(pageNumber) {
return true;
}
}, {
key: "pagesCount",
get: function get() {
return 0;
}
}, {
key: "page",
get: function get() {
return 0;
},
set: function set(value) {}
}, {
key: "rotation",
get: function get() {
return 0;
},
set: function set(value) {}
}]);
return SimpleLinkService;
}();
exports.SimpleLinkService = SimpleLinkService;