@progress/kendo-react-pdf-viewer 5.16.1 → 5.16.2-dev.202308021222

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.
package/dist/es/utils.js DELETED
@@ -1,274 +0,0 @@
1
- import { saveAs } from '@progress/kendo-file-saver';
2
- import { getDocument, renderTextLayer } from 'pdfjs-dist';
3
- /**
4
- * @hidden
5
- */
6
- export const DEFAULT_ZOOM_LEVEL = 1.25;
7
- const parsePdfFromBase64String = (base64String) => {
8
- return atob(base64String.replace(/^(data:application\/pdf;base64,)/ig, ''));
9
- };
10
- const getDocumentParameters = (options) => {
11
- let params = { verbosity: 0 };
12
- if (typeof options.data === 'string') {
13
- params.data = parsePdfFromBase64String(options.data);
14
- }
15
- else if (typeof options.url === 'string') {
16
- params.url = options.url;
17
- }
18
- else if (options.arrayBuffer instanceof ArrayBuffer) {
19
- params = options.arrayBuffer;
20
- }
21
- else if (options.typedArray) {
22
- params = options.typedArray;
23
- }
24
- return params;
25
- };
26
- /**
27
- * @hidden
28
- */
29
- export const removeChildren = (dom) => {
30
- while (dom.firstChild) {
31
- dom.removeChild(dom.firstChild);
32
- }
33
- };
34
- const appendPage = (dom, page, index) => {
35
- if (index === 0) {
36
- removeChildren(dom);
37
- }
38
- dom.appendChild(page);
39
- };
40
- /**
41
- * @hidden
42
- */
43
- export const download = (options, fileName = 'Document', saveOptions = {}, onDownload) => {
44
- if (options.pdf) {
45
- options.pdf.getData()
46
- .then((data) => new Blob([data], { type: 'application/pdf' }))
47
- .then((blob) => {
48
- if (!onDownload(blob, fileName, saveOptions)) {
49
- saveAs(blob, fileName, saveOptions);
50
- }
51
- })
52
- .catch((reason) => { options.error.call(undefined, reason); });
53
- }
54
- };
55
- /**
56
- * @hidden
57
- */
58
- export const loadPDF = (options) => {
59
- const params = getDocumentParameters(options);
60
- const { dom, zoom, done, error } = options;
61
- getDocument(params).promise.then((pdfDoc) => {
62
- const pages = [];
63
- for (let i = 1; i <= pdfDoc.numPages; i++) {
64
- pages.push(pdfDoc.getPage(i));
65
- }
66
- return { pages, pdfDoc };
67
- }).then(({ pages, pdfDoc }) => {
68
- Promise.all(pages)
69
- .then(all => all.map((page, i) => {
70
- appendPage(dom, renderPage(page, zoom, error), i);
71
- return page;
72
- }))
73
- .then((pdfPages) => {
74
- done({ pdfPages, pdfDoc, zoom });
75
- }).catch((reason) => { options.error(reason); });
76
- }).catch((reason) => { options.error(reason); });
77
- };
78
- /**
79
- * @hidden
80
- */
81
- export const reloadDocument = (params) => {
82
- const { pdfDoc, zoom, dom, done, error } = params;
83
- const pages = [];
84
- for (let i = 1; i <= pdfDoc.numPages; i++) {
85
- pages.push(pdfDoc.getPage(i));
86
- }
87
- Promise.all(pages)
88
- .then(all => all.map((page, i) => {
89
- appendPage(dom, renderPage(page, zoom, error), i);
90
- return page;
91
- }))
92
- .then(done)
93
- .catch(error);
94
- };
95
- /**
96
- * @hidden
97
- */
98
- export const print = (pages, done, error) => {
99
- const dom = document.createElement('div');
100
- let dones = pages.map(() => false);
101
- pages.forEach((page, index) => {
102
- const viewport = renderCanvas(page, (el) => {
103
- dom.appendChild(el);
104
- dones[index] = true;
105
- if (dones.every(Boolean)) {
106
- openPrintDialog(dom, viewport.width, viewport.height, done, error);
107
- }
108
- }, error);
109
- });
110
- };
111
- const openPrintDialog = (dom, width, height, done, onError) => {
112
- const printDialog = window.open('', '', 'innerWidth=' + width + ',innerHeight=' + height + 'location=no,titlebar=no,toolbar=no');
113
- if (!printDialog || !printDialog.document) {
114
- onError();
115
- return;
116
- }
117
- if (printDialog) {
118
- printDialog.document.body.appendChild(dom);
119
- printDialog.focus();
120
- setTimeout(() => {
121
- printDialog.print();
122
- done();
123
- }, 0);
124
- const onAfterPrint = () => {
125
- printDialog.removeEventListener('afterprint', onAfterPrint);
126
- printDialog.close();
127
- };
128
- printDialog.addEventListener('afterprint', onAfterPrint);
129
- }
130
- };
131
- const renderCanvas = (page, done, error) => {
132
- const viewport = page.getViewport({ scale: DEFAULT_ZOOM_LEVEL });
133
- const styles = {};
134
- const pageElement = createElement('div', '', styles);
135
- const canvas = createElement('canvas', '', { width: '100%', height: '100%' });
136
- const canvasContext = canvas.getContext('2d');
137
- canvas.height = viewport.height;
138
- canvas.width = viewport.width;
139
- pageElement.appendChild(canvas);
140
- page.render({ canvasContext, viewport }).promise.then(() => {
141
- const printContent = new Image();
142
- printContent.src = canvas.toDataURL();
143
- pageElement.removeChild(canvas);
144
- pageElement.appendChild(printContent);
145
- printContent.width = canvas.width;
146
- printContent.height = canvas.height;
147
- const onload = () => {
148
- printContent.removeEventListener('load', onload);
149
- done(pageElement);
150
- };
151
- printContent.addEventListener('load', onload);
152
- }).catch(error);
153
- return viewport;
154
- };
155
- const createElement = function (name, className, styles) {
156
- const element = document.createElement(name);
157
- if (className) {
158
- element.className = className;
159
- }
160
- Object.keys(styles).forEach(key => element.style[key] = styles[key]);
161
- return element;
162
- };
163
- const renderPage = (page, zoom, error) => {
164
- const viewport = page.getViewport({ scale: zoom });
165
- const styles = { width: viewport.width + 'px', height: viewport.height + 'px' };
166
- const pageElement = createElement('div', 'k-page', styles);
167
- const canvas = createElement('canvas', '', { width: '100%', height: '100%' });
168
- const canvasContext = canvas.getContext('2d');
169
- canvas.height = viewport.height;
170
- canvas.width = viewport.width;
171
- pageElement.appendChild(canvas);
172
- page.render({ canvasContext, viewport }).promise.then(() => {
173
- page.getTextContent().then((textContent) => {
174
- const textLayer = createElement('div', 'k-text-layer', styles);
175
- renderTextLayer({
176
- textContentSource: textContent,
177
- container: textLayer,
178
- viewport: viewport,
179
- textDivs: []
180
- }).promise.then(() => {
181
- pageElement.appendChild(textLayer);
182
- }).catch(error);
183
- });
184
- }).catch(error);
185
- return pageElement;
186
- };
187
- const searchMatchScrollLeftOffset = 0;
188
- const searchMatchScrollTopOffset = -64;
189
- const scrollToSearchMatch = (matchElement, ref) => {
190
- if (!matchElement) {
191
- return;
192
- }
193
- const closestCharElement = matchElement.closest('.k-text-char');
194
- const closestTextElement = closestCharElement ?
195
- closestCharElement.closest('span[role=\'presentation\']') :
196
- null;
197
- if (!closestTextElement) {
198
- return;
199
- }
200
- const closestPageElement = closestTextElement.closest('.k-page');
201
- if (!closestPageElement) {
202
- return;
203
- }
204
- const scrollLeft = closestPageElement.offsetLeft +
205
- (-1) * ref.scroller.element.offsetLeft +
206
- closestTextElement.offsetLeft + searchMatchScrollLeftOffset;
207
- const scrollTop = closestPageElement.offsetTop +
208
- (-1) * ref.scroller.element.offsetTop +
209
- closestTextElement.offsetTop + searchMatchScrollTopOffset;
210
- ref.scroller.scrollTo(scrollLeft, scrollTop, { trackScrollEvent: false });
211
- };
212
- /**
213
- * @hidden
214
- */
215
- export const goToNextSearchMatch = (ref) => {
216
- ref.search.markNextMatch();
217
- const matchElement = ref.search.getActiveMatchElement();
218
- scrollToSearchMatch(matchElement, ref);
219
- };
220
- /**
221
- * @hidden
222
- */
223
- export const goToPreviousSearchMatch = (ref) => {
224
- ref.search.markPreviousMatch();
225
- const matchElement = ref.search.getActiveMatchElement();
226
- scrollToSearchMatch(matchElement, ref);
227
- };
228
- /**
229
- * @hidden
230
- */
231
- export const calculateZoomLevel = (zoomLevel, zoomLevelType, currentZoom, dom) => {
232
- const documentContainer = dom.closest('.k-pdf-viewer-canvas');
233
- const page = dom.querySelector('.k-page');
234
- const pageSize = { width: page.offsetWidth, height: page.offsetHeight };
235
- let calculatedZoomLevel = zoomLevel;
236
- if (zoomLevelType === 'ActualWidth') {
237
- calculatedZoomLevel = 1;
238
- }
239
- else if (zoomLevelType === 'FitToWidth') {
240
- calculatedZoomLevel = (documentContainer.offsetWidth / (pageSize.width / currentZoom));
241
- }
242
- else if (zoomLevelType === 'FitToPage') {
243
- calculatedZoomLevel = (documentContainer.offsetHeight / (pageSize.height / currentZoom));
244
- }
245
- return calculatedZoomLevel;
246
- };
247
- /**
248
- * Scrolls the PDFViewer document to the passed page number.
249
- *
250
- * @param rootElement The root HTML element of the PDFViewer component.
251
- * @param pageNumber The page number.
252
- */
253
- export const scrollToPage = (rootElement, pageNumber) => {
254
- const pages = rootElement.querySelectorAll('.k-page');
255
- const page = pages[0];
256
- if (page instanceof HTMLDivElement) {
257
- const top = (page.offsetHeight + page.offsetTop) * Math.max(0, Math.min(pageNumber, pages.length - 1));
258
- const scrollElement = page.closest('.k-pdf-viewer-canvas');
259
- if (scrollElement) {
260
- scrollElement.scrollTo({ top, behavior: 'auto' });
261
- }
262
- }
263
- };
264
- /**
265
- * A function which gives you the page number of the document according to the scroll position.
266
- *
267
- * @param rootElement The root HTML element of the PDFViewer component.
268
- * @returns The page number.
269
- */
270
- export const currentPage = (rootElement) => {
271
- const scrollElement = rootElement.querySelector('.k-pdf-viewer-canvas');
272
- const page = rootElement.querySelector('.k-page');
273
- return (scrollElement && page) ? Math.floor((Math.round(scrollElement.scrollTop)) / (page.offsetHeight + page.offsetTop) + 0.01) : 0;
274
- };
@@ -1,34 +0,0 @@
1
- /**
2
- * @hidden
3
- */
4
- export declare class Scroller {
5
- options: any;
6
- state: any;
7
- element: any;
8
- throttledOnDrag: any;
9
- draggable: any;
10
- throttledOnElementScroll: any;
11
- constructor(element: any, options: any);
12
- destroy(): void;
13
- initDraggable(): void;
14
- destroyDraggable(): void;
15
- bindEvents(): void;
16
- bindDraggableEvents(): void;
17
- bindElementScroll(): void;
18
- unbindEvents(): void;
19
- unbindDraggableEvents(): void;
20
- unbindElementScroll(): void;
21
- setState(newState: any): void;
22
- resetState(): void;
23
- enablePanEventsTracking(): void;
24
- disablePanEventsTracking(): void;
25
- shouldTrackPanEvents(): any;
26
- onElementScroll: () => void;
27
- onDragStart: (e: any) => void;
28
- onDrag: (e: any) => void;
29
- onDragEnd: () => void;
30
- calculateEventLocationDelta(e: any): void;
31
- scrollTo(x: any, y: any, options?: {
32
- trackScrollEvent: boolean;
33
- }): void;
34
- }
@@ -1,230 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Scroller = void 0;
4
- const kendo_draggable_1 = require("@progress/kendo-draggable");
5
- const throttle = function (func, wait, options = {}) {
6
- let timeout, context, args, result;
7
- let previous = 0;
8
- options = options || {};
9
- const later = function () {
10
- previous = options.leading === false ? 0 : new Date().getTime();
11
- timeout = undefined;
12
- result = func.apply(context, args);
13
- if (!timeout) {
14
- context = args = null;
15
- }
16
- };
17
- const throttled = function () {
18
- const now = new Date().getTime();
19
- if (!previous && options.leading === false) {
20
- previous = now;
21
- }
22
- const remaining = wait - (now - previous);
23
- context = undefined; // this
24
- args = arguments;
25
- if (remaining <= 0 || remaining > wait) {
26
- if (timeout) {
27
- clearTimeout(timeout);
28
- timeout = undefined;
29
- }
30
- previous = now;
31
- result = func.apply(context, args);
32
- if (!timeout) {
33
- context = args = null;
34
- }
35
- }
36
- else if (!timeout && options.trailing !== false) {
37
- timeout = window.setTimeout(later, remaining);
38
- }
39
- return result;
40
- };
41
- return throttled;
42
- };
43
- const preventDefault = (e) => {
44
- if (e.preventDefault) {
45
- e.preventDefault();
46
- }
47
- if (e.originalEvent) {
48
- e.originalEvent.preventDefault();
49
- }
50
- };
51
- const matchesElementSelector = (element, selector) => {
52
- if (!element || !selector) {
53
- return false;
54
- }
55
- return element.closest(selector);
56
- };
57
- const FRAMES_PER_SECOND = 1000 / 60;
58
- const SCROLL = 'scroll';
59
- /**
60
- * @hidden
61
- */
62
- class Scroller {
63
- constructor(element, options) {
64
- this.options = {
65
- // the drag directions are actually reversed, e.g.
66
- // dragging to the right actually moves the document to the left
67
- scrollDirectionModifier: -1,
68
- // throttle the scroll events to get a more similar experience
69
- // to the scrolling behavior in Adobe Acrobat Reader
70
- // as well as allow a way to improve the scrolling performance for large files
71
- panScrollThrottleDelay: FRAMES_PER_SECOND,
72
- scrollThrottleDelay: FRAMES_PER_SECOND,
73
- filter: '',
74
- events: {
75
- [SCROLL]: () => { }
76
- }
77
- };
78
- this.onElementScroll = () => {
79
- const element = this.element;
80
- if (this.state.trackNextElementScroll) {
81
- this.scrollTo(element.scrollLeft, element.scrollTop);
82
- }
83
- else {
84
- // reset the state, so that consecutive scroll events can be handled
85
- this.state.trackNextElementScroll = true;
86
- }
87
- };
88
- this.onDragStart = (e) => {
89
- this.state.dragStarted = false;
90
- if (!this.shouldTrackPanEvents()) {
91
- return;
92
- }
93
- const target = e.target || (e.originalEvent || {}).target;
94
- if (this.options.filter && !matchesElementSelector(target, this.options.filter)) {
95
- return;
96
- }
97
- preventDefault(e);
98
- this.setState({
99
- dragStarted: true,
100
- location: {
101
- pageX: e.pageX,
102
- pageY: e.pageY
103
- },
104
- locationDelta: {
105
- x: 0,
106
- y: 0
107
- }
108
- });
109
- };
110
- this.onDrag = (e) => {
111
- if (!this.shouldTrackPanEvents() || !this.state.dragStarted) {
112
- return;
113
- }
114
- this.calculateEventLocationDelta(e);
115
- this.setState({
116
- location: {
117
- pageX: e.pageX,
118
- pageY: e.pageY
119
- }
120
- });
121
- const directionModifier = this.options.scrollDirectionModifier;
122
- const scrollLeft = this.element.scrollLeft + (directionModifier * this.state.locationDelta.x);
123
- const scrollTop = this.element.scrollTop + (directionModifier * this.state.locationDelta.y);
124
- this.scrollTo(scrollLeft, scrollTop);
125
- };
126
- this.onDragEnd = () => {
127
- if (!this.shouldTrackPanEvents()) {
128
- return;
129
- }
130
- };
131
- this.element = element;
132
- this.options = Object.assign({}, this.options, options);
133
- this.resetState();
134
- this.bindEvents();
135
- }
136
- destroy() {
137
- this.unbindEvents();
138
- }
139
- initDraggable() {
140
- this.destroyDraggable();
141
- if (this.options.panScrollThrottleDelay > 0) {
142
- this.throttledOnDrag = throttle(this.onDrag, this.options.panScrollThrottleDelay);
143
- }
144
- else {
145
- this.throttledOnDrag = this.onDrag;
146
- }
147
- this.draggable = new kendo_draggable_1.default({
148
- mouseOnly: false,
149
- press: this.onDragStart,
150
- drag: this.throttledOnDrag,
151
- release: this.onDragEnd
152
- });
153
- this.draggable.bindTo(this.element);
154
- }
155
- destroyDraggable() {
156
- if (this.draggable && this.draggable.destroy) {
157
- this.draggable.destroy();
158
- if (this.throttledOnDrag && this.throttledOnDrag.cancel) {
159
- this.throttledOnDrag.cancel();
160
- this.throttledOnDrag = null;
161
- }
162
- }
163
- }
164
- bindEvents() {
165
- this.bindDraggableEvents();
166
- this.bindElementScroll();
167
- }
168
- bindDraggableEvents() {
169
- this.initDraggable();
170
- }
171
- bindElementScroll() {
172
- if (this.options.scrollThrottleDelay > 0) {
173
- this.throttledOnElementScroll = throttle(this.onElementScroll, this.options.scrollThrottleDelay);
174
- }
175
- else {
176
- this.throttledOnElementScroll = this.onElementScroll;
177
- }
178
- this.element.addEventListener(SCROLL, this.throttledOnElementScroll);
179
- }
180
- unbindEvents() {
181
- this.unbindElementScroll();
182
- this.unbindDraggableEvents();
183
- }
184
- unbindDraggableEvents() {
185
- this.destroyDraggable();
186
- }
187
- unbindElementScroll() {
188
- if (this.throttledOnElementScroll && this.throttledOnElementScroll.cancel) {
189
- this.throttledOnElementScroll.cancel();
190
- this.throttledOnElementScroll = null;
191
- }
192
- this.element.removeEventListener(SCROLL, this.throttledOnElementScroll);
193
- }
194
- setState(newState) {
195
- this.state = Object.assign({}, this.state || {}, newState);
196
- }
197
- resetState() {
198
- this.setState({
199
- trackPanEvents: false,
200
- trackNextElementScroll: false,
201
- location: { pageX: 0, pageY: 0 },
202
- locationDelta: { x: 0, y: 0 }
203
- });
204
- }
205
- enablePanEventsTracking() {
206
- this.state.trackPanEvents = true;
207
- this.bindDraggableEvents();
208
- }
209
- disablePanEventsTracking() {
210
- this.unbindDraggableEvents();
211
- this.state.trackPanEvents = false;
212
- }
213
- shouldTrackPanEvents() {
214
- return this.state.trackPanEvents;
215
- }
216
- calculateEventLocationDelta(e) {
217
- this.state.locationDelta = {
218
- x: e.pageX - this.state.location.pageX,
219
- y: e.pageY - this.state.location.pageY
220
- };
221
- }
222
- scrollTo(x, y, options = { trackScrollEvent: true }) {
223
- if (!options.trackScrollEvent) {
224
- this.state.trackNextElementScroll = false;
225
- }
226
- this.element.scrollLeft = x;
227
- this.element.scrollTop = y;
228
- }
229
- }
230
- exports.Scroller = Scroller;
@@ -1,42 +0,0 @@
1
- /**
2
- * @hidden
3
- */
4
- export declare class SearchService {
5
- options: {
6
- highlightClass: string;
7
- highlightMarkClass: string;
8
- charClass: string;
9
- textContainers: never[];
10
- };
11
- state: any;
12
- constructor(options: any);
13
- destroy(): void;
14
- extendOptions(options: any): void;
15
- setState(newState: any): void;
16
- resetState(): void;
17
- search({ text, matchCase }: any): {
18
- startOffset: number;
19
- endOffset: number;
20
- }[];
21
- clearSearch(): void;
22
- restoreOriginalText(): void;
23
- shouldTransformText(): boolean;
24
- transformTextForSearch(): void;
25
- extractTextNodes(node: any): void;
26
- transformTextNodesForSearch(textNodes: any): void;
27
- transformTextNodeForSearch(node: any): void;
28
- splitTextByChars(text: any): string;
29
- forEachTextContainer(callback: any): void;
30
- highlightAllMatches(): void;
31
- addMatchHighlight(matchStartOffset: number, matchEndOffset: number, matchIndex: number): void;
32
- removeMatchHighlights(): void;
33
- addActiveMatchMark(): void;
34
- removeActiveMatchMark(): void;
35
- removeIndicators(): void;
36
- markNextMatch(): void;
37
- markPreviousMatch(): void;
38
- markNextMatchIndex(): void;
39
- markPreviousMatchIndex(): void;
40
- moveActiveMatchIndex(delta: any): void;
41
- getActiveMatchElement(): undefined;
42
- }