pdf_link_service.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.SimpleLinkService = exports.PDFLinkService = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. class PDFLinkService {
  29. constructor({
  30. eventBus,
  31. externalLinkTarget = null,
  32. externalLinkRel = null,
  33. externalLinkEnabled = true,
  34. ignoreDestinationZoom = false
  35. } = {}) {
  36. this.eventBus = eventBus;
  37. this.externalLinkTarget = externalLinkTarget;
  38. this.externalLinkRel = externalLinkRel;
  39. this.externalLinkEnabled = externalLinkEnabled;
  40. this._ignoreDestinationZoom = ignoreDestinationZoom;
  41. this.baseUrl = null;
  42. this.pdfDocument = null;
  43. this.pdfViewer = null;
  44. this.pdfHistory = null;
  45. this._pagesRefCache = null;
  46. }
  47. setDocument(pdfDocument, baseUrl = null) {
  48. this.baseUrl = baseUrl;
  49. this.pdfDocument = pdfDocument;
  50. this._pagesRefCache = Object.create(null);
  51. }
  52. setViewer(pdfViewer) {
  53. this.pdfViewer = pdfViewer;
  54. }
  55. setHistory(pdfHistory) {
  56. this.pdfHistory = pdfHistory;
  57. }
  58. get pagesCount() {
  59. return this.pdfDocument ? this.pdfDocument.numPages : 0;
  60. }
  61. get page() {
  62. return this.pdfViewer.currentPageNumber;
  63. }
  64. set page(value) {
  65. this.pdfViewer.currentPageNumber = value;
  66. }
  67. get rotation() {
  68. return this.pdfViewer.pagesRotation;
  69. }
  70. set rotation(value) {
  71. this.pdfViewer.pagesRotation = value;
  72. }
  73. navigateTo(dest) {
  74. const goToDestination = ({
  75. namedDest,
  76. explicitDest
  77. }) => {
  78. const destRef = explicitDest[0];
  79. let pageNumber;
  80. if (destRef instanceof Object) {
  81. pageNumber = this._cachedPageNumber(destRef);
  82. if (pageNumber === null) {
  83. this.pdfDocument.getPageIndex(destRef).then(pageIndex => {
  84. this.cachePageRef(pageIndex + 1, destRef);
  85. goToDestination({
  86. namedDest,
  87. explicitDest
  88. });
  89. }).catch(() => {
  90. console.error(`PDFLinkService.navigateTo: "${destRef}" is not ` + `a valid page reference, for dest="${dest}".`);
  91. });
  92. return;
  93. }
  94. } else if (Number.isInteger(destRef)) {
  95. pageNumber = destRef + 1;
  96. } else {
  97. console.error(`PDFLinkService.navigateTo: "${destRef}" is not ` + `a valid destination reference, for dest="${dest}".`);
  98. return;
  99. }
  100. if (!pageNumber || pageNumber < 1 || pageNumber > this.pagesCount) {
  101. console.error(`PDFLinkService.navigateTo: "${pageNumber}" is not ` + `a valid page number, for dest="${dest}".`);
  102. return;
  103. }
  104. if (this.pdfHistory) {
  105. this.pdfHistory.pushCurrentPosition();
  106. this.pdfHistory.push({
  107. namedDest,
  108. explicitDest,
  109. pageNumber
  110. });
  111. }
  112. this.pdfViewer.scrollPageIntoView({
  113. pageNumber,
  114. destArray: explicitDest,
  115. ignoreDestinationZoom: this._ignoreDestinationZoom
  116. });
  117. };
  118. new Promise((resolve, reject) => {
  119. if (typeof dest === "string") {
  120. this.pdfDocument.getDestination(dest).then(destArray => {
  121. resolve({
  122. namedDest: dest,
  123. explicitDest: destArray
  124. });
  125. });
  126. return;
  127. }
  128. resolve({
  129. namedDest: "",
  130. explicitDest: dest
  131. });
  132. }).then(data => {
  133. if (!Array.isArray(data.explicitDest)) {
  134. console.error(`PDFLinkService.navigateTo: "${data.explicitDest}" is` + ` not a valid destination array, for dest="${dest}".`);
  135. return;
  136. }
  137. goToDestination(data);
  138. });
  139. }
  140. getDestinationHash(dest) {
  141. if (typeof dest === "string") {
  142. return this.getAnchorUrl("#" + escape(dest));
  143. }
  144. if (Array.isArray(dest)) {
  145. const str = JSON.stringify(dest);
  146. return this.getAnchorUrl("#" + escape(str));
  147. }
  148. return this.getAnchorUrl("");
  149. }
  150. getAnchorUrl(anchor) {
  151. return (this.baseUrl || "") + anchor;
  152. }
  153. setHash(hash) {
  154. let pageNumber, dest;
  155. if (hash.includes("=")) {
  156. const params = (0, _ui_utils.parseQueryString)(hash);
  157. if ("search" in params) {
  158. this.eventBus.dispatch("findfromurlhash", {
  159. source: this,
  160. query: params.search.replace(/"/g, ""),
  161. phraseSearch: params.phrase === "true"
  162. });
  163. }
  164. if ("page" in params) {
  165. pageNumber = params.page | 0 || 1;
  166. }
  167. if ("zoom" in params) {
  168. const zoomArgs = params.zoom.split(",");
  169. const zoomArg = zoomArgs[0];
  170. const zoomArgNumber = parseFloat(zoomArg);
  171. if (!zoomArg.includes("Fit")) {
  172. dest = [null, {
  173. name: "XYZ"
  174. }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null, zoomArgs.length > 2 ? zoomArgs[2] | 0 : null, zoomArgNumber ? zoomArgNumber / 100 : zoomArg];
  175. } else {
  176. if (zoomArg === "Fit" || zoomArg === "FitB") {
  177. dest = [null, {
  178. name: zoomArg
  179. }];
  180. } else if (zoomArg === "FitH" || zoomArg === "FitBH" || zoomArg === "FitV" || zoomArg === "FitBV") {
  181. dest = [null, {
  182. name: zoomArg
  183. }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null];
  184. } else if (zoomArg === "FitR") {
  185. if (zoomArgs.length !== 5) {
  186. console.error('PDFLinkService.setHash: Not enough parameters for "FitR".');
  187. } else {
  188. dest = [null, {
  189. name: zoomArg
  190. }, zoomArgs[1] | 0, zoomArgs[2] | 0, zoomArgs[3] | 0, zoomArgs[4] | 0];
  191. }
  192. } else {
  193. console.error(`PDFLinkService.setHash: "${zoomArg}" is not ` + "a valid zoom value.");
  194. }
  195. }
  196. }
  197. if (dest) {
  198. this.pdfViewer.scrollPageIntoView({
  199. pageNumber: pageNumber || this.page,
  200. destArray: dest,
  201. allowNegativeOffset: true
  202. });
  203. } else if (pageNumber) {
  204. this.page = pageNumber;
  205. }
  206. if ("pagemode" in params) {
  207. this.eventBus.dispatch("pagemode", {
  208. source: this,
  209. mode: params.pagemode
  210. });
  211. }
  212. if ("nameddest" in params) {
  213. this.navigateTo(params.nameddest);
  214. }
  215. } else {
  216. dest = unescape(hash);
  217. try {
  218. dest = JSON.parse(dest);
  219. if (!Array.isArray(dest)) {
  220. dest = dest.toString();
  221. }
  222. } catch (ex) {}
  223. if (typeof dest === "string" || isValidExplicitDestination(dest)) {
  224. this.navigateTo(dest);
  225. return;
  226. }
  227. console.error(`PDFLinkService.setHash: "${unescape(hash)}" is not ` + "a valid destination.");
  228. }
  229. }
  230. executeNamedAction(action) {
  231. switch (action) {
  232. case "GoBack":
  233. if (this.pdfHistory) {
  234. this.pdfHistory.back();
  235. }
  236. break;
  237. case "GoForward":
  238. if (this.pdfHistory) {
  239. this.pdfHistory.forward();
  240. }
  241. break;
  242. case "NextPage":
  243. if (this.page < this.pagesCount) {
  244. this.page++;
  245. }
  246. break;
  247. case "PrevPage":
  248. if (this.page > 1) {
  249. this.page--;
  250. }
  251. break;
  252. case "LastPage":
  253. this.page = this.pagesCount;
  254. break;
  255. case "FirstPage":
  256. this.page = 1;
  257. break;
  258. default:
  259. break;
  260. }
  261. this.eventBus.dispatch("namedaction", {
  262. source: this,
  263. action
  264. });
  265. }
  266. cachePageRef(pageNum, pageRef) {
  267. if (!pageRef) {
  268. return;
  269. }
  270. const refStr = pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
  271. this._pagesRefCache[refStr] = pageNum;
  272. }
  273. _cachedPageNumber(pageRef) {
  274. const refStr = pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
  275. return this._pagesRefCache && this._pagesRefCache[refStr] || null;
  276. }
  277. isPageVisible(pageNumber) {
  278. return this.pdfViewer.isPageVisible(pageNumber);
  279. }
  280. }
  281. exports.PDFLinkService = PDFLinkService;
  282. function isValidExplicitDestination(dest) {
  283. if (!Array.isArray(dest)) {
  284. return false;
  285. }
  286. const destLength = dest.length;
  287. if (destLength < 2) {
  288. return false;
  289. }
  290. const page = dest[0];
  291. if (!(typeof page === "object" && Number.isInteger(page.num) && Number.isInteger(page.gen)) && !(Number.isInteger(page) && page >= 0)) {
  292. return false;
  293. }
  294. const zoom = dest[1];
  295. if (!(typeof zoom === "object" && typeof zoom.name === "string")) {
  296. return false;
  297. }
  298. let allowNull = true;
  299. switch (zoom.name) {
  300. case "XYZ":
  301. if (destLength !== 5) {
  302. return false;
  303. }
  304. break;
  305. case "Fit":
  306. case "FitB":
  307. return destLength === 2;
  308. case "FitH":
  309. case "FitBH":
  310. case "FitV":
  311. case "FitBV":
  312. if (destLength !== 3) {
  313. return false;
  314. }
  315. break;
  316. case "FitR":
  317. if (destLength !== 6) {
  318. return false;
  319. }
  320. allowNull = false;
  321. break;
  322. default:
  323. return false;
  324. }
  325. for (let i = 2; i < destLength; i++) {
  326. const param = dest[i];
  327. if (!(typeof param === "number" || allowNull && param === null)) {
  328. return false;
  329. }
  330. }
  331. return true;
  332. }
  333. class SimpleLinkService {
  334. constructor() {
  335. this.externalLinkTarget = null;
  336. this.externalLinkRel = null;
  337. this.externalLinkEnabled = true;
  338. this._ignoreDestinationZoom = false;
  339. }
  340. get pagesCount() {
  341. return 0;
  342. }
  343. get page() {
  344. return 0;
  345. }
  346. set page(value) {}
  347. get rotation() {
  348. return 0;
  349. }
  350. set rotation(value) {}
  351. navigateTo(dest) {}
  352. getDestinationHash(dest) {
  353. return "#";
  354. }
  355. getAnchorUrl(hash) {
  356. return "#";
  357. }
  358. setHash(hash) {}
  359. executeNamedAction(action) {}
  360. cachePageRef(pageNum, pageRef) {}
  361. isPageVisible(pageNumber) {
  362. return true;
  363. }
  364. }
  365. exports.SimpleLinkService = SimpleLinkService;