2
0

pdf_link_service.js 11 KB

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