@vaadin-component-factory/vcf-pdf-viewer 4.0.2 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,199 +1,199 @@
1
- import './pdf.js';
2
- import { R as RenderingCancelledException } from './display_utils.js';
3
- import './util.js';
4
- import './message_handler.js';
5
-
6
- /* Copyright 2012 Mozilla Foundation
7
- *
8
- * Licensed under the Apache License, Version 2.0 (the "License");
9
- * you may not use this file except in compliance with the License.
10
- * You may obtain a copy of the License at
11
- *
12
- * http://www.apache.org/licenses/LICENSE-2.0
13
- *
14
- * Unless required by applicable law or agreed to in writing, software
15
- * distributed under the License is distributed on an "AS IS" BASIS,
16
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
- * See the License for the specific language governing permissions and
18
- * limitations under the License.
19
- */
20
- const CLEANUP_TIMEOUT = 30000;
21
- const RenderingStates = {
22
- INITIAL: 0,
23
- RUNNING: 1,
24
- PAUSED: 2,
25
- FINISHED: 3
26
- };
27
- /**
28
- * Controls rendering of the views for pages and thumbnails.
29
- */
30
-
31
- class PDFRenderingQueue {
32
- constructor() {
33
- this.pdfViewer = null;
34
- this.pdfThumbnailViewer = null;
35
- this.onIdle = null;
36
- this.highestPriorityPage = null;
37
- this.idleTimeout = null;
38
- this.printing = false;
39
- this.isThumbnailViewEnabled = false;
40
- }
41
- /**
42
- * @param {PDFViewer} pdfViewer
43
- */
44
-
45
-
46
- setViewer(pdfViewer) {
47
- this.pdfViewer = pdfViewer;
48
- }
49
- /**
50
- * @param {PDFThumbnailViewer} pdfThumbnailViewer
51
- */
52
-
53
-
54
- setThumbnailViewer(pdfThumbnailViewer) {
55
- this.pdfThumbnailViewer = pdfThumbnailViewer;
56
- }
57
- /**
58
- * @param {IRenderableView} view
59
- * @returns {boolean}
60
- */
61
-
62
-
63
- isHighestPriority(view) {
64
- return this.highestPriorityPage === view.renderingId;
65
- }
66
- /**
67
- * @param {Object} currentlyVisiblePages
68
- */
69
-
70
-
71
- renderHighestPriority(currentlyVisiblePages) {
72
- if (this.idleTimeout) {
73
- clearTimeout(this.idleTimeout);
74
- this.idleTimeout = null;
75
- } // Pages have a higher priority than thumbnails, so check them first.
76
-
77
-
78
- if (this.pdfViewer.forceRendering(currentlyVisiblePages)) {
79
- return;
80
- } // No pages needed rendering, so check thumbnails.
81
-
82
-
83
- if (this.pdfThumbnailViewer && this.isThumbnailViewEnabled) {
84
- if (this.pdfThumbnailViewer.forceRendering()) {
85
- return;
86
- }
87
- }
88
-
89
- if (this.printing) {
90
- // If printing is currently ongoing do not reschedule cleanup.
91
- return;
92
- }
93
-
94
- if (this.onIdle) {
95
- this.idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
96
- }
97
- }
98
- /**
99
- * @param {Object} visible
100
- * @param {Array} views
101
- * @param {boolean} scrolledDown
102
- */
103
-
104
-
105
- getHighestPriority(visible, views, scrolledDown) {
106
- /**
107
- * The state has changed. Figure out which page has the highest priority to
108
- * render next (if any).
109
- *
110
- * Priority:
111
- * 1. visible pages
112
- * 2. if last scrolled down, the page after the visible pages, or
113
- * if last scrolled up, the page before the visible pages
114
- */
115
- const visibleViews = visible.views;
116
- const numVisible = visibleViews.length;
117
-
118
- if (numVisible === 0) {
119
- return null;
120
- }
121
-
122
- for (let i = 0; i < numVisible; ++i) {
123
- const view = visibleViews[i].view;
124
-
125
- if (!this.isViewFinished(view)) {
126
- return view;
127
- }
128
- } // All the visible views have rendered; try to render next/previous pages.
129
-
130
-
131
- if (scrolledDown) {
132
- const nextPageIndex = visible.last.id; // IDs start at 1, so no need to add 1.
133
-
134
- if (views[nextPageIndex] && !this.isViewFinished(views[nextPageIndex])) {
135
- return views[nextPageIndex];
136
- }
137
- } else {
138
- const previousPageIndex = visible.first.id - 2;
139
-
140
- if (views[previousPageIndex] && !this.isViewFinished(views[previousPageIndex])) {
141
- return views[previousPageIndex];
142
- }
143
- } // Everything that needs to be rendered has been.
144
-
145
-
146
- return null;
147
- }
148
- /**
149
- * @param {IRenderableView} view
150
- * @returns {boolean}
151
- */
152
-
153
-
154
- isViewFinished(view) {
155
- return view.renderingState === RenderingStates.FINISHED;
156
- }
157
- /**
158
- * Render a page or thumbnail view. This calls the appropriate function
159
- * based on the views state. If the view is already rendered it will return
160
- * `false`.
161
- *
162
- * @param {IRenderableView} view
163
- */
164
-
165
-
166
- renderView(view) {
167
- switch (view.renderingState) {
168
- case RenderingStates.FINISHED:
169
- return false;
170
-
171
- case RenderingStates.PAUSED:
172
- this.highestPriorityPage = view.renderingId;
173
- view.resume();
174
- break;
175
-
176
- case RenderingStates.RUNNING:
177
- this.highestPriorityPage = view.renderingId;
178
- break;
179
-
180
- case RenderingStates.INITIAL:
181
- this.highestPriorityPage = view.renderingId;
182
- view.draw().finally(() => {
183
- this.renderHighestPriority();
184
- }).catch(reason => {
185
- if (reason instanceof RenderingCancelledException) {
186
- return;
187
- }
188
-
189
- console.error(`renderView: "${reason}"`);
190
- });
191
- break;
192
- }
193
-
194
- return true;
195
- }
196
-
197
- }
198
-
199
- export { PDFRenderingQueue, RenderingStates };
1
+ import './pdf.js';
2
+ import { RenderingStates } from './ui_utils.js';
3
+ import { R as RenderingCancelledException } from './node_stream2.js';
4
+ import './util.js';
5
+ import './message_handler.js';
6
+
7
+ /* Copyright 2012 Mozilla Foundation
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ const CLEANUP_TIMEOUT = 30000;
23
+
24
+ /**
25
+ * Controls rendering of the views for pages and thumbnails.
26
+ */
27
+ class PDFRenderingQueue {
28
+ constructor() {
29
+ this.pdfViewer = null;
30
+ this.pdfThumbnailViewer = null;
31
+ this.onIdle = null;
32
+ this.highestPriorityPage = null;
33
+ /** @type {number} */
34
+ this.idleTimeout = null;
35
+ this.printing = false;
36
+ this.isThumbnailViewEnabled = false;
37
+ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
38
+ Object.defineProperty(this, "hasViewer", {
39
+ value: () => !!this.pdfViewer
40
+ });
41
+ }
42
+ }
43
+
44
+ /**
45
+ * @param {PDFViewer} pdfViewer
46
+ */
47
+ setViewer(pdfViewer) {
48
+ this.pdfViewer = pdfViewer;
49
+ }
50
+
51
+ /**
52
+ * @param {PDFThumbnailViewer} pdfThumbnailViewer
53
+ */
54
+ setThumbnailViewer(pdfThumbnailViewer) {
55
+ this.pdfThumbnailViewer = pdfThumbnailViewer;
56
+ }
57
+
58
+ /**
59
+ * @param {IRenderableView} view
60
+ * @returns {boolean}
61
+ */
62
+ isHighestPriority(view) {
63
+ return this.highestPriorityPage === view.renderingId;
64
+ }
65
+
66
+ /**
67
+ * @param {Object} currentlyVisiblePages
68
+ */
69
+ renderHighestPriority(currentlyVisiblePages) {
70
+ var _this$pdfThumbnailVie;
71
+ if (this.idleTimeout) {
72
+ clearTimeout(this.idleTimeout);
73
+ this.idleTimeout = null;
74
+ }
75
+
76
+ // Pages have a higher priority than thumbnails, so check them first.
77
+ if (this.pdfViewer.forceRendering(currentlyVisiblePages)) {
78
+ return;
79
+ }
80
+ // No pages needed rendering, so check thumbnails.
81
+ if (this.isThumbnailViewEnabled && (_this$pdfThumbnailVie = this.pdfThumbnailViewer) !== null && _this$pdfThumbnailVie !== void 0 && _this$pdfThumbnailVie.forceRendering()) {
82
+ return;
83
+ }
84
+ if (this.printing) {
85
+ // If printing is currently ongoing do not reschedule cleanup.
86
+ return;
87
+ }
88
+ if (this.onIdle) {
89
+ this.idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
90
+ }
91
+ }
92
+
93
+ /**
94
+ * @param {Object} visible
95
+ * @param {Array} views
96
+ * @param {boolean} scrolledDown
97
+ * @param {boolean} [preRenderExtra]
98
+ */
99
+ getHighestPriority(visible, views, scrolledDown, preRenderExtra = false) {
100
+ /**
101
+ * The state has changed. Figure out which page has the highest priority to
102
+ * render next (if any).
103
+ *
104
+ * Priority:
105
+ * 1. visible pages
106
+ * 2. if last scrolled down, the page after the visible pages, or
107
+ * if last scrolled up, the page before the visible pages
108
+ */
109
+ const visibleViews = visible.views,
110
+ numVisible = visibleViews.length;
111
+ if (numVisible === 0) {
112
+ return null;
113
+ }
114
+ for (let i = 0; i < numVisible; i++) {
115
+ const view = visibleViews[i].view;
116
+ if (!this.isViewFinished(view)) {
117
+ return view;
118
+ }
119
+ }
120
+ const firstId = visible.first.id,
121
+ lastId = visible.last.id;
122
+
123
+ // All the visible views have rendered; try to handle any "holes" in the
124
+ // page layout (can happen e.g. with spreadModes at higher zoom levels).
125
+ if (lastId - firstId + 1 > numVisible) {
126
+ const visibleIds = visible.ids;
127
+ for (let i = 1, ii = lastId - firstId; i < ii; i++) {
128
+ const holeId = scrolledDown ? firstId + i : lastId - i;
129
+ if (visibleIds.has(holeId)) {
130
+ continue;
131
+ }
132
+ const holeView = views[holeId - 1];
133
+ if (!this.isViewFinished(holeView)) {
134
+ return holeView;
135
+ }
136
+ }
137
+ }
138
+
139
+ // All the visible views have rendered; try to render next/previous page.
140
+ // (IDs start at 1, so no need to add 1 when `scrolledDown === true`.)
141
+ let preRenderIndex = scrolledDown ? lastId : firstId - 2;
142
+ let preRenderView = views[preRenderIndex];
143
+ if (preRenderView && !this.isViewFinished(preRenderView)) {
144
+ return preRenderView;
145
+ }
146
+ if (preRenderExtra) {
147
+ preRenderIndex += scrolledDown ? 1 : -1;
148
+ preRenderView = views[preRenderIndex];
149
+ if (preRenderView && !this.isViewFinished(preRenderView)) {
150
+ return preRenderView;
151
+ }
152
+ }
153
+ // Everything that needs to be rendered has been.
154
+ return null;
155
+ }
156
+
157
+ /**
158
+ * @param {IRenderableView} view
159
+ * @returns {boolean}
160
+ */
161
+ isViewFinished(view) {
162
+ return view.renderingState === RenderingStates.FINISHED;
163
+ }
164
+
165
+ /**
166
+ * Render a page or thumbnail view. This calls the appropriate function
167
+ * based on the views state. If the view is already rendered it will return
168
+ * `false`.
169
+ *
170
+ * @param {IRenderableView} view
171
+ */
172
+ renderView(view) {
173
+ switch (view.renderingState) {
174
+ case RenderingStates.FINISHED:
175
+ return false;
176
+ case RenderingStates.PAUSED:
177
+ this.highestPriorityPage = view.renderingId;
178
+ view.resume();
179
+ break;
180
+ case RenderingStates.RUNNING:
181
+ this.highestPriorityPage = view.renderingId;
182
+ break;
183
+ case RenderingStates.INITIAL:
184
+ this.highestPriorityPage = view.renderingId;
185
+ view.draw().finally(() => {
186
+ this.renderHighestPriority();
187
+ }).catch(reason => {
188
+ if (reason instanceof RenderingCancelledException) {
189
+ return;
190
+ }
191
+ console.error(`renderView: "${reason}"`);
192
+ });
193
+ break;
194
+ }
195
+ return true;
196
+ }
197
+ }
198
+
199
+ export { PDFRenderingQueue };