pdf_thumbnail_viewer.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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");
  28. var _pdf_thumbnail_view = require("./pdf_thumbnail_view");
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  31. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  32. var THUMBNAIL_SCROLL_MARGIN = -19;
  33. var THUMBNAIL_SELECTED_CLASS = 'selected';
  34. var PDFThumbnailViewer =
  35. /*#__PURE__*/
  36. function () {
  37. function PDFThumbnailViewer(_ref) {
  38. var container = _ref.container,
  39. linkService = _ref.linkService,
  40. renderingQueue = _ref.renderingQueue,
  41. _ref$l10n = _ref.l10n,
  42. l10n = _ref$l10n === void 0 ? _ui_utils.NullL10n : _ref$l10n;
  43. _classCallCheck(this, PDFThumbnailViewer);
  44. this.container = container;
  45. this.linkService = linkService;
  46. this.renderingQueue = renderingQueue;
  47. this.l10n = l10n;
  48. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdated.bind(this));
  49. this._resetView();
  50. }
  51. _createClass(PDFThumbnailViewer, [{
  52. key: "_scrollUpdated",
  53. value: function _scrollUpdated() {
  54. this.renderingQueue.renderHighestPriority();
  55. }
  56. }, {
  57. key: "getThumbnail",
  58. value: function getThumbnail(index) {
  59. return this._thumbnails[index];
  60. }
  61. }, {
  62. key: "_getVisibleThumbs",
  63. value: function _getVisibleThumbs() {
  64. return (0, _ui_utils.getVisibleElements)(this.container, this._thumbnails);
  65. }
  66. }, {
  67. key: "scrollThumbnailIntoView",
  68. value: function scrollThumbnailIntoView(pageNumber) {
  69. if (!this.pdfDocument) {
  70. return;
  71. }
  72. var thumbnailView = this._thumbnails[pageNumber - 1];
  73. if (!thumbnailView) {
  74. console.error('scrollThumbnailIntoView: Invalid "pageNumber" parameter.');
  75. return;
  76. }
  77. if (pageNumber !== this._currentPageNumber) {
  78. var prevThumbnailView = this._thumbnails[this._currentPageNumber - 1];
  79. prevThumbnailView.div.classList.remove(THUMBNAIL_SELECTED_CLASS);
  80. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  81. }
  82. var visibleThumbs = this._getVisibleThumbs();
  83. var numVisibleThumbs = visibleThumbs.views.length;
  84. if (numVisibleThumbs > 0) {
  85. var first = visibleThumbs.first.id;
  86. var last = numVisibleThumbs > 1 ? visibleThumbs.last.id : first;
  87. var shouldScroll = false;
  88. if (pageNumber <= first || pageNumber >= last) {
  89. shouldScroll = true;
  90. } else {
  91. visibleThumbs.views.some(function (view) {
  92. if (view.id !== pageNumber) {
  93. return false;
  94. }
  95. shouldScroll = view.percent < 100;
  96. return true;
  97. });
  98. }
  99. if (shouldScroll) {
  100. (0, _ui_utils.scrollIntoView)(thumbnailView.div, {
  101. top: THUMBNAIL_SCROLL_MARGIN
  102. });
  103. }
  104. }
  105. this._currentPageNumber = pageNumber;
  106. }
  107. }, {
  108. key: "cleanup",
  109. value: function cleanup() {
  110. _pdf_thumbnail_view.PDFThumbnailView.cleanup();
  111. }
  112. }, {
  113. key: "_resetView",
  114. value: function _resetView() {
  115. this._thumbnails = [];
  116. this._currentPageNumber = 1;
  117. this._pageLabels = null;
  118. this._pagesRotation = 0;
  119. this._pagesRequests = [];
  120. this.container.textContent = '';
  121. }
  122. }, {
  123. key: "setDocument",
  124. value: function setDocument(pdfDocument) {
  125. var _this = this;
  126. if (this.pdfDocument) {
  127. this._cancelRendering();
  128. this._resetView();
  129. }
  130. this.pdfDocument = pdfDocument;
  131. if (!pdfDocument) {
  132. return;
  133. }
  134. pdfDocument.getPage(1).then(function (firstPage) {
  135. var pagesCount = pdfDocument.numPages;
  136. var viewport = firstPage.getViewport({
  137. scale: 1
  138. });
  139. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  140. var thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
  141. container: _this.container,
  142. id: pageNum,
  143. defaultViewport: viewport.clone(),
  144. linkService: _this.linkService,
  145. renderingQueue: _this.renderingQueue,
  146. disableCanvasToImageConversion: false,
  147. l10n: _this.l10n
  148. });
  149. _this._thumbnails.push(thumbnail);
  150. }
  151. var thumbnailView = _this._thumbnails[_this._currentPageNumber - 1];
  152. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  153. })["catch"](function (reason) {
  154. console.error('Unable to initialize thumbnail viewer', reason);
  155. });
  156. }
  157. }, {
  158. key: "_cancelRendering",
  159. value: function _cancelRendering() {
  160. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  161. if (this._thumbnails[i]) {
  162. this._thumbnails[i].cancelRendering();
  163. }
  164. }
  165. }
  166. }, {
  167. key: "setPageLabels",
  168. value: function setPageLabels(labels) {
  169. if (!this.pdfDocument) {
  170. return;
  171. }
  172. if (!labels) {
  173. this._pageLabels = null;
  174. } else if (!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)) {
  175. this._pageLabels = null;
  176. console.error('PDFThumbnailViewer_setPageLabels: Invalid page labels.');
  177. } else {
  178. this._pageLabels = labels;
  179. }
  180. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  181. var label = this._pageLabels && this._pageLabels[i];
  182. this._thumbnails[i].setPageLabel(label);
  183. }
  184. }
  185. }, {
  186. key: "_ensurePdfPageLoaded",
  187. value: function _ensurePdfPageLoaded(thumbView) {
  188. var _this2 = this;
  189. if (thumbView.pdfPage) {
  190. return Promise.resolve(thumbView.pdfPage);
  191. }
  192. var pageNumber = thumbView.id;
  193. if (this._pagesRequests[pageNumber]) {
  194. return this._pagesRequests[pageNumber];
  195. }
  196. var promise = this.pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  197. thumbView.setPdfPage(pdfPage);
  198. _this2._pagesRequests[pageNumber] = null;
  199. return pdfPage;
  200. })["catch"](function (reason) {
  201. console.error('Unable to get page for thumb view', reason);
  202. _this2._pagesRequests[pageNumber] = null;
  203. });
  204. this._pagesRequests[pageNumber] = promise;
  205. return promise;
  206. }
  207. }, {
  208. key: "forceRendering",
  209. value: function forceRendering() {
  210. var _this3 = this;
  211. var visibleThumbs = this._getVisibleThumbs();
  212. var thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this._thumbnails, this.scroll.down);
  213. if (thumbView) {
  214. this._ensurePdfPageLoaded(thumbView).then(function () {
  215. _this3.renderingQueue.renderView(thumbView);
  216. });
  217. return true;
  218. }
  219. return false;
  220. }
  221. }, {
  222. key: "pagesRotation",
  223. get: function get() {
  224. return this._pagesRotation;
  225. },
  226. set: function set(rotation) {
  227. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  228. throw new Error('Invalid thumbnails rotation angle.');
  229. }
  230. if (!this.pdfDocument) {
  231. return;
  232. }
  233. if (this._pagesRotation === rotation) {
  234. return;
  235. }
  236. this._pagesRotation = rotation;
  237. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  238. this._thumbnails[i].update(rotation);
  239. }
  240. }
  241. }]);
  242. return PDFThumbnailViewer;
  243. }();
  244. exports.PDFThumbnailViewer = PDFThumbnailViewer;