pdf_thumbnail_viewer.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.PDFThumbnailViewer = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. var _pdf_thumbnail_view = require("./pdf_thumbnail_view.js");
  29. const THUMBNAIL_SCROLL_MARGIN = -19;
  30. const THUMBNAIL_SELECTED_CLASS = "selected";
  31. class PDFThumbnailViewer {
  32. constructor({
  33. container,
  34. eventBus,
  35. linkService,
  36. renderingQueue,
  37. l10n = _ui_utils.NullL10n
  38. }) {
  39. this.container = container;
  40. this.linkService = linkService;
  41. this.renderingQueue = renderingQueue;
  42. this.l10n = l10n;
  43. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdated.bind(this));
  44. this._resetView();
  45. eventBus._on("optionalcontentconfigchanged", () => {
  46. this._setImageDisabled = true;
  47. });
  48. }
  49. _scrollUpdated() {
  50. this.renderingQueue.renderHighestPriority();
  51. }
  52. getThumbnail(index) {
  53. return this._thumbnails[index];
  54. }
  55. _getVisibleThumbs() {
  56. return (0, _ui_utils.getVisibleElements)(this.container, this._thumbnails);
  57. }
  58. scrollThumbnailIntoView(pageNumber) {
  59. if (!this.pdfDocument) {
  60. return;
  61. }
  62. const thumbnailView = this._thumbnails[pageNumber - 1];
  63. if (!thumbnailView) {
  64. console.error('scrollThumbnailIntoView: Invalid "pageNumber" parameter.');
  65. return;
  66. }
  67. if (pageNumber !== this._currentPageNumber) {
  68. const prevThumbnailView = this._thumbnails[this._currentPageNumber - 1];
  69. prevThumbnailView.div.classList.remove(THUMBNAIL_SELECTED_CLASS);
  70. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  71. }
  72. const visibleThumbs = this._getVisibleThumbs();
  73. const numVisibleThumbs = visibleThumbs.views.length;
  74. if (numVisibleThumbs > 0) {
  75. const first = visibleThumbs.first.id;
  76. const last = numVisibleThumbs > 1 ? visibleThumbs.last.id : first;
  77. let shouldScroll = false;
  78. if (pageNumber <= first || pageNumber >= last) {
  79. shouldScroll = true;
  80. } else {
  81. visibleThumbs.views.some(function (view) {
  82. if (view.id !== pageNumber) {
  83. return false;
  84. }
  85. shouldScroll = view.percent < 100;
  86. return true;
  87. });
  88. }
  89. if (shouldScroll) {
  90. (0, _ui_utils.scrollIntoView)(thumbnailView.div, {
  91. top: THUMBNAIL_SCROLL_MARGIN
  92. });
  93. }
  94. }
  95. this._currentPageNumber = pageNumber;
  96. }
  97. get pagesRotation() {
  98. return this._pagesRotation;
  99. }
  100. set pagesRotation(rotation) {
  101. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  102. throw new Error("Invalid thumbnails rotation angle.");
  103. }
  104. if (!this.pdfDocument) {
  105. return;
  106. }
  107. if (this._pagesRotation === rotation) {
  108. return;
  109. }
  110. this._pagesRotation = rotation;
  111. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
  112. this._thumbnails[i].update(rotation);
  113. }
  114. }
  115. cleanup() {
  116. _pdf_thumbnail_view.PDFThumbnailView.cleanup();
  117. }
  118. _resetView() {
  119. this._thumbnails = [];
  120. this._currentPageNumber = 1;
  121. this._pageLabels = null;
  122. this._pagesRotation = 0;
  123. this._optionalContentConfigPromise = null;
  124. this._pagesRequests = new WeakMap();
  125. this._setImageDisabled = false;
  126. this.container.textContent = "";
  127. }
  128. setDocument(pdfDocument) {
  129. if (this.pdfDocument) {
  130. this._cancelRendering();
  131. this._resetView();
  132. }
  133. this.pdfDocument = pdfDocument;
  134. if (!pdfDocument) {
  135. return;
  136. }
  137. const firstPagePromise = pdfDocument.getPage(1);
  138. const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
  139. firstPagePromise.then(firstPdfPage => {
  140. this._optionalContentConfigPromise = optionalContentConfigPromise;
  141. const pagesCount = pdfDocument.numPages;
  142. const viewport = firstPdfPage.getViewport({
  143. scale: 1
  144. });
  145. const checkSetImageDisabled = () => {
  146. return this._setImageDisabled;
  147. };
  148. for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  149. const thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
  150. container: this.container,
  151. id: pageNum,
  152. defaultViewport: viewport.clone(),
  153. optionalContentConfigPromise,
  154. linkService: this.linkService,
  155. renderingQueue: this.renderingQueue,
  156. checkSetImageDisabled,
  157. disableCanvasToImageConversion: false,
  158. l10n: this.l10n
  159. });
  160. this._thumbnails.push(thumbnail);
  161. }
  162. const firstThumbnailView = this._thumbnails[0];
  163. if (firstThumbnailView) {
  164. firstThumbnailView.setPdfPage(firstPdfPage);
  165. }
  166. const thumbnailView = this._thumbnails[this._currentPageNumber - 1];
  167. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  168. }).catch(reason => {
  169. console.error("Unable to initialize thumbnail viewer", reason);
  170. });
  171. }
  172. _cancelRendering() {
  173. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
  174. if (this._thumbnails[i]) {
  175. this._thumbnails[i].cancelRendering();
  176. }
  177. }
  178. }
  179. setPageLabels(labels) {
  180. if (!this.pdfDocument) {
  181. return;
  182. }
  183. if (!labels) {
  184. this._pageLabels = null;
  185. } else if (!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)) {
  186. this._pageLabels = null;
  187. console.error("PDFThumbnailViewer_setPageLabels: Invalid page labels.");
  188. } else {
  189. this._pageLabels = labels;
  190. }
  191. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
  192. const label = this._pageLabels && this._pageLabels[i];
  193. this._thumbnails[i].setPageLabel(label);
  194. }
  195. }
  196. _ensurePdfPageLoaded(thumbView) {
  197. if (thumbView.pdfPage) {
  198. return Promise.resolve(thumbView.pdfPage);
  199. }
  200. if (this._pagesRequests.has(thumbView)) {
  201. return this._pagesRequests.get(thumbView);
  202. }
  203. const promise = this.pdfDocument.getPage(thumbView.id).then(pdfPage => {
  204. if (!thumbView.pdfPage) {
  205. thumbView.setPdfPage(pdfPage);
  206. }
  207. this._pagesRequests.delete(thumbView);
  208. return pdfPage;
  209. }).catch(reason => {
  210. console.error("Unable to get page for thumb view", reason);
  211. this._pagesRequests.delete(thumbView);
  212. });
  213. this._pagesRequests.set(thumbView, promise);
  214. return promise;
  215. }
  216. forceRendering() {
  217. const visibleThumbs = this._getVisibleThumbs();
  218. const thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this._thumbnails, this.scroll.down);
  219. if (thumbView) {
  220. this._ensurePdfPageLoaded(thumbView).then(() => {
  221. this.renderingQueue.renderView(thumbView);
  222. });
  223. return true;
  224. }
  225. return false;
  226. }
  227. }
  228. exports.PDFThumbnailViewer = PDFThumbnailViewer;