UNPKG

7.5 kBJavaScriptView Raw
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
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.PDFThumbnailViewer = void 0;
28
29var _ui_utils = require("./ui_utils.js");
30
31var _pdf_thumbnail_view = require("./pdf_thumbnail_view.js");
32
33const THUMBNAIL_SCROLL_MARGIN = -19;
34const THUMBNAIL_SELECTED_CLASS = "selected";
35
36class PDFThumbnailViewer {
37 constructor({
38 container,
39 eventBus,
40 linkService,
41 renderingQueue,
42 l10n,
43 pageColors
44 }) {
45 this.container = container;
46 this.linkService = linkService;
47 this.renderingQueue = renderingQueue;
48 this.l10n = l10n;
49 this.pageColors = pageColors || null;
50
51 if (this.pageColors && !(CSS.supports("color", this.pageColors.background) && CSS.supports("color", this.pageColors.foreground))) {
52 if (this.pageColors.background || this.pageColors.foreground) {
53 console.warn("PDFThumbnailViewer: Ignoring `pageColors`-option, since the browser doesn't support the values used.");
54 }
55
56 this.pageColors = null;
57 }
58
59 this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdated.bind(this));
60
61 this._resetView();
62 }
63
64 _scrollUpdated() {
65 this.renderingQueue.renderHighestPriority();
66 }
67
68 getThumbnail(index) {
69 return this._thumbnails[index];
70 }
71
72 _getVisibleThumbs() {
73 return (0, _ui_utils.getVisibleElements)({
74 scrollEl: this.container,
75 views: this._thumbnails
76 });
77 }
78
79 scrollThumbnailIntoView(pageNumber) {
80 if (!this.pdfDocument) {
81 return;
82 }
83
84 const thumbnailView = this._thumbnails[pageNumber - 1];
85
86 if (!thumbnailView) {
87 console.error('scrollThumbnailIntoView: Invalid "pageNumber" parameter.');
88 return;
89 }
90
91 if (pageNumber !== this._currentPageNumber) {
92 const prevThumbnailView = this._thumbnails[this._currentPageNumber - 1];
93 prevThumbnailView.div.classList.remove(THUMBNAIL_SELECTED_CLASS);
94 thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
95 }
96
97 const {
98 first,
99 last,
100 views
101 } = this._getVisibleThumbs();
102
103 if (views.length > 0) {
104 let shouldScroll = false;
105
106 if (pageNumber <= first.id || pageNumber >= last.id) {
107 shouldScroll = true;
108 } else {
109 for (const {
110 id,
111 percent
112 } of views) {
113 if (id !== pageNumber) {
114 continue;
115 }
116
117 shouldScroll = percent < 100;
118 break;
119 }
120 }
121
122 if (shouldScroll) {
123 (0, _ui_utils.scrollIntoView)(thumbnailView.div, {
124 top: THUMBNAIL_SCROLL_MARGIN
125 });
126 }
127 }
128
129 this._currentPageNumber = pageNumber;
130 }
131
132 get pagesRotation() {
133 return this._pagesRotation;
134 }
135
136 set pagesRotation(rotation) {
137 if (!(0, _ui_utils.isValidRotation)(rotation)) {
138 throw new Error("Invalid thumbnails rotation angle.");
139 }
140
141 if (!this.pdfDocument) {
142 return;
143 }
144
145 if (this._pagesRotation === rotation) {
146 return;
147 }
148
149 this._pagesRotation = rotation;
150 const updateArgs = {
151 rotation
152 };
153
154 for (const thumbnail of this._thumbnails) {
155 thumbnail.update(updateArgs);
156 }
157 }
158
159 cleanup() {
160 for (const thumbnail of this._thumbnails) {
161 if (thumbnail.renderingState !== _ui_utils.RenderingStates.FINISHED) {
162 thumbnail.reset();
163 }
164 }
165
166 _pdf_thumbnail_view.TempImageFactory.destroyCanvas();
167 }
168
169 _resetView() {
170 this._thumbnails = [];
171 this._currentPageNumber = 1;
172 this._pageLabels = null;
173 this._pagesRotation = 0;
174 this.container.textContent = "";
175 }
176
177 setDocument(pdfDocument) {
178 if (this.pdfDocument) {
179 this._cancelRendering();
180
181 this._resetView();
182 }
183
184 this.pdfDocument = pdfDocument;
185
186 if (!pdfDocument) {
187 return;
188 }
189
190 const firstPagePromise = pdfDocument.getPage(1);
191 const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
192 firstPagePromise.then(firstPdfPage => {
193 const pagesCount = pdfDocument.numPages;
194 const viewport = firstPdfPage.getViewport({
195 scale: 1
196 });
197
198 for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
199 const thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
200 container: this.container,
201 id: pageNum,
202 defaultViewport: viewport.clone(),
203 optionalContentConfigPromise,
204 linkService: this.linkService,
205 renderingQueue: this.renderingQueue,
206 l10n: this.l10n,
207 pageColors: this.pageColors
208 });
209
210 this._thumbnails.push(thumbnail);
211 }
212
213 const firstThumbnailView = this._thumbnails[0];
214
215 if (firstThumbnailView) {
216 firstThumbnailView.setPdfPage(firstPdfPage);
217 }
218
219 const thumbnailView = this._thumbnails[this._currentPageNumber - 1];
220 thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
221 }).catch(reason => {
222 console.error("Unable to initialize thumbnail viewer", reason);
223 });
224 }
225
226 _cancelRendering() {
227 for (const thumbnail of this._thumbnails) {
228 thumbnail.cancelRendering();
229 }
230 }
231
232 setPageLabels(labels) {
233 if (!this.pdfDocument) {
234 return;
235 }
236
237 if (!labels) {
238 this._pageLabels = null;
239 } else if (!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)) {
240 this._pageLabels = null;
241 console.error("PDFThumbnailViewer_setPageLabels: Invalid page labels.");
242 } else {
243 this._pageLabels = labels;
244 }
245
246 for (let i = 0, ii = this._thumbnails.length; i < ii; i++) {
247 this._thumbnails[i].setPageLabel(this._pageLabels?.[i] ?? null);
248 }
249 }
250
251 async #ensurePdfPageLoaded(thumbView) {
252 if (thumbView.pdfPage) {
253 return thumbView.pdfPage;
254 }
255
256 try {
257 const pdfPage = await this.pdfDocument.getPage(thumbView.id);
258
259 if (!thumbView.pdfPage) {
260 thumbView.setPdfPage(pdfPage);
261 }
262
263 return pdfPage;
264 } catch (reason) {
265 console.error("Unable to get page for thumb view", reason);
266 return null;
267 }
268 }
269
270 #getScrollAhead(visible) {
271 if (visible.first?.id === 1) {
272 return true;
273 } else if (visible.last?.id === this._thumbnails.length) {
274 return false;
275 }
276
277 return this.scroll.down;
278 }
279
280 forceRendering() {
281 const visibleThumbs = this._getVisibleThumbs();
282
283 const scrollAhead = this.#getScrollAhead(visibleThumbs);
284 const thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this._thumbnails, scrollAhead);
285
286 if (thumbView) {
287 this.#ensurePdfPageLoaded(thumbView).then(() => {
288 this.renderingQueue.renderView(thumbView);
289 });
290 return true;
291 }
292
293 return false;
294 }
295
296}
297
298exports.PDFThumbnailViewer = PDFThumbnailViewer;
\No newline at end of file