@soomo/react-text-annotator 3.0.0-staging.16 → 3.0.0-staging.18

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 (48) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/react-text-annotator.es.js +2 -0
  4. package/dist/react-text-annotator.es.js.map +1 -1
  5. package/dist/react-text-annotator.es10.js +210 -562
  6. package/dist/react-text-annotator.es10.js.map +1 -1
  7. package/dist/react-text-annotator.es11.js +535 -444
  8. package/dist/react-text-annotator.es11.js.map +1 -1
  9. package/dist/react-text-annotator.es12.js +488 -2
  10. package/dist/react-text-annotator.es12.js.map +1 -1
  11. package/dist/react-text-annotator.es13.js +2 -35
  12. package/dist/react-text-annotator.es13.js.map +1 -1
  13. package/dist/react-text-annotator.es14.js +24 -888
  14. package/dist/react-text-annotator.es14.js.map +1 -1
  15. package/dist/react-text-annotator.es15.js +891 -220
  16. package/dist/react-text-annotator.es15.js.map +1 -1
  17. package/dist/react-text-annotator.es16.js +218 -126
  18. package/dist/react-text-annotator.es16.js.map +1 -1
  19. package/dist/react-text-annotator.es17.js +136 -2
  20. package/dist/react-text-annotator.es17.js.map +1 -1
  21. package/dist/react-text-annotator.es18.js +2 -2
  22. package/dist/react-text-annotator.es19.js +2 -114
  23. package/dist/react-text-annotator.es19.js.map +1 -1
  24. package/dist/react-text-annotator.es2.js +2 -2
  25. package/dist/react-text-annotator.es20.js +107 -302
  26. package/dist/react-text-annotator.es20.js.map +1 -1
  27. package/dist/react-text-annotator.es21.js +290 -39
  28. package/dist/react-text-annotator.es21.js.map +1 -1
  29. package/dist/react-text-annotator.es22.js +55 -17
  30. package/dist/react-text-annotator.es22.js.map +1 -1
  31. package/dist/react-text-annotator.es23.js +17 -118
  32. package/dist/react-text-annotator.es23.js.map +1 -1
  33. package/dist/react-text-annotator.es24.js +107 -538
  34. package/dist/react-text-annotator.es24.js.map +1 -1
  35. package/dist/react-text-annotator.es25.js +555 -0
  36. package/dist/react-text-annotator.es25.js.map +1 -0
  37. package/dist/react-text-annotator.es3.js +2 -2
  38. package/dist/react-text-annotator.es4.js +2 -2
  39. package/dist/react-text-annotator.es5.js +4 -4
  40. package/dist/react-text-annotator.es6.js +178 -9
  41. package/dist/react-text-annotator.es6.js.map +1 -1
  42. package/dist/react-text-annotator.es7.js +12 -1
  43. package/dist/react-text-annotator.es7.js.map +1 -1
  44. package/dist/react-text-annotator.es9.js +20 -223
  45. package/dist/react-text-annotator.es9.js.map +1 -1
  46. package/package.json +13 -7
  47. package/dist/react-text-annotator.es8.js +0 -27
  48. package/dist/react-text-annotator.es8.js.map +0 -1
@@ -1,582 +1,230 @@
1
- import { offset as offset$1, autoPlacement as autoPlacement$1, shift as shift$1, inline as inline$1, computePosition as computePosition$1 } from "./react-text-annotator.es24.js";
2
- import { createCoords, rectToClientRect, round, floor, max, min } from "./react-text-annotator.es23.js";
3
- import { getOverflowAncestors, isElement, getWindow, getComputedStyle, getDocumentElement, isHTMLElement, isWebKit, getNodeName, isOverflowElement, getNodeScroll, getParentNode, isLastTraversableNode, isTableElement, isContainingBlock, getContainingBlock } from "./react-text-annotator.es16.js";
4
- function getCssDimensions(element) {
5
- const css = getComputedStyle(element);
6
- let width = parseFloat(css.width) || 0;
7
- let height = parseFloat(css.height) || 0;
8
- const hasOffset = isHTMLElement(element);
9
- const offsetWidth = hasOffset ? element.offsetWidth : width;
10
- const offsetHeight = hasOffset ? element.offsetHeight : height;
11
- const shouldFallback = round(width) !== offsetWidth || round(height) !== offsetHeight;
12
- if (shouldFallback) {
13
- width = offsetWidth;
14
- height = offsetHeight;
15
- }
16
- return {
17
- width,
18
- height,
19
- $: shouldFallback
20
- };
21
- }
22
- function unwrapElement(element) {
23
- return !isElement(element) ? element.contextElement : element;
24
- }
25
- function getScale(element) {
26
- const domElement = unwrapElement(element);
27
- if (!isHTMLElement(domElement)) {
28
- return createCoords(1);
29
- }
30
- const rect = domElement.getBoundingClientRect();
31
- const {
32
- width,
33
- height,
34
- $
35
- } = getCssDimensions(domElement);
36
- let x = ($ ? round(rect.width) : rect.width) / width;
37
- let y = ($ ? round(rect.height) : rect.height) / height;
38
- if (!x || !Number.isFinite(x)) {
39
- x = 1;
40
- }
41
- if (!y || !Number.isFinite(y)) {
42
- y = 1;
43
- }
44
- return {
45
- x,
46
- y
47
- };
48
- }
49
- const noOffsets = /* @__PURE__ */ createCoords(0);
50
- function getVisualOffsets(element) {
51
- const win = getWindow(element);
52
- if (!isWebKit() || !win.visualViewport) {
53
- return noOffsets;
54
- }
55
- return {
56
- x: win.visualViewport.offsetLeft,
57
- y: win.visualViewport.offsetTop
58
- };
59
- }
60
- function shouldAddVisualOffsets(element, isFixed, floatingOffsetParent) {
61
- if (isFixed === void 0) {
62
- isFixed = false;
63
- }
64
- if (!floatingOffsetParent || isFixed && floatingOffsetParent !== getWindow(element)) {
65
- return false;
66
- }
67
- return isFixed;
68
- }
69
- function getBoundingClientRect(element, includeScale, isFixedStrategy, offsetParent) {
70
- if (includeScale === void 0) {
71
- includeScale = false;
72
- }
73
- if (isFixedStrategy === void 0) {
74
- isFixedStrategy = false;
75
- }
76
- const clientRect = element.getBoundingClientRect();
77
- const domElement = unwrapElement(element);
78
- let scale = createCoords(1);
79
- if (includeScale) {
80
- if (offsetParent) {
81
- if (isElement(offsetParent)) {
82
- scale = getScale(offsetParent);
83
- }
84
- } else {
85
- scale = getScale(element);
86
- }
87
- }
88
- const visualOffsets = shouldAddVisualOffsets(domElement, isFixedStrategy, offsetParent) ? getVisualOffsets(domElement) : createCoords(0);
89
- let x = (clientRect.left + visualOffsets.x) / scale.x;
90
- let y = (clientRect.top + visualOffsets.y) / scale.y;
91
- let width = clientRect.width / scale.x;
92
- let height = clientRect.height / scale.y;
93
- if (domElement) {
94
- const win = getWindow(domElement);
95
- const offsetWin = offsetParent && isElement(offsetParent) ? getWindow(offsetParent) : offsetParent;
96
- let currentWin = win;
97
- let currentIFrame = currentWin.frameElement;
98
- while (currentIFrame && offsetParent && offsetWin !== currentWin) {
99
- const iframeScale = getScale(currentIFrame);
100
- const iframeRect = currentIFrame.getBoundingClientRect();
101
- const css = getComputedStyle(currentIFrame);
102
- const left = iframeRect.left + (currentIFrame.clientLeft + parseFloat(css.paddingLeft)) * iframeScale.x;
103
- const top = iframeRect.top + (currentIFrame.clientTop + parseFloat(css.paddingTop)) * iframeScale.y;
104
- x *= iframeScale.x;
105
- y *= iframeScale.y;
106
- width *= iframeScale.x;
107
- height *= iframeScale.y;
108
- x += left;
109
- y += top;
110
- currentWin = getWindow(currentIFrame);
111
- currentIFrame = currentWin.frameElement;
1
+ import * as React from "react";
2
+ import { useLayoutEffect, useEffect } from "react";
3
+ import { useFloating as useFloating$1 } from "./react-text-annotator.es16.js";
4
+ import { isElement } from "./react-text-annotator.es17.js";
5
+ import { getOverflowAncestors } from "./react-text-annotator.es17.js";
6
+ import "react-dom";
7
+ const SafeReact = {
8
+ ...React
9
+ };
10
+ const useInsertionEffect = SafeReact.useInsertionEffect;
11
+ const useSafeInsertionEffect = useInsertionEffect || ((fn) => fn());
12
+ function useEffectEvent(callback) {
13
+ const ref = React.useRef(() => {
14
+ if (process.env.NODE_ENV !== "production") {
15
+ throw new Error("Cannot call an event handler while rendering.");
112
16
  }
113
- }
114
- return rectToClientRect({
115
- width,
116
- height,
117
- x,
118
- y
119
17
  });
120
- }
121
- const topLayerSelectors = [":popover-open", ":modal"];
122
- function isTopLayer(element) {
123
- return topLayerSelectors.some((selector) => {
124
- try {
125
- return element.matches(selector);
126
- } catch (e) {
127
- return false;
128
- }
18
+ useSafeInsertionEffect(() => {
19
+ ref.current = callback;
129
20
  });
130
- }
131
- function convertOffsetParentRelativeRectToViewportRelativeRect(_ref) {
132
- let {
133
- elements,
134
- rect,
135
- offsetParent,
136
- strategy
137
- } = _ref;
138
- const isFixed = strategy === "fixed";
139
- const documentElement = getDocumentElement(offsetParent);
140
- const topLayer = elements ? isTopLayer(elements.floating) : false;
141
- if (offsetParent === documentElement || topLayer && isFixed) {
142
- return rect;
143
- }
144
- let scroll = {
145
- scrollLeft: 0,
146
- scrollTop: 0
147
- };
148
- let scale = createCoords(1);
149
- const offsets = createCoords(0);
150
- const isOffsetParentAnElement = isHTMLElement(offsetParent);
151
- if (isOffsetParentAnElement || !isOffsetParentAnElement && !isFixed) {
152
- if (getNodeName(offsetParent) !== "body" || isOverflowElement(documentElement)) {
153
- scroll = getNodeScroll(offsetParent);
154
- }
155
- if (isHTMLElement(offsetParent)) {
156
- const offsetRect = getBoundingClientRect(offsetParent);
157
- scale = getScale(offsetParent);
158
- offsets.x = offsetRect.x + offsetParent.clientLeft;
159
- offsets.y = offsetRect.y + offsetParent.clientTop;
160
- }
161
- }
162
- return {
163
- width: rect.width * scale.x,
164
- height: rect.height * scale.y,
165
- x: rect.x * scale.x - scroll.scrollLeft * scale.x + offsets.x,
166
- y: rect.y * scale.y - scroll.scrollTop * scale.y + offsets.y
167
- };
168
- }
169
- function getClientRects(element) {
170
- return Array.from(element.getClientRects());
171
- }
172
- function getWindowScrollBarX(element) {
173
- return getBoundingClientRect(getDocumentElement(element)).left + getNodeScroll(element).scrollLeft;
174
- }
175
- function getDocumentRect(element) {
176
- const html = getDocumentElement(element);
177
- const scroll = getNodeScroll(element);
178
- const body = element.ownerDocument.body;
179
- const width = max(html.scrollWidth, html.clientWidth, body.scrollWidth, body.clientWidth);
180
- const height = max(html.scrollHeight, html.clientHeight, body.scrollHeight, body.clientHeight);
181
- let x = -scroll.scrollLeft + getWindowScrollBarX(element);
182
- const y = -scroll.scrollTop;
183
- if (getComputedStyle(body).direction === "rtl") {
184
- x += max(html.clientWidth, body.clientWidth) - width;
185
- }
186
- return {
187
- width,
188
- height,
189
- x,
190
- y
191
- };
192
- }
193
- function getViewportRect(element, strategy) {
194
- const win = getWindow(element);
195
- const html = getDocumentElement(element);
196
- const visualViewport = win.visualViewport;
197
- let width = html.clientWidth;
198
- let height = html.clientHeight;
199
- let x = 0;
200
- let y = 0;
201
- if (visualViewport) {
202
- width = visualViewport.width;
203
- height = visualViewport.height;
204
- const visualViewportBased = isWebKit();
205
- if (!visualViewportBased || visualViewportBased && strategy === "fixed") {
206
- x = visualViewport.offsetLeft;
207
- y = visualViewport.offsetTop;
208
- }
209
- }
210
- return {
211
- width,
212
- height,
213
- x,
214
- y
215
- };
216
- }
217
- function getInnerBoundingClientRect(element, strategy) {
218
- const clientRect = getBoundingClientRect(element, true, strategy === "fixed");
219
- const top = clientRect.top + element.clientTop;
220
- const left = clientRect.left + element.clientLeft;
221
- const scale = isHTMLElement(element) ? getScale(element) : createCoords(1);
222
- const width = element.clientWidth * scale.x;
223
- const height = element.clientHeight * scale.y;
224
- const x = left * scale.x;
225
- const y = top * scale.y;
226
- return {
227
- width,
228
- height,
229
- x,
230
- y
231
- };
232
- }
233
- function getClientRectFromClippingAncestor(element, clippingAncestor, strategy) {
234
- let rect;
235
- if (clippingAncestor === "viewport") {
236
- rect = getViewportRect(element, strategy);
237
- } else if (clippingAncestor === "document") {
238
- rect = getDocumentRect(getDocumentElement(element));
239
- } else if (isElement(clippingAncestor)) {
240
- rect = getInnerBoundingClientRect(clippingAncestor, strategy);
241
- } else {
242
- const visualOffsets = getVisualOffsets(element);
243
- rect = {
244
- ...clippingAncestor,
245
- x: clippingAncestor.x - visualOffsets.x,
246
- y: clippingAncestor.y - visualOffsets.y
247
- };
248
- }
249
- return rectToClientRect(rect);
250
- }
251
- function hasFixedPositionAncestor(element, stopNode) {
252
- const parentNode = getParentNode(element);
253
- if (parentNode === stopNode || !isElement(parentNode) || isLastTraversableNode(parentNode)) {
254
- return false;
255
- }
256
- return getComputedStyle(parentNode).position === "fixed" || hasFixedPositionAncestor(parentNode, stopNode);
257
- }
258
- function getClippingElementAncestors(element, cache) {
259
- const cachedResult = cache.get(element);
260
- if (cachedResult) {
261
- return cachedResult;
262
- }
263
- let result = getOverflowAncestors(element, [], false).filter((el) => isElement(el) && getNodeName(el) !== "body");
264
- let currentContainingBlockComputedStyle = null;
265
- const elementIsFixed = getComputedStyle(element).position === "fixed";
266
- let currentNode = elementIsFixed ? getParentNode(element) : element;
267
- while (isElement(currentNode) && !isLastTraversableNode(currentNode)) {
268
- const computedStyle = getComputedStyle(currentNode);
269
- const currentNodeIsContaining = isContainingBlock(currentNode);
270
- if (!currentNodeIsContaining && computedStyle.position === "fixed") {
271
- currentContainingBlockComputedStyle = null;
272
- }
273
- const shouldDropCurrentNode = elementIsFixed ? !currentNodeIsContaining && !currentContainingBlockComputedStyle : !currentNodeIsContaining && computedStyle.position === "static" && !!currentContainingBlockComputedStyle && ["absolute", "fixed"].includes(currentContainingBlockComputedStyle.position) || isOverflowElement(currentNode) && !currentNodeIsContaining && hasFixedPositionAncestor(element, currentNode);
274
- if (shouldDropCurrentNode) {
275
- result = result.filter((ancestor) => ancestor !== currentNode);
276
- } else {
277
- currentContainingBlockComputedStyle = computedStyle;
278
- }
279
- currentNode = getParentNode(currentNode);
280
- }
281
- cache.set(element, result);
282
- return result;
283
- }
284
- function getClippingRect(_ref) {
285
- let {
286
- element,
287
- boundary,
288
- rootBoundary,
289
- strategy
290
- } = _ref;
291
- const elementClippingAncestors = boundary === "clippingAncestors" ? isTopLayer(element) ? [] : getClippingElementAncestors(element, this._c) : [].concat(boundary);
292
- const clippingAncestors = [...elementClippingAncestors, rootBoundary];
293
- const firstClippingAncestor = clippingAncestors[0];
294
- const clippingRect = clippingAncestors.reduce((accRect, clippingAncestor) => {
295
- const rect = getClientRectFromClippingAncestor(element, clippingAncestor, strategy);
296
- accRect.top = max(rect.top, accRect.top);
297
- accRect.right = min(rect.right, accRect.right);
298
- accRect.bottom = min(rect.bottom, accRect.bottom);
299
- accRect.left = max(rect.left, accRect.left);
300
- return accRect;
301
- }, getClientRectFromClippingAncestor(element, firstClippingAncestor, strategy));
302
- return {
303
- width: clippingRect.right - clippingRect.left,
304
- height: clippingRect.bottom - clippingRect.top,
305
- x: clippingRect.left,
306
- y: clippingRect.top
307
- };
308
- }
309
- function getDimensions(element) {
310
- const {
311
- width,
312
- height
313
- } = getCssDimensions(element);
314
- return {
315
- width,
316
- height
317
- };
318
- }
319
- function getRectRelativeToOffsetParent(element, offsetParent, strategy) {
320
- const isOffsetParentAnElement = isHTMLElement(offsetParent);
321
- const documentElement = getDocumentElement(offsetParent);
322
- const isFixed = strategy === "fixed";
323
- const rect = getBoundingClientRect(element, true, isFixed, offsetParent);
324
- let scroll = {
325
- scrollLeft: 0,
326
- scrollTop: 0
327
- };
328
- const offsets = createCoords(0);
329
- if (isOffsetParentAnElement || !isOffsetParentAnElement && !isFixed) {
330
- if (getNodeName(offsetParent) !== "body" || isOverflowElement(documentElement)) {
331
- scroll = getNodeScroll(offsetParent);
332
- }
333
- if (isOffsetParentAnElement) {
334
- const offsetRect = getBoundingClientRect(offsetParent, true, isFixed, offsetParent);
335
- offsets.x = offsetRect.x + offsetParent.clientLeft;
336
- offsets.y = offsetRect.y + offsetParent.clientTop;
337
- } else if (documentElement) {
338
- offsets.x = getWindowScrollBarX(documentElement);
339
- }
340
- }
341
- const x = rect.left + scroll.scrollLeft - offsets.x;
342
- const y = rect.top + scroll.scrollTop - offsets.y;
343
- return {
344
- x,
345
- y,
346
- width: rect.width,
347
- height: rect.height
348
- };
349
- }
350
- function isStaticPositioned(element) {
351
- return getComputedStyle(element).position === "static";
352
- }
353
- function getTrueOffsetParent(element, polyfill) {
354
- if (!isHTMLElement(element) || getComputedStyle(element).position === "fixed") {
355
- return null;
356
- }
357
- if (polyfill) {
358
- return polyfill(element);
359
- }
360
- return element.offsetParent;
361
- }
362
- function getOffsetParent(element, polyfill) {
363
- const win = getWindow(element);
364
- if (isTopLayer(element)) {
365
- return win;
366
- }
367
- if (!isHTMLElement(element)) {
368
- let svgOffsetParent = getParentNode(element);
369
- while (svgOffsetParent && !isLastTraversableNode(svgOffsetParent)) {
370
- if (isElement(svgOffsetParent) && !isStaticPositioned(svgOffsetParent)) {
371
- return svgOffsetParent;
372
- }
373
- svgOffsetParent = getParentNode(svgOffsetParent);
374
- }
375
- return win;
376
- }
377
- let offsetParent = getTrueOffsetParent(element, polyfill);
378
- while (offsetParent && isTableElement(offsetParent) && isStaticPositioned(offsetParent)) {
379
- offsetParent = getTrueOffsetParent(offsetParent, polyfill);
380
- }
381
- if (offsetParent && isLastTraversableNode(offsetParent) && isStaticPositioned(offsetParent) && !isContainingBlock(offsetParent)) {
382
- return win;
383
- }
384
- return offsetParent || getContainingBlock(element) || win;
385
- }
386
- const getElementRects = async function(data) {
387
- const getOffsetParentFn = this.getOffsetParent || getOffsetParent;
388
- const getDimensionsFn = this.getDimensions;
389
- const floatingDimensions = await getDimensionsFn(data.floating);
21
+ return React.useCallback(function() {
22
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
23
+ args[_key] = arguments[_key];
24
+ }
25
+ return ref.current == null ? void 0 : ref.current(...args);
26
+ }, []);
27
+ }
28
+ var index = typeof document !== "undefined" ? useLayoutEffect : useEffect;
29
+ let serverHandoffComplete = false;
30
+ let count = 0;
31
+ const genId = () => (
32
+ // Ensure the id is unique with multiple independent versions of Floating UI
33
+ // on <React 18
34
+ "floating-ui-" + Math.random().toString(36).slice(2, 6) + count++
35
+ );
36
+ function useFloatingId() {
37
+ const [id, setId] = React.useState(() => serverHandoffComplete ? genId() : void 0);
38
+ index(() => {
39
+ if (id == null) {
40
+ setId(genId());
41
+ }
42
+ }, []);
43
+ React.useEffect(() => {
44
+ serverHandoffComplete = true;
45
+ }, []);
46
+ return id;
47
+ }
48
+ const useReactId = SafeReact.useId;
49
+ const useId = useReactId || useFloatingId;
50
+ let devMessageSet;
51
+ if (process.env.NODE_ENV !== "production") {
52
+ devMessageSet = /* @__PURE__ */ new Set();
53
+ }
54
+ function error() {
55
+ var _devMessageSet3;
56
+ for (var _len2 = arguments.length, messages = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
57
+ messages[_key2] = arguments[_key2];
58
+ }
59
+ const message = "Floating UI: " + messages.join(" ");
60
+ if (!((_devMessageSet3 = devMessageSet) != null && _devMessageSet3.has(message))) {
61
+ var _devMessageSet4;
62
+ (_devMessageSet4 = devMessageSet) == null || _devMessageSet4.add(message);
63
+ console.error(message);
64
+ }
65
+ }
66
+ function createPubSub() {
67
+ const map = /* @__PURE__ */ new Map();
390
68
  return {
391
- reference: getRectRelativeToOffsetParent(data.reference, await getOffsetParentFn(data.floating), data.strategy),
392
- floating: {
393
- x: 0,
394
- y: 0,
395
- width: floatingDimensions.width,
396
- height: floatingDimensions.height
397
- }
398
- };
399
- };
400
- function isRTL(element) {
401
- return getComputedStyle(element).direction === "rtl";
402
- }
403
- const platform = {
404
- convertOffsetParentRelativeRectToViewportRelativeRect,
405
- getDocumentElement,
406
- getClippingRect,
407
- getOffsetParent,
408
- getElementRects,
409
- getClientRects,
410
- getDimensions,
411
- getScale,
412
- isElement,
413
- isRTL
69
+ emit(event, data) {
70
+ var _map$get;
71
+ (_map$get = map.get(event)) == null || _map$get.forEach((handler) => handler(data));
72
+ },
73
+ on(event, listener) {
74
+ map.set(event, [...map.get(event) || [], listener]);
75
+ },
76
+ off(event, listener) {
77
+ var _map$get2;
78
+ map.set(event, ((_map$get2 = map.get(event)) == null ? void 0 : _map$get2.filter((l) => l !== listener)) || []);
79
+ }
80
+ };
81
+ }
82
+ const FloatingNodeContext = /* @__PURE__ */ React.createContext(null);
83
+ const FloatingTreeContext = /* @__PURE__ */ React.createContext(null);
84
+ const useFloatingParentNodeId = () => {
85
+ var _React$useContext;
86
+ return ((_React$useContext = React.useContext(FloatingNodeContext)) == null ? void 0 : _React$useContext.id) || null;
414
87
  };
415
- function observeMove(element, onMove) {
416
- let io = null;
417
- let timeoutId;
418
- const root = getDocumentElement(element);
419
- function cleanup() {
420
- var _io;
421
- clearTimeout(timeoutId);
422
- (_io = io) == null || _io.disconnect();
423
- io = null;
424
- }
425
- function refresh(skip, threshold) {
426
- if (skip === void 0) {
427
- skip = false;
428
- }
429
- if (threshold === void 0) {
430
- threshold = 1;
431
- }
432
- cleanup();
433
- const {
434
- left,
435
- top,
436
- width,
437
- height
438
- } = element.getBoundingClientRect();
439
- if (!skip) {
440
- onMove();
441
- }
442
- if (!width || !height) {
443
- return;
444
- }
445
- const insetTop = floor(top);
446
- const insetRight = floor(root.clientWidth - (left + width));
447
- const insetBottom = floor(root.clientHeight - (top + height));
448
- const insetLeft = floor(left);
449
- const rootMargin = -insetTop + "px " + -insetRight + "px " + -insetBottom + "px " + -insetLeft + "px";
450
- const options = {
451
- rootMargin,
452
- threshold: max(0, min(1, threshold)) || 1
453
- };
454
- let isFirstUpdate = true;
455
- function handleObserve(entries) {
456
- const ratio = entries[0].intersectionRatio;
457
- if (ratio !== threshold) {
458
- if (!isFirstUpdate) {
459
- return refresh();
460
- }
461
- if (!ratio) {
462
- timeoutId = setTimeout(() => {
463
- refresh(false, 1e-7);
464
- }, 1e3);
465
- } else {
466
- refresh(false, ratio);
467
- }
468
- }
469
- isFirstUpdate = false;
470
- }
471
- try {
472
- io = new IntersectionObserver(handleObserve, {
473
- ...options,
474
- // Handle <iframe>s
475
- root: root.ownerDocument
476
- });
477
- } catch (e) {
478
- io = new IntersectionObserver(handleObserve, options);
479
- }
480
- io.observe(element);
481
- }
482
- refresh(true);
483
- return cleanup;
88
+ const useFloatingTree = () => React.useContext(FloatingTreeContext);
89
+ function useFloatingRootContext(options) {
90
+ const {
91
+ open = false,
92
+ onOpenChange: onOpenChangeProp,
93
+ elements: elementsProp
94
+ } = options;
95
+ const floatingId = useId();
96
+ const dataRef = React.useRef({});
97
+ const [events] = React.useState(() => createPubSub());
98
+ const nested = useFloatingParentNodeId() != null;
99
+ if (process.env.NODE_ENV !== "production") {
100
+ const optionDomReference = elementsProp.reference;
101
+ if (optionDomReference && !isElement(optionDomReference)) {
102
+ error("Cannot pass a virtual element to the `elements.reference` option,", "as it must be a real DOM element. Use `refs.setPositionReference()`", "instead.");
103
+ }
104
+ }
105
+ const [positionReference, setPositionReference] = React.useState(elementsProp.reference);
106
+ const onOpenChange = useEffectEvent((open2, event, reason) => {
107
+ dataRef.current.openEvent = open2 ? event : void 0;
108
+ events.emit("openchange", {
109
+ open: open2,
110
+ event,
111
+ reason,
112
+ nested
113
+ });
114
+ onOpenChangeProp == null || onOpenChangeProp(open2, event, reason);
115
+ });
116
+ const refs = React.useMemo(() => ({
117
+ setPositionReference
118
+ }), []);
119
+ const elements = React.useMemo(() => ({
120
+ reference: positionReference || elementsProp.reference || null,
121
+ floating: elementsProp.floating || null,
122
+ domReference: elementsProp.reference
123
+ }), [positionReference, elementsProp.reference, elementsProp.floating]);
124
+ return React.useMemo(() => ({
125
+ dataRef,
126
+ open,
127
+ onOpenChange,
128
+ elements,
129
+ events,
130
+ floatingId,
131
+ refs
132
+ }), [open, onOpenChange, elements, events, floatingId, refs]);
484
133
  }
485
- function autoUpdate(reference, floating, update, options) {
134
+ function useFloating(options) {
486
135
  if (options === void 0) {
487
136
  options = {};
488
137
  }
489
138
  const {
490
- ancestorScroll = true,
491
- ancestorResize = true,
492
- elementResize = typeof ResizeObserver === "function",
493
- layoutShift = typeof IntersectionObserver === "function",
494
- animationFrame = false
139
+ nodeId
495
140
  } = options;
496
- const referenceEl = unwrapElement(reference);
497
- const ancestors = ancestorScroll || ancestorResize ? [...referenceEl ? getOverflowAncestors(referenceEl) : [], ...getOverflowAncestors(floating)] : [];
498
- ancestors.forEach((ancestor) => {
499
- ancestorScroll && ancestor.addEventListener("scroll", update, {
500
- passive: true
501
- });
502
- ancestorResize && ancestor.addEventListener("resize", update);
141
+ const internalRootContext = useFloatingRootContext({
142
+ ...options,
143
+ elements: {
144
+ reference: null,
145
+ floating: null,
146
+ ...options.elements
147
+ }
503
148
  });
504
- const cleanupIo = referenceEl && layoutShift ? observeMove(referenceEl, update) : null;
505
- let reobserveFrame = -1;
506
- let resizeObserver = null;
507
- if (elementResize) {
508
- resizeObserver = new ResizeObserver((_ref) => {
509
- let [firstEntry] = _ref;
510
- if (firstEntry && firstEntry.target === referenceEl && resizeObserver) {
511
- resizeObserver.unobserve(floating);
512
- cancelAnimationFrame(reobserveFrame);
513
- reobserveFrame = requestAnimationFrame(() => {
514
- var _resizeObserver;
515
- (_resizeObserver = resizeObserver) == null || _resizeObserver.observe(floating);
516
- });
149
+ const rootContext = options.rootContext || internalRootContext;
150
+ const computedElements = rootContext.elements;
151
+ const [_domReference, setDomReference] = React.useState(null);
152
+ const [positionReference, _setPositionReference] = React.useState(null);
153
+ const optionDomReference = computedElements == null ? void 0 : computedElements.reference;
154
+ const domReference = optionDomReference || _domReference;
155
+ const domReferenceRef = React.useRef(null);
156
+ const tree = useFloatingTree();
157
+ index(() => {
158
+ if (domReference) {
159
+ domReferenceRef.current = domReference;
160
+ }
161
+ }, [domReference]);
162
+ const position = useFloating$1({
163
+ ...options,
164
+ elements: {
165
+ ...computedElements,
166
+ ...positionReference && {
167
+ reference: positionReference
517
168
  }
518
- update();
519
- });
520
- if (referenceEl && !animationFrame) {
521
- resizeObserver.observe(referenceEl);
522
- }
523
- resizeObserver.observe(floating);
524
- }
525
- let frameId;
526
- let prevRefRect = animationFrame ? getBoundingClientRect(reference) : null;
527
- if (animationFrame) {
528
- frameLoop();
529
- }
530
- function frameLoop() {
531
- const nextRefRect = getBoundingClientRect(reference);
532
- if (prevRefRect && (nextRefRect.x !== prevRefRect.x || nextRefRect.y !== prevRefRect.y || nextRefRect.width !== prevRefRect.width || nextRefRect.height !== prevRefRect.height)) {
533
- update();
534
169
  }
535
- prevRefRect = nextRefRect;
536
- frameId = requestAnimationFrame(frameLoop);
537
- }
538
- update();
539
- return () => {
540
- var _resizeObserver2;
541
- ancestors.forEach((ancestor) => {
542
- ancestorScroll && ancestor.removeEventListener("scroll", update);
543
- ancestorResize && ancestor.removeEventListener("resize", update);
544
- });
545
- cleanupIo == null || cleanupIo();
546
- (_resizeObserver2 = resizeObserver) == null || _resizeObserver2.disconnect();
547
- resizeObserver = null;
548
- if (animationFrame) {
549
- cancelAnimationFrame(frameId);
170
+ });
171
+ const setPositionReference = React.useCallback((node) => {
172
+ const computedPositionReference = isElement(node) ? {
173
+ getBoundingClientRect: () => node.getBoundingClientRect(),
174
+ contextElement: node
175
+ } : node;
176
+ _setPositionReference(computedPositionReference);
177
+ position.refs.setReference(computedPositionReference);
178
+ }, [position.refs]);
179
+ const setReference = React.useCallback((node) => {
180
+ if (isElement(node) || node === null) {
181
+ domReferenceRef.current = node;
182
+ setDomReference(node);
183
+ }
184
+ if (isElement(position.refs.reference.current) || position.refs.reference.current === null || // Don't allow setting virtual elements using the old technique back to
185
+ // `null` to support `positionReference` + an unstable `reference`
186
+ // callback ref.
187
+ node !== null && !isElement(node)) {
188
+ position.refs.setReference(node);
189
+ }
190
+ }, [position.refs]);
191
+ const refs = React.useMemo(() => ({
192
+ ...position.refs,
193
+ setReference,
194
+ setPositionReference,
195
+ domReference: domReferenceRef
196
+ }), [position.refs, setReference, setPositionReference]);
197
+ const elements = React.useMemo(() => ({
198
+ ...position.elements,
199
+ domReference
200
+ }), [position.elements, domReference]);
201
+ const context = React.useMemo(() => ({
202
+ ...position,
203
+ ...rootContext,
204
+ refs,
205
+ elements,
206
+ nodeId
207
+ }), [position, refs, elements, nodeId, rootContext]);
208
+ index(() => {
209
+ rootContext.dataRef.current.floatingContext = context;
210
+ const node = tree == null ? void 0 : tree.nodesRef.current.find((node2) => node2.id === nodeId);
211
+ if (node) {
212
+ node.context = context;
550
213
  }
551
- };
552
- }
553
- const offset = offset$1;
554
- const autoPlacement = autoPlacement$1;
555
- const shift = shift$1;
556
- const inline = inline$1;
557
- const computePosition = (reference, floating, options) => {
558
- const cache = /* @__PURE__ */ new Map();
559
- const mergedOptions = {
560
- platform,
561
- ...options
562
- };
563
- const platformWithCache = {
564
- ...mergedOptions.platform,
565
- _c: cache
566
- };
567
- return computePosition$1(reference, floating, {
568
- ...mergedOptions,
569
- platform: platformWithCache
570
214
  });
571
- };
215
+ return React.useMemo(() => ({
216
+ ...position,
217
+ context,
218
+ refs,
219
+ elements
220
+ }), [position, refs, elements, context]);
221
+ }
572
222
  export {
573
- autoPlacement,
574
- autoUpdate,
575
- computePosition,
576
223
  getOverflowAncestors,
577
- inline,
578
- offset,
579
- platform,
580
- shift
224
+ useFloating,
225
+ useFloatingParentNodeId,
226
+ useFloatingRootContext,
227
+ useFloatingTree,
228
+ useId
581
229
  };
582
230
  //# sourceMappingURL=react-text-annotator.es10.js.map