pdf_link_service.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 _pdf = require("../pdf");
  28. var _ui_utils = require("./ui_utils.js");
  29. class PDFLinkService {
  30. constructor({
  31. eventBus,
  32. externalLinkTarget = null,
  33. externalLinkRel = null,
  34. ignoreDestinationZoom = false
  35. } = {}) {
  36. this.eventBus = eventBus;
  37. this.externalLinkTarget = externalLinkTarget;
  38. this.externalLinkRel = externalLinkRel;
  39. this.externalLinkEnabled = true;
  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 (typeof destRef === "object" && destRef !== null) {
  77. pageNumber = this._cachedPageNumber(destRef);
  78. if (!pageNumber) {
  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. addLinkAttributes(link, url, newWindow = false) {
  147. (0, _pdf.addLinkAttributes)(link, {
  148. url,
  149. target: newWindow ? _pdf.LinkTarget.BLANK : this.externalLinkTarget,
  150. rel: this.externalLinkRel,
  151. enabled: this.externalLinkEnabled
  152. });
  153. }
  154. getDestinationHash(dest) {
  155. if (typeof dest === "string") {
  156. if (dest.length > 0) {
  157. return this.getAnchorUrl("#" + escape(dest));
  158. }
  159. } else if (Array.isArray(dest)) {
  160. const str = JSON.stringify(dest);
  161. if (str.length > 0) {
  162. return this.getAnchorUrl("#" + escape(str));
  163. }
  164. }
  165. return this.getAnchorUrl("");
  166. }
  167. getAnchorUrl(anchor) {
  168. return (this.baseUrl || "") + anchor;
  169. }
  170. setHash(hash) {
  171. if (!this.pdfDocument) {
  172. return;
  173. }
  174. let pageNumber, dest;
  175. if (hash.includes("=")) {
  176. const params = (0, _ui_utils.parseQueryString)(hash);
  177. if (params.has("search")) {
  178. this.eventBus.dispatch("findfromurlhash", {
  179. source: this,
  180. query: params.get("search").replace(/"/g, ""),
  181. phraseSearch: params.get("phrase") === "true"
  182. });
  183. }
  184. if (params.has("page")) {
  185. pageNumber = params.get("page") | 0 || 1;
  186. }
  187. if (params.has("zoom")) {
  188. const zoomArgs = params.get("zoom").split(",");
  189. const zoomArg = zoomArgs[0];
  190. const zoomArgNumber = parseFloat(zoomArg);
  191. if (!zoomArg.includes("Fit")) {
  192. dest = [null, {
  193. name: "XYZ"
  194. }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null, zoomArgs.length > 2 ? zoomArgs[2] | 0 : null, zoomArgNumber ? zoomArgNumber / 100 : zoomArg];
  195. } else {
  196. if (zoomArg === "Fit" || zoomArg === "FitB") {
  197. dest = [null, {
  198. name: zoomArg
  199. }];
  200. } else if (zoomArg === "FitH" || zoomArg === "FitBH" || zoomArg === "FitV" || zoomArg === "FitBV") {
  201. dest = [null, {
  202. name: zoomArg
  203. }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null];
  204. } else if (zoomArg === "FitR") {
  205. if (zoomArgs.length !== 5) {
  206. console.error('PDFLinkService.setHash: Not enough parameters for "FitR".');
  207. } else {
  208. dest = [null, {
  209. name: zoomArg
  210. }, zoomArgs[1] | 0, zoomArgs[2] | 0, zoomArgs[3] | 0, zoomArgs[4] | 0];
  211. }
  212. } else {
  213. console.error(`PDFLinkService.setHash: "${zoomArg}" is not ` + "a valid zoom value.");
  214. }
  215. }
  216. }
  217. if (dest) {
  218. this.pdfViewer.scrollPageIntoView({
  219. pageNumber: pageNumber || this.page,
  220. destArray: dest,
  221. allowNegativeOffset: true
  222. });
  223. } else if (pageNumber) {
  224. this.page = pageNumber;
  225. }
  226. if (params.has("pagemode")) {
  227. this.eventBus.dispatch("pagemode", {
  228. source: this,
  229. mode: params.get("pagemode")
  230. });
  231. }
  232. if (params.has("nameddest")) {
  233. this.goToDestination(params.get("nameddest"));
  234. }
  235. } else {
  236. dest = unescape(hash);
  237. try {
  238. dest = JSON.parse(dest);
  239. if (!Array.isArray(dest)) {
  240. dest = dest.toString();
  241. }
  242. } catch (ex) {}
  243. if (typeof dest === "string" || isValidExplicitDestination(dest)) {
  244. this.goToDestination(dest);
  245. return;
  246. }
  247. console.error(`PDFLinkService.setHash: "${unescape(hash)}" is not ` + "a valid destination.");
  248. }
  249. }
  250. executeNamedAction(action) {
  251. switch (action) {
  252. case "GoBack":
  253. this.pdfHistory?.back();
  254. break;
  255. case "GoForward":
  256. this.pdfHistory?.forward();
  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. if (!pageRef) {
  287. return null;
  288. }
  289. const refStr = pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
  290. return this._pagesRefCache?.[refStr] || null;
  291. }
  292. isPageVisible(pageNumber) {
  293. return this.pdfViewer.isPageVisible(pageNumber);
  294. }
  295. isPageCached(pageNumber) {
  296. return this.pdfViewer.isPageCached(pageNumber);
  297. }
  298. }
  299. exports.PDFLinkService = PDFLinkService;
  300. function isValidExplicitDestination(dest) {
  301. if (!Array.isArray(dest)) {
  302. return false;
  303. }
  304. const destLength = dest.length;
  305. if (destLength < 2) {
  306. return false;
  307. }
  308. const page = dest[0];
  309. if (!(typeof page === "object" && Number.isInteger(page.num) && Number.isInteger(page.gen)) && !(Number.isInteger(page) && page >= 0)) {
  310. return false;
  311. }
  312. const zoom = dest[1];
  313. if (!(typeof zoom === "object" && typeof zoom.name === "string")) {
  314. return false;
  315. }
  316. let allowNull = true;
  317. switch (zoom.name) {
  318. case "XYZ":
  319. if (destLength !== 5) {
  320. return false;
  321. }
  322. break;
  323. case "Fit":
  324. case "FitB":
  325. return destLength === 2;
  326. case "FitH":
  327. case "FitBH":
  328. case "FitV":
  329. case "FitBV":
  330. if (destLength !== 3) {
  331. return false;
  332. }
  333. break;
  334. case "FitR":
  335. if (destLength !== 6) {
  336. return false;
  337. }
  338. allowNull = false;
  339. break;
  340. default:
  341. return false;
  342. }
  343. for (let i = 2; i < destLength; i++) {
  344. const param = dest[i];
  345. if (!(typeof param === "number" || allowNull && param === null)) {
  346. return false;
  347. }
  348. }
  349. return true;
  350. }
  351. class SimpleLinkService {
  352. constructor() {
  353. this.externalLinkEnabled = true;
  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. addLinkAttributes(link, url, newWindow = false) {
  369. (0, _pdf.addLinkAttributes)(link, {
  370. url,
  371. enabled: this.externalLinkEnabled
  372. });
  373. }
  374. getDestinationHash(dest) {
  375. return "#";
  376. }
  377. getAnchorUrl(hash) {
  378. return "#";
  379. }
  380. setHash(hash) {}
  381. executeNamedAction(action) {}
  382. cachePageRef(pageNum, pageRef) {}
  383. isPageVisible(pageNumber) {
  384. return true;
  385. }
  386. isPageCached(pageNumber) {
  387. return true;
  388. }
  389. }
  390. exports.SimpleLinkService = SimpleLinkService;