pdf_thumbnail_viewer.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. const updateArgs = {
  116. rotation
  117. };
  118. for (const thumbnail of this._thumbnails) {
  119. thumbnail.update(updateArgs);
  120. }
  121. }
  122. cleanup() {
  123. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
  124. if (this._thumbnails[i] && this._thumbnails[i].renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  125. this._thumbnails[i].reset();
  126. }
  127. }
  128. _pdf_thumbnail_view.TempImageFactory.destroyCanvas();
  129. }
  130. _resetView() {
  131. this._thumbnails = [];
  132. this._currentPageNumber = 1;
  133. this._pageLabels = null;
  134. this._pagesRotation = 0;
  135. this._optionalContentConfigPromise = null;
  136. this._pagesRequests = new WeakMap();
  137. this._setImageDisabled = false;
  138. this.container.textContent = "";
  139. }
  140. setDocument(pdfDocument) {
  141. if (this.pdfDocument) {
  142. this._cancelRendering();
  143. this._resetView();
  144. }
  145. this.pdfDocument = pdfDocument;
  146. if (!pdfDocument) {
  147. return;
  148. }
  149. const firstPagePromise = pdfDocument.getPage(1);
  150. const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
  151. firstPagePromise.then(firstPdfPage => {
  152. this._optionalContentConfigPromise = optionalContentConfigPromise;
  153. const pagesCount = pdfDocument.numPages;
  154. const viewport = firstPdfPage.getViewport({
  155. scale: 1
  156. });
  157. const checkSetImageDisabled = () => {
  158. return this._setImageDisabled;
  159. };
  160. for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  161. const thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
  162. container: this.container,
  163. id: pageNum,
  164. defaultViewport: viewport.clone(),
  165. optionalContentConfigPromise,
  166. linkService: this.linkService,
  167. renderingQueue: this.renderingQueue,
  168. checkSetImageDisabled,
  169. l10n: this.l10n
  170. });
  171. this._thumbnails.push(thumbnail);
  172. }
  173. const firstThumbnailView = this._thumbnails[0];
  174. if (firstThumbnailView) {
  175. firstThumbnailView.setPdfPage(firstPdfPage);
  176. }
  177. const thumbnailView = this._thumbnails[this._currentPageNumber - 1];
  178. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  179. }).catch(reason => {
  180. console.error("Unable to initialize thumbnail viewer", reason);
  181. });
  182. }
  183. _cancelRendering() {
  184. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
  185. if (this._thumbnails[i]) {
  186. this._thumbnails[i].cancelRendering();
  187. }
  188. }
  189. }
  190. setPageLabels(labels) {
  191. if (!this.pdfDocument) {
  192. return;
  193. }
  194. if (!labels) {
  195. this._pageLabels = null;
  196. } else if (!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)) {
  197. this._pageLabels = null;
  198. console.error("PDFThumbnailViewer_setPageLabels: Invalid page labels.");
  199. } else {
  200. this._pageLabels = labels;
  201. }
  202. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
  203. this._thumbnails[i].setPageLabel(this._pageLabels?.[i] ?? null);
  204. }
  205. }
  206. _ensurePdfPageLoaded(thumbView) {
  207. if (thumbView.pdfPage) {
  208. return Promise.resolve(thumbView.pdfPage);
  209. }
  210. if (this._pagesRequests.has(thumbView)) {
  211. return this._pagesRequests.get(thumbView);
  212. }
  213. const promise = this.pdfDocument.getPage(thumbView.id).then(pdfPage => {
  214. if (!thumbView.pdfPage) {
  215. thumbView.setPdfPage(pdfPage);
  216. }
  217. this._pagesRequests.delete(thumbView);
  218. return pdfPage;
  219. }).catch(reason => {
  220. console.error("Unable to get page for thumb view", reason);
  221. this._pagesRequests.delete(thumbView);
  222. });
  223. this._pagesRequests.set(thumbView, promise);
  224. return promise;
  225. }
  226. forceRendering() {
  227. const visibleThumbs = this._getVisibleThumbs();
  228. const thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this._thumbnails, this.scroll.down);
  229. if (thumbView) {
  230. this._ensurePdfPageLoaded(thumbView).then(() => {
  231. this.renderingQueue.renderView(thumbView);
  232. });
  233. return true;
  234. }
  235. return false;
  236. }
  237. }
  238. exports.PDFThumbnailViewer = PDFThumbnailViewer;