@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,176 @@
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 _ImageManager_instances, _a, _ImageManager_baseId, _ImageManager_id, _ImageManager_cache, _ImageManager_get;
16
+ import { __awaiter, __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
17
+ import { fetchData, shadow } from "pdfjs-dist/legacy/build/pdf.mjs";
18
+ import { getUuid } from "../shared/utils";
19
+ /**
20
+ * Class to manage the images used by the editors.
21
+ * The main idea is to try to minimize the memory used by the images.
22
+ * The images are cached and reused when possible
23
+ * We use a refCounter to know when an image is not used anymore but we need to
24
+ * be able to restore an image after a remove+undo, so we keep a file reference
25
+ * or an url one.
26
+ */
27
+ export class ImageManager {
28
+ constructor() {
29
+ _ImageManager_instances.add(this);
30
+ _ImageManager_baseId.set(this, getUuid());
31
+ _ImageManager_id.set(this, 0);
32
+ _ImageManager_cache.set(this, null);
33
+ }
34
+ static get _isSVGFittingCanvas() {
35
+ // By default, Firefox doesn't rescale without preserving the aspect ratio
36
+ // when drawing an SVG image on a canvas, see https://bugzilla.mozilla.org/1547776.
37
+ // The "workaround" is to append "svgView(preserveAspectRatio(none))" to the
38
+ // url, but according to comment #15, it seems that it leads to unexpected
39
+ // behavior in Safari.
40
+ const svg = `data:image/svg+xml;charset=UTF-8,<svg viewBox="0 0 1 1" width="1" height="1" xmlns="http://www.w3.org/2000/svg"><rect width="1" height="1" style="fill:red;"/></svg>`;
41
+ const canvas = new OffscreenCanvas(1, 3);
42
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
43
+ const image = new Image();
44
+ image.src = svg;
45
+ const promise = image.decode().then(() => {
46
+ ctx.drawImage(image, 0, 0, 1, 1, 0, 0, 1, 3);
47
+ return new Uint32Array(ctx.getImageData(0, 0, 1, 1).data.buffer)[0] === 0;
48
+ });
49
+ return shadow(this, "_isSVGFittingCanvas", promise);
50
+ }
51
+ getFromFile(file) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ const { lastModified, name, size, type } = file;
54
+ return __classPrivateFieldGet(this, _ImageManager_instances, "m", _ImageManager_get).call(this, `${lastModified}_${name}_${size}_${type}`, file);
55
+ });
56
+ }
57
+ getFromUrl(url) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ return __classPrivateFieldGet(this, _ImageManager_instances, "m", _ImageManager_get).call(this, url, url);
60
+ });
61
+ }
62
+ getFromId(id) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ __classPrivateFieldSet(this, _ImageManager_cache, __classPrivateFieldGet(this, _ImageManager_cache, "f") || new Map(), "f");
65
+ const data = __classPrivateFieldGet(this, _ImageManager_cache, "f").get(id);
66
+ if (!data) {
67
+ return null;
68
+ }
69
+ if (data.bitmap) {
70
+ data.refCounter += 1;
71
+ return data;
72
+ }
73
+ if (data.file) {
74
+ return this.getFromFile(data.file);
75
+ }
76
+ return this.getFromUrl(data.url);
77
+ });
78
+ }
79
+ getSvgUrl(id) {
80
+ const data = __classPrivateFieldGet(this, _ImageManager_cache, "f").get(id);
81
+ if (!(data === null || data === void 0 ? void 0 : data.isSvg)) {
82
+ return null;
83
+ }
84
+ return data.svgUrl;
85
+ }
86
+ deleteId(id) {
87
+ __classPrivateFieldSet(this, _ImageManager_cache, __classPrivateFieldGet(this, _ImageManager_cache, "f") || new Map(), "f");
88
+ const data = __classPrivateFieldGet(this, _ImageManager_cache, "f").get(id);
89
+ if (!data) {
90
+ return;
91
+ }
92
+ data.refCounter -= 1;
93
+ if (data.refCounter !== 0) {
94
+ return;
95
+ }
96
+ data.bitmap = null;
97
+ }
98
+ // We can use the id only if it belongs this manager.
99
+ // We must take care of having the right manager because we can copy/paste
100
+ // some images from other documents, hence it'd be a pity to use an id from an
101
+ // other manager.
102
+ isValidId(id) {
103
+ return id.startsWith(`image_${__classPrivateFieldGet(this, _ImageManager_baseId, "f")}_`);
104
+ }
105
+ }
106
+ _a = ImageManager, _ImageManager_baseId = new WeakMap(), _ImageManager_id = new WeakMap(), _ImageManager_cache = new WeakMap(), _ImageManager_instances = new WeakSet(), _ImageManager_get = function _ImageManager_get(key, rawData) {
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ var _b, _c;
109
+ __classPrivateFieldSet(this, _ImageManager_cache, __classPrivateFieldGet(this, _ImageManager_cache, "f") || new Map(), "f");
110
+ let data = __classPrivateFieldGet(this, _ImageManager_cache, "f").get(key);
111
+ if (data === null) {
112
+ // We already tried to load the image but it failed.
113
+ return null;
114
+ }
115
+ if (data === null || data === void 0 ? void 0 : data.bitmap) {
116
+ data.refCounter += 1;
117
+ return data;
118
+ }
119
+ try {
120
+ data || (data = {
121
+ bitmap: null,
122
+ id: `image_${__classPrivateFieldGet(this, _ImageManager_baseId, "f")}_${__classPrivateFieldSet(this, _ImageManager_id, (_c = __classPrivateFieldGet(this, _ImageManager_id, "f"), _b = _c++, _c), "f"), _b}`,
123
+ refCounter: 0,
124
+ isSvg: false,
125
+ });
126
+ let image;
127
+ if (typeof rawData === "string") {
128
+ data.url = rawData;
129
+ image = yield fetchData(rawData, "blob");
130
+ }
131
+ else {
132
+ image = data.file = rawData;
133
+ }
134
+ if (image.type === "image/svg+xml") {
135
+ // Unfortunately, createImageBitmap doesn't work with SVG images.
136
+ // (see https://bugzilla.mozilla.org/1841972).
137
+ const mustRemoveAspectRatioPromise = _a._isSVGFittingCanvas;
138
+ const fileReader = new FileReader();
139
+ const imageElement = new Image();
140
+ const imagePromise = new Promise((resolve, reject) => {
141
+ imageElement.onload = () => {
142
+ data.bitmap = imageElement;
143
+ data.isSvg = true;
144
+ // @ts-expect-error(TS:2794)
145
+ resolve();
146
+ };
147
+ fileReader.onload = () => __awaiter(this, void 0, void 0, function* () {
148
+ const url = (data.svgUrl = fileReader.result);
149
+ // We need to set the preserveAspectRatio to none in order to let
150
+ // the image fits the canvas when resizing.
151
+ // @ts-expect-error(TS:2332)
152
+ imageElement.src = (yield mustRemoveAspectRatioPromise)
153
+ ? `${url}#svgView(preserveAspectRatio(none))`
154
+ : url;
155
+ });
156
+ imageElement.onerror = fileReader.onerror = reject;
157
+ });
158
+ fileReader.readAsDataURL(image);
159
+ yield imagePromise;
160
+ }
161
+ else {
162
+ data.bitmap = yield createImageBitmap(image);
163
+ }
164
+ data.refCounter = 1;
165
+ }
166
+ catch (e) {
167
+ console.error(e);
168
+ data = null;
169
+ }
170
+ __classPrivateFieldGet(this, _ImageManager_cache, "f").set(key, data);
171
+ if (data) {
172
+ __classPrivateFieldGet(this, _ImageManager_cache, "f").set(data.id, data);
173
+ }
174
+ return data;
175
+ });
176
+ };
@@ -0,0 +1,212 @@
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 _TextAccessibilityManager_instances, _a, _TextAccessibilityManager_enabled, _TextAccessibilityManager_textChildren, _TextAccessibilityManager_textNodes, _TextAccessibilityManager_waitingElements, _TextAccessibilityManager_compareElementPositions, _TextAccessibilityManager_addIdToAriaOwns;
16
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
17
+ import { binarySearchFirstItem } from "../shared/ui_utils";
18
+ /**
19
+ * This class aims to provide some methods:
20
+ * - to reorder elements in the DOM with respect to the visual order;
21
+ * - to create a link, using aria-owns, between spans in the textLayer and
22
+ * annotations in the annotationLayer. The goal is to help to know
23
+ * where the annotations are in the text flow.
24
+ */
25
+ class TextAccessibilityManager {
26
+ constructor() {
27
+ _TextAccessibilityManager_instances.add(this);
28
+ _TextAccessibilityManager_enabled.set(this, false);
29
+ _TextAccessibilityManager_textChildren.set(this, null);
30
+ _TextAccessibilityManager_textNodes.set(this, new Map());
31
+ _TextAccessibilityManager_waitingElements.set(this, new Map());
32
+ }
33
+ setTextMapping(textDivs) {
34
+ __classPrivateFieldSet(this, _TextAccessibilityManager_textChildren, textDivs, "f");
35
+ }
36
+ /**
37
+ * Function called when the text layer has finished rendering.
38
+ */
39
+ enable() {
40
+ if (__classPrivateFieldGet(this, _TextAccessibilityManager_enabled, "f")) {
41
+ // throw new Error("TextAccessibilityManager is already enabled.");
42
+ }
43
+ if (!__classPrivateFieldGet(this, _TextAccessibilityManager_textChildren, "f")) {
44
+ throw new Error("Text divs and strings have not been set.");
45
+ }
46
+ __classPrivateFieldSet(this, _TextAccessibilityManager_enabled, true, "f");
47
+ __classPrivateFieldSet(this, _TextAccessibilityManager_textChildren, __classPrivateFieldGet(this, _TextAccessibilityManager_textChildren, "f").slice(), "f");
48
+ __classPrivateFieldGet(this, _TextAccessibilityManager_textChildren, "f").sort(__classPrivateFieldGet(_a, _a, "m", _TextAccessibilityManager_compareElementPositions));
49
+ if (__classPrivateFieldGet(this, _TextAccessibilityManager_textNodes, "f").size > 0) {
50
+ // Some links have been made before this manager has been disabled, hence
51
+ // we restore them.
52
+ const textChildren = __classPrivateFieldGet(this, _TextAccessibilityManager_textChildren, "f");
53
+ for (const [id, nodeIndex] of __classPrivateFieldGet(this, _TextAccessibilityManager_textNodes, "f")) {
54
+ const element = document.getElementById(id);
55
+ if (!element) {
56
+ // If the page was *fully* reset the element no longer exists, and it
57
+ // will be re-inserted later (i.e. when the annotationLayer renders).
58
+ __classPrivateFieldGet(this, _TextAccessibilityManager_textNodes, "f").delete(id);
59
+ continue;
60
+ }
61
+ __classPrivateFieldGet(this, _TextAccessibilityManager_instances, "m", _TextAccessibilityManager_addIdToAriaOwns).call(this, id, textChildren[nodeIndex]);
62
+ }
63
+ }
64
+ for (const [element, isRemovable] of __classPrivateFieldGet(this, _TextAccessibilityManager_waitingElements, "f")) {
65
+ this.addPointerInTextLayer(element, isRemovable);
66
+ }
67
+ __classPrivateFieldGet(this, _TextAccessibilityManager_waitingElements, "f").clear();
68
+ }
69
+ disable() {
70
+ if (!__classPrivateFieldGet(this, _TextAccessibilityManager_enabled, "f")) {
71
+ return;
72
+ }
73
+ // Don't clear this.#textNodes which is used to rebuild the aria-owns
74
+ // in case it's re-enabled at some point.
75
+ __classPrivateFieldGet(this, _TextAccessibilityManager_waitingElements, "f").clear();
76
+ __classPrivateFieldSet(this, _TextAccessibilityManager_textChildren, null, "f");
77
+ __classPrivateFieldSet(this, _TextAccessibilityManager_enabled, false, "f");
78
+ }
79
+ /**
80
+ * Remove an aria-owns id from a node in the text layer.
81
+ * @param {HTMLElement} element
82
+ */
83
+ removePointerInTextLayer(element) {
84
+ if (!__classPrivateFieldGet(this, _TextAccessibilityManager_enabled, "f")) {
85
+ __classPrivateFieldGet(this, _TextAccessibilityManager_waitingElements, "f").delete(element);
86
+ return;
87
+ }
88
+ const children = __classPrivateFieldGet(this, _TextAccessibilityManager_textChildren, "f");
89
+ if (!children || children.length === 0) {
90
+ return;
91
+ }
92
+ const { id } = element;
93
+ const nodeIndex = __classPrivateFieldGet(this, _TextAccessibilityManager_textNodes, "f").get(id);
94
+ if (nodeIndex === undefined) {
95
+ return;
96
+ }
97
+ const node = children[nodeIndex];
98
+ __classPrivateFieldGet(this, _TextAccessibilityManager_textNodes, "f").delete(id);
99
+ let owns = node.getAttribute("aria-owns");
100
+ if (owns === null || owns === void 0 ? void 0 : owns.includes(id)) {
101
+ owns = owns
102
+ .split(" ")
103
+ .filter(x => x !== id)
104
+ .join(" ");
105
+ if (owns) {
106
+ node.setAttribute("aria-owns", owns);
107
+ }
108
+ else {
109
+ node.removeAttribute("aria-owns");
110
+ node.setAttribute("role", "presentation");
111
+ }
112
+ }
113
+ }
114
+ /**
115
+ * Find the text node which is the nearest and add an aria-owns attribute
116
+ * in order to correctly position this editor in the text flow.
117
+ * @param {HTMLElement} element
118
+ * @param {boolean} isRemovable
119
+ * @returns {string|null} The id in the struct tree if any.
120
+ */
121
+ addPointerInTextLayer(element, isRemovable) {
122
+ // todo: mocked highlight editor is null here
123
+ if (!element) {
124
+ console.warn("no element");
125
+ return;
126
+ }
127
+ const { id } = element;
128
+ if (!id) {
129
+ return null;
130
+ }
131
+ if (!__classPrivateFieldGet(this, _TextAccessibilityManager_enabled, "f")) {
132
+ // The text layer needs to be there, so we postpone the association.
133
+ __classPrivateFieldGet(this, _TextAccessibilityManager_waitingElements, "f").set(element, isRemovable);
134
+ return null;
135
+ }
136
+ if (isRemovable) {
137
+ this.removePointerInTextLayer(element);
138
+ }
139
+ const children = __classPrivateFieldGet(this, _TextAccessibilityManager_textChildren, "f");
140
+ if (!children || children.length === 0) {
141
+ return null;
142
+ }
143
+ const index = binarySearchFirstItem(children, node => __classPrivateFieldGet(_a, _a, "m", _TextAccessibilityManager_compareElementPositions).call(_a, element, node) < 0);
144
+ const nodeIndex = Math.max(0, index - 1);
145
+ const child = children[nodeIndex];
146
+ __classPrivateFieldGet(this, _TextAccessibilityManager_instances, "m", _TextAccessibilityManager_addIdToAriaOwns).call(this, id, child);
147
+ __classPrivateFieldGet(this, _TextAccessibilityManager_textNodes, "f").set(id, nodeIndex);
148
+ const parent = child.parentNode;
149
+ // return parent?.classList.contains("markedContent") ? parent.id : null;
150
+ return (parent === null || parent === void 0 ? void 0 : parent.classList.contains("markedContent")) ||
151
+ (parent === null || parent === void 0 ? void 0 : parent.classList.contains("k-marked-content")) ? parent.id : null;
152
+ }
153
+ /**
154
+ * Move a div in the DOM in order to respect the visual order.
155
+ * @param {HTMLDivElement} element
156
+ * @returns {string|null} The id in the struct tree if any.
157
+ */
158
+ moveElementInDOM(container, element, contentElement, isRemovable) {
159
+ const id = this.addPointerInTextLayer(contentElement, isRemovable);
160
+ if (!container.hasChildNodes()) {
161
+ container.append(element);
162
+ return id;
163
+ }
164
+ // todo: use children to skip text nodes
165
+ // const children = Array.from(container.childNodes).filter(
166
+ const children = Array.from(container.children).filter(node => node !== element);
167
+ if (children.length === 0) {
168
+ return id;
169
+ }
170
+ const elementToCompare = contentElement || element;
171
+ const index = binarySearchFirstItem(children, node => __classPrivateFieldGet(_a, _a, "m", _TextAccessibilityManager_compareElementPositions).call(_a, elementToCompare, node) < 0);
172
+ if (index === 0) {
173
+ children[0].before(element);
174
+ }
175
+ else {
176
+ children[index - 1].after(element);
177
+ }
178
+ return id;
179
+ }
180
+ }
181
+ _a = TextAccessibilityManager, _TextAccessibilityManager_enabled = new WeakMap(), _TextAccessibilityManager_textChildren = new WeakMap(), _TextAccessibilityManager_textNodes = new WeakMap(), _TextAccessibilityManager_waitingElements = new WeakMap(), _TextAccessibilityManager_instances = new WeakSet(), _TextAccessibilityManager_compareElementPositions = function _TextAccessibilityManager_compareElementPositions(e1, e2) {
182
+ const rect1 = e1.getBoundingClientRect();
183
+ const rect2 = e2.getBoundingClientRect();
184
+ if (rect1.width === 0 && rect1.height === 0) {
185
+ return +1;
186
+ }
187
+ if (rect2.width === 0 && rect2.height === 0) {
188
+ return -1;
189
+ }
190
+ const top1 = rect1.y;
191
+ const bot1 = rect1.y + rect1.height;
192
+ const mid1 = rect1.y + rect1.height / 2;
193
+ const top2 = rect2.y;
194
+ const bot2 = rect2.y + rect2.height;
195
+ const mid2 = rect2.y + rect2.height / 2;
196
+ if (mid1 <= top2 && mid2 >= bot1) {
197
+ return -1;
198
+ }
199
+ if (mid2 <= top1 && mid1 >= bot2) {
200
+ return +1;
201
+ }
202
+ const centerX1 = rect1.x + rect1.width / 2;
203
+ const centerX2 = rect2.x + rect2.width / 2;
204
+ return centerX1 - centerX2;
205
+ }, _TextAccessibilityManager_addIdToAriaOwns = function _TextAccessibilityManager_addIdToAriaOwns(id, node) {
206
+ const owns = node.getAttribute("aria-owns");
207
+ if (!(owns === null || owns === void 0 ? void 0 : owns.includes(id))) {
208
+ node.setAttribute("aria-owns", owns ? `${owns} ${id}` : id);
209
+ }
210
+ node.removeAttribute("role");
211
+ };
212
+ export { TextAccessibilityManager };
@@ -0,0 +1,19 @@
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
+ export function bindEvents(obj, element, names) {
16
+ for (const name of names) {
17
+ element.addEventListener(name, obj[name].bind(obj));
18
+ }
19
+ }