pdf_thumbnail_viewer.js 7.6 KB

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