pdf_thumbnail_viewer.js 6.8 KB

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