pdf_link_service.js 11 KB

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