pdf_thumbnail_viewer.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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,
  38. pageColors
  39. }) {
  40. this.container = container;
  41. this.linkService = linkService;
  42. this.renderingQueue = renderingQueue;
  43. this.l10n = l10n;
  44. this.pageColors = pageColors || null;
  45. if (this.pageColors && !(CSS.supports("color", this.pageColors.background) && CSS.supports("color", this.pageColors.foreground))) {
  46. if (this.pageColors.background || this.pageColors.foreground) {
  47. console.warn("PDFThumbnailViewer: Ignoring `pageColors`-option, since the browser doesn't support the values used.");
  48. }
  49. this.pageColors = null;
  50. }
  51. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdated.bind(this));
  52. this._resetView();
  53. }
  54. _scrollUpdated() {
  55. this.renderingQueue.renderHighestPriority();
  56. }
  57. getThumbnail(index) {
  58. return this._thumbnails[index];
  59. }
  60. _getVisibleThumbs() {
  61. return (0, _ui_utils.getVisibleElements)({
  62. scrollEl: this.container,
  63. views: this._thumbnails
  64. });
  65. }
  66. scrollThumbnailIntoView(pageNumber) {
  67. if (!this.pdfDocument) {
  68. return;
  69. }
  70. const thumbnailView = this._thumbnails[pageNumber - 1];
  71. if (!thumbnailView) {
  72. console.error('scrollThumbnailIntoView: Invalid "pageNumber" parameter.');
  73. return;
  74. }
  75. if (pageNumber !== this._currentPageNumber) {
  76. const prevThumbnailView = this._thumbnails[this._currentPageNumber - 1];
  77. prevThumbnailView.div.classList.remove(THUMBNAIL_SELECTED_CLASS);
  78. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  79. }
  80. const {
  81. first,
  82. last,
  83. views
  84. } = this._getVisibleThumbs();
  85. if (views.length > 0) {
  86. let shouldScroll = false;
  87. if (pageNumber <= first.id || pageNumber >= last.id) {
  88. shouldScroll = true;
  89. } else {
  90. for (const {
  91. id,
  92. percent
  93. } of views) {
  94. if (id !== pageNumber) {
  95. continue;
  96. }
  97. shouldScroll = percent < 100;
  98. break;
  99. }
  100. }
  101. if (shouldScroll) {
  102. (0, _ui_utils.scrollIntoView)(thumbnailView.div, {
  103. top: THUMBNAIL_SCROLL_MARGIN
  104. });
  105. }
  106. }
  107. this._currentPageNumber = pageNumber;
  108. }
  109. get pagesRotation() {
  110. return this._pagesRotation;
  111. }
  112. set pagesRotation(rotation) {
  113. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  114. throw new Error("Invalid thumbnails rotation angle.");
  115. }
  116. if (!this.pdfDocument) {
  117. return;
  118. }
  119. if (this._pagesRotation === rotation) {
  120. return;
  121. }
  122. this._pagesRotation = rotation;
  123. const updateArgs = {
  124. rotation
  125. };
  126. for (const thumbnail of this._thumbnails) {
  127. thumbnail.update(updateArgs);
  128. }
  129. }
  130. cleanup() {
  131. for (const thumbnail of this._thumbnails) {
  132. if (thumbnail.renderingState !== _ui_utils.RenderingStates.FINISHED) {
  133. thumbnail.reset();
  134. }
  135. }
  136. _pdf_thumbnail_view.TempImageFactory.destroyCanvas();
  137. }
  138. _resetView() {
  139. this._thumbnails = [];
  140. this._currentPageNumber = 1;
  141. this._pageLabels = null;
  142. this._pagesRotation = 0;
  143. this.container.textContent = "";
  144. }
  145. setDocument(pdfDocument) {
  146. if (this.pdfDocument) {
  147. this._cancelRendering();
  148. this._resetView();
  149. }
  150. this.pdfDocument = pdfDocument;
  151. if (!pdfDocument) {
  152. return;
  153. }
  154. const firstPagePromise = pdfDocument.getPage(1);
  155. const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
  156. firstPagePromise.then(firstPdfPage => {
  157. const pagesCount = pdfDocument.numPages;
  158. const viewport = firstPdfPage.getViewport({
  159. scale: 1
  160. });
  161. for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  162. const thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
  163. container: this.container,
  164. id: pageNum,
  165. defaultViewport: viewport.clone(),
  166. optionalContentConfigPromise,
  167. linkService: this.linkService,
  168. renderingQueue: this.renderingQueue,
  169. l10n: this.l10n,
  170. pageColors: this.pageColors
  171. });
  172. this._thumbnails.push(thumbnail);
  173. }
  174. this._thumbnails[0]?.setPdfPage(firstPdfPage);
  175. const thumbnailView = this._thumbnails[this._currentPageNumber - 1];
  176. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  177. }).catch(reason => {
  178. console.error("Unable to initialize thumbnail viewer", reason);
  179. });
  180. }
  181. _cancelRendering() {
  182. for (const thumbnail of this._thumbnails) {
  183. thumbnail.cancelRendering();
  184. }
  185. }
  186. setPageLabels(labels) {
  187. if (!this.pdfDocument) {
  188. return;
  189. }
  190. if (!labels) {
  191. this._pageLabels = null;
  192. } else if (!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)) {
  193. this._pageLabels = null;
  194. console.error("PDFThumbnailViewer_setPageLabels: Invalid page labels.");
  195. } else {
  196. this._pageLabels = labels;
  197. }
  198. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
  199. this._thumbnails[i].setPageLabel(this._pageLabels?.[i] ?? null);
  200. }
  201. }
  202. async #ensurePdfPageLoaded(thumbView) {
  203. if (thumbView.pdfPage) {
  204. return thumbView.pdfPage;
  205. }
  206. try {
  207. const pdfPage = await this.pdfDocument.getPage(thumbView.id);
  208. if (!thumbView.pdfPage) {
  209. thumbView.setPdfPage(pdfPage);
  210. }
  211. return pdfPage;
  212. } catch (reason) {
  213. console.error("Unable to get page for thumb view", reason);
  214. return null;
  215. }
  216. }
  217. #getScrollAhead(visible) {
  218. if (visible.first?.id === 1) {
  219. return true;
  220. } else if (visible.last?.id === this._thumbnails.length) {
  221. return false;
  222. }
  223. return this.scroll.down;
  224. }
  225. forceRendering() {
  226. const visibleThumbs = this._getVisibleThumbs();
  227. const scrollAhead = this.#getScrollAhead(visibleThumbs);
  228. const thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this._thumbnails, scrollAhead);
  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;