@progress/kendo-pdfviewer-common 0.3.0-dev.202409251459 → 0.3.0-dev.202410100645

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.
Files changed (149) hide show
  1. package/dist/es/annotations/annotation-editor-layer-builder.js +139 -0
  2. package/dist/es/annotations/annotation-editor-layer.js +780 -0
  3. package/dist/es/annotations/annotation-editor-ui-manager.js +1690 -0
  4. package/dist/es/annotations/annotation-layer-builder.js +217 -0
  5. package/dist/es/annotations/annotation-layer.js +2793 -0
  6. package/dist/es/annotations/draw-layer-builder.js +65 -0
  7. package/dist/es/annotations/draw-layer.js +220 -0
  8. package/dist/es/annotations/editors/annotation-editor.js +1519 -0
  9. package/dist/es/annotations/editors/free-text-editor.js +783 -0
  10. package/dist/es/annotations/editors/highlight-editor.js +717 -0
  11. package/dist/es/annotations/editors/outliner.js +747 -0
  12. package/dist/es/annotations/helpers/annotation-storage.js +294 -0
  13. package/dist/es/annotations/helpers/color-manager.js +72 -0
  14. package/dist/es/annotations/helpers/command-manager.js +133 -0
  15. package/dist/es/annotations/helpers/id-manager.js +34 -0
  16. package/dist/es/annotations/helpers/image-manager.js +176 -0
  17. package/dist/es/annotations/helpers/text-accessibility-manager.js +212 -0
  18. package/dist/es/annotations/helpers/tools.js +19 -0
  19. package/dist/es/annotations/shared/display_utils.js +1024 -0
  20. package/dist/es/annotations/shared/event_utils.js +213 -0
  21. package/dist/es/annotations/shared/murmurhash3.js +126 -0
  22. package/dist/es/annotations/shared/scripting_utils.js +90 -0
  23. package/dist/es/annotations/shared/ui_utils.js +823 -0
  24. package/dist/es/annotations/shared/utils.js +909 -0
  25. package/dist/es/common/component.js +33 -0
  26. package/dist/es/common/core.js +91 -0
  27. package/dist/es/common/dom.js +11 -0
  28. package/dist/es/common/main.js +4 -0
  29. package/dist/es/common/math.js +1 -0
  30. package/dist/es/enums/PdfViewerInteractionMode.js +6 -0
  31. package/dist/es/links/link-service.js +505 -0
  32. package/dist/es/main.js +2 -0
  33. package/dist/es/scroller.js +29 -30
  34. package/dist/es/text/text-layer-builder.js +288 -0
  35. package/dist/es/utils.js +5 -5
  36. package/dist/es/widget/page.js +762 -0
  37. package/dist/es/widget/pdfviewer.js +1659 -0
  38. package/dist/es2015/annotations/annotation-editor-layer-builder.js +139 -0
  39. package/dist/es2015/annotations/annotation-editor-layer.js +780 -0
  40. package/dist/es2015/annotations/annotation-editor-ui-manager.js +1690 -0
  41. package/dist/es2015/annotations/annotation-layer-builder.js +217 -0
  42. package/dist/es2015/annotations/annotation-layer.js +2793 -0
  43. package/dist/es2015/annotations/draw-layer-builder.js +65 -0
  44. package/dist/es2015/annotations/draw-layer.js +220 -0
  45. package/dist/es2015/annotations/editors/annotation-editor.js +1519 -0
  46. package/dist/es2015/annotations/editors/free-text-editor.js +783 -0
  47. package/dist/es2015/annotations/editors/highlight-editor.js +717 -0
  48. package/dist/es2015/annotations/editors/outliner.js +747 -0
  49. package/dist/es2015/annotations/helpers/annotation-storage.js +294 -0
  50. package/dist/es2015/annotations/helpers/color-manager.js +72 -0
  51. package/dist/es2015/annotations/helpers/command-manager.js +133 -0
  52. package/dist/es2015/annotations/helpers/id-manager.js +34 -0
  53. package/dist/es2015/annotations/helpers/image-manager.js +176 -0
  54. package/dist/es2015/annotations/helpers/text-accessibility-manager.js +212 -0
  55. package/dist/es2015/annotations/helpers/tools.js +19 -0
  56. package/dist/es2015/annotations/shared/display_utils.js +1024 -0
  57. package/dist/es2015/annotations/shared/event_utils.js +213 -0
  58. package/dist/es2015/annotations/shared/murmurhash3.js +126 -0
  59. package/dist/es2015/annotations/shared/scripting_utils.js +90 -0
  60. package/dist/es2015/annotations/shared/ui_utils.js +823 -0
  61. package/dist/es2015/annotations/shared/utils.js +909 -0
  62. package/dist/es2015/common/component.js +33 -0
  63. package/dist/es2015/common/core.js +91 -0
  64. package/dist/es2015/common/dom.js +11 -0
  65. package/dist/es2015/common/main.js +4 -0
  66. package/dist/es2015/common/math.js +1 -0
  67. package/dist/es2015/enums/PdfViewerInteractionMode.js +6 -0
  68. package/dist/es2015/links/link-service.js +505 -0
  69. package/dist/es2015/main.js +2 -0
  70. package/dist/es2015/scroller.js +29 -30
  71. package/dist/es2015/text/text-layer-builder.js +288 -0
  72. package/dist/es2015/utils.js +5 -5
  73. package/dist/es2015/widget/page.js +762 -0
  74. package/dist/es2015/widget/pdfviewer.js +1659 -0
  75. package/dist/npm/annotations/annotation-editor-layer-builder.d.ts +39 -0
  76. package/dist/npm/annotations/annotation-editor-layer-builder.js +142 -0
  77. package/dist/npm/annotations/annotation-editor-layer.d.ts +196 -0
  78. package/dist/npm/annotations/annotation-editor-layer.js +783 -0
  79. package/dist/npm/annotations/annotation-editor-ui-manager.d.ts +281 -0
  80. package/dist/npm/annotations/annotation-editor-ui-manager.js +1694 -0
  81. package/dist/npm/annotations/annotation-layer-builder.d.ts +58 -0
  82. package/dist/npm/annotations/annotation-layer-builder.js +220 -0
  83. package/dist/npm/annotations/annotation-layer.d.ts +134 -0
  84. package/dist/npm/annotations/annotation-layer.js +2794 -0
  85. package/dist/npm/annotations/draw-layer-builder.d.ts +21 -0
  86. package/dist/npm/annotations/draw-layer-builder.js +68 -0
  87. package/dist/npm/annotations/draw-layer.d.ts +33 -0
  88. package/dist/npm/annotations/draw-layer.js +223 -0
  89. package/dist/npm/annotations/editors/annotation-editor.d.ts +349 -0
  90. package/dist/npm/annotations/editors/annotation-editor.js +1523 -0
  91. package/dist/npm/annotations/editors/free-text-editor.d.ts +108 -0
  92. package/dist/npm/annotations/editors/free-text-editor.js +786 -0
  93. package/dist/npm/annotations/editors/highlight-editor.d.ts +114 -0
  94. package/dist/npm/annotations/editors/highlight-editor.js +717 -0
  95. package/dist/npm/annotations/editors/outliner.d.ts +51 -0
  96. package/dist/npm/annotations/editors/outliner.js +750 -0
  97. package/dist/npm/annotations/helpers/annotation-storage.d.ts +98 -0
  98. package/dist/npm/annotations/helpers/annotation-storage.js +299 -0
  99. package/dist/npm/annotations/helpers/color-manager.d.ts +6 -0
  100. package/dist/npm/annotations/helpers/color-manager.js +76 -0
  101. package/dist/npm/annotations/helpers/command-manager.d.ts +42 -0
  102. package/dist/npm/annotations/helpers/command-manager.js +137 -0
  103. package/dist/npm/annotations/helpers/id-manager.d.ts +6 -0
  104. package/dist/npm/annotations/helpers/id-manager.js +38 -0
  105. package/dist/npm/annotations/helpers/image-manager.d.ts +18 -0
  106. package/dist/npm/annotations/helpers/image-manager.js +180 -0
  107. package/dist/npm/annotations/helpers/text-accessibility-manager.d.ts +36 -0
  108. package/dist/npm/annotations/helpers/text-accessibility-manager.js +215 -0
  109. package/dist/npm/annotations/helpers/tools.d.ts +1 -0
  110. package/dist/npm/annotations/helpers/tools.js +23 -0
  111. package/dist/npm/annotations/shared/display_utils.d.ts +6 -0
  112. package/dist/npm/annotations/shared/display_utils.js +1030 -0
  113. package/dist/npm/annotations/shared/event_utils.d.ts +33 -0
  114. package/dist/npm/annotations/shared/event_utils.js +165 -0
  115. package/dist/npm/annotations/shared/murmurhash3.d.ts +8 -0
  116. package/dist/npm/annotations/shared/murmurhash3.js +129 -0
  117. package/dist/npm/annotations/shared/scripting_utils.d.ts +17 -0
  118. package/dist/npm/annotations/shared/scripting_utils.js +93 -0
  119. package/dist/npm/annotations/shared/ui_utils.d.ts +54 -0
  120. package/dist/npm/annotations/shared/ui_utils.js +250 -0
  121. package/dist/npm/annotations/shared/utils.d.ts +148 -0
  122. package/dist/npm/annotations/shared/utils.js +872 -0
  123. package/dist/npm/common/component.d.ts +9 -0
  124. package/dist/npm/common/component.js +37 -0
  125. package/dist/npm/common/core.d.ts +18 -0
  126. package/dist/npm/common/core.js +112 -0
  127. package/dist/npm/common/dom.d.ts +3 -0
  128. package/dist/npm/common/dom.js +17 -0
  129. package/dist/npm/common/main.d.ts +4 -0
  130. package/dist/npm/common/main.js +7 -0
  131. package/dist/npm/common/math.d.ts +1 -0
  132. package/dist/npm/common/math.js +5 -0
  133. package/dist/npm/enums/PdfViewerInteractionMode.d.ts +4 -0
  134. package/dist/npm/enums/PdfViewerInteractionMode.js +9 -0
  135. package/dist/npm/links/link-service.d.ts +116 -0
  136. package/dist/npm/links/link-service.js +501 -0
  137. package/dist/npm/main.d.ts +2 -0
  138. package/dist/npm/main.js +2 -0
  139. package/dist/npm/scroller.d.ts +7 -3
  140. package/dist/npm/scroller.js +32 -32
  141. package/dist/npm/text/text-layer-builder.d.ts +30 -0
  142. package/dist/npm/text/text-layer-builder.js +291 -0
  143. package/dist/npm/utils.d.ts +20 -2
  144. package/dist/npm/utils.js +11 -8
  145. package/dist/npm/widget/page.d.ts +75 -0
  146. package/dist/npm/widget/page.js +763 -0
  147. package/dist/npm/widget/pdfviewer.d.ts +222 -0
  148. package/dist/npm/widget/pdfviewer.js +1663 -0
  149. package/package.json +13 -9
@@ -0,0 +1,1663 @@
1
+ "use strict";
2
+ var _PdfViewer_instances, _PdfViewer_annotationEditorMode, _PdfViewer_switchAnnotationEditorModeTimeoutId, _PdfViewer_eventAbortController, _PdfViewer_onPageRenderedCallback, _PdfViewer_switchToEditAnnotationMode, _PdfViewer_scrollIntoView;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.PdfViewer = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const main_1 = require("../common/main");
7
+ const utils_1 = require("../utils");
8
+ const search_1 = require("../search");
9
+ const scroller_1 = require("../scroller");
10
+ const scroller_2 = require("../scroller");
11
+ const kendo_file_saver_1 = require("@progress/kendo-file-saver");
12
+ const pdf_mjs_1 = require("pdfjs-dist/legacy/build/pdf.mjs");
13
+ const annotation_editor_ui_manager_1 = require("../annotations/annotation-editor-ui-manager");
14
+ const event_utils_1 = require("../annotations/shared/event_utils");
15
+ const page_1 = require("./page");
16
+ const utils_2 = require("../annotations/shared/utils");
17
+ const ui_utils_1 = require("../annotations/shared/ui_utils");
18
+ const annotation_storage_1 = require("../annotations/helpers/annotation-storage");
19
+ const link_service_1 = require("../links/link-service");
20
+ const PdfViewerInteractionMode_1 = require("../enums/PdfViewerInteractionMode");
21
+ const PDF_JS = "pdfjs";
22
+ const ERROR = "error";
23
+ const ZOOM_START = "zoomStart";
24
+ const ZOOM_END = "zoomEnd";
25
+ const PAGES_LOAD = "pagesLoad";
26
+ const PRINT_END = "printEnd";
27
+ const SCROLL = "scroll";
28
+ const DOWNLOAD = "download";
29
+ const ANNOTATION_EDITOR_TOOLBAR_SHOW = "annotationEditorToolBarShow";
30
+ const ANNOTATION_EDITOR_TOOLBAR_HIDE = "annotationEditorToolBarHide";
31
+ const DEFAULT_ZOOM_LEVEL = 1.25;
32
+ const FRAMES_PER_SECOND = 1000 / 60;
33
+ class PdfViewer extends main_1.Component {
34
+ constructor(element, options) {
35
+ super(element, options);
36
+ _PdfViewer_instances.add(this);
37
+ this.options = {
38
+ pdfServiceType: PDF_JS,
39
+ // this is a flag used mainly in tests as CSSStyleSheet.replaceSync
40
+ // fails with jsdom
41
+ // a better solution is not to load a stylesheet in setHiddenCanvasStyles
42
+ // but rather create a class like "k-hidden-canvas-element"
43
+ // and override the rendering from pdf.js
44
+ pdfjsSetHiddenCanvasStyles: true,
45
+ fileData: [],
46
+ page: 1,
47
+ fileDescriptor: null,
48
+ downloadFileName: "Document",
49
+ minZoom: 0.5,
50
+ maxZoom: 4,
51
+ zoomRate: 0.25,
52
+ zoomLevel: DEFAULT_ZOOM_LEVEL,
53
+ zoomBeforePrint: false,
54
+ zoomLevelForPrint: 3,
55
+ searchMatchScrollLeftOffset: 0,
56
+ // add some default offset, so that the found match
57
+ // does not appear on the top edge of the page when navigated to
58
+ searchMatchScrollTopOffset: -64,
59
+ // what percentage of the page should be scrolled,
60
+ // so that the page is considered to be changed to the next one
61
+ pageChangeScrollThreshold: 0.85,
62
+ // values higher than 200ms can result in browser zoom
63
+ pageWheelThrottleDelay: 200,
64
+ // ported from pdf.js
65
+ printResolution: 150,
66
+ loadOnDemand: false,
67
+ loadOnDemandPageSize: 2,
68
+ messages: {
69
+ freeTextEditorPlaceholder: "Start typing",
70
+ errorMessages: {
71
+ parseError: "PDF file fails to process.",
72
+ notFound: "File is not found.",
73
+ popupBlocked: "Popup is blocked."
74
+ }
75
+ },
76
+ annotations: {
77
+ highlight: {
78
+ color: "#ffff00"
79
+ }
80
+ },
81
+ elementSelectors: {
82
+ pageSelector: "k-page:not(.k-blank-page)",
83
+ pagesContainerClass: "k-pdf-viewer-pages",
84
+ pageCanvasWrapper: "k-canvas-wrapper",
85
+ documentContainerClass: "k-pdf-viewer-canvas",
86
+ textLayerClass: "k-text-layer",
87
+ searchHighlightClass: "k-search-highlight",
88
+ searchHighlightMarkClass: "k-search-highlight-mark",
89
+ charClass: "k-text-char",
90
+ textElementSelector: "span[role='presentation']",
91
+ renderedPageSelector: "k-page[data-page-number]",
92
+ enabledTextSelectionClass: "k-enable-text-select",
93
+ enabledPanningClass: "k-enable-panning"
94
+ },
95
+ events: {
96
+ [PAGES_LOAD]: main_1.noop,
97
+ [PRINT_END]: main_1.noop,
98
+ [ZOOM_START]: main_1.noop,
99
+ [ZOOM_END]: main_1.noop,
100
+ [ERROR]: main_1.noop,
101
+ [SCROLL]: main_1.noop,
102
+ [ANNOTATION_EDITOR_TOOLBAR_SHOW]: main_1.noop,
103
+ [ANNOTATION_EDITOR_TOOLBAR_HIDE]: main_1.noop
104
+ }
105
+ };
106
+ // todo: props
107
+ // isPageEditing = null;
108
+ this.interactionMode = PdfViewerInteractionMode_1.PdfViewerInteractionMode.TextSelection;
109
+ this.printContainer = null;
110
+ // todo: props ported from pdf.js
111
+ this._scrollMode = ui_utils_1.ScrollMode.PAGE;
112
+ this.isInPresentationMode = false;
113
+ this._currentPageNumber = 1;
114
+ this.linkService = null;
115
+ this.annotationStorage = null;
116
+ this._annotationStorageModified = false;
117
+ this._hasAnnotationEditors = false;
118
+ _PdfViewer_annotationEditorMode.set(this, pdf_mjs_1.AnnotationEditorType.NONE);
119
+ this.annotationEditorUIManager = null;
120
+ _PdfViewer_switchAnnotationEditorModeTimeoutId.set(this, null);
121
+ // #buffer = null;
122
+ // #altTextManager = null;
123
+ // #annotationEditorHighlightColors = null;
124
+ // #annotationEditorMode = AnnotationEditorType.NONE;
125
+ // #annotationEditorUIManager = null;
126
+ // #annotationMode = AnnotationMode.ENABLE_FORMS;
127
+ // #containerTopLeft = null;
128
+ // #enableHWA = false;
129
+ // #enableHighlightFloatingButton = false;
130
+ // #enablePermissions = false;
131
+ // #enableUpdatedAddImage = false;
132
+ // #enableNewAltTextWhenAddingImage = false;
133
+ _PdfViewer_eventAbortController.set(this, null);
134
+ // #mlManager = null;
135
+ _PdfViewer_onPageRenderedCallback.set(this, null);
136
+ // #switchAnnotationEditorModeTimeoutId = null;
137
+ // #getAllTextInProgress = false;
138
+ // #hiddenCopyElement = null;
139
+ // #interruptCopyCondition = false;
140
+ // #previousContainerHeight = 0;
141
+ // #resizeObserver = new ResizeObserver(this.#resizeObserverCallback.bind(this));
142
+ // #scrollModePageState = null;
143
+ // #scaleTimeoutId = null;
144
+ // #textLayerMode = TextLayerMode.ENABLE;
145
+ // todo: props
146
+ this.state = {};
147
+ this.pdfDocument = null;
148
+ this.pages = [];
149
+ this.triggerError = (e) => {
150
+ this.trigger(ERROR, {
151
+ error: e
152
+ });
153
+ };
154
+ this.triggerPrintEnd = (e) => {
155
+ this.trigger(PRINT_END, e);
156
+ };
157
+ this.onPagesLoad = (args) => {
158
+ this.state.pdfDocument = args.pdfDoc;
159
+ this.state.pdfPages = args.pdfPages;
160
+ this.clearSearch();
161
+ this.initSearchService();
162
+ this.enableDocumentScrollEventsTracking();
163
+ this.triggerPagesLoad(args);
164
+ };
165
+ this.onDocumentScrollerScroll = (e) => {
166
+ const currentPageIndex = this.getCurrentPageIndex();
167
+ const pages = this.pages;
168
+ const page = pages[currentPageIndex];
169
+ const nextPage = pages[currentPageIndex + 1];
170
+ const previousPage = pages[currentPageIndex - 1];
171
+ if (page && page.isBlank()) {
172
+ this.drawPageAsync({ pageIndex: currentPageIndex });
173
+ }
174
+ if (nextPage && nextPage.isBlank()) {
175
+ this.drawPageAsync({ pageIndex: currentPageIndex + 1 });
176
+ }
177
+ if (previousPage && previousPage.isBlank()) {
178
+ this.drawPageAsync({ pageIndex: currentPageIndex - 1 });
179
+ }
180
+ this.tryTriggerScroll(e);
181
+ };
182
+ // this is mainly used by blazor to track the page number
183
+ // as this can only be obtained by JS
184
+ this.tryTriggerScroll = (e) => {
185
+ let isPageChanged = false;
186
+ const pageNumber = (0, utils_1.currentPage)(this.element) + 1;
187
+ if (pageNumber !== this.state.activePageNumber && (pageNumber >= 1 && pageNumber <= this.state.pdfDocument.numPagesh)) {
188
+ isPageChanged = true;
189
+ this.state.activePageNumber = pageNumber;
190
+ }
191
+ if (this.shouldPreventScroll) {
192
+ this.shouldPreventScroll = false;
193
+ }
194
+ else {
195
+ this.trigger(SCROLL, {
196
+ scrollLeft: e.scrollLeft,
197
+ scrollTop: e.scrollTop,
198
+ pageNumber: this.state.activePageNumber,
199
+ isPageChanged: isPageChanged
200
+ });
201
+ }
202
+ };
203
+ this.printDocumentAsync = () => {
204
+ this.initPrintContainer();
205
+ const pagePrintPromises = this.getPagesPrintContentAsync(this.printContainer);
206
+ const printPromise = (0, main_1.createPromise)();
207
+ Promise.all(pagePrintPromises)
208
+ .then(() => {
209
+ // print the document during next render task
210
+ setTimeout(() => {
211
+ this.openPrintDialog();
212
+ // @ts-expect-error TS()
213
+ printPromise.resolve();
214
+ }, FRAMES_PER_SECOND);
215
+ });
216
+ return printPromise;
217
+ };
218
+ this.onDocumentWheel = (e) => {
219
+ if (!e.ctrlKey && !e.metaKey) {
220
+ return;
221
+ }
222
+ e.preventDefault();
223
+ const wheelDelta = (0, main_1.mousewheelDelta)(e);
224
+ const zoomModifier = wheelDelta < 0 ? 1 : -1;
225
+ const zoomLevel = this.state.zoomLevel + (zoomModifier * this.options.zoomRate);
226
+ this.triggerZoomStart({
227
+ zoomLevel: zoomLevel
228
+ });
229
+ this.zoom({
230
+ zoomLevel: zoomLevel
231
+ });
232
+ this.triggerZoomEnd({
233
+ zoomLevel: zoomLevel
234
+ });
235
+ };
236
+ this.extendOptions(options);
237
+ this.throwIfInvalidOptions();
238
+ this.wrapper = this.element;
239
+ this.initEventBus();
240
+ tslib_1.__classPrivateFieldSet(this, _PdfViewer_eventAbortController, new AbortController(), "f");
241
+ this.resetState();
242
+ this.bindEvents();
243
+ this.initLinkService();
244
+ this.initDocumentContainer();
245
+ this.loadFile(this.options.fileDescriptor || {
246
+ data: this.options.fileData,
247
+ page: this.options.page
248
+ });
249
+ }
250
+ destroy() {
251
+ this.destroyEventBus();
252
+ this.unbindEvents();
253
+ this.destroySearchService();
254
+ this.destroyAnnotationEditorUIManager();
255
+ this.destroyDocumentScroller();
256
+ }
257
+ throwIfInvalidOptions() {
258
+ if (this.options.minZoom > this.options.maxZoom) {
259
+ throw new Error("The min zoom should be greater than max zoom");
260
+ }
261
+ }
262
+ set annotationEditorParams({ type, value }) {
263
+ if (!this.annotationEditorUIManager) {
264
+ return;
265
+ }
266
+ this.annotationEditorUIManager.updateParams(type, value);
267
+ }
268
+ get annotationEditorMode() {
269
+ return this.annotationEditorUIManager
270
+ ? tslib_1.__classPrivateFieldGet(this, _PdfViewer_annotationEditorMode, "f")
271
+ : pdf_mjs_1.AnnotationEditorType.DISABLE;
272
+ }
273
+ // @ts-expect-error(TS)
274
+ set annotationEditorMode({ mode, editId = null, isFromKeyboard = false }) {
275
+ // todo: set isFromKeyboard intentionally to allow editor creation
276
+ // set annotationEditorMode({ mode, editId = null, isFromKeyboard = true }) {
277
+ if (!this.annotationEditorUIManager) {
278
+ // throw new Error(`The AnnotationEditor is not enabled.`);
279
+ }
280
+ if (tslib_1.__classPrivateFieldGet(this, _PdfViewer_annotationEditorMode, "f") === mode) {
281
+ return; // The AnnotationEditor mode didn't change.
282
+ }
283
+ if (!this.isValidAnnotationEditorMode(mode)) {
284
+ // throw new Error(`Invalid AnnotationEditor mode: ${mode}`);
285
+ return;
286
+ }
287
+ if (!this.pdfDocument) {
288
+ return;
289
+ }
290
+ if (mode === pdf_mjs_1.AnnotationEditorType.STAMP) {
291
+ // this.#mlManager?.loadModel("altText");
292
+ }
293
+ const { eventBus } = this;
294
+ const updater = () => {
295
+ this.cleanupSwitchAnnotationEditorMode();
296
+ tslib_1.__classPrivateFieldSet(this, _PdfViewer_annotationEditorMode, mode, "f");
297
+ this.annotationEditorUIManager.updateMode(mode, editId, isFromKeyboard);
298
+ eventBus.dispatch("annotationeditormodechanged", {
299
+ source: this,
300
+ mode,
301
+ });
302
+ };
303
+ // // todo: fix
304
+ // const isEditingPage = (this.pages || []).some(x => x.isEditing);
305
+ if (mode === pdf_mjs_1.AnnotationEditorType.NONE || tslib_1.__classPrivateFieldGet(this, _PdfViewer_annotationEditorMode, "f") === pdf_mjs_1.AnnotationEditorType.NONE) {
306
+ const isEditing = mode !== pdf_mjs_1.AnnotationEditorType.NONE;
307
+ if (!isEditing) {
308
+ this.pdfDocument.annotationStorage.resetModifiedIds();
309
+ }
310
+ for (const pageView of this.pages) {
311
+ pageView.toggleEditingMode(isEditing);
312
+ }
313
+ // We must call #switchToEditAnnotationMode unconditionally to ensure that
314
+ // page is rendered if it's useful or not.
315
+ const idsToRefresh = tslib_1.__classPrivateFieldGet(this, _PdfViewer_instances, "m", _PdfViewer_switchToEditAnnotationMode).call(this);
316
+ if (isEditing && idsToRefresh) {
317
+ // We're editing so we must switch to editing mode when the rendering is
318
+ // done.
319
+ this.cleanupSwitchAnnotationEditorMode();
320
+ tslib_1.__classPrivateFieldSet(this, _PdfViewer_onPageRenderedCallback, ({ pageNumber }) => {
321
+ idsToRefresh.delete(pageNumber);
322
+ if (idsToRefresh.size === 0) {
323
+ tslib_1.__classPrivateFieldSet(this, _PdfViewer_switchAnnotationEditorModeTimeoutId, setTimeout(updater, 0), "f");
324
+ }
325
+ }, "f");
326
+ const { signal } = tslib_1.__classPrivateFieldGet(this, _PdfViewer_eventAbortController, "f");
327
+ eventBus._on("pagerendered", tslib_1.__classPrivateFieldGet(this, _PdfViewer_onPageRenderedCallback, "f"), { signal });
328
+ }
329
+ }
330
+ updater();
331
+ }
332
+ extendOptions(options) {
333
+ this.options = (0, main_1.deepExtend)(this.options, options);
334
+ }
335
+ setOptions(options) {
336
+ this.options = options;
337
+ }
338
+ bindEvents() {
339
+ this.bindPagesWheel();
340
+ }
341
+ bindPagesWheel() {
342
+ const documentContainer = this.getDocumentContainer();
343
+ if (!documentContainer) {
344
+ return;
345
+ }
346
+ if (this.options.pageWheelThrottleDelay > 0) {
347
+ // @ts-expect-error TS(2339): Property 'throttledPagesWheel' does not exist on t... Remove this comment to see the full error message
348
+ this.throttledPagesWheel = (0, scroller_2.throttle)(this.onDocumentWheel, this.options.pageWheelThrottleDelay);
349
+ }
350
+ else {
351
+ // @ts-expect-error TS(2339): Property 'throttledPagesWheel' does not exist on t... Remove this comment to see the full error message
352
+ this.throttledPagesWheel = this.onDocumentWheel;
353
+ }
354
+ // @ts-expect-error TS(2339): Property 'throttledPagesWheel' does not exist on t... Remove this comment to see the full error message
355
+ documentContainer.addEventListener("wheel", this.throttledPagesWheel);
356
+ }
357
+ unbindEvents() {
358
+ this.unbindPagesWheel();
359
+ }
360
+ unbindPagesWheel() {
361
+ const documentContainer = this.getDocumentContainer();
362
+ if (!documentContainer) {
363
+ return;
364
+ }
365
+ // @ts-expect-error TS(2339): Property 'throttledPagesWheel' does not exist on t... Remove this comment to see the full error message
366
+ if (this.throttledPagesWheel && this.throttledPagesWheel.cancel) {
367
+ // @ts-expect-error TS(2339): Property 'throttledPagesWheel' does not exist on t... Remove this comment to see the full error message
368
+ this.throttledPagesWheel.cancel();
369
+ }
370
+ // @ts-expect-error TS(2339): Property 'throttledPagesWheel' does not exist on t... Remove this comment to see the full error message
371
+ documentContainer.removeEventListener("wheel", this.throttledPagesWheel);
372
+ }
373
+ setState(newState) {
374
+ this.state = (0, main_1.deepExtend)(this.state || {}, newState);
375
+ }
376
+ resetState() {
377
+ this.setState({
378
+ activePageNumber: 1,
379
+ visiblePagesCount: 1,
380
+ pagePromises: [],
381
+ downloadFilePromise: null,
382
+ pdfDocument: null,
383
+ zoomLevel: this.options.zoomLevel,
384
+ zoomLevelType: this.options.zoomLevelType,
385
+ zoomLevelBeforePrint: null
386
+ });
387
+ }
388
+ initEventBus() {
389
+ this.eventBus = new event_utils_1.EventBus();
390
+ }
391
+ destroyEventBus() {
392
+ }
393
+ initLinkService() {
394
+ var _a;
395
+ this.linkService = new link_service_1.PDFLinkService({
396
+ eventBus: this.eventBus,
397
+ externalLinkTarget: link_service_1.LinkTarget.BLANK,
398
+ externalLinkRel: "noopener noreferrer nofollow",
399
+ ignoreDestinationZoom: false,
400
+ });
401
+ (_a = this.linkService) === null || _a === void 0 ? void 0 : _a.setViewer(this);
402
+ }
403
+ initAnnotationEditorUIManager() {
404
+ this.annotationEditorUIManager = new annotation_editor_ui_manager_1.AnnotationEditorUIManager({
405
+ container: this.getDocumentContainer(),
406
+ // todo: pdf.js expects a dom element and not a component
407
+ viewer: this.element,
408
+ pdfViewer: this,
409
+ pdfDocument: this.state.pdfDocument,
410
+ eventBus: this.eventBus
411
+ });
412
+ this.setAnnotationEditorUIManagerScale();
413
+ }
414
+ setAnnotationEditorUIManagerScale() {
415
+ var _a;
416
+ // this is required when loading a new document
417
+ // so that annotation editors have correct positions during dragging
418
+ (_a = this.annotationEditorUIManager) === null || _a === void 0 ? void 0 : _a.onScaleChanging({
419
+ scale: this.options.zoomLevel
420
+ });
421
+ }
422
+ destroyAnnotationEditorUIManager() {
423
+ var _a;
424
+ (_a = this.annotationEditorUIManager) === null || _a === void 0 ? void 0 : _a.destroy();
425
+ }
426
+ bindEventBusAnnotationsEvents() {
427
+ const eventBus = this.eventBus = this.eventBus || new event_utils_1.EventBus();
428
+ eventBus._on("switchannotationeditorparams", (evt) => {
429
+ this.annotationEditorParams = evt;
430
+ }
431
+ // { signal }
432
+ );
433
+ eventBus._on("switchannotationeditormode", evt => {
434
+ // todo: inline the call to this.annotationEditorMode = evt;
435
+ // this.annotationEditorMode = evt;
436
+ this.annotationEditorUIManager.updateParams(evt.type, evt.value);
437
+ const { mode, editId, isFromKeyboard } = evt;
438
+ this.annotationEditorUIManager.annotationEditorMode = evt;
439
+ this.annotationEditorUIManager.updateMode(mode, editId, isFromKeyboard);
440
+ eventBus.dispatch("annotationeditormodechanged", {
441
+ source: this,
442
+ mode,
443
+ });
444
+ }
445
+ // { signal }
446
+ );
447
+ eventBus._on("kendoAnnotationEditorToolBarShow", (evt) => {
448
+ var _a;
449
+ if (!evt) {
450
+ return;
451
+ }
452
+ const annotationEditorMode = this.annotationEditorUIManager.getMode();
453
+ if (annotationEditorMode === pdf_mjs_1.AnnotationEditorType.HIGHLIGHT ||
454
+ annotationEditorMode === pdf_mjs_1.AnnotationEditorType.FREETEXT) {
455
+ if (annotationEditorMode === pdf_mjs_1.AnnotationEditorType.HIGHLIGHT) {
456
+ this.addHighlightAnnotation();
457
+ }
458
+ this.triggerAnnotationEditorToolBarShow(Object.assign(Object.assign({}, evt), { annotationEditorMode: annotationEditorMode === pdf_mjs_1.AnnotationEditorType.HIGHLIGHT ? "highlight" : "freeText", anchor: evt.anchor || ((_a = this.annotationEditorUIManager.firstSelectedEditor) === null || _a === void 0 ? void 0 : _a.div) }));
459
+ }
460
+ }
461
+ // { signal }
462
+ );
463
+ eventBus._on("kendoAnnotationEditorToolBarHide", (evt) => {
464
+ if (!evt) {
465
+ return;
466
+ }
467
+ const annotationEditorMode = this.annotationEditorUIManager.getMode();
468
+ if (annotationEditorMode === pdf_mjs_1.AnnotationEditorType.HIGHLIGHT ||
469
+ annotationEditorMode === pdf_mjs_1.AnnotationEditorType.FREETEXT) {
470
+ this.triggerAnnotationEditorToolBarHide(Object.assign(Object.assign({}, evt), { annotationEditorMode: annotationEditorMode === pdf_mjs_1.AnnotationEditorType.HIGHLIGHT ? "highlight" : "freeText" }));
471
+ }
472
+ }
473
+ // { signal }
474
+ );
475
+ eventBus._on("showannotationeditorui", (evt) => {
476
+ switch (evt.mode) {
477
+ case pdf_mjs_1.AnnotationEditorType.HIGHLIGHT:
478
+ // editorHighlightButton.click();
479
+ eventBus.dispatch("switchannotationeditormode", {
480
+ source: this,
481
+ mode: pdf_mjs_1.AnnotationEditorType.HIGHLIGHT,
482
+ // {},...eventDetails,
483
+ // evt.detail is the number of clicks.
484
+ // isFromKeyboard: evt.detail === 0,
485
+ isFromKeyboard: false
486
+ });
487
+ break;
488
+ default: break;
489
+ }
490
+ }
491
+ // { signal }
492
+ );
493
+ }
494
+ unbindEventBusAnnotationsEvents() {
495
+ // this.eventBus.off();
496
+ }
497
+ loadFile(args) {
498
+ const fileData = args.data;
499
+ const loadFileOptions = {
500
+ data: null,
501
+ url: "",
502
+ arrayBuffer: null,
503
+ typedArray: null
504
+ };
505
+ if (fileData && (fileData.buffer instanceof ArrayBuffer || fileData instanceof ArrayBuffer)) {
506
+ loadFileOptions.arrayBuffer = fileData.buffer || fileData;
507
+ }
508
+ else if ((0, main_1.isString)(fileData) && fileData !== "") {
509
+ loadFileOptions.data = fileData;
510
+ loadFileOptions.url = null;
511
+ }
512
+ else if ((0, main_1.isString)(args.url) && args.url !== "") {
513
+ loadFileOptions.data = null;
514
+ loadFileOptions.url = args.url;
515
+ }
516
+ else if (args.typedArray) {
517
+ loadFileOptions.typedArray = args.typedArray;
518
+ }
519
+ if (args.data || args.url || args.arrayBuffer || args.typedArray) {
520
+ const readOptions = Object.assign({ dom: this.getPagesContainer(), zoom: this.options.zoomLevel }, loadFileOptions);
521
+ this.disableDocumentScrollEventsTracking();
522
+ this.clearDocument();
523
+ this.loadDocument(readOptions);
524
+ this.setScaleFactor(this.options.zoomLevel);
525
+ }
526
+ }
527
+ loadDocument(options) {
528
+ const params = this.getDocumentParameters(options);
529
+ const { zoom } = options;
530
+ this.clearDocumentState();
531
+ (0, pdf_mjs_1.getDocument)(params)
532
+ .promise.then((pdfDoc) => {
533
+ var _a;
534
+ (_a = this.linkService) === null || _a === void 0 ? void 0 : _a.setDocument(pdfDoc);
535
+ this.initDocumentAnnotationStorage(pdfDoc);
536
+ this.initAnnotationEditorUIManager();
537
+ this.bindEventBusAnnotationsEvents();
538
+ const pagesPromises = [];
539
+ for (let i = 1; i <= pdfDoc.numPages; i++) {
540
+ pagesPromises.push(pdfDoc.getPage(i));
541
+ }
542
+ return { pagesPromises: pagesPromises, pdfDoc };
543
+ })
544
+ .then(({ pagesPromises: pagesPromises, pdfDoc }) => {
545
+ this.state.pagesPromises = pagesPromises;
546
+ this.destroyPageComponents();
547
+ return this.drawPagesFromPromisesAsync(pagesPromises)
548
+ .then((pdfPages) => {
549
+ this.setHiddenCanvasStyles();
550
+ this.onPagesLoad({ pdfPages, pdfDoc, zoom });
551
+ })
552
+ .catch((e) => {
553
+ this.triggerError(e);
554
+ });
555
+ })
556
+ .catch((e) => {
557
+ this.triggerError(e);
558
+ });
559
+ }
560
+ initDocumentAnnotationStorage(pdfDocument) {
561
+ this.state.pdfDocument = pdfDocument;
562
+ this.pdfDocument = pdfDocument;
563
+ this.annotationStorage = new annotation_storage_1.AnnotationStorage();
564
+ // todo: intentionally override the default value
565
+ // as it relies on checks like "instanceof <something>"
566
+ // but the classes from pdf.js are not exported
567
+ // and thus they are migrated, see https://github.com/mozilla/pdf.js/issues/18656
568
+ // Object.defineProperty(Object.getPrototypeOf(this.pdfDocument), 'annotationStorage', {
569
+ // get: () => {
570
+ // if (this.state.isPrintingInProgress) {
571
+ // return this.annotationStorage?.print;
572
+ // } else {
573
+ // return this.annotationStorage;
574
+ // }
575
+ // },
576
+ // set: (value) => {
577
+ // this.annotationStorage = value;
578
+ // },
579
+ // // this is required
580
+ // configurable: true
581
+ // });
582
+ Object.defineProperty(Object.getPrototypeOf(this.pdfDocument._transport), 'annotationStorage', {
583
+ get: () => {
584
+ var _a;
585
+ if (this.state.isPrintingInProgress) {
586
+ return (_a = this.annotationStorage) === null || _a === void 0 ? void 0 : _a.print;
587
+ }
588
+ else {
589
+ return this.annotationStorage;
590
+ }
591
+ },
592
+ set: (value) => {
593
+ this.annotationStorage = value;
594
+ },
595
+ // this is required
596
+ configurable: true
597
+ });
598
+ this._initializeAnnotationStorageCallbacks(pdfDocument);
599
+ }
600
+ _initializeAnnotationStorageCallbacks(pdfDocument) {
601
+ if (pdfDocument !== this.pdfDocument) {
602
+ return;
603
+ }
604
+ const { annotationStorage } = pdfDocument;
605
+ annotationStorage.onSetModified = () => {
606
+ // window.addEventListener("beforeunload", beforeUnload);
607
+ // if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
608
+ this._annotationStorageModified = true;
609
+ // }
610
+ };
611
+ annotationStorage.onResetModified = () => {
612
+ // window.removeEventListener("beforeunload", beforeUnload);
613
+ // if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
614
+ delete this._annotationStorageModified;
615
+ // }
616
+ };
617
+ annotationStorage.onAnnotationEditor = typeStr => {
618
+ this._hasAnnotationEditors = !!typeStr;
619
+ // this.setTitle();
620
+ };
621
+ }
622
+ drawPagesFromPromisesAsync(pagesPromises) {
623
+ return Promise.all(pagesPromises)
624
+ .then((pdfPages) => tslib_1.__awaiter(this, void 0, void 0, function* () {
625
+ const pages = yield Promise.all(this.drawPagesAsync(pdfPages));
626
+ this.appendPages(pages);
627
+ return pdfPages;
628
+ }));
629
+ }
630
+ drawPagesAsync(pdfPages) {
631
+ pdfPages = pdfPages || this.state.pdfPages;
632
+ const hasPages = (this.pages || []).length > 0;
633
+ if (!hasPages) {
634
+ this.initPageComponents(pdfPages);
635
+ }
636
+ else {
637
+ for (let i = 0; i < this.pages.length; i++) {
638
+ const pageViewport = this.pages[i].getViewport({
639
+ scale: this.state.zoomLevel
640
+ });
641
+ // todo: adjust page viewport manually
642
+ // otherwise, loading a document with links to the document
643
+ // will not scroll to the specific page on click
644
+ // pdf.js handles this in a more complex way
645
+ // that will require porting more code
646
+ this.pages[i].viewport = pageViewport;
647
+ }
648
+ }
649
+ const pageRenderPromises = [];
650
+ pdfPages.map((pdfPage, pageIndex) => tslib_1.__awaiter(this, void 0, void 0, function* () {
651
+ const page = this.pages[pageIndex];
652
+ if (this.options.loadOnDemand &&
653
+ pageIndex >= this.options.loadOnDemandPageSize &&
654
+ !page.element) {
655
+ const blankPageElements = this.createBlankPageElements({ pdfPage, zoom: this.state.zoomLevel });
656
+ if (page && !page.element) {
657
+ page.element = blankPageElements.pageElement;
658
+ page.setBlank(true);
659
+ }
660
+ const pageRenderPromise = (0, main_1.createPromise)();
661
+ pageRenderPromises.push(pageRenderPromise);
662
+ pageRenderPromise.then((renderedPage) => {
663
+ return renderedPage;
664
+ });
665
+ pageRenderPromise.resolve(page.element);
666
+ }
667
+ else if (this.options.loadOnDemand && page.isBlank()) {
668
+ // keep blank pages on reload
669
+ const pageRenderPromise = (0, main_1.createPromise)();
670
+ pageRenderPromises.push(pageRenderPromise);
671
+ pageRenderPromise.then((renderedPage) => {
672
+ return renderedPage;
673
+ });
674
+ pageRenderPromise.resolve(page.element);
675
+ }
676
+ else {
677
+ const pageRenderPromise = this.drawPageAsync({ pageIndex });
678
+ pageRenderPromises.push(pageRenderPromise);
679
+ pageRenderPromise.then((renderedPage) => {
680
+ return renderedPage;
681
+ });
682
+ }
683
+ }));
684
+ return pageRenderPromises;
685
+ }
686
+ createBlankPageElements({ pdfPage, zoom }) {
687
+ const pageElements = this.createPageElements({ pdfPage, zoom });
688
+ pageElements.canvasWrapper.appendChild(pageElements.canvas);
689
+ pageElements.pageElement.appendChild(pageElements.canvasWrapper);
690
+ return pageElements;
691
+ }
692
+ getDocumentParameters(options) {
693
+ let params = {
694
+ verbosity: 0,
695
+ isEvalSupported: true
696
+ };
697
+ if (typeof options.data === 'string' && options.data !== "") {
698
+ params.data = (0, utils_1.parsePdfFromBase64String)(options.data);
699
+ }
700
+ else if (typeof options.url === 'string' && options.url !== "") {
701
+ params.url = options.url;
702
+ }
703
+ else if (options.arrayBuffer instanceof ArrayBuffer) {
704
+ params = options.arrayBuffer;
705
+ }
706
+ else if (options.typedArray) {
707
+ params = options.typedArray;
708
+ }
709
+ return params;
710
+ }
711
+ appendPages(pages) {
712
+ pages.forEach(page => this.appendPage(page));
713
+ }
714
+ appendPage(page, pageIndex = null) {
715
+ const pagesContainer = this.getPagesContainer();
716
+ if ((0, main_1.hasValue)(pageIndex)) {
717
+ pagesContainer === null || pagesContainer === void 0 ? void 0 : pagesContainer.insertBefore(page, pagesContainer.children[pageIndex]);
718
+ }
719
+ else {
720
+ pagesContainer === null || pagesContainer === void 0 ? void 0 : pagesContainer.appendChild(page);
721
+ }
722
+ }
723
+ drawPageAsync({ pageIndex = 0 }) {
724
+ var _a;
725
+ const index = (0, main_1.clamp)(pageIndex, 0, Math.max(0, this.state.pdfDocument.numPages - 1));
726
+ return (_a = this.pages[index]) === null || _a === void 0 ? void 0 : _a.draw().then(() => {
727
+ return this.pages[index].element;
728
+ });
729
+ }
730
+ createPageElements({ pdfPage = null, zoom = 1, }) {
731
+ if (zoom <= 0) {
732
+ return;
733
+ }
734
+ const viewportScale = (0, utils_1.scale)();
735
+ const scaleNum = zoom || viewportScale;
736
+ const viewport = pdfPage === null || pdfPage === void 0 ? void 0 : pdfPage.getViewport({ scale: viewportScale });
737
+ const { adjustedWidth, adjustedHeight, adjustRatio } = (0, utils_1.adjustCanvasSize)(viewport.width, viewport.height);
738
+ // const styles = {
739
+ // width: Math.floor(viewport.width / scaleNum) * zoom + 'pt',
740
+ // height: Math.floor(viewport.height / scaleNum) * zoom + 'pt'
741
+ // };
742
+ // use PageView.setLayerDimensions
743
+ const { pageWidth, pageHeight } = viewport.rawDims;
744
+ const styles = {
745
+ // "round" is supported in all browsers since May 2024
746
+ // https://developer.mozilla.org/en-US/docs/Web/CSS/round#browser_compatibility
747
+ width: `round(var(--scale-factor) * ${pageWidth}px, 1px)`,
748
+ height: `round(var(--scale-factor) * ${pageHeight}px, 1px)`
749
+ };
750
+ const pageElement = (0, utils_1.createElement)("div", "k-page", styles);
751
+ const canvas = (0, utils_1.createElement)("canvas", '', {
752
+ width: "100%",
753
+ height: "100%"
754
+ });
755
+ canvas.height = adjustedHeight;
756
+ canvas.width = adjustedWidth;
757
+ const canvasContext = canvas.getContext("2d");
758
+ const canvasWrapper = (0, main_1.convertToHtml)(`
759
+ <div class="${this.options.elementSelectors.pageCanvasWrapper}"></div>
760
+ `);
761
+ // canvasWrapper.appendChild(canvas);
762
+ // pageElement.appendChild(canvasWrapper);
763
+ const adjustedScale = adjustRatio * scaleNum;
764
+ viewport.width = adjustedWidth;
765
+ viewport.height = adjustedHeight;
766
+ viewport.scale = adjustedScale;
767
+ return {
768
+ canvasContext,
769
+ canvasWrapper,
770
+ canvas,
771
+ viewport,
772
+ scaleNum,
773
+ pageElement,
774
+ styles,
775
+ };
776
+ }
777
+ triggerPagesLoad(e) {
778
+ this.trigger(PAGES_LOAD, e);
779
+ }
780
+ triggerZoomStart(e) {
781
+ this.trigger(ZOOM_START, e);
782
+ }
783
+ triggerZoomEnd(e) {
784
+ this.trigger(ZOOM_END, e);
785
+ }
786
+ triggerDownload(e) {
787
+ return this.trigger(DOWNLOAD, e);
788
+ }
789
+ triggerAnnotationEditorToolBarShow(e) {
790
+ this.trigger(ANNOTATION_EDITOR_TOOLBAR_SHOW, e);
791
+ }
792
+ triggerAnnotationEditorToolBarHide(e) {
793
+ this.trigger(ANNOTATION_EDITOR_TOOLBAR_HIDE, e);
794
+ }
795
+ clearDocument() {
796
+ var _a;
797
+ this.clearPagesElements();
798
+ this.clearDocumentState();
799
+ (_a = this.linkService) === null || _a === void 0 ? void 0 : _a.setDocument(null);
800
+ }
801
+ clearDocumentState() {
802
+ this.state.pdfDocument = null;
803
+ this.state.pdfPages = [];
804
+ this.pages.forEach(x => x.destroy());
805
+ this.pages = [];
806
+ }
807
+ initPageComponents(pdfPages) {
808
+ // const scale = this.currentScale;
809
+ // const currentScale = scale();
810
+ const currentScale = this.options.zoomLevel;
811
+ const viewport = pdfPages[0].getViewport({
812
+ scale: currentScale * pdf_mjs_1.PixelsPerInch.PDF_TO_CSS_UNITS,
813
+ });
814
+ for (let pageNum = 1; pageNum <= pdfPages.length; pageNum++) {
815
+ const pageIndex = pageNum - 1;
816
+ const pageView = new page_1.Page(null, {
817
+ pdfViewer: this,
818
+ container: this.element,
819
+ eventBus: this.eventBus,
820
+ id: pageNum,
821
+ scale: currentScale,
822
+ viewport: viewport,
823
+ defaultViewport: viewport,
824
+ // optionalContentConfigPromise,
825
+ // renderingQueue: this.renderingQueue,
826
+ textLayerMode: ui_utils_1.TextLayerMode.ENABLE,
827
+ annotationMode: utils_2.AnnotationMode.ENABLE,
828
+ // imageResourcesPath: this.imageResourcesPath,
829
+ // maxCanvasPixels: this.maxCanvasPixels,
830
+ // pageColors,
831
+ // l10n: this.l10n,
832
+ layerProperties: this._layerProperties,
833
+ // enableHWA: this.#enableHWA,
834
+ });
835
+ this.pages.push(pageView);
836
+ this.pages[pageIndex].setPdfPage(pdfPages[pageIndex]);
837
+ }
838
+ }
839
+ destroyPageComponents() {
840
+ this.pages.forEach(x => x.destroy());
841
+ this.pages = [];
842
+ }
843
+ clearPagesElements() {
844
+ const documentContainer = this.getDocumentContainer();
845
+ const pagesContainer = this.getPagesContainer();
846
+ if (!documentContainer || !pagesContainer || !this.hasDocument()) {
847
+ return;
848
+ }
849
+ const pages = this.getPagesElements();
850
+ pages.forEach(page => {
851
+ // @ts-expect-error TS(2345): Argument of type 'unknown' is not assignable to pa... Remove this comment to see the full error message
852
+ pagesContainer.removeChild(page);
853
+ });
854
+ }
855
+ initDocumentContainer() {
856
+ if (this.documentContainer) {
857
+ return;
858
+ }
859
+ this.documentContainer = this.getDocumentContainer();
860
+ this.initDocumentScroller(this.documentContainer);
861
+ }
862
+ initDocumentScroller(element) {
863
+ if (this.documentScroller || !element) {
864
+ return;
865
+ }
866
+ this.documentScroller = this.createDocumentScroller(element, {
867
+ filter: (0, main_1.toClassSelector)(this.options.elementSelectors.pageSelector),
868
+ events: {
869
+ [SCROLL]: this.onDocumentScrollerScroll
870
+ }
871
+ });
872
+ }
873
+ createDocumentScroller(element, options) {
874
+ return new scroller_1.Scroller(element, options);
875
+ }
876
+ destroyDocumentScroller() {
877
+ if (!this.documentScroller) {
878
+ return;
879
+ }
880
+ this.documentScroller.destroy();
881
+ this.documentScroller = null;
882
+ }
883
+ enableDocumentScrollEventsTracking() {
884
+ var _a;
885
+ (_a = this.documentScroller) === null || _a === void 0 ? void 0 : _a.enableScrollEventsTracking();
886
+ }
887
+ disableDocumentScrollEventsTracking() {
888
+ var _a;
889
+ (_a = this.documentScroller) === null || _a === void 0 ? void 0 : _a.disableScrollEventsTracking();
890
+ }
891
+ getCurrentPageIndex() {
892
+ return (0, utils_1.currentPage)(this.element) || 0;
893
+ }
894
+ downloadFile({ fileName, saveOptions = {} }) {
895
+ this.downloadDocument({
896
+ options: {
897
+ pdf: this.state.pdfDocument
898
+ },
899
+ fileName: fileName || this.options.downloadFileName,
900
+ saveOptions: saveOptions
901
+ });
902
+ }
903
+ downloadDocument({ options, fileName = "Document", saveOptions = {}, }) {
904
+ if (options.pdf) {
905
+ let exportMethod = options.pdf.getData.bind(options.pdf);
906
+ let serializedAnnotationStorage = {};
907
+ let originalAnnotationStorage = {};
908
+ if (options.pdf.annotationStorage.size > 0) {
909
+ exportMethod = options.pdf.saveDocument.bind(options.pdf);
910
+ // saveDocument() has checks that test "editor instanceof AnnotationEditor", but they fail
911
+ // because AnnotationEditor from "pdfjs-dist/legacy/build/pdf.mjs" is not exported
912
+ // thus replace instances of editors with their serialized version
913
+ // and then revert
914
+ serializedAnnotationStorage = {};
915
+ originalAnnotationStorage = options.pdf.annotationStorage.getAll();
916
+ Object.keys(originalAnnotationStorage).forEach((key) => {
917
+ serializedAnnotationStorage[key] = originalAnnotationStorage[key].toJSON();
918
+ });
919
+ Object.keys(originalAnnotationStorage).forEach((key) => {
920
+ options.pdf.annotationStorage.remove(key);
921
+ });
922
+ Object.keys(serializedAnnotationStorage).forEach((key) => {
923
+ options.pdf.annotationStorage.setValue(key, serializedAnnotationStorage[key]);
924
+ });
925
+ }
926
+ exportMethod()
927
+ .then((data) => {
928
+ return new Blob([data], { type: 'application/pdf' });
929
+ })
930
+ .then((blob) => {
931
+ if (!this.triggerDownload({ blob, fileName, saveOptions })) {
932
+ this.saveFile(blob, fileName, saveOptions);
933
+ }
934
+ })
935
+ .catch((e) => {
936
+ this.triggerError(e);
937
+ })
938
+ .finally(() => {
939
+ var _a;
940
+ if (((_a = options.pdf) === null || _a === void 0 ? void 0 : _a.annotationStorage.size) <= 0) {
941
+ return;
942
+ }
943
+ const currentAnnotationStorage = options.pdf.annotationStorage.getAll();
944
+ Object.keys(currentAnnotationStorage).forEach((key) => {
945
+ options.pdf.annotationStorage.remove(key);
946
+ });
947
+ Object.keys(originalAnnotationStorage).forEach((key) => {
948
+ options.pdf.annotationStorage.setValue(key, originalAnnotationStorage[key]);
949
+ });
950
+ });
951
+ }
952
+ }
953
+ ;
954
+ saveFile(blob, fileName, saveOptions) {
955
+ try {
956
+ (0, kendo_file_saver_1.saveAs)(blob, fileName, saveOptions);
957
+ }
958
+ catch (_a) {
959
+ return false;
960
+ }
961
+ return true;
962
+ }
963
+ hasDocument() {
964
+ return (0, main_1.hasValue)(this.state.pdfDocument);
965
+ }
966
+ zoom(args) {
967
+ args.zoomLevel = this.calculateZoomLevel({
968
+ zoomLevel: args.zoomLevel,
969
+ zoomLevelType: args.zoomLevelType,
970
+ currentZoom: this.options.zoomLevel,
971
+ });
972
+ this.options.zoomLevel = args.zoomLevel;
973
+ this.state.zoomLevel = args.zoomLevel;
974
+ this.eventBus.dispatch("scalechanging", {
975
+ source: this,
976
+ scale: this.options.zoomLevel,
977
+ });
978
+ this.reloadFile(this.state.pdfDocument);
979
+ }
980
+ calculateZoomLevel({ zoomLevel, zoomLevelType, currentZoom }) {
981
+ const documentContainer = this.getDocumentContainer();
982
+ const page = documentContainer.querySelector((0, main_1.toClassSelector)(this.options.elementSelectors.pageSelector));
983
+ const pageSize = { width: page.offsetWidth, height: page.offsetHeight };
984
+ let calculatedZoomLevel = (0, main_1.clamp)(zoomLevel || DEFAULT_ZOOM_LEVEL, this.options.minZoom, this.options.maxZoom);
985
+ if (zoomLevelType === "ActualWidth" || zoomLevelType === "actualWidth") {
986
+ calculatedZoomLevel = 1;
987
+ }
988
+ else if (zoomLevelType === "FitToWidth" || zoomLevelType === "fitToWidth") {
989
+ calculatedZoomLevel = documentContainer.offsetWidth / (pageSize.width / currentZoom);
990
+ }
991
+ else if (zoomLevelType === "FitToPage" || zoomLevelType === "fitToPage") {
992
+ calculatedZoomLevel = documentContainer.offsetHeight / (pageSize.height / currentZoom);
993
+ }
994
+ return calculatedZoomLevel;
995
+ }
996
+ reloadFile(pdfDoc) {
997
+ const pagesPromises = [];
998
+ for (let i = 1; i <= pdfDoc.numPages; i++) {
999
+ pagesPromises.push(pdfDoc.getPage(i));
1000
+ }
1001
+ this.disableDocumentScrollEventsTracking();
1002
+ this.drawPagesFromPromisesAsync(pagesPromises)
1003
+ .then((pdfPages) => {
1004
+ this.setHiddenCanvasStyles();
1005
+ this.onPagesLoad({ pdfPages, pdfDoc });
1006
+ })
1007
+ .catch((e) => {
1008
+ this.triggerError(e);
1009
+ });
1010
+ this.setScaleFactor(this.state.zoomLevel);
1011
+ }
1012
+ setHiddenCanvasStyles() {
1013
+ if (!this.options.pdfjsSetHiddenCanvasStyles) {
1014
+ return;
1015
+ }
1016
+ // required after pdf.js 4.x version
1017
+ const sheet = new CSSStyleSheet();
1018
+ sheet.replaceSync(`.hiddenCanvasElement {
1019
+ position: absolute;
1020
+ top: 0;
1021
+ left: 0;
1022
+ width: 0;
1023
+ height: 0;
1024
+ display: none;
1025
+ }`);
1026
+ this.getPagesContainer().ownerDocument.adoptedStyleSheets = [sheet];
1027
+ }
1028
+ printFile() {
1029
+ if (!this.hasDocument()) {
1030
+ return;
1031
+ }
1032
+ this.setState({
1033
+ isPrintingInProgress: true
1034
+ });
1035
+ if (this.options.zoomBeforePrint) {
1036
+ // such option existed in the Kendo jQuery version internally
1037
+ this.cacheZoomLevelBeforePrint();
1038
+ this.loadDocumentForPrintAsync({
1039
+ zoomLevel: this.options.zoomLevelBeforePrint,
1040
+ force: true
1041
+ })
1042
+ .then(() => {
1043
+ this.printDocumentAsync()
1044
+ .then(() => {
1045
+ this.trigger(PRINT_END);
1046
+ });
1047
+ })
1048
+ .catch(this.triggerError)
1049
+ .finally(() => {
1050
+ this.setState({
1051
+ isPrintingInProgress: false
1052
+ });
1053
+ this.restoreZoomLevelBeforePrint();
1054
+ });
1055
+ }
1056
+ else {
1057
+ this.loadDocumentForPrintAsync({
1058
+ zoomLevel: this.state.zoomLevel,
1059
+ force: true
1060
+ })
1061
+ .then(() => {
1062
+ this.printDocumentAsync()
1063
+ .then(() => {
1064
+ this.trigger(PRINT_END);
1065
+ });
1066
+ })
1067
+ .catch(this.triggerError)
1068
+ .finally(() => {
1069
+ this.setState({
1070
+ isPrintingInProgress: false
1071
+ });
1072
+ });
1073
+ }
1074
+ }
1075
+ loadDocumentForPrintAsync({ zoomLevel = DEFAULT_ZOOM_LEVEL, force = false }) {
1076
+ const pages = this.pages;
1077
+ const pageLoadPromises = [];
1078
+ const loadPromise = (0, main_1.createPromise)();
1079
+ for (let i = 0; i < pages.length; i++) {
1080
+ const pageLoadPromise = pages[i].loadForPrintAsync({
1081
+ zoomLevel: zoomLevel,
1082
+ force: force
1083
+ });
1084
+ pageLoadPromises.push(pageLoadPromise);
1085
+ }
1086
+ Promise.all(pageLoadPromises)
1087
+ .then(() => {
1088
+ // @ts-expect-error TS()
1089
+ loadPromise.resolve();
1090
+ });
1091
+ return loadPromise;
1092
+ }
1093
+ getPagesPrintContentAsync(printContainer) {
1094
+ const pages = this.pages;
1095
+ const pagePrintPromises = [];
1096
+ for (let i = 0; i < pages.length; i++) {
1097
+ const pagePrintPromise = pages[i].getPrintContentAsync();
1098
+ pagePrintPromise.then((pagePrintContent) => {
1099
+ const pageWrapper = (0, main_1.convertToHtml)(`<div></div>`);
1100
+ pageWrapper.appendChild(pagePrintContent);
1101
+ printContainer.appendChild(pageWrapper);
1102
+ });
1103
+ pagePrintPromises.push(pagePrintPromise);
1104
+ }
1105
+ return pagePrintPromises;
1106
+ }
1107
+ initPrintContainer() {
1108
+ this.printContainer = (0, main_1.convertToHtml)(`<div></div>`);
1109
+ }
1110
+ openPrintDialog() {
1111
+ const pages = this.pages;
1112
+ const width = pages[0].rawWidth;
1113
+ const height = pages[0].rawHeight;
1114
+ const printDialog = window.open('', '', 'innerWidth=' + width + ',innerHeight=' + height + 'location=no,titlebar=no,toolbar=no');
1115
+ if (!printDialog || !printDialog.document) {
1116
+ this.triggerError({
1117
+ message: this.options.messages.errorMessages.popupBlocked
1118
+ });
1119
+ return;
1120
+ }
1121
+ printDialog.document.body.style.margin = '0';
1122
+ printDialog.document.body.appendChild(this.printContainer);
1123
+ const afterPrintHandler = () => {
1124
+ printDialog.removeEventListener("afterprint", afterPrintHandler);
1125
+ printDialog.close();
1126
+ };
1127
+ printDialog.addEventListener("afterprint", afterPrintHandler);
1128
+ printDialog.focus();
1129
+ printDialog.print();
1130
+ // setTimeout(() => {
1131
+ // printDialog.print();
1132
+ // done();
1133
+ // });
1134
+ }
1135
+ cacheZoomLevelBeforePrint() {
1136
+ this.setState({
1137
+ zoomLevelBeforePrint: this.state.zoomLevel
1138
+ });
1139
+ }
1140
+ restoreZoomLevelBeforePrint() {
1141
+ if (this.state.zoomLevelBeforePrint === this.state.zoomLevel) {
1142
+ return;
1143
+ }
1144
+ this.zoom({
1145
+ zoomLevel: this.state.zoomLevelBeforePrint,
1146
+ zoomLevelType: this.state.zoomLevelType,
1147
+ currentZoom: this.state.zoomLevel
1148
+ });
1149
+ }
1150
+ searchText(options = { text: "", matchCase: false }) {
1151
+ var _a;
1152
+ this.initSearchService();
1153
+ return (_a = this.searchService) === null || _a === void 0 ? void 0 : _a.search(options);
1154
+ }
1155
+ clearSearch() {
1156
+ this.destroySearchService();
1157
+ }
1158
+ initSearchService() {
1159
+ if (this.searchService) {
1160
+ return;
1161
+ }
1162
+ const pagesTextLayers = this.wrapper.querySelectorAll((0, main_1.toClassSelector)(this.options.elementSelectors.textLayerClass));
1163
+ this.searchService = new search_1.SearchService({
1164
+ textContainers: pagesTextLayers || [],
1165
+ highlightClass: this.options.elementSelectors.searchHighlightClass,
1166
+ highlightMarkClass: this.options.elementSelectors.searchHighlightMarkClass,
1167
+ charClass: this.options.elementSelectors.charClass
1168
+ });
1169
+ }
1170
+ destroySearchService() {
1171
+ if (!this.searchService) {
1172
+ return;
1173
+ }
1174
+ this.searchService.destroy();
1175
+ this.searchService = null;
1176
+ }
1177
+ getPagesContainer() {
1178
+ return this.element.querySelector((0, main_1.toClassSelector)(this.options.elementSelectors.pagesContainerClass));
1179
+ }
1180
+ getDocumentContainer() {
1181
+ return this.wrapper.querySelector((0, main_1.toClassSelector)(this.options.elementSelectors.documentContainerClass));
1182
+ }
1183
+ getPagesElements() {
1184
+ const documentContainer = this.getDocumentContainer();
1185
+ if (!documentContainer) {
1186
+ return [];
1187
+ }
1188
+ return (0, main_1.toArray)(documentContainer.querySelectorAll((0, main_1.toClassSelector)(this.options.elementSelectors.pageSelector)));
1189
+ }
1190
+ goToNextSearchMatch() {
1191
+ if (!this.searchService) {
1192
+ return;
1193
+ }
1194
+ this.searchService.markNextMatch();
1195
+ const matchElement = this.searchService.getActiveMatchElement();
1196
+ this.scrollToSearchMatch(matchElement);
1197
+ }
1198
+ goToPreviousSearchMatch() {
1199
+ if (!this.searchService) {
1200
+ return;
1201
+ }
1202
+ this.searchService.markPreviousMatch();
1203
+ const matchElement = this.searchService.getActiveMatchElement();
1204
+ this.scrollToSearchMatch(matchElement);
1205
+ }
1206
+ scrollToSearchMatch(matchElement) {
1207
+ if (!matchElement) {
1208
+ return;
1209
+ }
1210
+ const closestCharElement = matchElement.closest('.k-text-char');
1211
+ const closestTextElement = closestCharElement
1212
+ ? closestCharElement.closest('span[role="presentation"]')
1213
+ : null;
1214
+ if (!closestTextElement) {
1215
+ return;
1216
+ }
1217
+ const closestPageElement = closestTextElement.closest('.k-page');
1218
+ if (!closestPageElement) {
1219
+ return;
1220
+ }
1221
+ const scrollLeft = closestPageElement.offsetLeft +
1222
+ -1 * this.documentScroller.element.offsetLeft +
1223
+ closestTextElement.offsetLeft +
1224
+ this.options.searchMatchScrollLeftOffset;
1225
+ const scrollTop = closestPageElement.offsetTop +
1226
+ -1 * this.documentScroller.element.offsetTop +
1227
+ closestTextElement.offsetTop +
1228
+ this.options.searchMatchScrollTopOffset;
1229
+ this.documentScroller.scrollTo(scrollLeft, scrollTop, { trackScrollEvent: false });
1230
+ }
1231
+ resetInteractionMode() {
1232
+ this.setInteractionMode({
1233
+ mode: PdfViewerInteractionMode_1.PdfViewerInteractionMode.TextSelection
1234
+ });
1235
+ }
1236
+ setInteractionMode(args) {
1237
+ this.interactionMode = args.mode;
1238
+ if (this.interactionMode === PdfViewerInteractionMode_1.PdfViewerInteractionMode.TextSelection) {
1239
+ this.disableScrollerEventsTracking();
1240
+ (0, main_1.addClass)(this.options.elementSelectors.enabledTextSelectionClass, this.documentScroller.element);
1241
+ (0, main_1.removeClass)(this.options.elementSelectors.enabledPanningClass, this.documentScroller.element);
1242
+ }
1243
+ else if (this.interactionMode === PdfViewerInteractionMode_1.PdfViewerInteractionMode.Pan) {
1244
+ this.enableScrollerEventsTracking();
1245
+ (0, main_1.removeClass)(this.options.elementSelectors.enabledTextSelectionClass, this.documentScroller.element);
1246
+ (0, main_1.addClass)(this.options.elementSelectors.enabledPanningClass, this.documentScroller.element);
1247
+ }
1248
+ }
1249
+ enableScrollerEventsTracking() {
1250
+ if (!this.documentScroller) {
1251
+ return;
1252
+ }
1253
+ this.documentScroller.enablePanEventsTracking();
1254
+ }
1255
+ disableScrollerEventsTracking() {
1256
+ if (!this.documentScroller) {
1257
+ return;
1258
+ }
1259
+ this.documentScroller.disablePanEventsTracking();
1260
+ }
1261
+ setScaleFactor(scaleFactor) {
1262
+ this.element.style.setProperty('--scale-factor', String(scaleFactor * pdf_mjs_1.PixelsPerInch.PDF_TO_CSS_UNITS));
1263
+ }
1264
+ activatePageNumber(pageNumber) {
1265
+ const page = this.getPageByNumber(pageNumber);
1266
+ if (!page) {
1267
+ return;
1268
+ }
1269
+ const pageIndex = pageNumber - 1;
1270
+ if (this.state.activePageNumber === pageNumber) {
1271
+ return;
1272
+ }
1273
+ this.state.activePageNumber = pageNumber;
1274
+ if (page.isBlank()) {
1275
+ this.drawPageAsync({ pageIndex: pageIndex })
1276
+ .then(() => {
1277
+ this.scrollToPage({
1278
+ pageNumber
1279
+ });
1280
+ });
1281
+ }
1282
+ else {
1283
+ this.scrollToPage({
1284
+ pageNumber
1285
+ });
1286
+ }
1287
+ }
1288
+ scrollToPage({ pageNumber }) {
1289
+ const { left, top } = this.getPageNumberScrollOffset(pageNumber);
1290
+ this.scrollDocument({
1291
+ left: left,
1292
+ top: top
1293
+ });
1294
+ }
1295
+ scrollDocument({ left, top }) {
1296
+ var _a;
1297
+ (_a = this.documentScroller) === null || _a === void 0 ? void 0 : _a.scrollTo(left, top, {
1298
+ // as we are manually scrolling to the page
1299
+ // we want to ignore the scroller's element scroll event
1300
+ // to avoid double handling of the scrolling
1301
+ trackScrollEvent: false
1302
+ });
1303
+ }
1304
+ getPageNumberScrollOffset(pageNumber) {
1305
+ const page = this.getPageByNumber(pageNumber);
1306
+ if (!page) {
1307
+ return {
1308
+ left: 0,
1309
+ top: 0
1310
+ };
1311
+ }
1312
+ // pages might have different sizes
1313
+ // although pdf.js does not fully support this
1314
+ const previousPages = this.pages.slice(0, Math.max(0, pageNumber - 1));
1315
+ const previousPagesScrollOffset = previousPages
1316
+ .map(x => {
1317
+ const pageScrollOffset = this.getPageScrollOffset(x);
1318
+ return pageScrollOffset;
1319
+ })
1320
+ .reduce((sum, pageScrollOffset) => sum + pageScrollOffset, 0);
1321
+ return {
1322
+ left: 0,
1323
+ top: previousPagesScrollOffset
1324
+ };
1325
+ }
1326
+ getPageScrollOffset(page) {
1327
+ var _a;
1328
+ const pageHeight = ((_a = page === null || page === void 0 ? void 0 : page.element) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect().height) || 0;
1329
+ const computedStyles = getComputedStyle(page.element);
1330
+ const pageMarginTop = parseFloat(computedStyles.marginTop) || 0;
1331
+ return pageHeight + pageMarginTop;
1332
+ }
1333
+ getPageByNumber(pageNumber) {
1334
+ const page = this.pages && this.pages[pageNumber - 1];
1335
+ return page;
1336
+ }
1337
+ addHighlightAnnotation() {
1338
+ var _a;
1339
+ (_a = this.annotationEditorUIManager) === null || _a === void 0 ? void 0 : _a.highlightSelection();
1340
+ }
1341
+ setHighlightColor(color) {
1342
+ this.executeAnnotationCommand({
1343
+ commandType: pdf_mjs_1.AnnotationEditorParamsType.HIGHLIGHT_COLOR,
1344
+ commandValue: color
1345
+ });
1346
+ }
1347
+ deleteAnnotation() {
1348
+ var _a;
1349
+ (_a = this.annotationEditorUIManager) === null || _a === void 0 ? void 0 : _a.delete();
1350
+ }
1351
+ resetAnnotationEditorMode() {
1352
+ this.setAnnotationEditorMode({
1353
+ mode: pdf_mjs_1.AnnotationEditorType.NONE,
1354
+ editId: null,
1355
+ isFromKeyboard: false
1356
+ });
1357
+ }
1358
+ setAnnotationEditorMode(modeDescriptor) {
1359
+ this.annotationEditorMode = modeDescriptor;
1360
+ }
1361
+ setFreeTextColor(color) {
1362
+ this.executeAnnotationCommand({
1363
+ commandType: pdf_mjs_1.AnnotationEditorParamsType.FREETEXT_COLOR,
1364
+ commandValue: color
1365
+ });
1366
+ }
1367
+ setFreeTextFontSize(fontSize) {
1368
+ this.executeAnnotationCommand({
1369
+ commandType: pdf_mjs_1.AnnotationEditorParamsType.FREETEXT_SIZE,
1370
+ commandValue: parseFloat(fontSize)
1371
+ });
1372
+ }
1373
+ executeAnnotationCommand({ commandType, commandValue }) {
1374
+ this.eventBus.dispatch("switchannotationeditorparams", {
1375
+ source: this,
1376
+ type: commandType,
1377
+ value: commandValue
1378
+ });
1379
+ }
1380
+ // todo: ported from pdf.js
1381
+ isValidAnnotationEditorMode(mode) {
1382
+ return Object.values(pdf_mjs_1.AnnotationEditorType).includes(mode) && mode !== pdf_mjs_1.AnnotationEditorType.DISABLE;
1383
+ }
1384
+ cleanupSwitchAnnotationEditorMode() {
1385
+ if (tslib_1.__classPrivateFieldGet(this, _PdfViewer_onPageRenderedCallback, "f")) {
1386
+ this.eventBus._off("pagerendered", tslib_1.__classPrivateFieldGet(this, _PdfViewer_onPageRenderedCallback, "f"));
1387
+ tslib_1.__classPrivateFieldSet(this, _PdfViewer_onPageRenderedCallback, null, "f");
1388
+ }
1389
+ if (tslib_1.__classPrivateFieldGet(this, _PdfViewer_switchAnnotationEditorModeTimeoutId, "f") !== null) {
1390
+ clearTimeout(tslib_1.__classPrivateFieldGet(this, _PdfViewer_switchAnnotationEditorModeTimeoutId, "f"));
1391
+ tslib_1.__classPrivateFieldSet(this, _PdfViewer_switchAnnotationEditorModeTimeoutId, null, "f");
1392
+ }
1393
+ }
1394
+ _getVisiblePages() {
1395
+ // const views =
1396
+ // this._scrollMode === ScrollMode.PAGE
1397
+ // ? this.#scrollModePageState.pages
1398
+ // : this._pages,
1399
+ // horizontal = this._scrollMode === ScrollMode.HORIZONTAL,
1400
+ // rtl = horizontal && this._isContainerRtl;
1401
+ // return getVisibleElements({
1402
+ // scrollEl: this.container,
1403
+ // views,
1404
+ // sortByVisibility: true,
1405
+ // horizontal,
1406
+ // rtl,
1407
+ // });
1408
+ return this.pages;
1409
+ }
1410
+ get _layerProperties() {
1411
+ const self = this;
1412
+ return (0, pdf_mjs_1.shadow)(this, "_layerProperties", {
1413
+ get annotationEditorUIManager() {
1414
+ return self.annotationEditorUIManager;
1415
+ },
1416
+ get annotationStorage() {
1417
+ var _a;
1418
+ return (_a = self.pdfDocument) === null || _a === void 0 ? void 0 : _a.annotationStorage;
1419
+ },
1420
+ // get downloadManager() {
1421
+ // return self.downloadManager;
1422
+ // },
1423
+ // get enableScripting() {
1424
+ // return !!self._scriptingManager;
1425
+ // },
1426
+ // get fieldObjectsPromise() {
1427
+ // return self.pdfDocument?.getFieldObjects();
1428
+ // },
1429
+ // get findController() {
1430
+ // return self.findController;
1431
+ // },
1432
+ // get hasJSActionsPromise() {
1433
+ // return self.pdfDocument?.hasJSActions();
1434
+ // },
1435
+ get linkService() {
1436
+ return self.linkService;
1437
+ },
1438
+ });
1439
+ }
1440
+ /**
1441
+ * Scrolls page into view.
1442
+ * @param {ScrollPageIntoViewParameters} params
1443
+ */
1444
+ scrollPageIntoView({ pageNumber, destArray = null, allowNegativeOffset = false, ignoreDestinationZoom = false, }) {
1445
+ if (!this.pdfDocument) {
1446
+ return;
1447
+ }
1448
+ const pageView = Number.isInteger(pageNumber) && this.pages[pageNumber - 1];
1449
+ if (!pageView) {
1450
+ // console.error(
1451
+ // `scrollPageIntoView: "${pageNumber}" is not a valid pageNumber parameter.`
1452
+ // );
1453
+ return;
1454
+ }
1455
+ if (this.isInPresentationMode || !destArray) {
1456
+ this._setCurrentPageNumber(pageNumber, /* resetCurrentPageView = */ true);
1457
+ return;
1458
+ }
1459
+ let x = 0, y = 0;
1460
+ const width = 0, height = 0;
1461
+ // widthScale,
1462
+ // heightScale;
1463
+ const changeOrientation = pageView.rotation % 180 !== 0;
1464
+ // const pageWidth =
1465
+ // (changeOrientation ? pageView.height : pageView.width) /
1466
+ // pageView.scale /
1467
+ // PixelsPerInch.PDF_TO_CSS_UNITS;
1468
+ const pageHeight = (changeOrientation ? pageView.rawWidth : pageView.rawHeight) /
1469
+ pageView.scale /
1470
+ pdf_mjs_1.PixelsPerInch.PDF_TO_CSS_UNITS;
1471
+ // let scale: any = 0;
1472
+ switch (destArray[1].name) {
1473
+ case "XYZ":
1474
+ x = destArray[2];
1475
+ y = destArray[3];
1476
+ // scale = destArray[4];
1477
+ // If x and/or y coordinates are not supplied, default to
1478
+ // _top_ left of the page (not the obvious bottom left,
1479
+ // since aligning the bottom of the intended page with the
1480
+ // top of the window is rarely helpful).
1481
+ x = x !== null ? x : 0;
1482
+ y = y !== null ? y : pageHeight;
1483
+ break;
1484
+ // case "Fit":
1485
+ // case "FitB":
1486
+ // scale = "page-fit";
1487
+ // break;
1488
+ // case "FitH":
1489
+ // case "FitBH":
1490
+ // y = destArray[2];
1491
+ // scale = "page-width";
1492
+ // // According to the PDF spec, section 12.3.2.2, a `null` value in the
1493
+ // // parameter should maintain the position relative to the new page.
1494
+ // if (y === null && this._location) {
1495
+ // x = this._location.left;
1496
+ // y = this._location.top;
1497
+ // } else if (typeof y !== "number" || y < 0) {
1498
+ // // The "top" value isn't optional, according to the spec, however some
1499
+ // // bad PDF generators will pretend that it is (fixes bug 1663390).
1500
+ // y = pageHeight;
1501
+ // }
1502
+ // break;
1503
+ // case "FitV":
1504
+ // case "FitBV":
1505
+ // x = destArray[2];
1506
+ // width = pageWidth;
1507
+ // height = pageHeight;
1508
+ // scale = "page-height";
1509
+ // break;
1510
+ // case "FitR":
1511
+ // x = destArray[2];
1512
+ // y = destArray[3];
1513
+ // width = destArray[4] - x;
1514
+ // height = destArray[5] - y;
1515
+ // let hPadding = SCROLLBAR_PADDING,
1516
+ // vPadding = VERTICAL_PADDING;
1517
+ // if (
1518
+ // (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
1519
+ // this.removePageBorders
1520
+ // ) {
1521
+ // hPadding = vPadding = 0;
1522
+ // }
1523
+ // widthScale =
1524
+ // (this.container.clientWidth - hPadding) /
1525
+ // width /
1526
+ // PixelsPerInch.PDF_TO_CSS_UNITS;
1527
+ // heightScale =
1528
+ // (this.container.clientHeight - vPadding) /
1529
+ // height /
1530
+ // PixelsPerInch.PDF_TO_CSS_UNITS;
1531
+ // scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
1532
+ // break;
1533
+ default:
1534
+ // console.error(
1535
+ // `scrollPageIntoView: "${destArray[1].name}" is not a valid destination type.`
1536
+ // );
1537
+ return;
1538
+ }
1539
+ if (!ignoreDestinationZoom) {
1540
+ // if (scale && scale !== this.options.zoomLevel) {
1541
+ // }
1542
+ // if (scale && scale !== this._currentScale) {
1543
+ // this.currentScaleValue = scale;
1544
+ // } else if (this._currentScale === UNKNOWN_SCALE) {
1545
+ // this.currentScaleValue = DEFAULT_SCALE_VALUE;
1546
+ // }
1547
+ }
1548
+ // if (scale === "page-fit" && !destArray[4]) {
1549
+ // this.#scrollIntoView(pageView);
1550
+ // return;
1551
+ // }
1552
+ const boundingRect = [
1553
+ pageView.viewport.convertToViewportPoint(x, y),
1554
+ pageView.viewport.convertToViewportPoint(x + width, y + height),
1555
+ ];
1556
+ let left = Math.min(boundingRect[0][0], boundingRect[1][0]);
1557
+ let top = Math.min(boundingRect[0][1], boundingRect[1][1]);
1558
+ if (!allowNegativeOffset) {
1559
+ // Some bad PDF generators will create destinations with e.g. top values
1560
+ // that exceeds the page height. Ensure that offsets are not negative,
1561
+ // to prevent a previous page from becoming visible (fixes bug 874482).
1562
+ left = Math.max(left, 0);
1563
+ top = Math.max(top, 0);
1564
+ }
1565
+ // left *= scale || this.state.zoomLevel;
1566
+ // top *= scale || this.state.zoomLevel;
1567
+ tslib_1.__classPrivateFieldGet(this, _PdfViewer_instances, "m", _PdfViewer_scrollIntoView).call(this, pageView, /* pageSpot = */ { left, top });
1568
+ }
1569
+ /**
1570
+ * @returns {boolean} Whether the pageNumber is valid (within bounds).
1571
+ * @private
1572
+ */
1573
+ _setCurrentPageNumber(val, resetCurrentPageView = false) {
1574
+ if (this._currentPageNumber === val) {
1575
+ if (resetCurrentPageView) {
1576
+ // this.#resetCurrentPageView();
1577
+ }
1578
+ return true;
1579
+ }
1580
+ // if (!(0 < val && val <= this.pagesCount)) {
1581
+ if (!(0 < val && val <= this.state.pdfDocument.numPages)) {
1582
+ return false;
1583
+ }
1584
+ const previous = this._currentPageNumber;
1585
+ this._currentPageNumber = val;
1586
+ this.eventBus.dispatch("pagechanging", {
1587
+ source: this,
1588
+ pageNumber: val,
1589
+ // pageLabel: this._pageLabels?.[val - 1] ?? null,
1590
+ previous,
1591
+ });
1592
+ // if (resetCurrentPageView) {
1593
+ // this.#resetCurrentPageView();
1594
+ // }
1595
+ return true;
1596
+ }
1597
+ }
1598
+ exports.PdfViewer = PdfViewer;
1599
+ _PdfViewer_annotationEditorMode = new WeakMap(), _PdfViewer_switchAnnotationEditorModeTimeoutId = new WeakMap(), _PdfViewer_eventAbortController = new WeakMap(), _PdfViewer_onPageRenderedCallback = new WeakMap(), _PdfViewer_instances = new WeakSet(), _PdfViewer_switchToEditAnnotationMode = function _PdfViewer_switchToEditAnnotationMode() {
1600
+ // const visible = this._getVisiblePages();
1601
+ // const visible = this._getVisiblePages();
1602
+ const pagesToRefresh = [];
1603
+ // const { ids, views } = visible;
1604
+ // for (const page of views) {
1605
+ const ids = new Set();
1606
+ this.pages.forEach(x => {
1607
+ ids.add(x.id);
1608
+ });
1609
+ for (const page of this.pages) {
1610
+ // const { view } = page;
1611
+ const view = page;
1612
+ if (!view.hasEditableAnnotations()) {
1613
+ ids.delete(view.id);
1614
+ continue;
1615
+ }
1616
+ pagesToRefresh.push(page);
1617
+ }
1618
+ if (pagesToRefresh.length === 0) {
1619
+ return null;
1620
+ }
1621
+ // this.renderingQueue.renderHighestPriority({
1622
+ // first: pagesToRefresh[0],
1623
+ // last: pagesToRefresh.at(-1),
1624
+ // views: pagesToRefresh,
1625
+ // ids,
1626
+ // });
1627
+ // todo: render pages instead of using renderingQueue
1628
+ // as this will require porting more code
1629
+ this.drawPagesAsync(this.state.pdfPages);
1630
+ return ids;
1631
+ }, _PdfViewer_scrollIntoView = function _PdfViewer_scrollIntoView(pageView, pageSpot = null) {
1632
+ const { div, id } = pageView;
1633
+ // Ensure that `this._currentPageNumber` is correct, when `#scrollIntoView`
1634
+ // is called directly (and not from `#resetCurrentPageView`).
1635
+ if (this._currentPageNumber !== id) {
1636
+ this._setCurrentPageNumber(id);
1637
+ }
1638
+ // if (this._scrollMode === ScrollMode.PAGE) {
1639
+ // this.#ensurePageViewVisible();
1640
+ // // Ensure that rendering always occurs, to avoid showing a blank page,
1641
+ // // even if the current position doesn't change when the page is scrolled.
1642
+ // this.update();
1643
+ // }
1644
+ if (!pageSpot && !this.isInPresentationMode) {
1645
+ const left = div.offsetLeft + div.clientLeft, right = left + div.clientWidth;
1646
+ // const { scrollLeft, clientWidth } = this.container;
1647
+ const { scrollLeft, clientWidth } = this.element;
1648
+ if (this._scrollMode === ui_utils_1.ScrollMode.HORIZONTAL ||
1649
+ left < scrollLeft ||
1650
+ right > scrollLeft + clientWidth) {
1651
+ pageSpot = { left: 0, top: 0 };
1652
+ }
1653
+ }
1654
+ (0, ui_utils_1.scrollIntoView)(div, pageSpot);
1655
+ // // Ensure that the correct *initial* document position is set, when any
1656
+ // // OpenParameters are used, for documents with non-default Scroll/Spread
1657
+ // // modes (fixes issue 15695). This is necessary since the scroll-handler
1658
+ // // invokes the `update`-method asynchronously, and `this._location` could
1659
+ // // thus be wrong when the initial zooming occurs in the default viewer.
1660
+ // if (!this._currentScaleValue && this._location) {
1661
+ // this._location = null;
1662
+ // }
1663
+ };