pdf_thumbnail_viewer.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. }) {
  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)({
  57. scrollEl: this.container,
  58. views: this._thumbnails
  59. });
  60. }
  61. scrollThumbnailIntoView(pageNumber) {
  62. if (!this.pdfDocument) {
  63. return;
  64. }
  65. const thumbnailView = this._thumbnails[pageNumber - 1];
  66. if (!thumbnailView) {
  67. console.error('scrollThumbnailIntoView: Invalid "pageNumber" parameter.');
  68. return;
  69. }
  70. if (pageNumber !== this._currentPageNumber) {
  71. const prevThumbnailView = this._thumbnails[this._currentPageNumber - 1];
  72. prevThumbnailView.div.classList.remove(THUMBNAIL_SELECTED_CLASS);
  73. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  74. }
  75. const {
  76. first,
  77. last,
  78. views
  79. } = this._getVisibleThumbs();
  80. if (views.length > 0) {
  81. let shouldScroll = false;
  82. if (pageNumber <= first.id || pageNumber >= last.id) {
  83. shouldScroll = true;
  84. } else {
  85. for (const {
  86. id,
  87. percent
  88. } of views) {
  89. if (id !== pageNumber) {
  90. continue;
  91. }
  92. shouldScroll = percent < 100;
  93. break;
  94. }
  95. }
  96. if (shouldScroll) {
  97. (0, _ui_utils.scrollIntoView)(thumbnailView.div, {
  98. top: THUMBNAIL_SCROLL_MARGIN
  99. });
  100. }
  101. }
  102. this._currentPageNumber = pageNumber;
  103. }
  104. get pagesRotation() {
  105. return this._pagesRotation;
  106. }
  107. set pagesRotation(rotation) {
  108. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  109. throw new Error("Invalid thumbnails rotation angle.");
  110. }
  111. if (!this.pdfDocument) {
  112. return;
  113. }
  114. if (this._pagesRotation === rotation) {
  115. return;
  116. }
  117. this._pagesRotation = rotation;
  118. const updateArgs = {
  119. rotation
  120. };
  121. for (const thumbnail of this._thumbnails) {
  122. thumbnail.update(updateArgs);
  123. }
  124. }
  125. cleanup() {
  126. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
  127. if (this._thumbnails[i] && this._thumbnails[i].renderingState !== _ui_utils.RenderingStates.FINISHED) {
  128. this._thumbnails[i].reset();
  129. }
  130. }
  131. _pdf_thumbnail_view.TempImageFactory.destroyCanvas();
  132. }
  133. _resetView() {
  134. this._thumbnails = [];
  135. this._currentPageNumber = 1;
  136. this._pageLabels = null;
  137. this._pagesRotation = 0;
  138. this._optionalContentConfigPromise = null;
  139. this._setImageDisabled = false;
  140. this.container.textContent = "";
  141. }
  142. setDocument(pdfDocument) {
  143. if (this.pdfDocument) {
  144. this._cancelRendering();
  145. this._resetView();
  146. }
  147. this.pdfDocument = pdfDocument;
  148. if (!pdfDocument) {
  149. return;
  150. }
  151. const firstPagePromise = pdfDocument.getPage(1);
  152. const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
  153. firstPagePromise.then(firstPdfPage => {
  154. this._optionalContentConfigPromise = optionalContentConfigPromise;
  155. const pagesCount = pdfDocument.numPages;
  156. const viewport = firstPdfPage.getViewport({
  157. scale: 1
  158. });
  159. const checkSetImageDisabled = () => {
  160. return this._setImageDisabled;
  161. };
  162. for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  163. const thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
  164. container: this.container,
  165. id: pageNum,
  166. defaultViewport: viewport.clone(),
  167. optionalContentConfigPromise,
  168. linkService: this.linkService,
  169. renderingQueue: this.renderingQueue,
  170. checkSetImageDisabled,
  171. l10n: this.l10n
  172. });
  173. this._thumbnails.push(thumbnail);
  174. }
  175. const firstThumbnailView = this._thumbnails[0];
  176. if (firstThumbnailView) {
  177. firstThumbnailView.setPdfPage(firstPdfPage);
  178. }
  179. const thumbnailView = this._thumbnails[this._currentPageNumber - 1];
  180. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  181. }).catch(reason => {
  182. console.error("Unable to initialize thumbnail viewer", reason);
  183. });
  184. }
  185. _cancelRendering() {
  186. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
  187. if (this._thumbnails[i]) {
  188. this._thumbnails[i].cancelRendering();
  189. }
  190. }
  191. }
  192. setPageLabels(labels) {
  193. if (!this.pdfDocument) {
  194. return;
  195. }
  196. if (!labels) {
  197. this._pageLabels = null;
  198. } else if (!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)) {
  199. this._pageLabels = null;
  200. console.error("PDFThumbnailViewer_setPageLabels: Invalid page labels.");
  201. } else {
  202. this._pageLabels = labels;
  203. }
  204. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
  205. this._thumbnails[i].setPageLabel(this._pageLabels?.[i] ?? null);
  206. }
  207. }
  208. async #ensurePdfPageLoaded(thumbView) {
  209. if (thumbView.pdfPage) {
  210. return thumbView.pdfPage;
  211. }
  212. try {
  213. const pdfPage = await this.pdfDocument.getPage(thumbView.id);
  214. if (!thumbView.pdfPage) {
  215. thumbView.setPdfPage(pdfPage);
  216. }
  217. return pdfPage;
  218. } catch (reason) {
  219. console.error("Unable to get page for thumb view", reason);
  220. return null;
  221. }
  222. }
  223. #getScrollAhead(visible) {
  224. if (visible.first?.id === 1) {
  225. return true;
  226. } else if (visible.last?.id === this._thumbnails.length) {
  227. return false;
  228. }
  229. return this.scroll.down;
  230. }
  231. forceRendering() {
  232. const visibleThumbs = this._getVisibleThumbs();
  233. const scrollAhead = this.#getScrollAhead(visibleThumbs);
  234. const thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this._thumbnails, scrollAhead);
  235. if (thumbView) {
  236. this.#ensurePdfPageLoaded(thumbView).then(() => {
  237. this.renderingQueue.renderView(thumbView);
  238. });
  239. return true;
  240. }
  241. return false;
  242. }
  243. }
  244. exports.PDFThumbnailViewer = PDFThumbnailViewer;