@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,783 @@
1
+ /* Copyright 2022 Mozilla Foundation
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+ var _FreeTextEditor_instances, _a, _FreeTextEditor_color, _FreeTextEditor_content, _FreeTextEditor_editorDivId, _FreeTextEditor_editModeAC, _FreeTextEditor_fontSize, _FreeTextEditor_initialData, _FreeTextEditor_updateFontSize, _FreeTextEditor_updateColor, _FreeTextEditor_extractText, _FreeTextEditor_setEditorDimensions, _FreeTextEditor_getNodeContent, _FreeTextEditor_setContent, _FreeTextEditor_serializeContent, _FreeTextEditor_deserializeContent, _FreeTextEditor_hasElementChanged;
16
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
17
+ // eslint-disable-next-line max-len
18
+ /** @typedef {import("./annotation_editor_layer.js").AnnotationEditorLayer} AnnotationEditorLayer */
19
+ import { AnnotationEditorParamsType, AnnotationEditorType, Util, } from "pdfjs-dist/legacy/build/pdf.mjs";
20
+ // import {
21
+ // AnnotationEditorUIManager,
22
+ // bindEvents,
23
+ // KeyboardManager,
24
+ // } from "./tools.js";
25
+ import { AnnotationEditor } from "./annotation-editor";
26
+ import { bindEvents } from "../helpers/tools";
27
+ import { FreeTextAnnotationElement } from "../annotation-layer";
28
+ // import { FreeTextAnnotationElement } from "../annotation-layer";
29
+ const EOL_PATTERN = /\r\n?|\n/g;
30
+ class FreeTextEditor extends AnnotationEditor {
31
+ constructor(params) {
32
+ super(Object.assign(Object.assign({}, params), { name: "k-free-text-editor freeTextEditor" }));
33
+ _FreeTextEditor_instances.add(this);
34
+ // todo: props
35
+ this.editorDiv = null;
36
+ this.overlayDiv = null;
37
+ // todo: props
38
+ _FreeTextEditor_color.set(this, void 0);
39
+ _FreeTextEditor_content.set(this, "");
40
+ _FreeTextEditor_editorDivId.set(this, `${this.id}-editor`);
41
+ _FreeTextEditor_editModeAC.set(this, null);
42
+ _FreeTextEditor_fontSize.set(this, void 0);
43
+ _FreeTextEditor_initialData.set(this, null);
44
+ __classPrivateFieldSet(this, _FreeTextEditor_color, params.color ||
45
+ _a._defaultColor ||
46
+ AnnotationEditor._defaultLineColor, "f");
47
+ __classPrivateFieldSet(this, _FreeTextEditor_fontSize, params.fontSize || _a._defaultFontSize, "f");
48
+ }
49
+ /** @inheritdoc */
50
+ static initialize(l10n, uiManager) {
51
+ AnnotationEditor.initialize(l10n, uiManager, {
52
+ strings: ["pdfjs-free-text-default-content"],
53
+ });
54
+ const style = getComputedStyle(document.documentElement);
55
+ // if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
56
+ // const lineHeight = parseFloat(
57
+ // style.getPropertyValue("--freetext-line-height")
58
+ // );
59
+ // assert(
60
+ // lineHeight === LINE_FACTOR,
61
+ // "Update the CSS variable to agree with the constant."
62
+ // );
63
+ // }
64
+ // todo: read the variable
65
+ this._internalPadding = parseFloat(style.getPropertyValue("--freetext-padding")) || 2;
66
+ }
67
+ /** @inheritdoc */
68
+ static updateDefaultParams(type, value) {
69
+ switch (type) {
70
+ case AnnotationEditorParamsType.FREETEXT_SIZE:
71
+ _a._defaultFontSize = value;
72
+ break;
73
+ case AnnotationEditorParamsType.FREETEXT_COLOR:
74
+ _a._defaultColor = value;
75
+ break;
76
+ default: break;
77
+ }
78
+ }
79
+ /** @inheritdoc */
80
+ updateParams(type, value) {
81
+ switch (type) {
82
+ case AnnotationEditorParamsType.FREETEXT_SIZE:
83
+ __classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_updateFontSize).call(this, value);
84
+ break;
85
+ case AnnotationEditorParamsType.FREETEXT_COLOR:
86
+ __classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_updateColor).call(this, value);
87
+ break;
88
+ default: break;
89
+ }
90
+ }
91
+ /** @inheritdoc */
92
+ static get defaultPropertiesToUpdate() {
93
+ return [
94
+ [
95
+ AnnotationEditorParamsType.FREETEXT_SIZE,
96
+ _a._defaultFontSize,
97
+ ],
98
+ [
99
+ AnnotationEditorParamsType.FREETEXT_COLOR,
100
+ _a._defaultColor || AnnotationEditor._defaultLineColor,
101
+ ],
102
+ ];
103
+ }
104
+ /** @inheritdoc */
105
+ get propertiesToUpdate() {
106
+ return [
107
+ [AnnotationEditorParamsType.FREETEXT_SIZE, __classPrivateFieldGet(this, _FreeTextEditor_fontSize, "f")],
108
+ [AnnotationEditorParamsType.FREETEXT_COLOR, __classPrivateFieldGet(this, _FreeTextEditor_color, "f")],
109
+ ];
110
+ }
111
+ /**
112
+ * Helper to translate the editor with the keyboard when it's empty.
113
+ * @param {number} x in page units.
114
+ * @param {number} y in page units.
115
+ */
116
+ _translateEmpty(x, y) {
117
+ this._uiManager.translateSelectedEditors(x, y, /* noCommit = */ true);
118
+ }
119
+ getInitialTranslation() {
120
+ // The start of the base line is where the user clicked.
121
+ const scale = this.parentScale;
122
+ return [
123
+ -_a._internalPadding * scale,
124
+ -(_a._internalPadding + __classPrivateFieldGet(this, _FreeTextEditor_fontSize, "f")) * scale,
125
+ ];
126
+ }
127
+ /** @inheritdoc */
128
+ rebuild() {
129
+ if (!this.parent) {
130
+ return;
131
+ }
132
+ super.rebuild();
133
+ if (this.div === null) {
134
+ return;
135
+ }
136
+ if (!this.isAttachedToDOM) {
137
+ // At some point this editor was removed and we're rebuilting it,
138
+ // hence we must add it to its parent.
139
+ this.parent.add(this);
140
+ }
141
+ }
142
+ enableEditMode() {
143
+ if (this.isInEditMode()) {
144
+ return;
145
+ }
146
+ this.parent.setEditingState(false);
147
+ this.parent.updateToolbar(AnnotationEditorType.FREETEXT);
148
+ super.enableEditMode();
149
+ this.overlayDiv.classList.remove("enabled");
150
+ this.editorDiv.contentEditable = true;
151
+ this._isDraggable = false;
152
+ this.div.removeAttribute("aria-activedescendant");
153
+ // if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
154
+ // assert(
155
+ // !this.#editModeAC,
156
+ // "No `this.#editModeAC` AbortController should exist."
157
+ // );
158
+ // }
159
+ __classPrivateFieldSet(this, _FreeTextEditor_editModeAC, new AbortController(), "f");
160
+ const signal = this._uiManager.combinedSignal(__classPrivateFieldGet(this, _FreeTextEditor_editModeAC, "f"));
161
+ this.editorDiv.addEventListener("keydown", this.editorDivKeydown.bind(this), { signal });
162
+ this.editorDiv.addEventListener("focus", this.editorDivFocus.bind(this), {
163
+ signal,
164
+ });
165
+ this.editorDiv.addEventListener("blur", this.editorDivBlur.bind(this), {
166
+ signal,
167
+ });
168
+ this.editorDiv.addEventListener("input", this.editorDivInput.bind(this), {
169
+ signal,
170
+ });
171
+ this.editorDiv.addEventListener("paste", this.editorDivPaste.bind(this), {
172
+ signal,
173
+ });
174
+ }
175
+ disableEditMode() {
176
+ var _b;
177
+ if (!this.isInEditMode()) {
178
+ return;
179
+ }
180
+ this.parent.setEditingState(true);
181
+ super.disableEditMode();
182
+ // this.overlayDiv.classList.add("enabled");
183
+ this.editorDiv.contentEditable = false;
184
+ this.div.setAttribute("aria-activedescendant", __classPrivateFieldGet(this, _FreeTextEditor_editorDivId, "f"));
185
+ this._isDraggable = true;
186
+ (_b = __classPrivateFieldGet(this, _FreeTextEditor_editModeAC, "f")) === null || _b === void 0 ? void 0 : _b.abort();
187
+ __classPrivateFieldSet(this, _FreeTextEditor_editModeAC, null, "f");
188
+ // On Chrome, the focus is given to <body> when contentEditable is set to
189
+ // false, hence we focus the div.
190
+ this.div.focus({
191
+ preventScroll: true /* See issue #15744 */,
192
+ });
193
+ // In case the blur callback hasn't been called.
194
+ this.isEditing = false;
195
+ // this.parent.div.classList.add("freetextEditing");
196
+ }
197
+ focusin(event) {
198
+ var _b;
199
+ if (!this._focusEventsAllowed) {
200
+ return;
201
+ }
202
+ if (((_b = this._uiManager) === null || _b === void 0 ? void 0 : _b.getMode()) !== AnnotationEditorType.FREETEXT) {
203
+ // prevent focusing the freetext editor while in highlight mode
204
+ // in pdf.js this situation does free highlight annotation isntead
205
+ // which is not currently implemented
206
+ return;
207
+ }
208
+ super.focusin(event);
209
+ if (event.target !== this.editorDiv) {
210
+ this.editorDiv.focus();
211
+ }
212
+ }
213
+ /** @inheritdoc */
214
+ onceAdded() {
215
+ var _b;
216
+ if (this.width) {
217
+ // The editor was created in using ctrl+c.
218
+ return;
219
+ }
220
+ this.enableEditMode();
221
+ this.editorDiv.focus();
222
+ if ((_b = this._initialOptions) === null || _b === void 0 ? void 0 : _b.isCentered) {
223
+ this.center();
224
+ }
225
+ this._initialOptions = null;
226
+ }
227
+ /** @inheritdoc */
228
+ isEmpty() {
229
+ var _b, _c;
230
+ return !this.editorDiv || ((_c = (_b = this.editorDiv) === null || _b === void 0 ? void 0 : _b.innerText) === null || _c === void 0 ? void 0 : _c.trim()) === "";
231
+ }
232
+ /** @inheritdoc */
233
+ remove() {
234
+ this.isEditing = false;
235
+ if (this.parent) {
236
+ this.parent.setEditingState(true);
237
+ // this.parent.div.classList.add("freetextEditing");
238
+ }
239
+ super.remove();
240
+ }
241
+ /**
242
+ * Commit the content we have in this editor.
243
+ * @returns {undefined}
244
+ */
245
+ commit() {
246
+ if (!this.isInEditMode()) {
247
+ return;
248
+ }
249
+ super.commit();
250
+ this.disableEditMode();
251
+ const savedText = __classPrivateFieldGet(this, _FreeTextEditor_content, "f");
252
+ const newText = (__classPrivateFieldSet(this, _FreeTextEditor_content, __classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_extractText).call(this).trimEnd(), "f"));
253
+ if (savedText === newText) {
254
+ return;
255
+ }
256
+ const setText = text => {
257
+ __classPrivateFieldSet(this, _FreeTextEditor_content, text, "f");
258
+ if (!text) {
259
+ this.remove();
260
+ return;
261
+ }
262
+ __classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_setContent).call(this);
263
+ this._uiManager.rebuild(this);
264
+ __classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_setEditorDimensions).call(this);
265
+ };
266
+ this.addCommands({
267
+ cmd: () => {
268
+ setText(newText);
269
+ },
270
+ undo: () => {
271
+ setText(savedText);
272
+ },
273
+ mustExec: false,
274
+ });
275
+ __classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_setEditorDimensions).call(this);
276
+ }
277
+ /** @inheritdoc */
278
+ shouldGetKeyboardEvents() {
279
+ return this.isInEditMode();
280
+ }
281
+ /** @inheritdoc */
282
+ enterInEditMode() {
283
+ this.enableEditMode();
284
+ this.editorDiv.focus();
285
+ }
286
+ /**
287
+ * ondblclick callback.
288
+ * @param {MouseEvent} event
289
+ */
290
+ dblclick(event) {
291
+ if (!event) {
292
+ return;
293
+ }
294
+ this.enterInEditMode();
295
+ }
296
+ /**
297
+ * onkeydown callback.
298
+ * @param {KeyboardEvent} event
299
+ */
300
+ keydown(event) {
301
+ if (event.target === this.div && event.key === "Enter") {
302
+ this.enterInEditMode();
303
+ // Avoid to add an unwanted new line.
304
+ event.preventDefault();
305
+ }
306
+ }
307
+ editorDivKeydown() {
308
+ // FreeTextEditor._keyboardManager.exec(this, event);
309
+ }
310
+ editorDivFocus() {
311
+ this.isEditing = true;
312
+ }
313
+ editorDivBlur() {
314
+ this.isEditing = false;
315
+ }
316
+ editorDivInput() {
317
+ // this.parent.div.classList.toggle("freetextEditing", this.isEmpty());
318
+ }
319
+ /** @inheritdoc */
320
+ disableEditing() {
321
+ this.editorDiv.setAttribute("role", "comment");
322
+ this.editorDiv.removeAttribute("aria-multiline");
323
+ }
324
+ /** @inheritdoc */
325
+ enableEditing() {
326
+ this.editorDiv.setAttribute("role", "textbox");
327
+ this.editorDiv.setAttribute("aria-multiline", true);
328
+ }
329
+ /** @inheritdoc */
330
+ render() {
331
+ var _b;
332
+ if (this.div) {
333
+ return this.div;
334
+ }
335
+ let baseX, baseY;
336
+ if (this.width) {
337
+ baseX = this.x;
338
+ baseY = this.y;
339
+ }
340
+ super.render();
341
+ this.editorDiv = document.createElement("div");
342
+ this.editorDiv.className = "k-internal internal";
343
+ // todo: move to CSS
344
+ // this.editorDiv.style.position = "absolute";
345
+ // this.div.style.position = "absolute";
346
+ this.editorDiv.setAttribute("id", __classPrivateFieldGet(this, _FreeTextEditor_editorDivId, "f"));
347
+ this.editorDiv.setAttribute("data-l10n-id", "pdfjs-free-text");
348
+ this.enableEditing();
349
+ // AnnotationEditor._l10nPromise
350
+ // .get("pdfjs-free-text-default-content")
351
+ // .then(msg => this.editorDiv?.setAttribute("default-content", msg));
352
+ // todo: fix localization
353
+ (_b = this.editorDiv) === null || _b === void 0 ? void 0 : _b.setAttribute("default-content", this._uiManager.pdfViewer.options.messages.freeTextEditorPlaceholder);
354
+ this.editorDiv.contentEditable = true;
355
+ const { style } = this.editorDiv;
356
+ style.fontSize = `calc(${__classPrivateFieldGet(this, _FreeTextEditor_fontSize, "f")}px * var(--scale-factor))`;
357
+ style.color = __classPrivateFieldGet(this, _FreeTextEditor_color, "f");
358
+ this.div.append(this.editorDiv);
359
+ this.overlayDiv = document.createElement("div");
360
+ // this.overlayDiv.classList.add("overlay", "enabled");
361
+ this.div.append(this.overlayDiv);
362
+ bindEvents(this, this.div, ["dblclick", "keydown"]);
363
+ if (this.width) {
364
+ // This editor was created in using copy (ctrl+c).
365
+ const [parentWidth, parentHeight] = this.parentDimensions;
366
+ if (this.annotationElementId) {
367
+ // This stuff is hard to test: if something is changed here, please
368
+ // test with the following PDF file:
369
+ // - freetexts.pdf
370
+ // - rotated_freetexts.pdf
371
+ // Only small variations between the original annotation and its editor
372
+ // are allowed.
373
+ // position is the position of the first glyph in the annotation
374
+ // and it's relative to its container.
375
+ const { position } = __classPrivateFieldGet(this, _FreeTextEditor_initialData, "f");
376
+ let [tx, ty] = this.getInitialTranslation();
377
+ [tx, ty] = this.pageTranslationToScreen(tx, ty);
378
+ const [pageWidth, pageHeight] = this.pageDimensions;
379
+ const [pageX, pageY] = this.pageTranslation;
380
+ let posX, posY;
381
+ switch (this.rotation) {
382
+ case 0:
383
+ posX = baseX + (position[0] - pageX) / pageWidth;
384
+ posY = baseY + this.height - (position[1] - pageY) / pageHeight;
385
+ break;
386
+ case 90:
387
+ posX = baseX + (position[0] - pageX) / pageWidth;
388
+ posY = baseY - (position[1] - pageY) / pageHeight;
389
+ [tx, ty] = [ty, -tx];
390
+ break;
391
+ case 180:
392
+ posX = baseX - this.width + (position[0] - pageX) / pageWidth;
393
+ posY = baseY - (position[1] - pageY) / pageHeight;
394
+ [tx, ty] = [-tx, -ty];
395
+ break;
396
+ case 270:
397
+ posX =
398
+ baseX +
399
+ (position[0] - pageX - this.height * pageHeight) / pageWidth;
400
+ posY =
401
+ baseY +
402
+ (position[1] - pageY - this.width * pageWidth) / pageHeight;
403
+ [tx, ty] = [-ty, tx];
404
+ break;
405
+ default: break;
406
+ }
407
+ this.setAt(posX * parentWidth, posY * parentHeight, tx, ty);
408
+ }
409
+ else {
410
+ this.setAt(baseX * parentWidth, baseY * parentHeight, this.width * parentWidth, this.height * parentHeight);
411
+ }
412
+ __classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_setContent).call(this);
413
+ this._isDraggable = true;
414
+ this.editorDiv.contentEditable = false;
415
+ }
416
+ else {
417
+ this._isDraggable = false;
418
+ this.editorDiv.contentEditable = true;
419
+ }
420
+ // if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) {
421
+ // this.div.setAttribute("annotation-id", this.annotationElementId);
422
+ // }
423
+ return this.div;
424
+ }
425
+ editorDivPaste(event) {
426
+ // @ts-expect-error TS(2551):
427
+ const clipboardData = event.clipboardData || window.clipboardData;
428
+ const { types } = clipboardData;
429
+ if (types.length === 1 && types[0] === "text/plain") {
430
+ return;
431
+ }
432
+ event.preventDefault();
433
+ const paste = __classPrivateFieldGet(_a, _a, "m", _FreeTextEditor_deserializeContent).call(_a, clipboardData.getData("text") || "").replaceAll(EOL_PATTERN, "\n");
434
+ if (!paste) {
435
+ return;
436
+ }
437
+ const selection = window.getSelection();
438
+ if (!selection.rangeCount) {
439
+ return;
440
+ }
441
+ this.editorDiv.normalize();
442
+ selection.deleteFromDocument();
443
+ const range = selection.getRangeAt(0);
444
+ if (!paste.includes("\n")) {
445
+ range.insertNode(document.createTextNode(paste));
446
+ this.editorDiv.normalize();
447
+ selection.collapseToStart();
448
+ return;
449
+ }
450
+ // Collect the text before and after the caret.
451
+ const { startContainer, startOffset } = range;
452
+ const bufferBefore = [];
453
+ const bufferAfter = [];
454
+ if (startContainer.nodeType === Node.TEXT_NODE) {
455
+ const parent = startContainer.parentElement;
456
+ bufferAfter.push(
457
+ // @ts-expect-error TS(2556):
458
+ startContainer.nodeValue.slice(startOffset).replaceAll(EOL_PATTERN, ""));
459
+ if (parent !== this.editorDiv) {
460
+ let buffer = bufferBefore;
461
+ for (const child of this.editorDiv.childNodes) {
462
+ if (child === parent) {
463
+ buffer = bufferAfter;
464
+ continue;
465
+ }
466
+ buffer.push(__classPrivateFieldGet(_a, _a, "m", _FreeTextEditor_getNodeContent).call(_a, child));
467
+ }
468
+ }
469
+ bufferBefore.push(startContainer.nodeValue
470
+ .slice(0, startOffset)
471
+ // @ts-expect-error TS(2556):
472
+ .replaceAll(EOL_PATTERN, ""));
473
+ }
474
+ else if (startContainer === this.editorDiv) {
475
+ let buffer = bufferBefore;
476
+ let i = 0;
477
+ for (const child of this.editorDiv.childNodes) {
478
+ if (i++ === startOffset) {
479
+ buffer = bufferAfter;
480
+ }
481
+ buffer.push(__classPrivateFieldGet(_a, _a, "m", _FreeTextEditor_getNodeContent).call(_a, child));
482
+ }
483
+ }
484
+ __classPrivateFieldSet(this, _FreeTextEditor_content, `${bufferBefore.join("\n")}${paste}${bufferAfter.join("\n")}`, "f");
485
+ __classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_setContent).call(this);
486
+ // Set the caret at the right position.
487
+ const newRange = new Range();
488
+ let beforeLength = bufferBefore.reduce((acc, line) => acc + line.length, 0);
489
+ for (const { firstChild } of this.editorDiv.childNodes) {
490
+ // Each child is either a div with a text node or a br element.
491
+ if (firstChild.nodeType === Node.TEXT_NODE) {
492
+ const length = firstChild.nodeValue.length;
493
+ if (beforeLength <= length) {
494
+ newRange.setStart(firstChild, beforeLength);
495
+ newRange.setEnd(firstChild, beforeLength);
496
+ break;
497
+ }
498
+ beforeLength -= length;
499
+ }
500
+ }
501
+ selection.removeAllRanges();
502
+ selection.addRange(newRange);
503
+ }
504
+ /** @inheritdoc */
505
+ get contentDiv() {
506
+ return this.editorDiv;
507
+ }
508
+ /** @inheritdoc */
509
+ static deserialize(data, parent, uiManager) {
510
+ let initialData = null;
511
+ if (data instanceof FreeTextAnnotationElement) {
512
+ const { data: { defaultAppearanceData: { fontSize, fontColor }, rect, rotation, id, }, textContent, textPosition, parent: { page: { pageNumber }, }, } = data;
513
+ // textContent is supposed to be an array of strings containing each line
514
+ // of text. However, it can be null or empty.
515
+ if (!textContent || textContent.length === 0) {
516
+ // Empty annotation.
517
+ return null;
518
+ }
519
+ initialData = data = {
520
+ annotationType: AnnotationEditorType.FREETEXT,
521
+ color: Array.from(fontColor),
522
+ fontSize,
523
+ value: textContent.join("\n"),
524
+ position: textPosition,
525
+ pageIndex: pageNumber - 1,
526
+ rect: rect.slice(0),
527
+ rotation,
528
+ id,
529
+ deleted: false,
530
+ };
531
+ }
532
+ const editor = super.deserialize(data, parent, uiManager);
533
+ __classPrivateFieldSet(editor, _FreeTextEditor_fontSize, data.fontSize, "f");
534
+ // @ts-expect-error TS(2556):
535
+ __classPrivateFieldSet(editor, _FreeTextEditor_color, Util.makeHexColor(...data.color), "f");
536
+ __classPrivateFieldSet(editor, _FreeTextEditor_content, __classPrivateFieldGet(_a, _a, "m", _FreeTextEditor_deserializeContent).call(_a, data.value), "f");
537
+ editor.annotationElementId = data.id || null;
538
+ __classPrivateFieldSet(editor, _FreeTextEditor_initialData, initialData, "f");
539
+ return editor;
540
+ }
541
+ /** @inheritdoc */
542
+ serialize(isForCopying = false) {
543
+ if (this.isEmpty()) {
544
+ return null;
545
+ }
546
+ if (this.deleted) {
547
+ return {
548
+ pageIndex: this.pageIndex,
549
+ id: this.annotationElementId,
550
+ deleted: true,
551
+ };
552
+ }
553
+ const padding = _a._internalPadding * this.parentScale;
554
+ const rect = this.getRect(padding, padding);
555
+ const color = AnnotationEditor._colorManager.convert(this.isAttachedToDOM
556
+ ? getComputedStyle(this.editorDiv).color
557
+ : __classPrivateFieldGet(this, _FreeTextEditor_color, "f"));
558
+ const serialized = {
559
+ annotationType: AnnotationEditorType.FREETEXT,
560
+ color,
561
+ fontSize: __classPrivateFieldGet(this, _FreeTextEditor_fontSize, "f"),
562
+ value: __classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_serializeContent).call(this),
563
+ pageIndex: this.pageIndex,
564
+ rect,
565
+ rotation: this.rotation,
566
+ structTreeParentId: this._structTreeParentId,
567
+ };
568
+ if (isForCopying) {
569
+ // Don't add the id when copying because the pasted editor mustn't be
570
+ // linked to an existing annotation.
571
+ return serialized;
572
+ }
573
+ if (this.annotationElementId && !__classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_hasElementChanged).call(this, serialized)) {
574
+ return null;
575
+ }
576
+ // @ts-expect-error TS(2556):
577
+ serialized.id = this.annotationElementId;
578
+ return serialized;
579
+ }
580
+ /** @inheritdoc */
581
+ renderAnnotationElement(annotation) {
582
+ const content = super.renderAnnotationElement(annotation);
583
+ if (this.deleted) {
584
+ return content;
585
+ }
586
+ const { style } = content;
587
+ style.fontSize = `calc(${__classPrivateFieldGet(this, _FreeTextEditor_fontSize, "f")}px * var(--scale-factor))`;
588
+ style.color = __classPrivateFieldGet(this, _FreeTextEditor_color, "f");
589
+ content.replaceChildren();
590
+ for (const line of __classPrivateFieldGet(this, _FreeTextEditor_content, "f").split("\n")) {
591
+ const div = document.createElement("div");
592
+ div.append(line ? document.createTextNode(line) : document.createElement("br"));
593
+ content.append(div);
594
+ }
595
+ const padding = _a._internalPadding * this.parentScale;
596
+ annotation.updateEdited({
597
+ rect: this.getRect(padding, padding),
598
+ popupContent: __classPrivateFieldGet(this, _FreeTextEditor_content, "f"),
599
+ });
600
+ return content;
601
+ }
602
+ resetAnnotationElement(annotation) {
603
+ super.resetAnnotationElement(annotation);
604
+ annotation.resetEdited();
605
+ }
606
+ // todo: this is necessary
607
+ // saveDocument() has checks that test "editor instanceof AnnotationEditor", but they fail
608
+ // because AnnotationEditor from "pdfjs-dist/legacy/build/pdf.mjs" is not exported
609
+ // thus replace instances of editors with their serialized version
610
+ toJSON() {
611
+ const data = this.serialize();
612
+ return data;
613
+ }
614
+ }
615
+ _a = FreeTextEditor, _FreeTextEditor_color = new WeakMap(), _FreeTextEditor_content = new WeakMap(), _FreeTextEditor_editorDivId = new WeakMap(), _FreeTextEditor_editModeAC = new WeakMap(), _FreeTextEditor_fontSize = new WeakMap(), _FreeTextEditor_initialData = new WeakMap(), _FreeTextEditor_instances = new WeakSet(), _FreeTextEditor_updateFontSize = function _FreeTextEditor_updateFontSize(fontSize) {
616
+ const setFontsize = size => {
617
+ this.editorDiv.style.fontSize = `calc(${size}px * var(--scale-factor))`;
618
+ this.translate(0, -(size - __classPrivateFieldGet(this, _FreeTextEditor_fontSize, "f")) * this.parentScale);
619
+ __classPrivateFieldSet(this, _FreeTextEditor_fontSize, size, "f");
620
+ __classPrivateFieldGet(this, _FreeTextEditor_instances, "m", _FreeTextEditor_setEditorDimensions).call(this);
621
+ };
622
+ const savedFontsize = __classPrivateFieldGet(this, _FreeTextEditor_fontSize, "f");
623
+ this.addCommands({
624
+ cmd: setFontsize.bind(this, fontSize),
625
+ undo: setFontsize.bind(this, savedFontsize),
626
+ post: this._uiManager.updateUI.bind(this._uiManager, this),
627
+ mustExec: true,
628
+ type: AnnotationEditorParamsType.FREETEXT_SIZE,
629
+ overwriteIfSameType: true,
630
+ keepUndo: true,
631
+ });
632
+ }, _FreeTextEditor_updateColor = function _FreeTextEditor_updateColor(color) {
633
+ const setColor = col => {
634
+ __classPrivateFieldSet(this, _FreeTextEditor_color, this.editorDiv.style.color = col, "f");
635
+ };
636
+ const savedColor = __classPrivateFieldGet(this, _FreeTextEditor_color, "f");
637
+ this.addCommands({
638
+ cmd: setColor.bind(this, color),
639
+ undo: setColor.bind(this, savedColor),
640
+ post: this._uiManager.updateUI.bind(this._uiManager, this),
641
+ mustExec: true,
642
+ type: AnnotationEditorParamsType.FREETEXT_COLOR,
643
+ overwriteIfSameType: true,
644
+ keepUndo: true,
645
+ });
646
+ }, _FreeTextEditor_extractText = function _FreeTextEditor_extractText() {
647
+ // We don't use innerText because there are some bugs with line breaks.
648
+ const buffer = [];
649
+ this.editorDiv.normalize();
650
+ for (const child of this.editorDiv.childNodes) {
651
+ buffer.push(__classPrivateFieldGet(_a, _a, "m", _FreeTextEditor_getNodeContent).call(_a, child));
652
+ }
653
+ return buffer.join("\n");
654
+ }, _FreeTextEditor_setEditorDimensions = function _FreeTextEditor_setEditorDimensions() {
655
+ const [parentWidth, parentHeight] = this.parentDimensions;
656
+ let rect;
657
+ if (this.isAttachedToDOM) {
658
+ rect = this.div.getBoundingClientRect();
659
+ }
660
+ else {
661
+ // This editor isn't on screen but we need to get its dimensions, so
662
+ // we just insert it in the DOM, get its bounding box and then remove it.
663
+ const { currentLayer, div } = this;
664
+ const savedDisplay = div.style.display;
665
+ const savedVisibility = div.classList.contains("hidden") || div.classList.contains("k-hidden");
666
+ div.classList.remove("hidden");
667
+ div.classList.remove("k-hidden");
668
+ div.style.display = "hidden";
669
+ currentLayer.div.append(this.div);
670
+ rect = div.getBoundingClientRect();
671
+ div.remove();
672
+ div.style.display = savedDisplay;
673
+ // div.classList.toggle("hidden", savedVisibility);
674
+ div.classList.toggle("k-hidden", savedVisibility);
675
+ }
676
+ // The dimensions are relative to the rotation of the page, hence we need to
677
+ // take that into account (see issue #16636).
678
+ if (this.rotation % 180 === this.parentRotation % 180) {
679
+ this.width = rect.width / parentWidth;
680
+ this.height = rect.height / parentHeight;
681
+ }
682
+ else {
683
+ this.width = rect.height / parentWidth;
684
+ this.height = rect.width / parentHeight;
685
+ }
686
+ this.fixAndSetPosition();
687
+ }, _FreeTextEditor_getNodeContent = function _FreeTextEditor_getNodeContent(node) {
688
+ return (node.nodeType === Node.TEXT_NODE ? node.nodeValue : node.innerText).replaceAll(EOL_PATTERN, "");
689
+ }, _FreeTextEditor_setContent = function _FreeTextEditor_setContent() {
690
+ this.editorDiv.replaceChildren();
691
+ if (!__classPrivateFieldGet(this, _FreeTextEditor_content, "f")) {
692
+ return;
693
+ }
694
+ for (const line of __classPrivateFieldGet(this, _FreeTextEditor_content, "f").split("\n")) {
695
+ const div = document.createElement("div");
696
+ div.append(line ? document.createTextNode(line) : document.createElement("br"));
697
+ this.editorDiv.append(div);
698
+ }
699
+ }, _FreeTextEditor_serializeContent = function _FreeTextEditor_serializeContent() {
700
+ // @ts-expect-error TS(2556):
701
+ return __classPrivateFieldGet(this, _FreeTextEditor_content, "f").replaceAll("\xa0", " ");
702
+ }, _FreeTextEditor_deserializeContent = function _FreeTextEditor_deserializeContent(content) {
703
+ return content.replaceAll(" ", "\xa0");
704
+ }, _FreeTextEditor_hasElementChanged = function _FreeTextEditor_hasElementChanged(serialized) {
705
+ const { value, fontSize, color, pageIndex } = __classPrivateFieldGet(this, _FreeTextEditor_initialData, "f");
706
+ return (this._hasBeenMoved ||
707
+ serialized.value !== value ||
708
+ serialized.fontSize !== fontSize ||
709
+ serialized.color.some((c, i) => c !== color[i]) ||
710
+ serialized.pageIndex !== pageIndex);
711
+ };
712
+ FreeTextEditor._freeTextDefaultContent = "";
713
+ FreeTextEditor._internalPadding = 0;
714
+ // static _defaultColor = null;
715
+ FreeTextEditor._defaultColor = "#000000";
716
+ FreeTextEditor._defaultFontSize = 10;
717
+ // static get _keyboardManager() {
718
+ // const proto = FreeTextEditor.prototype;
719
+ // const arrowChecker = self => self.isEmpty();
720
+ // const small = AnnotationEditorUIManager.TRANSLATE_SMALL;
721
+ // const big = AnnotationEditorUIManager.TRANSLATE_BIG;
722
+ // return shadow(
723
+ // this,
724
+ // "_keyboardManager",
725
+ // new KeyboardManager([
726
+ // [
727
+ // // Commit the text in case the user use ctrl+s to save the document.
728
+ // // The event must bubble in order to be caught by the viewer.
729
+ // // See bug 1831574.
730
+ // ["ctrl+s", "mac+meta+s", "ctrl+p", "mac+meta+p"],
731
+ // proto.commitOrRemove,
732
+ // { bubbles: true },
733
+ // ],
734
+ // [
735
+ // ["ctrl+Enter", "mac+meta+Enter", "Escape", "mac+Escape"],
736
+ // proto.commitOrRemove,
737
+ // ],
738
+ // [
739
+ // ["ArrowLeft", "mac+ArrowLeft"],
740
+ // proto._translateEmpty,
741
+ // { args: [-small, 0], checker: arrowChecker },
742
+ // ],
743
+ // [
744
+ // ["ctrl+ArrowLeft", "mac+shift+ArrowLeft"],
745
+ // proto._translateEmpty,
746
+ // { args: [-big, 0], checker: arrowChecker },
747
+ // ],
748
+ // [
749
+ // ["ArrowRight", "mac+ArrowRight"],
750
+ // proto._translateEmpty,
751
+ // { args: [small, 0], checker: arrowChecker },
752
+ // ],
753
+ // [
754
+ // ["ctrl+ArrowRight", "mac+shift+ArrowRight"],
755
+ // proto._translateEmpty,
756
+ // { args: [big, 0], checker: arrowChecker },
757
+ // ],
758
+ // [
759
+ // ["ArrowUp", "mac+ArrowUp"],
760
+ // proto._translateEmpty,
761
+ // { args: [0, -small], checker: arrowChecker },
762
+ // ],
763
+ // [
764
+ // ["ctrl+ArrowUp", "mac+shift+ArrowUp"],
765
+ // proto._translateEmpty,
766
+ // { args: [0, -big], checker: arrowChecker },
767
+ // ],
768
+ // [
769
+ // ["ArrowDown", "mac+ArrowDown"],
770
+ // proto._translateEmpty,
771
+ // { args: [0, small], checker: arrowChecker },
772
+ // ],
773
+ // [
774
+ // ["ctrl+ArrowDown", "mac+shift+ArrowDown"],
775
+ // proto._translateEmpty,
776
+ // { args: [0, big], checker: arrowChecker },
777
+ // ],
778
+ // ])
779
+ // );
780
+ // }
781
+ FreeTextEditor._type = "freetext";
782
+ FreeTextEditor._editorType = AnnotationEditorType.FREETEXT;
783
+ export { FreeTextEditor };