pdf_link_service.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. _goToDestinationHelper(rawDest, namedDest = null, explicitDest) {
  74. const destRef = explicitDest[0];
  75. let pageNumber;
  76. if (destRef instanceof Object) {
  77. pageNumber = this._cachedPageNumber(destRef);
  78. if (pageNumber === null) {
  79. this.pdfDocument.getPageIndex(destRef).then(pageIndex => {
  80. this.cachePageRef(pageIndex + 1, destRef);
  81. this._goToDestinationHelper(rawDest, namedDest, explicitDest);
  82. }).catch(() => {
  83. console.error(`PDFLinkService._goToDestinationHelper: "${destRef}" is not ` + `a valid page reference, for dest="${rawDest}".`);
  84. });
  85. return;
  86. }
  87. } else if (Number.isInteger(destRef)) {
  88. pageNumber = destRef + 1;
  89. } else {
  90. console.error(`PDFLinkService._goToDestinationHelper: "${destRef}" is not ` + `a valid destination reference, for dest="${rawDest}".`);
  91. return;
  92. }
  93. if (!pageNumber || pageNumber < 1 || pageNumber > this.pagesCount) {
  94. console.error(`PDFLinkService._goToDestinationHelper: "${pageNumber}" is not ` + `a valid page number, for dest="${rawDest}".`);
  95. return;
  96. }
  97. if (this.pdfHistory) {
  98. this.pdfHistory.pushCurrentPosition();
  99. this.pdfHistory.push({
  100. namedDest,
  101. explicitDest,
  102. pageNumber
  103. });
  104. }
  105. this.pdfViewer.scrollPageIntoView({
  106. pageNumber,
  107. destArray: explicitDest,
  108. ignoreDestinationZoom: this._ignoreDestinationZoom
  109. });
  110. }
  111. async goToDestination(dest) {
  112. if (!this.pdfDocument) {
  113. return;
  114. }
  115. let namedDest, explicitDest;
  116. if (typeof dest === "string") {
  117. namedDest = dest;
  118. explicitDest = await this.pdfDocument.getDestination(dest);
  119. } else {
  120. namedDest = null;
  121. explicitDest = await dest;
  122. }
  123. if (!Array.isArray(explicitDest)) {
  124. console.error(`PDFLinkService.goToDestination: "${explicitDest}" is not ` + `a valid destination array, for dest="${dest}".`);
  125. return;
  126. }
  127. this._goToDestinationHelper(dest, namedDest, explicitDest);
  128. }
  129. goToPage(val) {
  130. if (!this.pdfDocument) {
  131. return;
  132. }
  133. const pageNumber = typeof val === "string" && this.pdfViewer.pageLabelToPageNumber(val) || val | 0;
  134. if (!(Number.isInteger(pageNumber) && pageNumber > 0 && pageNumber <= this.pagesCount)) {
  135. console.error(`PDFLinkService.goToPage: "${val}" is not a valid page.`);
  136. return;
  137. }
  138. if (this.pdfHistory) {
  139. this.pdfHistory.pushCurrentPosition();
  140. this.pdfHistory.pushPage(pageNumber);
  141. }
  142. this.pdfViewer.scrollPageIntoView({
  143. pageNumber
  144. });
  145. }
  146. getDestinationHash(dest) {
  147. if (typeof dest === "string") {
  148. if (dest.length > 0) {
  149. return this.getAnchorUrl("#" + escape(dest));
  150. }
  151. } else if (Array.isArray(dest)) {
  152. const str = JSON.stringify(dest);
  153. if (str.length > 0) {
  154. return this.getAnchorUrl("#" + escape(str));
  155. }
  156. }
  157. return this.getAnchorUrl("");
  158. }
  159. getAnchorUrl(anchor) {
  160. return (this.baseUrl || "") + anchor;
  161. }
  162. setHash(hash) {
  163. if (!this.pdfDocument) {
  164. return;
  165. }
  166. let pageNumber, dest;
  167. if (hash.includes("=")) {
  168. const params = (0, _ui_utils.parseQueryString)(hash);
  169. if ("search" in params) {
  170. this.eventBus.dispatch("findfromurlhash", {
  171. source: this,
  172. query: params.search.replace(/"/g, ""),
  173. phraseSearch: params.phrase === "true"
  174. });
  175. }
  176. if ("page" in params) {
  177. pageNumber = params.page | 0 || 1;
  178. }
  179. if ("zoom" in params) {
  180. const zoomArgs = params.zoom.split(",");
  181. const zoomArg = zoomArgs[0];
  182. const zoomArgNumber = parseFloat(zoomArg);
  183. if (!zoomArg.includes("Fit")) {
  184. dest = [null, {
  185. name: "XYZ"
  186. }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null, zoomArgs.length > 2 ? zoomArgs[2] | 0 : null, zoomArgNumber ? zoomArgNumber / 100 : zoomArg];
  187. } else {
  188. if (zoomArg === "Fit" || zoomArg === "FitB") {
  189. dest = [null, {
  190. name: zoomArg
  191. }];
  192. } else if (zoomArg === "FitH" || zoomArg === "FitBH" || zoomArg === "FitV" || zoomArg === "FitBV") {
  193. dest = [null, {
  194. name: zoomArg
  195. }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null];
  196. } else if (zoomArg === "FitR") {
  197. if (zoomArgs.length !== 5) {
  198. console.error('PDFLinkService.setHash: Not enough parameters for "FitR".');
  199. } else {
  200. dest = [null, {
  201. name: zoomArg
  202. }, zoomArgs[1] | 0, zoomArgs[2] | 0, zoomArgs[3] | 0, zoomArgs[4] | 0];
  203. }
  204. } else {
  205. console.error(`PDFLinkService.setHash: "${zoomArg}" is not ` + "a valid zoom value.");
  206. }
  207. }
  208. }
  209. if (dest) {
  210. this.pdfViewer.scrollPageIntoView({
  211. pageNumber: pageNumber || this.page,
  212. destArray: dest,
  213. allowNegativeOffset: true
  214. });
  215. } else if (pageNumber) {
  216. this.page = pageNumber;
  217. }
  218. if ("pagemode" in params) {
  219. this.eventBus.dispatch("pagemode", {
  220. source: this,
  221. mode: params.pagemode
  222. });
  223. }
  224. if ("nameddest" in params) {
  225. this.goToDestination(params.nameddest);
  226. }
  227. } else {
  228. dest = unescape(hash);
  229. try {
  230. dest = JSON.parse(dest);
  231. if (!Array.isArray(dest)) {
  232. dest = dest.toString();
  233. }
  234. } catch (ex) {}
  235. if (typeof dest === "string" || isValidExplicitDestination(dest)) {
  236. this.goToDestination(dest);
  237. return;
  238. }
  239. console.error(`PDFLinkService.setHash: "${unescape(hash)}" is not ` + "a valid destination.");
  240. }
  241. }
  242. executeNamedAction(action) {
  243. switch (action) {
  244. case "GoBack":
  245. if (this.pdfHistory) {
  246. this.pdfHistory.back();
  247. }
  248. break;
  249. case "GoForward":
  250. if (this.pdfHistory) {
  251. this.pdfHistory.forward();
  252. }
  253. break;
  254. case "NextPage":
  255. this.pdfViewer.nextPage();
  256. break;
  257. case "PrevPage":
  258. this.pdfViewer.previousPage();
  259. break;
  260. case "LastPage":
  261. this.page = this.pagesCount;
  262. break;
  263. case "FirstPage":
  264. this.page = 1;
  265. break;
  266. default:
  267. break;
  268. }
  269. this.eventBus.dispatch("namedaction", {
  270. source: this,
  271. action
  272. });
  273. }
  274. cachePageRef(pageNum, pageRef) {
  275. if (!pageRef) {
  276. return;
  277. }
  278. const refStr = pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
  279. this._pagesRefCache[refStr] = pageNum;
  280. }
  281. _cachedPageNumber(pageRef) {
  282. const refStr = pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
  283. return this._pagesRefCache?.[refStr] || null;
  284. }
  285. isPageVisible(pageNumber) {
  286. return this.pdfViewer.isPageVisible(pageNumber);
  287. }
  288. isPageCached(pageNumber) {
  289. return this.pdfViewer.isPageCached(pageNumber);
  290. }
  291. }
  292. exports.PDFLinkService = PDFLinkService;
  293. function isValidExplicitDestination(dest) {
  294. if (!Array.isArray(dest)) {
  295. return false;
  296. }
  297. const destLength = dest.length;
  298. if (destLength < 2) {
  299. return false;
  300. }
  301. const page = dest[0];
  302. if (!(typeof page === "object" && Number.isInteger(page.num) && Number.isInteger(page.gen)) && !(Number.isInteger(page) && page >= 0)) {
  303. return false;
  304. }
  305. const zoom = dest[1];
  306. if (!(typeof zoom === "object" && typeof zoom.name === "string")) {
  307. return false;
  308. }
  309. let allowNull = true;
  310. switch (zoom.name) {
  311. case "XYZ":
  312. if (destLength !== 5) {
  313. return false;
  314. }
  315. break;
  316. case "Fit":
  317. case "FitB":
  318. return destLength === 2;
  319. case "FitH":
  320. case "FitBH":
  321. case "FitV":
  322. case "FitBV":
  323. if (destLength !== 3) {
  324. return false;
  325. }
  326. break;
  327. case "FitR":
  328. if (destLength !== 6) {
  329. return false;
  330. }
  331. allowNull = false;
  332. break;
  333. default:
  334. return false;
  335. }
  336. for (let i = 2; i < destLength; i++) {
  337. const param = dest[i];
  338. if (!(typeof param === "number" || allowNull && param === null)) {
  339. return false;
  340. }
  341. }
  342. return true;
  343. }
  344. class SimpleLinkService {
  345. constructor() {
  346. this.externalLinkTarget = null;
  347. this.externalLinkRel = null;
  348. this.externalLinkEnabled = true;
  349. this._ignoreDestinationZoom = false;
  350. }
  351. get pagesCount() {
  352. return 0;
  353. }
  354. get page() {
  355. return 0;
  356. }
  357. set page(value) {}
  358. get rotation() {
  359. return 0;
  360. }
  361. set rotation(value) {}
  362. async goToDestination(dest) {}
  363. goToPage(val) {}
  364. getDestinationHash(dest) {
  365. return "#";
  366. }
  367. getAnchorUrl(hash) {
  368. return "#";
  369. }
  370. setHash(hash) {}
  371. executeNamedAction(action) {}
  372. cachePageRef(pageNum, pageRef) {}
  373. isPageVisible(pageNumber) {
  374. return true;
  375. }
  376. isPageCached(pageNumber) {
  377. return true;
  378. }
  379. }
  380. exports.SimpleLinkService = SimpleLinkService;